@@ -17,20 +17,14 @@ import (
17
17
"golang.org/x/tools/txtar"
18
18
)
19
19
20
- // CopyDirToTmp copies dir to a temporary test directory using
21
- // CopyTestFiles and returns the path to the test directory.
22
- func CopyDirToTmp (t testing.TB , srcdir string ) string {
23
- dst := t .TempDir ()
24
- if err := CopyFS (dst , os .DirFS (srcdir )); err != nil {
25
- t .Fatal (err )
26
- }
27
- return dst
28
- }
29
-
30
- // CopyFS copies the files and directories in src to a
31
- // destination directory dst. Paths to files and directories
32
- // ending in a ".test" extension have the ".test" extension
33
- // removed. This allows tests to hide files whose names have
20
+ // CopyToTmp copies the files and directories in src to a new temporary testing
21
+ // directory dst, and returns dst on success.
22
+ //
23
+ // After copying the files, it processes each of the 'old,new,' rename
24
+ // directives in order. Each rename directive moves the relative path "old"
25
+ // to the relative path "new" within the directory.
26
+ //
27
+ // Renaming allows tests to hide files whose names have
34
28
// special meaning, such as "go.mod" files or "testdata" directories
35
29
// from the go command, or ill-formed Go source files from gofmt.
36
30
//
@@ -41,42 +35,31 @@ func CopyDirToTmp(t testing.TB, srcdir string) string {
41
35
// a/a.go
42
36
// b/b.go
43
37
//
44
- // The resulting files will be:
38
+ // with the rename "go.mod.test,go.mod", the resulting files will be:
45
39
//
46
40
// dst/
47
41
// go.mod
48
42
// a/a.go
49
43
// b/b.go
50
- func CopyFS (dstdir string , src fs.FS ) error {
44
+ func CopyToTmp (t testing.TB , src fs.FS , rename ... string ) string {
45
+ dstdir := t .TempDir ()
46
+
51
47
if err := copyFS (dstdir , src ); err != nil {
52
- return err
48
+ t . Fatal ( err )
53
49
}
54
-
55
- // Collect ".test" paths in lexical order.
56
- var rename []string
57
- err := fs .WalkDir (os .DirFS (dstdir ), "." , func (path string , d fs.DirEntry , err error ) error {
58
- if err != nil {
59
- return err
50
+ for _ , r := range rename {
51
+ old , new , found := strings .Cut (r , "," )
52
+ if ! found {
53
+ t .Fatalf ("rename directive %q does not contain delimiter %q" , r , "," )
60
54
}
61
- if strings .HasSuffix (path , ".test" ) {
62
- rename = append (rename , path )
55
+ oldpath := filepath .Join (dstdir , old )
56
+ newpath := filepath .Join (dstdir , new )
57
+ if err := os .Rename (oldpath , newpath ); err != nil {
58
+ t .Fatal (err )
63
59
}
64
- return nil
65
- })
66
- if err != nil {
67
- return err
68
60
}
69
61
70
- // Rename the .test paths in reverse lexical order, e.g.
71
- // in d.test/a.test renames a.test to d.test/a then d.test to d.
72
- for i := len (rename ) - 1 ; i >= 0 ; i -- {
73
- oldpath := filepath .Join (dstdir , rename [i ])
74
- newpath := strings .TrimSuffix (oldpath , ".test" )
75
- if err != os .Rename (oldpath , newpath ) {
76
- return err
77
- }
78
- }
79
- return nil
62
+ return dstdir
80
63
}
81
64
82
65
// Copy the files in src to dst.
@@ -106,46 +89,18 @@ func copyFS(dstdir string, src fs.FS) error {
106
89
})
107
90
}
108
91
109
- // ExtractTxtar writes each archive file to the corresponding location beneath dir.
110
- //
111
- // TODO(adonovan): move this to txtar package, we need it all the time (#61386).
112
- func ExtractTxtar (dstdir string , ar * txtar.Archive ) error {
113
- for _ , file := range ar .Files {
114
- name := filepath .Join (dstdir , file .Name )
115
- if err := os .MkdirAll (filepath .Dir (name ), 0777 ); err != nil {
116
- return err
117
- }
118
- if err := os .WriteFile (name , file .Data , 0666 ); err != nil {
119
- return err
120
- }
121
- }
122
- return nil
123
- }
124
-
125
92
// ExtractTxtarFileToTmp read a txtar archive on a given path,
126
93
// extracts it to a temporary directory, and returns the
127
94
// temporary directory.
128
- func ExtractTxtarFileToTmp (t testing.TB , archiveFile string ) string {
129
- ar , err := txtar .ParseFile (archiveFile )
95
+ func ExtractTxtarFileToTmp (t testing.TB , file string ) string {
96
+ ar , err := txtar .ParseFile (file )
130
97
if err != nil {
131
98
t .Fatal (err )
132
99
}
133
100
134
- dir := t .TempDir ()
135
- err = ExtractTxtar (dir , ar )
136
- if err != nil {
137
- t .Fatal (err )
138
- }
139
- return dir
140
- }
141
-
142
- // ExtractTxtarToTmp extracts the given archive to a temp directory, and
143
- // returns that temporary directory.
144
- func ExtractTxtarToTmp (t testing.TB , ar * txtar.Archive ) string {
145
- dir := t .TempDir ()
146
- err := ExtractTxtar (dir , ar )
101
+ fs , err := txtar .FS (ar )
147
102
if err != nil {
148
103
t .Fatal (err )
149
104
}
150
- return dir
105
+ return CopyToTmp ( t , fs )
151
106
}
0 commit comments