Skip to content

Commit

Permalink
Merge pull request #28 from andrewiggins/windows-test-fixes
Browse files Browse the repository at this point in the history
Fix tests on Windows
  • Loading branch information
usergenic authored Apr 10, 2020
2 parents e6a921f + e1c3eaa commit 0f4ecf9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/koa-node-resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {parse as parseURL} from 'url';
import {moduleSpecifierTransform, ModuleSpecifierTransformOptions} from './koa-module-specifier-transform';
import {prefixedLogger} from './support/logger';
import {Logger} from './support/logger';
import {noLeadingSlash} from './support/path-utils';
import {noLeadingSlashInURL} from './support/path-utils';
import {resolveNodeSpecifier} from './support/resolve-node-specifier';

export {Logger} from './support/logger';
Expand All @@ -42,7 +42,7 @@ export const nodeResolve =
resolveNodeSpecifier(
resolvePath(
resolvePath(options.root || '.'),
noLeadingSlash(parseURL(baseURL).pathname || '/')),
noLeadingSlashInURL(parseURL(baseURL).pathname || '/')),
specifier,
logger),
Object.assign({}, options, {logger}));
Expand Down
24 changes: 13 additions & 11 deletions src/support/path-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,33 @@
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/
import {relative} from 'path';
import {posix, sep as pathSeparator} from 'path';

const dirnameRegex = process.platform === 'win32' ? /[^\\]+$/ : /[^\/]+$/;

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

export const ensureLeadingDot = (path: string): string =>
export const ensureLeadingDotInURL = (path: string): string =>
(path.startsWith('../') || path.startsWith('./')) ? path : './' + path;

export const ensureTrailingSlash = (path: string): string =>
path.endsWith('/') ? path : path + '/';
export const ensureTrailingSlashInPath = (path: string): string =>
path.endsWith(pathSeparator) ? path : path + pathSeparator;

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

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

export const noLeadingSlash = (href: string): string => href.replace(/^\//, '');
export const noLeadingSlashInURL = (href: string): string =>
href.replace(/^\//, '');

export const relativePath = (from: string, to: string): string =>
ensureLeadingDot(relative(
getBasePath(forwardSlashesOnlyPlease(from)),
export const relativePathToUrl = (from: string, to: string): string =>
ensureLeadingDotInURL(posix.relative(
getBaseUrl(forwardSlashesOnlyPlease(from)),
forwardSlashesOnlyPlease(to)));
4 changes: 2 additions & 2 deletions src/support/resolve-node-specifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import nodeResolve from 'resolve';

import {Logger} from './logger';

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

export const resolveNodeSpecifier =
(modulePath: string, specifier: string, logger: Logger): string => {
Expand All @@ -35,7 +35,7 @@ export const resolveNodeSpecifier =
packageJson.main
})
});
const resolvedURL = relativePath(modulePath, dependencyPath);
const resolvedURL = relativePathToUrl(modulePath, dependencyPath);
if (resolvedURL !== specifier) {
logger.debug &&
logger.debug(`Resolved Node module specifier "${specifier}" to "${
Expand Down
4 changes: 2 additions & 2 deletions src/test/resolve-node-specifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import {resolve as resolvePath} from 'path';
import test from 'tape';

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

const logger = testLogger();
const fixturesPath =
ensureTrailingSlash(resolvePath(__dirname, '../../test/fixtures/'));
ensureTrailingSlashInPath(resolvePath(__dirname, '../../test/fixtures/'));
const resolve = (specifier: string): string =>
resolveNodeSpecifier(fixturesPath, specifier, logger);

Expand Down

0 comments on commit 0f4ecf9

Please sign in to comment.