Skip to content

Commit 9f0699f

Browse files
committed
unit tests: run using both windows and unix paths
1 parent 1f7273c commit 9f0699f

10 files changed

+394
-47
lines changed

internal/bundler/bundler.go

+5
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,11 @@ func lowestCommonAncestorDirectory(fs fs.FS, entryPoints []graph.EntryPoint) str
16321632
lastSlash = a
16331633
}
16341634
} else if boundaryA != boundaryB || unicode.ToLower(runeA) != unicode.ToLower(runeB) {
1635+
// If we're at the top-level directory, then keep the slash
1636+
if lastSlash < len(absDir) && !strings.ContainsAny(absDir[:lastSlash], "\\/") {
1637+
lastSlash++
1638+
}
1639+
16351640
// If both paths are different at this point, stop and set the lowest so
16361641
// far to the common parent directory. Compare using a case-insensitive
16371642
// comparison to handle paths on Windows.

internal/bundler/bundler_default_test.go

+36-5
Original file line numberDiff line numberDiff line change
@@ -2416,7 +2416,7 @@ func TestImportAbsPathAsFile(t *testing.T) {
24162416
}
24172417

24182418
func TestImportAbsPathAsDir(t *testing.T) {
2419-
default_suite.expectBundled(t, bundled{
2419+
default_suite.expectBundledUnix(t, bundled{
24202420
files: map[string]string{
24212421
"/Users/user/project/entry.js": `
24222422
import pkg from '/Users/user/project/node_modules/pkg'
@@ -2432,6 +2432,23 @@ func TestImportAbsPathAsDir(t *testing.T) {
24322432
AbsOutputDir: "/out",
24332433
},
24342434
})
2435+
2436+
default_suite.expectBundledWindows(t, bundled{
2437+
files: map[string]string{
2438+
"/Users/user/project/entry.js": `
2439+
import pkg from 'C:\\Users\\user\\project\\node_modules\\pkg'
2440+
console.log(pkg)
2441+
`,
2442+
"/Users/user/project/node_modules/pkg/index.js": `
2443+
export default 123
2444+
`,
2445+
},
2446+
entryPaths: []string{"/Users/user/project/entry.js"},
2447+
options: config.Options{
2448+
Mode: config.ModeBundle,
2449+
AbsOutputDir: "/out",
2450+
},
2451+
})
24352452
}
24362453

24372454
func TestAutoExternal(t *testing.T) {
@@ -3898,7 +3915,7 @@ func TestMinifyArguments(t *testing.T) {
38983915
}
38993916

39003917
func TestWarningsInsideNodeModules(t *testing.T) {
3901-
default_suite.expectBundled(t, bundled{
3918+
default_suite.expectBundledUnix(t, bundled{
39023919
files: map[string]string{
39033920
"/entry.js": `
39043921
import "./dup-case.js"; import "./node_modules/dup-case.js"; import "@plugin/dup-case.js"
@@ -4055,7 +4072,7 @@ entry.js: WARNING: "@scope/missing-pkg" should be marked as external for use wit
40554072
}
40564073

40574074
func TestInjectMissing(t *testing.T) {
4058-
default_suite.expectBundled(t, bundled{
4075+
default_suite.expectBundledUnix(t, bundled{
40594076
files: map[string]string{
40604077
"/entry.js": ``,
40614078
},
@@ -4067,8 +4084,22 @@ func TestInjectMissing(t *testing.T) {
40674084
"/inject.js",
40684085
},
40694086
},
4070-
expectedScanLog: `ERROR: Could not read from file: /inject.js
4071-
`,
4087+
expectedScanLog: "ERROR: Could not read from file: /inject.js\n",
4088+
})
4089+
4090+
default_suite.expectBundledWindows(t, bundled{
4091+
files: map[string]string{
4092+
"/entry.js": ``,
4093+
},
4094+
entryPaths: []string{"/entry.js"},
4095+
options: config.Options{
4096+
Mode: config.ModeBundle,
4097+
AbsOutputFile: "/out.js",
4098+
InjectAbsPaths: []string{
4099+
"/inject.js",
4100+
},
4101+
},
4102+
expectedScanLog: "ERROR: Could not read from file: C:\\inject.js\n",
40724103
})
40734104
}
40744105

internal/bundler/bundler_test.go

+74-6
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,34 @@ type suite struct {
6969

7070
func (s *suite) expectBundled(t *testing.T, args bundled) {
7171
t.Helper()
72+
s.__expectBundledImpl(t, args, fs.MockUnix)
73+
s.__expectBundledImpl(t, args, fs.MockWindows)
74+
}
75+
76+
func (s *suite) expectBundledUnix(t *testing.T, args bundled) {
77+
t.Helper()
78+
s.__expectBundledImpl(t, args, fs.MockUnix)
79+
}
80+
81+
func (s *suite) expectBundledWindows(t *testing.T, args bundled) {
82+
t.Helper()
83+
s.__expectBundledImpl(t, args, fs.MockWindows)
84+
}
85+
86+
// Don't call this directly. Call the helpers above instead.
87+
func (s *suite) __expectBundledImpl(t *testing.T, args bundled, fsKind fs.MockKind) {
88+
t.Helper()
89+
7290
testName := t.Name()
73-
t.Run("", func(t *testing.T) {
91+
subName := "Unix"
92+
if fsKind == fs.MockWindows {
93+
subName = "Windows"
94+
}
95+
96+
t.Run(subName, func(t *testing.T) {
7497
t.Helper()
75-
fs := fs.MockFS(args.files)
98+
99+
// Prepare the options
76100
if args.options.ExtensionOrder == nil {
77101
args.options.ExtensionOrder = []string{".tsx", ".ts", ".jsx", ".js", ".css", ".json"}
78102
}
@@ -87,15 +111,40 @@ func (s *suite) expectBundled(t *testing.T, args bundled) {
87111
if args.debugLogs {
88112
logKind = logger.DeferLogAll
89113
}
90-
log := logger.NewDeferLog(logKind, nil)
91-
caches := cache.MakeCacheSet()
92-
resolver := resolver.NewResolver(fs, log, caches, args.options)
93114
entryPoints := make([]EntryPoint, 0, len(args.entryPaths)+len(args.entryPathsAdvanced))
94115
for _, path := range args.entryPaths {
95116
entryPoints = append(entryPoints, EntryPoint{InputPath: path})
96117
}
97118
entryPoints = append(entryPoints, args.entryPathsAdvanced...)
98-
bundle := ScanBundle(log, fs, resolver, caches, entryPoints, args.options, nil)
119+
120+
// Handle conversion to Windows-style paths
121+
if fsKind == fs.MockWindows {
122+
for i, entry := range entryPoints {
123+
entry.InputPath = unix2win(entry.InputPath)
124+
entryPoints[i] = entry
125+
}
126+
127+
for i, absPath := range args.options.InjectAbsPaths {
128+
args.options.InjectAbsPaths[i] = unix2win(absPath)
129+
}
130+
131+
replace := make(map[string]bool)
132+
for k, v := range args.options.ExternalSettings.PostResolve.Exact {
133+
replace[unix2win(k)] = v
134+
}
135+
args.options.ExternalSettings.PostResolve.Exact = replace
136+
137+
args.options.AbsOutputFile = unix2win(args.options.AbsOutputFile)
138+
args.options.AbsOutputDir = unix2win(args.options.AbsOutputDir)
139+
args.options.TsConfigOverride = unix2win(args.options.TsConfigOverride)
140+
}
141+
142+
// Run the bundler
143+
log := logger.NewDeferLog(logKind, nil)
144+
caches := cache.MakeCacheSet()
145+
mockFS := fs.MockFS(args.files, fsKind)
146+
resolver := resolver.NewResolver(mockFS, log, caches, args.options)
147+
bundle := ScanBundle(log, mockFS, resolver, caches, entryPoints, args.options, nil)
99148
msgs := log.Done()
100149
assertLog(t, msgs, args.expectedScanLog)
101150

@@ -124,6 +173,9 @@ func (s *suite) expectBundled(t *testing.T, args bundled) {
124173
if generated != "" {
125174
generated += "\n"
126175
}
176+
if fsKind == fs.MockWindows {
177+
result.AbsPath = win2unix(result.AbsPath)
178+
}
127179
generated += fmt.Sprintf("---------- %s ----------\n%s", result.AbsPath, string(result.Contents))
128180
}
129181
}
@@ -235,3 +287,19 @@ func TestMain(m *testing.M) {
235287
}
236288
os.Exit(code)
237289
}
290+
291+
func win2unix(p string) string {
292+
if strings.HasPrefix(p, "C:\\") {
293+
p = p[2:]
294+
}
295+
p = strings.ReplaceAll(p, "\\", "/")
296+
return p
297+
}
298+
299+
func unix2win(p string) string {
300+
p = strings.ReplaceAll(p, "/", "\\")
301+
if strings.HasPrefix(p, "\\") {
302+
p = "C:" + p
303+
}
304+
return p
305+
}

internal/bundler/bundler_tsconfig_test.go

+30-1
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ func TestTsconfigJsonExtends(t *testing.T) {
859859
}
860860

861861
func TestTsconfigJsonExtendsAbsolute(t *testing.T) {
862-
tsconfig_suite.expectBundled(t, bundled{
862+
tsconfig_suite.expectBundledUnix(t, bundled{
863863
files: map[string]string{
864864
"/Users/user/project/entry.jsx": `
865865
console.log(<div/>, <></>)
@@ -887,6 +887,35 @@ func TestTsconfigJsonExtendsAbsolute(t *testing.T) {
887887
AbsOutputFile: "/out.js",
888888
},
889889
})
890+
891+
tsconfig_suite.expectBundledWindows(t, bundled{
892+
files: map[string]string{
893+
"/Users/user/project/entry.jsx": `
894+
console.log(<div/>, <></>)
895+
`,
896+
"/Users/user/project/tsconfig.json": `
897+
{
898+
"extends": "C:\\Users\\user\\project\\base.json",
899+
"compilerOptions": {
900+
"jsxFragmentFactory": "derivedFragment"
901+
}
902+
}
903+
`,
904+
"/Users/user/project/base.json": `
905+
{
906+
"compilerOptions": {
907+
"jsxFactory": "baseFactory",
908+
"jsxFragmentFactory": "baseFragment"
909+
}
910+
}
911+
`,
912+
},
913+
entryPaths: []string{"/Users/user/project/entry.jsx"},
914+
options: config.Options{
915+
Mode: config.ModeBundle,
916+
AbsOutputFile: "/out.js",
917+
},
918+
})
890919
}
891920

892921
func TestTsconfigJsonExtendsThreeLevels(t *testing.T) {

0 commit comments

Comments
 (0)