fs_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2020 Google Inc. All rights reserved.
  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 fs
  15. import (
  16. "os"
  17. "testing"
  18. )
  19. func TestMockFs_LstatStatSymlinks(t *testing.T) {
  20. // setup filesystem
  21. filesystem := NewMockFs(nil)
  22. Create(t, "/tmp/realdir/hi.txt", filesystem)
  23. Create(t, "/tmp/realdir/ignoreme.txt", filesystem)
  24. Link(t, "/tmp/links/dir", "../realdir", filesystem)
  25. Link(t, "/tmp/links/file", "../realdir/hi.txt", filesystem)
  26. Link(t, "/tmp/links/broken", "nothingHere", filesystem)
  27. Link(t, "/tmp/links/recursive", "recursive", filesystem)
  28. assertStat := func(t *testing.T, stat os.FileInfo, err error, wantName string, wantMode os.FileMode) {
  29. t.Helper()
  30. if err != nil {
  31. t.Error(err)
  32. return
  33. }
  34. if g, w := stat.Name(), wantName; g != w {
  35. t.Errorf("want name %q, got %q", w, g)
  36. }
  37. if g, w := stat.Mode(), wantMode; g != w {
  38. t.Errorf("%s: want mode %q, got %q", wantName, w, g)
  39. }
  40. }
  41. assertErr := func(t *testing.T, err error, wantErr string) {
  42. if err == nil || err.Error() != wantErr {
  43. t.Errorf("want error %q, got %q", wantErr, err)
  44. }
  45. }
  46. stat, err := filesystem.Lstat("/tmp/links/dir")
  47. assertStat(t, stat, err, "dir", os.ModeSymlink)
  48. stat, err = filesystem.Stat("/tmp/links/dir")
  49. assertStat(t, stat, err, "realdir", os.ModeDir)
  50. stat, err = filesystem.Lstat("/tmp/links/file")
  51. assertStat(t, stat, err, "file", os.ModeSymlink)
  52. stat, err = filesystem.Stat("/tmp/links/file")
  53. assertStat(t, stat, err, "hi.txt", 0)
  54. stat, err = filesystem.Lstat("/tmp/links/broken")
  55. assertStat(t, stat, err, "broken", os.ModeSymlink)
  56. stat, err = filesystem.Stat("/tmp/links/broken")
  57. assertErr(t, err, "stat /tmp/links/nothingHere: file does not exist")
  58. stat, err = filesystem.Lstat("/tmp/links/recursive")
  59. assertStat(t, stat, err, "recursive", os.ModeSymlink)
  60. stat, err = filesystem.Stat("/tmp/links/recursive")
  61. assertErr(t, err, "read /tmp/links/recursive: too many levels of symbolic links")
  62. }