diff options
| author | 2023-04-28 01:36:44 +0800 | |
|---|---|---|
| committer | 2023-04-28 01:36:44 +0800 | |
| commit | dd84b9d64fb98746a230cd24233ff50a562c39c9 (patch) | |
| tree | b583261ef00b3afe72ec4d6dacb31e57779a6faf /cli/internal/fs/fs_test.go | |
| parent | 0b46fcd72ac34382387b2bcf9095233efbcc52f4 (diff) | |
| download | HydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.tar.gz HydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.zip | |
Diffstat (limited to 'cli/internal/fs/fs_test.go')
| -rw-r--r-- | cli/internal/fs/fs_test.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/cli/internal/fs/fs_test.go b/cli/internal/fs/fs_test.go new file mode 100644 index 0000000..0598d43 --- /dev/null +++ b/cli/internal/fs/fs_test.go @@ -0,0 +1,60 @@ +package fs + +import ( + "path/filepath" + "testing" +) + +func Test_DirContainsPath(t *testing.T) { + parent, err := filepath.Abs(filepath.Join("some", "path")) + if err != nil { + t.Fatalf("failed to construct parent path %v", err) + } + testcases := []struct { + target []string + want bool + }{ + { + []string{"..", "elsewhere"}, + false, + }, + { + []string{"sibling"}, + false, + }, + { + // The same path as parent + []string{"some", "path"}, + true, + }, + { + []string{"some", "path", "..", "path", "inside", "parent"}, + true, + }, + { + []string{"some", "path", "inside", "..", "inside", "parent"}, + true, + }, + { + []string{"some", "path", "inside", "..", "..", "outside", "parent"}, + false, + }, + { + []string{"some", "pathprefix"}, + false, + }, + } + for _, tc := range testcases { + target, err := filepath.Abs(filepath.Join(tc.target...)) + if err != nil { + t.Fatalf("failed to construct path for %v: %v", tc.target, err) + } + got, err := DirContainsPath(parent, target) + if err != nil { + t.Fatalf("failed to check ") + } + if got != tc.want { + t.Errorf("DirContainsPath(%v, %v) got %v, want %v", parent, target, got, tc.want) + } + } +} |
