@@ -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
230231func 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-
303256func 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 {
0 commit comments