@@ -6,39 +6,103 @@ import (
6
6
"path/filepath"
7
7
"regexp"
8
8
"strings"
9
+ "sync"
9
10
"testing"
10
11
12
+ "github.com/microsoft/typescript-go/internal/core"
11
13
"github.com/microsoft/typescript-go/internal/repo"
14
+ "github.com/microsoft/typescript-go/internal/tspath"
12
15
"github.com/pkg/diff"
16
+ "gotest.tools/v3/assert"
13
17
)
14
18
15
19
type Options struct {
16
- Subfolder string
17
- IsSubmodule bool
18
- DiffFixupOld func (string ) string
20
+ Subfolder string
21
+ IsSubmodule bool
22
+ IsSubmoduleAccepted bool
23
+ DiffFixupOld func (string ) string
19
24
}
20
25
21
- const (
22
- NoContent = "<no content>"
23
- submoduleFolder = "submodule"
24
- )
26
+ const NoContent = "<no content>"
25
27
26
28
func Run (t * testing.T , fileName string , actual string , opts Options ) {
27
- if opts .IsSubmodule {
28
- opts .Subfolder = filepath .Join (submoduleFolder , opts .Subfolder )
29
- diff := getBaselineDiff (t , actual , fileName , opts .DiffFixupOld )
30
- diffFileName := fileName + ".diff"
31
- writeComparison (t , diff , diffFileName , false , opts )
29
+ origSubfolder := opts .Subfolder
30
+
31
+ {
32
+ subfolder := opts .Subfolder
33
+ if opts .IsSubmodule {
34
+ subfolder = filepath .Join ("submodule" , subfolder )
35
+ }
36
+
37
+ localPath := filepath .Join (localRoot , subfolder , fileName )
38
+ referencePath := filepath .Join (referenceRoot , subfolder , fileName )
39
+
40
+ writeComparison (t , actual , localPath , referencePath , false )
41
+ }
42
+
43
+ if ! opts .IsSubmodule {
44
+ // Not a submodule, no diffs.
45
+ return
46
+ }
47
+
48
+ submoduleReference := filepath .Join (submoduleReferenceRoot , fileName )
49
+ submoduleExpected := readFileOrNoContent (submoduleReference )
50
+
51
+ const (
52
+ submoduleFolder = "submodule"
53
+ submoduleAcceptedFolder = "submoduleAccepted"
54
+ )
55
+
56
+ diffFileName := fileName + ".diff"
57
+ isSubmoduleAccepted := opts .IsSubmoduleAccepted || submoduleAcceptedFileNames ().Has (origSubfolder + "/" + diffFileName )
58
+
59
+ outRoot := core .IfElse (isSubmoduleAccepted , submoduleAcceptedFolder , submoduleFolder )
60
+ unusedOutRoot := core .IfElse (isSubmoduleAccepted , submoduleFolder , submoduleAcceptedFolder )
61
+
62
+ {
63
+ localPath := filepath .Join (localRoot , outRoot , origSubfolder , diffFileName )
64
+ referencePath := filepath .Join (referenceRoot , outRoot , origSubfolder , diffFileName )
65
+
66
+ diff := getBaselineDiff (t , actual , submoduleExpected , fileName , opts .DiffFixupOld )
67
+ writeComparison (t , diff , localPath , referencePath , false )
68
+ }
69
+
70
+ // Delete the other diff file if it exists
71
+ {
72
+ localPath := filepath .Join (localRoot , unusedOutRoot , origSubfolder , diffFileName )
73
+ referencePath := filepath .Join (referenceRoot , unusedOutRoot , origSubfolder , diffFileName )
74
+ writeComparison (t , NoContent , localPath , referencePath , false )
32
75
}
33
- writeComparison (t , actual , fileName , false , opts )
34
76
}
35
77
36
- func getBaselineDiff (t * testing.T , actual string , fileName string , fixupOld func (string ) string ) string {
37
- expected := NoContent
38
- refFileName := submoduleReferencePath (fileName , "" /*subfolder*/ )
39
- if content , err := os .ReadFile (refFileName ); err == nil {
40
- expected = string (content )
78
+ var submoduleAcceptedFileNames = sync .OnceValue (func () * core.Set [string ] {
79
+ var set core.Set [string ]
80
+
81
+ submoduleAccepted := filepath .Join (repo .TestDataPath , "submoduleAccepted.txt" )
82
+ if content , err := os .ReadFile (submoduleAccepted ); err == nil {
83
+ for line := range strings .SplitSeq (string (content ), "\n " ) {
84
+ line = strings .TrimSpace (line )
85
+ if line == "" || line [0 ] == '#' {
86
+ continue
87
+ }
88
+ set .Add (line )
89
+ }
90
+ } else {
91
+ panic (fmt .Sprintf ("failed to read submodule accepted file: %v" , err ))
92
+ }
93
+
94
+ return & set
95
+ })
96
+
97
+ func readFileOrNoContent (fileName string ) string {
98
+ content , err := os .ReadFile (fileName )
99
+ if err != nil {
100
+ return NoContent
41
101
}
102
+ return string (content )
103
+ }
104
+
105
+ func getBaselineDiff (t * testing.T , actual string , expected string , fileName string , fixupOld func (string ) string ) string {
42
106
if fixupOld != nil {
43
107
expected = fixupOld (expected )
44
108
}
@@ -77,84 +141,72 @@ func getBaselineDiff(t *testing.T, actual string, fileName string, fixupOld func
77
141
var fixUnifiedDiff = regexp .MustCompile (`@@ -\d+,\d+ \+\d+,\d+ @@` )
78
142
79
143
func RunAgainstSubmodule (t * testing.T , fileName string , actual string , opts Options ) {
80
- writeComparison (t , actual , fileName , true /*useSubmodule*/ , opts )
144
+ local := filepath .Join (localRoot , opts .Subfolder , fileName )
145
+ reference := filepath .Join (submoduleReferenceRoot , opts .Subfolder , fileName )
146
+ writeComparison (t , actual , local , reference , true )
81
147
}
82
148
83
- func writeComparison (t * testing.T , actual string , relativeFileName string , useSubmodule bool , opts Options ) {
84
- if actual == "" {
149
+ func writeComparison (t * testing.T , actualContent string , local , reference string , comparingAgainstSubmodule bool ) {
150
+ if actualContent == "" {
85
151
panic ("the generated content was \" \" . Return 'baseline.NoContent' if no baselining is required." )
86
152
}
87
- var (
88
- localFileName string
89
- referenceFileName string
90
- )
91
-
92
- if useSubmodule {
93
- localFileName = submoduleLocalPath (relativeFileName , opts .Subfolder )
94
- referenceFileName = submoduleReferencePath (relativeFileName , opts .Subfolder )
95
- } else {
96
- localFileName = localPath (relativeFileName , opts .Subfolder )
97
- referenceFileName = referencePath (relativeFileName , opts .Subfolder )
98
- }
99
153
100
- if err := os .MkdirAll (filepath .Dir (localFileName ), 0o755 ); err != nil {
101
- t .Error (fmt .Errorf ("failed to create directories for the local baseline file %s: %w" , localFileName , err ))
154
+ if err := os .MkdirAll (filepath .Dir (local ), 0o755 ); err != nil {
155
+ t .Error (fmt .Errorf ("failed to create directories for the local baseline file %s: %w" , local , err ))
102
156
return
103
157
}
104
158
105
- if _ , err := os .Stat (localFileName ); err == nil {
106
- if err := os .Remove (localFileName ); err != nil {
107
- t .Error (fmt .Errorf ("failed to remove the local baseline file %s: %w" , localFileName , err ))
159
+ if _ , err := os .Stat (local ); err == nil {
160
+ if err := os .Remove (local ); err != nil {
161
+ t .Error (fmt .Errorf ("failed to remove the local baseline file %s: %w" , local , err ))
108
162
return
109
163
}
110
164
}
111
165
112
166
expected := NoContent
113
167
foundExpected := false
114
- if content , err := os .ReadFile (referenceFileName ); err == nil {
168
+ if content , err := os .ReadFile (reference ); err == nil {
115
169
expected = string (content )
116
170
foundExpected = true
117
171
}
118
172
119
- if expected != actual || actual == NoContent && foundExpected {
120
- if actual == NoContent {
121
- if err := os .WriteFile (localFileName + ".delete" , []byte {}, 0o644 ); err != nil {
122
- t .Error (fmt .Errorf ("failed to write the local baseline file %s: %w" , localFileName + ".delete" , err ))
173
+ if expected != actualContent || actualContent == NoContent && foundExpected {
174
+ if actualContent == NoContent {
175
+ if err := os .WriteFile (local + ".delete" , []byte {}, 0o644 ); err != nil {
176
+ t .Error (fmt .Errorf ("failed to write the local baseline file %s: %w" , local + ".delete" , err ))
123
177
return
124
178
}
125
179
} else {
126
- if err := os .WriteFile (localFileName , []byte (actual ), 0o644 ); err != nil {
127
- t .Error (fmt .Errorf ("failed to write the local baseline file %s: %w" , localFileName , err ))
180
+ if err := os .WriteFile (local , []byte (actualContent ), 0o644 ); err != nil {
181
+ t .Error (fmt .Errorf ("failed to write the local baseline file %s: %w" , local , err ))
128
182
return
129
183
}
130
184
}
131
185
132
- if _ , err := os .Stat (referenceFileName ); err != nil {
133
- if useSubmodule {
134
- t .Errorf ("the baseline file %s does not exist in the TypeScript submodule" , referenceFileName )
186
+ relReference , err := filepath .Rel (repo .RootPath , reference )
187
+ assert .NilError (t , err )
188
+ relReference = tspath .NormalizeSlashes (relReference )
189
+
190
+ relLocal , err := filepath .Rel (repo .RootPath , local )
191
+ assert .NilError (t , err )
192
+ relLocal = tspath .NormalizeSlashes (relLocal )
193
+
194
+ if _ , err := os .Stat (reference ); err != nil {
195
+ if comparingAgainstSubmodule {
196
+ t .Errorf ("the baseline file %s does not exist in the TypeScript submodule" , relReference )
135
197
} else {
136
- t .Errorf ("new baseline created at %s." , localFileName )
198
+ t .Errorf ("new baseline created at %s." , relLocal )
137
199
}
138
- } else if useSubmodule {
139
- t .Errorf ("the baseline file %s does not match the reference in the TypeScript submodule" , relativeFileName )
200
+ } else if comparingAgainstSubmodule {
201
+ t .Errorf ("the baseline file %s does not match the reference in the TypeScript submodule" , relReference )
140
202
} else {
141
- t .Errorf ("the baseline file %s has changed. (Run `hereby baseline-accept` if the new baseline is correct.)" , relativeFileName )
203
+ t .Errorf ("the baseline file %s has changed. (Run `hereby baseline-accept` if the new baseline is correct.)" , relReference )
142
204
}
143
205
}
144
206
}
145
207
146
- func localPath (fileName string , subfolder string ) string {
147
- return filepath .Join (repo .TestDataPath , "baselines" , "local" , subfolder , fileName )
148
- }
149
-
150
- func submoduleLocalPath (fileName string , subfolder string ) string {
151
- return filepath .Join (repo .TestDataPath , "baselines" , "tmp" , subfolder , fileName )
152
- }
153
-
154
- func referencePath (fileName string , subfolder string ) string {
155
- return filepath .Join (repo .TestDataPath , "baselines" , "reference" , subfolder , fileName )
156
- }
157
-
158
- func submoduleReferencePath (fileName string , subfolder string ) string {
159
- return filepath .Join (repo .TypeScriptSubmodulePath , "tests" , "baselines" , "reference" , subfolder , fileName )
160
- }
208
+ var (
209
+ localRoot = filepath .Join (repo .TestDataPath , "baselines" , "local" )
210
+ referenceRoot = filepath .Join (repo .TestDataPath , "baselines" , "reference" )
211
+ submoduleReferenceRoot = filepath .Join (repo .TypeScriptSubmodulePath , "tests" , "baselines" , "reference" )
212
+ )
0 commit comments