-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine_test.go
More file actions
168 lines (157 loc) · 5.08 KB
/
Copy pathengine_test.go
File metadata and controls
168 lines (157 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package clitest
import (
"os"
"path/filepath"
"testing"
)
// When recursive discovery finds act.sh+assert.txt in a directory, that directory
// is a case and its subdirectories are not scanned. A nested act.sh/assert.txt pair
// under a case is fixture material (see tests/dogfood/case_boundary).
func TestDiscoverTestCases_stopsAtCaseBoundary(t *testing.T) {
root := t.TempDir()
base := filepath.Join(root, "suite")
parent := filepath.Join(base, "parent")
nested := filepath.Join(parent, "files", "nested_fixture")
for _, dir := range []string{parent, nested} {
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatal(err)
}
}
writeCase := func(dir string) {
if err := os.WriteFile(filepath.Join(dir, "act.sh"), []byte("#!/bin/sh\n"), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "assert.txt"), []byte("ok\n"), 0644); err != nil {
t.Fatal(err)
}
}
writeCase(parent)
writeCase(nested)
cases, err := DiscoverTestCases([]string{base}, root, true)
if err != nil {
t.Fatal(err)
}
if len(cases) != 1 {
t.Fatalf("got %d cases, want 1: recursive walk must stop at parent, not pick up nested_fixture", len(cases))
}
if filepath.Clean(cases[0].Path) != filepath.Clean(parent) {
t.Fatalf("got case path %q, want %q", cases[0].Path, parent)
}
}
// Cases link one staged copy of the binary: no case writes a new executable
// (slow to start on first run), and nothing a case does reaches the
// project's binary.
func TestStagedBinaryIsLinkedNotCopied(t *testing.T) {
project := t.TempDir()
bin := filepath.Join(project, "app")
if err := os.WriteFile(bin, []byte("#!/bin/sh\necho v1\n"), 0755); err != nil {
t.Fatal(err)
}
stage, err := StageBinary(project, "app")
if err != nil {
t.Fatal(err)
}
defer CleanStageDir(stage)
if _, err := os.Stat(filepath.Join(stage, StageSentinel)); err != nil {
t.Fatalf("missing stage sentinel %s: %v", StageSentinel, err)
}
staged, err := os.Stat(filepath.Join(stage, "app"))
if err != nil {
t.Fatal(err)
}
if staged.Mode().Perm()&0100 == 0 {
t.Errorf("staged binary mode %v, want executable", staged.Mode())
}
fixtures := t.TempDir()
var caseBins []string
for i := 0; i < 2; i++ {
dir := t.TempDir()
if err := PrepareCaseDir(fixtures, dir, project, stage, "app", nil); err != nil {
t.Fatal(err)
}
caseBins = append(caseBins, filepath.Join(dir, "app"))
}
for _, b := range caseBins {
fi, err := os.Stat(b)
if err != nil {
t.Fatal(err)
}
if !os.SameFile(fi, staged) {
t.Errorf("%s is not the staged file: each case would write (and first-run) a new executable", b)
}
}
// A case that rewrites its binary changes the stage, never the project.
if err := os.WriteFile(caseBins[0], []byte("changed"), 0755); err != nil {
t.Fatal(err)
}
if got, _ := os.ReadFile(bin); string(got) != "#!/bin/sh\necho v1\n" {
t.Errorf("project binary changed through a case: %q", got)
}
}
// Without a stage the binary is copied, as before; and where a link can't
// be made, linkOrCopy copies (executable) instead.
func TestBinaryCopiedWithoutStageOrLink(t *testing.T) {
project := t.TempDir()
if err := os.WriteFile(filepath.Join(project, "app"), []byte("bin"), 0755); err != nil {
t.Fatal(err)
}
dir := t.TempDir()
if err := PrepareCaseDir(t.TempDir(), dir, project, "", "app", nil); err != nil {
t.Fatal(err)
}
a, _ := os.Stat(filepath.Join(project, "app"))
b, err := os.Stat(filepath.Join(dir, "app"))
if err != nil {
t.Fatal(err)
}
if os.SameFile(a, b) {
t.Error("no stage: the case must get a copy, not the project's own file")
}
// os.Link refuses an existing dst, which takes the copy path.
dst := filepath.Join(t.TempDir(), "app")
if err := os.WriteFile(dst, []byte("old"), 0644); err != nil {
t.Fatal(err)
}
if err := linkOrCopy(filepath.Join(project, "app"), dst); err != nil {
t.Fatal(err)
}
fi, err := os.Stat(dst)
if err != nil {
t.Fatal(err)
}
if got, _ := os.ReadFile(dst); string(got) != "bin" || fi.Mode().Perm()&0100 == 0 {
t.Errorf("fallback copy = %q mode %v, want the binary, executable", got, fi.Mode())
}
}
func TestCleanStageDir(t *testing.T) {
// Empty directory is a no-op
if err := CleanStageDir(""); err != nil {
t.Errorf("CleanStageDir(\"\") = %v, want nil", err)
}
// Refuses to delete directory without sentinel
dir := t.TempDir()
canary := filepath.Join(dir, "important.txt")
if err := os.WriteFile(canary, []byte("preserve"), 0644); err != nil {
t.Fatal(err)
}
if err := CleanStageDir(dir); err == nil {
t.Errorf("CleanStageDir on dir without sentinel succeeded; want error")
}
if _, err := os.Stat(canary); err != nil {
t.Errorf("canary was deleted when sentinel was absent")
}
// Deletes directory when sentinel is present
stageDir, err := os.MkdirTemp("", "clitest-stage-test-*")
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(stageDir, StageSentinel), nil, 0600); err != nil {
t.Fatal(err)
}
if err := CleanStageDir(stageDir); err != nil {
t.Errorf("CleanStageDir failed on valid stage dir: %v", err)
}
if _, err := os.Stat(stageDir); !os.IsNotExist(err) {
t.Errorf("stageDir still exists after CleanStageDir")
}
}