Debian Codegraph @

tools/antithesis-go-instrumentor/common/files_test.go

raw
1 package common
2
3 import (
4 "os"
5 "path/filepath"
6 "testing"
7 )
8
9 func TestPathFromBaseDirectory(t *testing.T) {
10 // Use a temp dir so CanonicalizeDirectory's filepath.EvalSymlinks
11 // has real paths to walk. Build a small tree:
12 // <tmp>/repo/
13 // <tmp>/repo/server/
14 // <tmp>/repo/server/test/
15 // <tmp>/repo/tools/inner/leaf/
16 // <tmp>/other/ (sibling, not under repo)
17 tmp := t.TempDir()
18 repo := filepath.Join(tmp, "repo")
19 for _, d := range []string{
20 filepath.Join(repo, "server", "test"),
21 filepath.Join(repo, "tools", "inner", "leaf"),
22 filepath.Join(tmp, "other"),
23 } {
24 if err := os.MkdirAll(d, 0755); err != nil {
25 t.Fatal(err)
26 }
27 }
28
29 tests := []struct {
30 name string
31 base string
32 some string
33 wantRel string
34 }{
35 {
36 name: "same_dir_returns_empty",
37 base: repo,
38 some: repo,
39 wantRel: "",
40 },
41 {
42 name: "direct_child",
43 base: repo,
44 some: filepath.Join(repo, "server"),
45 wantRel: "server",
46 },
47 {
48 name: "two_level_child",
49 base: repo,
50 some: filepath.Join(repo, "server", "test"),
51 wantRel: filepath.Join("server", "test"),
52 },
53 {
54 name: "three_level_child",
55 base: repo,
56 some: filepath.Join(repo, "tools", "inner", "leaf"),
57 wantRel: filepath.Join("tools", "inner", "leaf"),
58 },
59 {
60 name: "not_a_child_returns_empty",
61 base: repo,
62 some: filepath.Join(tmp, "other"),
63 wantRel: "",
64 },
65 {
66 name: "trailing_slash_on_base_is_normalized",
67 base: repo + string(filepath.Separator),
68 some: filepath.Join(repo, "server", "test"),
69 wantRel: filepath.Join("server", "test"),
70 },
71 }
72
73 for _, tc := range tests {
74 t.Run(tc.name, func(t *testing.T) {
75 got := PathFromBaseDirectory(tc.base, tc.some)
76 if got != tc.wantRel {
77 t.Errorf("PathFromBaseDirectory(%q, %q) = %q, want %q", tc.base, tc.some, got, tc.wantRel)
78 }
79 })
80 }
81 }
82
83 // Regression test for ENG-3940: the previous implementation used
84 // filepath.Match with a `baseDir/*` pattern, which does not cross path
85 // separators. Multi-level submodules silently returned the *absolute* path
86 // of someDir as the "offset", which the caller then joined onto the
87 // customer output dir producing customer/<input-abspath>/... directories.
88 func TestPathFromBaseDirectory_DeepSubmodule_NoAbsolutePath(t *testing.T) {
89 tmp := t.TempDir()
90 repo := filepath.Join(tmp, "repo")
91 deep := filepath.Join(repo, "tools", "rw-heatmaps")
92 if err := os.MkdirAll(deep, 0755); err != nil {
93 t.Fatal(err)
94 }
95
96 got := PathFromBaseDirectory(repo, deep)
97 want := filepath.Join("tools", "rw-heatmaps")
98 if got != want {
99 t.Fatalf("PathFromBaseDirectory(%q, %q) = %q, want %q (regression: returning the absolute path instead of a relative offset)",
100 repo, deep, got, want)
101 }
102 if filepath.IsAbs(got) {
103 t.Fatalf("PathFromBaseDirectory returned an absolute path %q for a child under base (this is the ENG-3940 bug)", got)
104 }
105 }
106
107 func TestPathFromBaseDirectory_Symlink(t *testing.T) {
108 tmp := t.TempDir()
109 repo := filepath.Join(tmp, "repo")
110 real := filepath.Join(repo, "real", "deep")
111 if err := os.MkdirAll(real, 0755); err != nil {
112 t.Fatal(err)
113 }
114 link := filepath.Join(repo, "alias")
115 if err := os.Symlink(filepath.Join(repo, "real"), link); err != nil {
116 t.Skipf("symlink unsupported: %v", err)
117 }
118
119 // Passing the symlinked path should resolve to the real path and
120 // produce a relative offset against the base.
121 got := PathFromBaseDirectory(repo, filepath.Join(link, "deep"))
122 want := filepath.Join("real", "deep")
123 if got != want {
124 t.Errorf("PathFromBaseDirectory(%q, %q) = %q, want %q", repo, filepath.Join(link, "deep"), got, want)
125 }
126 }