@@ -10,11 +10,13 @@ import (
10
10
"encoding/json"
11
11
"flag"
12
12
"fmt"
13
+ "go/build"
13
14
"io/ioutil"
14
15
"os"
15
16
"os/exec"
16
17
"path"
17
18
"path/filepath"
19
+ "reflect"
18
20
"runtime"
19
21
"strings"
20
22
"testing"
@@ -35,28 +37,59 @@ func TestFullCycle(t *testing.T) {
35
37
t .Skipf ("skipping because 'go' command is unavailable: %v" , err )
36
38
}
37
39
38
- const path = "./testdata"
39
- dirs , err := ioutil .ReadDir (path )
40
+ GOPATH , err := ioutil .TempDir ("" , "pipeline_test" )
41
+ if err != nil {
42
+ t .Fatal (err )
43
+ }
44
+ defer os .RemoveAll (GOPATH )
45
+ testdata := filepath .Join (GOPATH , "src" , "testdata" )
46
+
47
+ // Copy the testdata contents into a new module.
48
+ copyTestdata (t , testdata )
49
+ initTestdataModule (t , testdata )
50
+
51
+ // Several places hard-code the use of build.Default.
52
+ // Adjust it to match the test's temporary GOPATH.
53
+ defer func (prev string ) { build .Default .GOPATH = prev }(build .Default .GOPATH )
54
+ build .Default .GOPATH = GOPATH + string (filepath .ListSeparator ) + build .Default .GOPATH
55
+ if wd := reflect .ValueOf (& build .Default ).Elem ().FieldByName ("WorkingDir" ); wd .IsValid () {
56
+ defer func (prev string ) { wd .SetString (prev ) }(wd .String ())
57
+ wd .SetString (testdata )
58
+ }
59
+
60
+ // To work around https://golang.org/issue/34860, execute the commands
61
+ // that (transitively) use go/build in the working directory of the
62
+ // corresponding module.
63
+ wd , _ := os .Getwd ()
64
+ defer os .Chdir (wd )
65
+
66
+ dirs , err := ioutil .ReadDir (testdata )
40
67
if err != nil {
41
68
t .Fatal (err )
42
69
}
43
70
for _ , f := range dirs {
71
+ if ! f .IsDir () {
72
+ continue
73
+ }
44
74
t .Run (f .Name (), func (t * testing.T ) {
45
75
chk := func (t * testing.T , err error ) {
46
76
setHelper (t )
47
77
if err != nil {
48
78
t .Fatal (err )
49
79
}
50
80
}
51
- dir := filepath .Join (path , f .Name ())
52
- pkgPath := fmt . Sprintf ( "%s/%s" , path , f .Name () )
81
+ dir := filepath .Join (testdata , f .Name ())
82
+ pkgPath := "testdata/" + f .Name ()
53
83
config := Config {
54
84
SourceLanguage : language .AmericanEnglish ,
55
85
Packages : []string {pkgPath },
56
86
Dir : filepath .Join (dir , "locales" ),
57
87
GenFile : "catalog_gen.go" ,
58
88
GenPackage : pkgPath ,
59
89
}
90
+
91
+ os .Chdir (dir )
92
+
60
93
// TODO: load config if available.
61
94
s , err := Extract (& config )
62
95
chk (t , err )
@@ -69,34 +102,82 @@ func TestFullCycle(t *testing.T) {
69
102
chk (t , s .Export ())
70
103
chk (t , s .Generate ())
71
104
105
+ os .Chdir (wd )
106
+
72
107
writeJSON (t , filepath .Join (dir , "extracted.gotext.json" ), s .Extracted )
73
- checkOutput (t , dir )
108
+ checkOutput (t , dir , f . Name () )
74
109
})
75
110
}
76
111
}
77
112
78
- func checkOutput (t * testing.T , p string ) {
79
- filepath .Walk (p , func (p string , f os.FileInfo , err error ) error {
113
+ func copyTestdata (t * testing.T , dst string ) {
114
+ err := filepath .Walk ("testdata" , func (p string , f os.FileInfo , err error ) error {
115
+ if p == "testdata" || strings .HasSuffix (p , ".want" ) {
116
+ return nil
117
+ }
118
+
119
+ rel := strings .TrimPrefix (p , "testdata" + string (filepath .Separator ))
120
+ if f .IsDir () {
121
+ return os .MkdirAll (filepath .Join (dst , rel ), 0755 )
122
+ }
123
+
124
+ data , err := ioutil .ReadFile (p )
125
+ if err != nil {
126
+ return err
127
+ }
128
+ return ioutil .WriteFile (filepath .Join (dst , rel ), data , 0644 )
129
+ })
130
+ if err != nil {
131
+ t .Fatal (err )
132
+ }
133
+ }
134
+
135
+ func initTestdataModule (t * testing.T , dst string ) {
136
+ xTextDir , err := filepath .Abs ("../.." )
137
+ if err != nil {
138
+ t .Fatal (err )
139
+ }
140
+
141
+ goMod := fmt .Sprintf (`module testdata
142
+ go 1.11
143
+ require golang.org/x/text v0.0.0-00010101000000-000000000000
144
+ replace golang.org/x/text v0.0.0-00010101000000-000000000000 => %s
145
+ ` , xTextDir )
146
+ if err := ioutil .WriteFile (filepath .Join (dst , "go.mod" ), []byte (goMod ), 0644 ); err != nil {
147
+ t .Fatal (err )
148
+ }
149
+
150
+ data , err := ioutil .ReadFile (filepath .Join (xTextDir , "go.sum" ))
151
+ if err := ioutil .WriteFile (filepath .Join (dst , "go.sum" ), data , 0644 ); err != nil {
152
+ t .Fatal (err )
153
+ }
154
+ }
155
+
156
+ func checkOutput (t * testing.T , gen string , testdataDir string ) {
157
+ err := filepath .Walk (gen , func (gotFile string , f os.FileInfo , err error ) error {
80
158
if f .IsDir () {
81
159
return nil
82
160
}
83
- if filepath .Ext (p ) != ".want" {
161
+ rel := strings .TrimPrefix (gotFile , gen + string (filepath .Separator ))
162
+
163
+ wantFile := filepath .Join ("testdata" , testdataDir , rel + ".want" )
164
+ if _ , err := os .Stat (wantFile ); os .IsNotExist (err ) {
84
165
return nil
85
166
}
86
- gotFile := p [: len ( p ) - len ( ".want" )]
167
+
87
168
got , err := ioutil .ReadFile (gotFile )
88
169
if err != nil {
89
- t .Errorf ("failed to read %q" , p )
170
+ t .Errorf ("failed to read %q" , gotFile )
90
171
return nil
91
172
}
92
173
if * genFiles {
93
- if err := ioutil .WriteFile (p , got , 0644 ); err != nil {
174
+ if err := ioutil .WriteFile (wantFile , got , 0644 ); err != nil {
94
175
t .Fatal (err )
95
176
}
96
177
}
97
- want , err := ioutil .ReadFile (p )
178
+ want , err := ioutil .ReadFile (wantFile )
98
179
if err != nil {
99
- t .Errorf ("failed to read %q" , p )
180
+ t .Errorf ("failed to read %q" , wantFile )
100
181
} else {
101
182
scanGot := bufio .NewScanner (bytes .NewReader (got ))
102
183
scanWant := bufio .NewScanner (bytes .NewReader (want ))
@@ -122,6 +203,9 @@ func checkOutput(t *testing.T, p string) {
122
203
}
123
204
return nil
124
205
})
206
+ if err != nil {
207
+ t .Fatal (err )
208
+ }
125
209
}
126
210
127
211
func writeJSON (t * testing.T , path string , x interface {}) {
0 commit comments