Skip to content

Commit bd685f8

Browse files
committed
test: flatten source patch fixture cases
1 parent ee679ac commit bd685f8

File tree

30 files changed

+26
-69
lines changed

30 files changed

+26
-69
lines changed

internal/build/source_patch_test.go

Lines changed: 12 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,13 @@ func runSourcePatchCase(t *testing.T, caseName string) {
209209
assetRoot := filepath.Join(env.LLGoRuntimeDir(), "_patch", "_test", caseName)
210210
goroot := t.TempDir()
211211
runtimeDir := t.TempDir()
212+
const pkgPath = "demo"
213+
srcDir := filepath.Join(goroot, "src", pkgPath)
214+
patchDir := filepath.Join(runtimeDir, "_patch", pkgPath)
212215

213-
copyTree(t, filepath.Join(assetRoot, "pkg"), filepath.Join(goroot, "src"))
214-
copyTree(t, filepath.Join(assetRoot, "patch"), filepath.Join(runtimeDir, "_patch"))
216+
copyTree(t, filepath.Join(assetRoot, "pkg"), srcDir)
217+
copyTree(t, filepath.Join(assetRoot, "patch"), patchDir)
215218

216-
pkgPath := singlePackagePath(t, filepath.Join(assetRoot, "patch"))
217219
changed, overlay, err := applySourcePatchForPkg(nil, nil, runtimeDir, goroot, pkgPath, sourcePatchBuildContext{})
218220
if err != nil {
219221
t.Fatal(err)
@@ -222,9 +224,8 @@ func runSourcePatchCase(t *testing.T, caseName string) {
222224
t.Fatal("expected source patch overlay to change package")
223225
}
224226

225-
srcRoot := filepath.Join(goroot, "src")
226-
assertOverlayMatchesOutput(t, overlay, srcRoot, filepath.Join(assetRoot, "output"), runtimeDir)
227-
assertGeneratedPatchPackagePositions(t, overlay, srcRoot, filepath.Join(runtimeDir, "_patch"))
227+
assertOverlayMatchesOutput(t, overlay, srcDir, filepath.Join(assetRoot, "output"), runtimeDir)
228+
assertGeneratedPatchPackagePositions(t, overlay, srcDir, patchDir)
228229
}
229230

230231
func copyTree(t *testing.T, srcRoot, dstRoot string) {
@@ -252,54 +253,6 @@ func copyTree(t *testing.T, srcRoot, dstRoot string) {
252253
}
253254
}
254255

255-
func singlePackagePath(t *testing.T, root string) string {
256-
t.Helper()
257-
var pkgPath string
258-
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
259-
if err != nil {
260-
return err
261-
}
262-
if d.IsDir() {
263-
return nil
264-
}
265-
if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
266-
return nil
267-
}
268-
rel, err := filepath.Rel(root, filepath.Dir(path))
269-
if err != nil {
270-
return err
271-
}
272-
if rel == "." {
273-
rel = ""
274-
}
275-
rel = filepath.ToSlash(rel)
276-
if pkgPath == "" {
277-
pkgPath = rel
278-
return nil
279-
}
280-
if pkgPath != rel {
281-
return &pathMismatchError{want: pkgPath, got: rel}
282-
}
283-
return nil
284-
})
285-
if err != nil {
286-
t.Fatal(err)
287-
}
288-
if pkgPath == "" {
289-
t.Fatal("no patch package found")
290-
}
291-
return pkgPath
292-
}
293-
294-
type pathMismatchError struct {
295-
want string
296-
got string
297-
}
298-
299-
func (e *pathMismatchError) Error() string {
300-
return "multiple patch packages found: " + e.want + " and " + e.got
301-
}
302-
303256
func assertOverlayMatchesOutput(t *testing.T, overlay map[string][]byte, srcRoot, outputRoot, runtimeDir string) {
304257
t.Helper()
305258

@@ -349,7 +302,11 @@ func readTextFiles(t *testing.T, root, runtimeDir string) map[string]string {
349302
if err != nil {
350303
return err
351304
}
352-
out[filepath.ToSlash(rel)] = expandSourcePatchOutputTemplate(string(data), runtimeDir)
305+
key := filepath.ToSlash(rel)
306+
if strings.HasSuffix(key, ".txt") {
307+
key = strings.TrimSuffix(key, ".txt")
308+
}
309+
out[key] = expandSourcePatchOutputTemplate(string(data), runtimeDir)
353310
return nil
354311
})
355312
if err != nil {

runtime/_patch/_test/default-override/output/demo/demo.go renamed to runtime/_patch/_test/default-override/output/demo.go.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package demo
22

3-
// func Old() string { return "old" }
3+
// func Old() string { return "old" }
44
func Keep() string { return "keep" }

runtime/_patch/_test/default-override/output/demo/z_llgo_patch_patch.go renamed to runtime/_patch/_test/default-override/output/z_llgo_patch_patch.go.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//line {{PATCH_ROOT}}/demo/patch.go:1
22
package demo
33

4-
func Old() string { return "new" }
4+
func Old() string { return "new" }
55
func Added() string { return "added" }

runtime/_patch/_test/default-override/patch/demo/patch.go renamed to runtime/_patch/_test/default-override/patch/patch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package demo
22

3-
func Old() string { return "new" }
3+
func Old() string { return "new" }
44
func Added() string { return "added" }
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package demo
22

3-
func Old() string { return "old" }
3+
func Old() string { return "old" }
44
func Keep() string { return "keep" }

runtime/_patch/_test/multi-file-skipall/output/demo/a.go renamed to runtime/_patch/_test/multi-file-skipall/output/a.go.txt

File renamed without changes.

runtime/_patch/_test/multi-file-skipall/output/demo/b.go renamed to runtime/_patch/_test/multi-file-skipall/output/b.go.txt

File renamed without changes.

runtime/_patch/_test/multi-file-skipall/output/demo/z_llgo_patch_a.go renamed to runtime/_patch/_test/multi-file-skipall/output/z_llgo_patch_a.go.txt

File renamed without changes.

runtime/_patch/_test/multi-file-skipall/output/demo/z_llgo_patch_b.go renamed to runtime/_patch/_test/multi-file-skipall/output/z_llgo_patch_b.go.txt

File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)