Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 0f4ecf9

Browse files
authored
Merge pull request #28 from andrewiggins/windows-test-fixes
Fix tests on Windows
2 parents e6a921f + e1c3eaa commit 0f4ecf9

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

src/koa-node-resolve.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {parse as parseURL} from 'url';
1818
import {moduleSpecifierTransform, ModuleSpecifierTransformOptions} from './koa-module-specifier-transform';
1919
import {prefixedLogger} from './support/logger';
2020
import {Logger} from './support/logger';
21-
import {noLeadingSlash} from './support/path-utils';
21+
import {noLeadingSlashInURL} from './support/path-utils';
2222
import {resolveNodeSpecifier} from './support/resolve-node-specifier';
2323

2424
export {Logger} from './support/logger';
@@ -42,7 +42,7 @@ export const nodeResolve =
4242
resolveNodeSpecifier(
4343
resolvePath(
4444
resolvePath(options.root || '.'),
45-
noLeadingSlash(parseURL(baseURL).pathname || '/')),
45+
noLeadingSlashInURL(parseURL(baseURL).pathname || '/')),
4646
specifier,
4747
logger),
4848
Object.assign({}, options, {logger}));

src/support/path-utils.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,33 @@
1111
* subject to an additional IP rights grant found at
1212
* http://polymer.github.io/PATENTS.txt
1313
*/
14-
import {relative} from 'path';
14+
import {posix, sep as pathSeparator} from 'path';
15+
16+
const dirnameRegex = process.platform === 'win32' ? /[^\\]+$/ : /[^\/]+$/;
1517

1618
/**
1719
* Similar to `path.dirname()` except includes trailing slash and for a
1820
* path `/like/this/` will return `/like/this/` instead of `/like` since the
1921
* trailing slash indicates `this` is a folder name not a file name.
2022
* (`path.dirname('/like/this/')` returns `/like`.)
2123
*/
22-
export const dirname = (path: string): string => path.replace(/[^\/]+$/, '');
24+
export const dirname = (path: string): string => path.replace(dirnameRegex, '');
2325

24-
export const ensureLeadingDot = (path: string): string =>
26+
export const ensureLeadingDotInURL = (path: string): string =>
2527
(path.startsWith('../') || path.startsWith('./')) ? path : './' + path;
2628

27-
export const ensureTrailingSlash = (path: string): string =>
28-
path.endsWith('/') ? path : path + '/';
29+
export const ensureTrailingSlashInPath = (path: string): string =>
30+
path.endsWith(pathSeparator) ? path : path + pathSeparator;
2931

3032
export const forwardSlashesOnlyPlease = (path: string): string =>
3133
path.replace(/\\/g, '/');
3234

33-
export const getBasePath = (href: string): string =>
34-
href.replace(/[^\/]+$/, '');
35+
export const getBaseUrl = (href: string): string => href.replace(/[^\/]+$/, '');
3536

36-
export const noLeadingSlash = (href: string): string => href.replace(/^\//, '');
37+
export const noLeadingSlashInURL = (href: string): string =>
38+
href.replace(/^\//, '');
3739

38-
export const relativePath = (from: string, to: string): string =>
39-
ensureLeadingDot(relative(
40-
getBasePath(forwardSlashesOnlyPlease(from)),
40+
export const relativePathToUrl = (from: string, to: string): string =>
41+
ensureLeadingDotInURL(posix.relative(
42+
getBaseUrl(forwardSlashesOnlyPlease(from)),
4143
forwardSlashesOnlyPlease(to)));

src/support/resolve-node-specifier.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import nodeResolve from 'resolve';
1515

1616
import {Logger} from './logger';
1717

18-
import {dirname, relativePath} from './path-utils';
18+
import {dirname, relativePathToUrl} from './path-utils';
1919

2020
export const resolveNodeSpecifier =
2121
(modulePath: string, specifier: string, logger: Logger): string => {
@@ -35,7 +35,7 @@ export const resolveNodeSpecifier =
3535
packageJson.main
3636
})
3737
});
38-
const resolvedURL = relativePath(modulePath, dependencyPath);
38+
const resolvedURL = relativePathToUrl(modulePath, dependencyPath);
3939
if (resolvedURL !== specifier) {
4040
logger.debug &&
4141
logger.debug(`Resolved Node module specifier "${specifier}" to "${

src/test/resolve-node-specifier.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
import {resolve as resolvePath} from 'path';
1515
import test from 'tape';
1616

17-
import {ensureTrailingSlash} from '../support/path-utils';
17+
import {ensureTrailingSlashInPath} from '../support/path-utils';
1818
import {resolveNodeSpecifier} from '../support/resolve-node-specifier';
1919
import {testLogger} from './test-utils';
2020

2121
const logger = testLogger();
2222
const fixturesPath =
23-
ensureTrailingSlash(resolvePath(__dirname, '../../test/fixtures/'));
23+
ensureTrailingSlashInPath(resolvePath(__dirname, '../../test/fixtures/'));
2424
const resolve = (specifier: string): string =>
2525
resolveNodeSpecifier(fixturesPath, specifier, logger);
2626

0 commit comments

Comments
 (0)