lstater_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright ©2018 Steve Francia <spf@spf13.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package afero
  15. import (
  16. "os"
  17. "path/filepath"
  18. "testing"
  19. )
  20. func TestLstatIfPossible(t *testing.T) {
  21. wd, _ := os.Getwd()
  22. defer func() {
  23. os.Chdir(wd)
  24. }()
  25. osFs := &OsFs{}
  26. workDir, err := TempDir(osFs, "", "afero-lstate")
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. defer func() {
  31. osFs.RemoveAll(workDir)
  32. }()
  33. memWorkDir := "/lstate"
  34. memFs := NewMemMapFs()
  35. overlayFs1 := &CopyOnWriteFs{base: osFs, layer: memFs}
  36. overlayFs2 := &CopyOnWriteFs{base: memFs, layer: osFs}
  37. overlayFsMemOnly := &CopyOnWriteFs{base: memFs, layer: NewMemMapFs()}
  38. basePathFs := &BasePathFs{source: osFs, path: workDir}
  39. basePathFsMem := &BasePathFs{source: memFs, path: memWorkDir}
  40. roFs := &ReadOnlyFs{source: osFs}
  41. roFsMem := &ReadOnlyFs{source: memFs}
  42. pathFileMem := filepath.Join(memWorkDir, "aferom.txt")
  43. WriteFile(osFs, filepath.Join(workDir, "afero.txt"), []byte("Hi, Afero!"), 0777)
  44. WriteFile(memFs, filepath.Join(pathFileMem), []byte("Hi, Afero!"), 0777)
  45. os.Chdir(workDir)
  46. if err := os.Symlink("afero.txt", "symafero.txt"); err != nil {
  47. t.Fatal(err)
  48. }
  49. pathFile := filepath.Join(workDir, "afero.txt")
  50. pathSymlink := filepath.Join(workDir, "symafero.txt")
  51. checkLstat := func(l Lstater, name string, shouldLstat bool) os.FileInfo {
  52. statFile, isLstat, err := l.LstatIfPossible(name)
  53. if err != nil {
  54. t.Fatalf("Lstat check failed: %s", err)
  55. }
  56. if isLstat != shouldLstat {
  57. t.Fatalf("Lstat status was %t for %s", isLstat, name)
  58. }
  59. return statFile
  60. }
  61. testLstat := func(l Lstater, pathFile, pathSymlink string) {
  62. shouldLstat := pathSymlink != ""
  63. statRegular := checkLstat(l, pathFile, shouldLstat)
  64. statSymlink := checkLstat(l, pathSymlink, shouldLstat)
  65. if statRegular == nil || statSymlink == nil {
  66. t.Fatal("got nil FileInfo")
  67. }
  68. symSym := statSymlink.Mode()&os.ModeSymlink == os.ModeSymlink
  69. if symSym == (pathSymlink == "") {
  70. t.Fatal("expected the FileInfo to describe the symlink")
  71. }
  72. _, _, err := l.LstatIfPossible("this-should-not-exist.txt")
  73. if err == nil || !os.IsNotExist(err) {
  74. t.Fatalf("expected file to not exist, got %s", err)
  75. }
  76. }
  77. testLstat(osFs, pathFile, pathSymlink)
  78. testLstat(overlayFs1, pathFile, pathSymlink)
  79. testLstat(overlayFs2, pathFile, pathSymlink)
  80. testLstat(basePathFs, "afero.txt", "symafero.txt")
  81. testLstat(overlayFsMemOnly, pathFileMem, "")
  82. testLstat(basePathFsMem, "aferom.txt", "")
  83. testLstat(roFs, pathFile, pathSymlink)
  84. testLstat(roFsMem, pathFileMem, "")
  85. }