Skip to content

Commit 8da8946

Browse files
committed
fix: unit tests
1 parent 0cca6a4 commit 8da8946

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/common/urlUtils.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
*--------------------------------------------------------*/
44
import { expect } from 'chai';
55
import { promises as dns } from 'dns';
6-
import * as os from 'os';
76
import { SinonStub, stub } from 'sinon';
87
import {
98
createTargetFilter,
109
fileUrlToAbsolutePath,
1110
isLoopback,
11+
overridePlatform,
1212
resetCaseSensitivePaths,
13+
resetPlatform,
1314
setCaseSensitivePaths,
1415
urlToRegex,
1516
} from './urlUtils';
@@ -27,11 +28,11 @@ describe('urlUtils', () => {
2728
});
2829

2930
it('ensures local path starts with / on OSX', () => {
30-
const platform = stub(os, 'platform').returns('darwin');
31+
overridePlatform('darwin');
3132
expect(fileUrlToAbsolutePath('file:///Users/scripts/app.js')).to.equal(
3233
'/Users/scripts/app.js',
3334
);
34-
platform.restore();
35+
resetPlatform();
3536
});
3637

3738
it('force lowercase drive letter on Win to match VS Code', () => {

src/common/urlUtils.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ export function fileUrlToNetworkPath(urlOrPath: string): string {
270270

271271
// TODO: this does not escape/unescape special characters, but it should.
272272
export function absolutePathToFileUrl(absolutePath: string): string {
273-
if (process.platform === 'win32') {
273+
if (platform === 'win32') {
274274
return 'file:///' + platformPathToUrlPath(absolutePath);
275275
}
276276
return 'file://' + platformPathToUrlPath(absolutePath);
@@ -422,8 +422,18 @@ export function maybeAbsolutePathToFileUrl(
422422
return sourceUrl;
423423
}
424424

425+
let platform = process.platform;
426+
427+
export const overridePlatform = (newPlatform: NodeJS.Platform) => {
428+
platform = newPlatform;
429+
};
430+
431+
export const resetPlatform = () => {
432+
platform = process.platform;
433+
};
434+
425435
export function urlPathToPlatformPath(p: string): string {
426-
if (process.platform === 'win32') {
436+
if (platform === 'win32') {
427437
p = p.replace(/\//g, '\\');
428438
}
429439

@@ -433,7 +443,7 @@ export function urlPathToPlatformPath(p: string): string {
433443
export function platformPathToUrlPath(p: string): string {
434444
p = platformPathToPreferredCase(p);
435445

436-
if (process.platform === 'win32') {
446+
if (platform === 'win32') {
437447
return p
438448
.split(/[\\//]/g)
439449
.map((p, i) => (i > 0 ? encodeURIComponent(p) : p))
@@ -446,7 +456,7 @@ export function platformPathToUrlPath(p: string): string {
446456
export function platformPathToPreferredCase(p: string): string;
447457
export function platformPathToPreferredCase(p: string | undefined): string | undefined;
448458
export function platformPathToPreferredCase(p: string | undefined): string | undefined {
449-
if (p && process.platform === 'win32' && p[1] === ':') return p[0].toUpperCase() + p.substring(1);
459+
if (p && platform === 'win32' && p[1] === ':') return p[0].toUpperCase() + p.substring(1);
450460
return p;
451461
}
452462

0 commit comments

Comments
 (0)