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