Skip to content

Commit 13aace3

Browse files
committed
remove C: assumption from windows snapshot tests
1 parent f1f413f commit 13aace3

File tree

6 files changed

+76
-58
lines changed

6 files changed

+76
-58
lines changed

internal/bundler_tests/bundler_default_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2568,18 +2568,18 @@ func TestImportAbsPathAsDir(t *testing.T) {
25682568

25692569
default_suite.expectBundledWindows(t, bundled{
25702570
files: map[string]string{
2571-
"/Users/user/project/entry.js": `
2571+
"C:\\Users\\user\\project\\entry.js": `
25722572
import pkg from 'C:\\Users\\user\\project\\node_modules\\pkg'
25732573
console.log(pkg)
25742574
`,
2575-
"/Users/user/project/node_modules/pkg/index.js": `
2575+
"C:\\Users\\user\\project\\node_modules\\pkg\\index.js": `
25762576
export default 123
25772577
`,
25782578
},
2579-
entryPaths: []string{"/Users/user/project/entry.js"},
2579+
entryPaths: []string{"C:\\Users\\user\\project\\entry.js"},
25802580
options: config.Options{
25812581
Mode: config.ModeBundle,
2582-
AbsOutputDir: "/out",
2582+
AbsOutputDir: "C:\\out",
25832583
},
25842584
})
25852585
}
@@ -4731,14 +4731,14 @@ func TestInjectMissing(t *testing.T) {
47314731

47324732
default_suite.expectBundledWindows(t, bundled{
47334733
files: map[string]string{
4734-
"/entry.js": ``,
4734+
"C:\\entry.js": ``,
47354735
},
4736-
entryPaths: []string{"/entry.js"},
4736+
entryPaths: []string{"C:\\entry.js"},
47374737
options: config.Options{
47384738
Mode: config.ModeBundle,
4739-
AbsOutputFile: "/out.js",
4739+
AbsOutputFile: "C:\\out.js",
47404740
InjectPaths: []string{
4741-
"/inject.js",
4741+
"C:\\inject.js",
47424742
},
47434743
},
47444744
expectedScanLog: "ERROR: Could not resolve \"C:\\\\inject.js\"\n",

internal/bundler_tests/bundler_loader_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,11 +1837,11 @@ func TestLoaderInlineSourceMapAbsolutePathIssue4075Windows(t *testing.T) {
18371837

18381838
loader_suite.expectBundledWindows(t, bundled{
18391839
files: map[string]string{
1840-
"/home/user/project/src/entry.css": `
1840+
"C:\\home\\user\\project\\src\\entry.css": `
18411841
@import "./styles1.css";
18421842
@import "./styles2.css";
18431843
`,
1844-
"/home/user/project/src/styles1.css": `/* You can add global styles to this file, and also import other style files */
1844+
"C:\\home\\user\\project\\src\\styles1.css": `/* You can add global styles to this file, and also import other style files */
18451845
* {
18461846
content: "foo";
18471847
}
@@ -1852,7 +1852,7 @@ func TestLoaderInlineSourceMapAbsolutePathIssue4075Windows(t *testing.T) {
18521852
`ut%22,%22sourcesContent%22:%5B%22/*%20You%20can%20add%20global%20styles` +
18531853
`%20to%20this%20file,%20and%20also%20import%20other%20style%20files%20%2` +
18541854
`A/%5Cn*%20%7B%5Cn%20%20content:%20%5C%22foo%5C%22%5Cn%7D%5Cn%22%5D%7D */`,
1855-
"/home/user/project/src/styles2.css": `/* You can add global styles to this file, and also import other style files */
1855+
"C:\\home\\user\\project\\src\\styles2.css": `/* You can add global styles to this file, and also import other style files */
18561856
* {
18571857
content: "bar";
18581858
}
@@ -1864,11 +1864,11 @@ func TestLoaderInlineSourceMapAbsolutePathIssue4075Windows(t *testing.T) {
18641864
`s%20to%20this%20file,%20and%20also%20import%20other%20style%20files%20%` +
18651865
`2A/%5Cn*%20%7B%5Cn%20%20content:%20%5C%22bar%5C%22%5Cn%7D%5Cn%22%5D%7D */`,
18661866
},
1867-
entryPaths: []string{"/home/user/project/src/entry.css"},
1867+
entryPaths: []string{"C:\\home\\user\\project\\src\\entry.css"},
18681868
options: config.Options{
18691869
Mode: config.ModeBundle,
18701870
SourceMap: config.SourceMapLinkedWithComment,
1871-
AbsOutputDir: "/out",
1871+
AbsOutputDir: "C:\\out",
18721872
},
18731873
})
18741874
}

internal/bundler_tests/bundler_test.go

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,47 @@ type suite struct {
7272
func (s *suite) expectBundled(t *testing.T, args bundled) {
7373
t.Helper()
7474
s.__expectBundledImpl(t, args, fs.MockUnix)
75+
76+
// Handle conversion to Windows-style paths
77+
{
78+
files := make(map[string]string)
79+
for k, v := range args.files {
80+
files[unix2win(k)] = v
81+
}
82+
args.files = files
83+
84+
args.entryPaths = append([]string{}, args.entryPaths...)
85+
for i, entry := range args.entryPaths {
86+
args.entryPaths[i] = unix2win(entry)
87+
}
88+
args.absWorkingDir = unix2win(args.absWorkingDir)
89+
90+
args.options.InjectPaths = append([]string{}, args.options.InjectPaths...)
91+
for i, absPath := range args.options.InjectPaths {
92+
args.options.InjectPaths[i] = unix2win(absPath)
93+
}
94+
95+
aliases := make(map[string]string)
96+
for k, v := range args.options.PackageAliases {
97+
if strings.HasPrefix(v, "/") {
98+
v = unix2win(v)
99+
}
100+
aliases[k] = v
101+
}
102+
args.options.PackageAliases = aliases
103+
104+
replace := make(map[string]bool)
105+
for k, v := range args.options.ExternalSettings.PostResolve.Exact {
106+
replace[unix2win(k)] = v
107+
}
108+
args.options.ExternalSettings.PostResolve.Exact = replace
109+
110+
args.options.AbsOutputFile = unix2win(args.options.AbsOutputFile)
111+
args.options.AbsOutputBase = unix2win(args.options.AbsOutputBase)
112+
args.options.AbsOutputDir = unix2win(args.options.AbsOutputDir)
113+
args.options.TSConfigPath = unix2win(args.options.TSConfigPath)
114+
}
115+
75116
s.__expectBundledImpl(t, args, fs.MockWindows)
76117
}
77118

@@ -103,7 +144,11 @@ func (s *suite) __expectBundledImpl(t *testing.T, args bundled, fsKind fs.MockKi
103144
args.options.ExtensionOrder = []string{".tsx", ".ts", ".jsx", ".js", ".css", ".json"}
104145
}
105146
if args.options.AbsOutputFile != "" {
106-
args.options.AbsOutputDir = path.Dir(args.options.AbsOutputFile)
147+
if fsKind == fs.MockWindows {
148+
args.options.AbsOutputDir = unix2win(path.Dir(win2unix(args.options.AbsOutputFile)))
149+
} else {
150+
args.options.AbsOutputDir = path.Dir(args.options.AbsOutputFile)
151+
}
107152
}
108153
if args.options.Mode == config.ModeBundle || (args.options.Mode == config.ModeConvertFormat && args.options.OutputFormat == config.FormatIIFE) {
109154
// Apply this default to all tests since it was not configurable when the tests were written
@@ -123,42 +168,16 @@ func (s *suite) __expectBundledImpl(t *testing.T, args bundled, fsKind fs.MockKi
123168
}
124169
entryPoints = append(entryPoints, args.entryPathsAdvanced...)
125170
if args.absWorkingDir == "" {
126-
args.absWorkingDir = "/"
171+
if fsKind == fs.MockWindows {
172+
args.absWorkingDir = "C:\\"
173+
} else {
174+
args.absWorkingDir = "/"
175+
}
127176
}
128177
if args.options.AbsOutputDir == "" {
129178
args.options.AbsOutputDir = args.absWorkingDir // Match the behavior of the API in this case
130179
}
131180

132-
// Handle conversion to Windows-style paths
133-
if fsKind == fs.MockWindows {
134-
for i, entry := range entryPoints {
135-
entry.InputPath = unix2win(entry.InputPath)
136-
entryPoints[i] = entry
137-
}
138-
args.absWorkingDir = unix2win(args.absWorkingDir)
139-
140-
for i, absPath := range args.options.InjectPaths {
141-
args.options.InjectPaths[i] = unix2win(absPath)
142-
}
143-
144-
for key, value := range args.options.PackageAliases {
145-
if strings.HasPrefix(value, "/") {
146-
args.options.PackageAliases[key] = unix2win(value)
147-
}
148-
}
149-
150-
replace := make(map[string]bool)
151-
for k, v := range args.options.ExternalSettings.PostResolve.Exact {
152-
replace[unix2win(k)] = v
153-
}
154-
args.options.ExternalSettings.PostResolve.Exact = replace
155-
156-
args.options.AbsOutputFile = unix2win(args.options.AbsOutputFile)
157-
args.options.AbsOutputBase = unix2win(args.options.AbsOutputBase)
158-
args.options.AbsOutputDir = unix2win(args.options.AbsOutputDir)
159-
args.options.TSConfigPath = unix2win(args.options.TSConfigPath)
160-
}
161-
162181
// Run the bundler
163182
log := logger.NewDeferLog(logKind, nil)
164183
caches := cache.MakeCacheSet()

internal/bundler_tests/bundler_tsconfig_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -942,18 +942,18 @@ func TestTsconfigJsonExtendsAbsolute(t *testing.T) {
942942

943943
tsconfig_suite.expectBundledWindows(t, bundled{
944944
files: map[string]string{
945-
"/Users/user/project/entry.jsx": `
945+
"C:\\Users\\user\\project\\entry.jsx": `
946946
console.log(<div/>, <></>)
947947
`,
948-
"/Users/user/project/tsconfig.json": `
948+
"C:\\Users\\user\\project\\tsconfig.json": `
949949
{
950950
"extends": "C:\\Users\\user\\project\\base.json",
951951
"compilerOptions": {
952952
"jsxFragmentFactory": "derivedFragment"
953953
}
954954
}
955955
`,
956-
"/Users/user/project/base.json": `
956+
"C:\\Users\\user\\project\\base.json": `
957957
{
958958
"compilerOptions": {
959959
"jsxFactory": "baseFactory",
@@ -962,10 +962,10 @@ func TestTsconfigJsonExtendsAbsolute(t *testing.T) {
962962
}
963963
`,
964964
},
965-
entryPaths: []string{"/Users/user/project/entry.jsx"},
965+
entryPaths: []string{"C:\\Users\\user\\project\\entry.jsx"},
966966
options: config.Options{
967967
Mode: config.ModeBundle,
968-
AbsOutputFile: "/out.js",
968+
AbsOutputFile: "C:\\out.js",
969969
},
970970
})
971971
}

internal/fs/fs_mock.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,18 @@ func MockFS(input map[string]string, kind MockKind, absWorkingDir string) FS {
3030
files := make(map[string]string)
3131

3232
for k, v := range input {
33-
key := k
33+
files[k] = v
3434
if kind == MockWindows {
35-
key = "C:" + strings.ReplaceAll(key, "/", "\\")
35+
k = win2unix(k)
3636
}
37-
files[key] = v
3837
original := k
3938

4039
// Build the directory map
4140
for {
4241
kDir := path.Dir(k)
4342
key := kDir
4443
if kind == MockWindows {
45-
key = "C:" + strings.ReplaceAll(key, "/", "\\")
44+
key = unix2win(key)
4645
}
4746
dir, ok := dirs[key]
4847
if !ok {

internal/fs/fs_mock_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ func TestMockFSBasicUnix(t *testing.T) {
7474

7575
func TestMockFSBasicWindows(t *testing.T) {
7676
fs := MockFS(map[string]string{
77-
"/README.md": "// README.md",
78-
"/package.json": "// package.json",
79-
"/src/index.js": "// src/index.js",
80-
"/src/util.js": "// src/util.js",
77+
"C:\\README.md": "// README.md",
78+
"C:\\package.json": "// package.json",
79+
"C:\\src\\index.js": "// src/index.js",
80+
"C:\\src\\util.js": "// src/util.js",
8181
}, MockWindows, "C:\\")
8282

8383
// Test a missing file

0 commit comments

Comments
 (0)