Skip to content

Commit

Permalink
convert drive letter for windows path
Browse files Browse the repository at this point in the history
  • Loading branch information
connectdotz committed May 19, 2021
1 parent 2a3b524 commit 584b070
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 86 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Please add your own contribution below inside the Master section
Bug-fixes within the same version aren't needed
## Master
* fix debug problem for windows users (#694) - @connectdotz
-->

### 4.0.1
Expand Down
11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-jest",
"displayName": "Jest",
"description": "Use Facebook's Jest With Pleasure.",
"version": "4.0.2-rc.1",
"version": "4.0.2",
"publisher": "Orta",
"engines": {
"vscode": "^1.45.0"
Expand Down Expand Up @@ -318,8 +318,7 @@
"program": "^\"\\${workspaceFolder}/node_modules/.bin/jest\"",
"args": [
"--runInBand",
"--runTestsByPath",
"${file}"
"--watchAll=false"
],
"cwd": "^\"\\${workspaceFolder}\"",
"console": "integratedTerminal",
Expand All @@ -342,8 +341,7 @@
"test",
"--env=jsdom",
"--runInBand",
"--runTestsByPath",
"${file}"
"--watchAll=false"
],
"cwd": "^\"\\${workspaceFolder}\"",
"console": "integratedTerminal",
Expand All @@ -363,8 +361,7 @@
"args": [
"--env=jsdom",
"--runInBand",
"--runTestsByPath",
"${file}"
"--watchAll=false"
],
"cwd": "^\"\\${workspaceFolder}\"",
"console": "integratedTerminal",
Expand Down
22 changes: 9 additions & 13 deletions src/DebugConfigurationProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import { escapeFilePath, getTestCommand, isCreateReactAppTestCommand } from './helpers';
import { toFilePath, getTestCommand, isCreateReactAppTestCommand } from './helpers';

export class DebugConfigurationProvider implements vscode.DebugConfigurationProvider {
private fileNameToRun = '';
Expand Down Expand Up @@ -29,25 +29,21 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
// necessary for running CRA test scripts in non-watch mode
debugConfiguration.env.CI = 'vscode-jest-tests';

if (!debugConfiguration.args) {
debugConfiguration.args = [];
}
const args = debugConfiguration.args || [];

if (this.fileNameToRun) {
if (this.testToRun) {
debugConfiguration.args.push('--testNamePattern');
debugConfiguration.args.push(this.testToRun);
}
if (!debugConfiguration.args.includes('--runTestsByPath')) {
debugConfiguration.args.push('--runTestsByPath');
}
if (!debugConfiguration.args.includes('${file}')) {
debugConfiguration.args.push(escapeFilePath(this.fileNameToRun));
args.push('--testNamePattern');
args.push(this.testToRun);
}
args.push('--runTestsByPath');
args.push(toFilePath(this.fileNameToRun));

this.fileNameToRun = '';
this.testToRun = '';
}

debugConfiguration.args = args;
return debugConfiguration;
}

Expand All @@ -66,7 +62,7 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
type: 'node',
name: 'vscode-jest-tests',
request: 'launch',
args: ['--runInBand', '--runTestsByPath', '${file}'],
args: ['--runInBand', '--watchAll=false'],
cwd: '${workspaceFolder}',
console: 'integratedTerminal',
internalConsoleOptions: 'neverOpen',
Expand Down
12 changes: 4 additions & 8 deletions src/JestProcessManagement/JestProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { extensionId } from '../appGlobals';
import { Logging } from '../logging';
import { JestProcessRequest } from './types';
import { requestString } from './helper';
import { escapeFilePath, removeSurroundingQuote } from '../helpers';
import { toFilePath, removeSurroundingQuote } from '../helpers';

export const RunnerEvents: RunnerEvent[] = [
'processClose',
Expand Down Expand Up @@ -90,7 +90,7 @@ export class JestProcess {
return join(extensionPath, 'out', 'reporter.js');
}
private quoteFileName(fileName: string): string {
return `"${removeSurroundingQuote(fileName)}"`;
return `"${toFilePath(removeSurroundingQuote(fileName))}"`;
}
private startRunner(): Promise<void> {
if (this.task) {
Expand All @@ -110,9 +110,7 @@ export class JestProcess {
}
break;
case 'by-file': {
options.testFileNamePattern = this.quoteFileName(
escapeFilePath(this.request.testFileNamePattern)
);
options.testFileNamePattern = this.quoteFileName(this.request.testFileNamePattern);
const args: string[] = ['--findRelatedTests'];
if (this.request.updateSnapshot) {
args.push('--updateSnapshot');
Expand All @@ -122,9 +120,7 @@ export class JestProcess {
}

case 'by-file-test': {
options.testFileNamePattern = this.quoteFileName(
escapeFilePath(this.request.testFileNamePattern)
);
options.testFileNamePattern = this.quoteFileName(this.request.testFileNamePattern);
options.testNamePattern = this.request.testNamePattern;
const args: string[] = ['--runTestsByPath'];
if (this.request.updateSnapshot) {
Expand Down
13 changes: 3 additions & 10 deletions src/TestResults/TestResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TestReconciliationStateType } from './TestReconciliationState';
import { JestFileResults, JestTotalResults } from 'jest-editor-support';
import { FileCoverage } from 'istanbul-lib-coverage';
import * as path from 'path';
import { cleanAnsi } from '../helpers';
import { cleanAnsi, toLowerCaseDriveLetter } from '../helpers';

export interface Location {
/** Zero-based column number */
Expand Down Expand Up @@ -46,15 +46,8 @@ export interface TestResult extends LocationRange {
reason?: MatchResultReason;
}

export const withLowerCaseWindowsDriveLetter = (filePath: string): string | undefined => {
const match = filePath.match(/^([A-Z]:\\)(.*)$/);
if (match) {
return `${match[1].toLowerCase()}${match[2]}`;
}
};

function testResultWithLowerCaseWindowsDriveLetter(testResult: JestFileResults): JestFileResults {
const newFilePath = withLowerCaseWindowsDriveLetter(testResult.name);
const newFilePath = toLowerCaseDriveLetter(testResult.name);
if (newFilePath) {
return {
...testResult,
Expand All @@ -76,7 +69,7 @@ export const testResultsWithLowerCaseWindowsDriveLetters = (
};

function fileCoverageWithLowerCaseWindowsDriveLetter(fileCoverage: FileCoverage) {
const newFilePath = withLowerCaseWindowsDriveLetter(fileCoverage.path);
const newFilePath = toLowerCaseDriveLetter(fileCoverage.path);
if (newFilePath) {
return {
...fileCoverage,
Expand Down
22 changes: 19 additions & 3 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,28 @@ export function testIdString(type: IdStringType, identifier: TestIdentifier): st
}
}

/** convert the upper-case drive letter filePath to lower-case. If path does not contain upper-case drive letter, returns undefined. */
// note: this should probably be replaced by vscode.URI.file(filePath).fsPath ...
export function toLowerCaseDriveLetter(filePath: string): string | undefined {
const match = filePath.match(/^([A-Z]:\\)(.*)$/);
if (match) {
return `${match[1].toLowerCase()}${match[2]}`;
}
}
/** convert the lower-case drive letter filePath (like vscode.URI.fsPath) to lower-case. If path does not contain lower-case drive letter, returns undefined. */
export function toUpperCaseDriveLetter(filePath: string): string | undefined {
const match = filePath.match(/^([a-z]:\\)(.*)$/);
if (match) {
return `${match[1].toUpperCase()}${match[2]}`;
}
}

/**
* escape windows file path '\' to '\\', see https://jestjs.io/docs/cli#jest-regexfortestfiles
* convert vscode.URI.fsPath to the actual file system file-path, i.e. convert drive letter to upper-case for windows
* @param filePath
*/
export function escapeFilePath(filePath: string): string {
return filePath.replace(/\\/g, '\\\\');
export function toFilePath(filePath: string): string {
return toUpperCaseDriveLetter(filePath) || filePath;
}

/**
Expand Down
47 changes: 20 additions & 27 deletions tests/DebugConfigurationProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
jest.unmock('../src/DebugConfigurationProvider');

import { DebugConfigurationProvider } from '../src/DebugConfigurationProvider';
import { getTestCommand, isCreateReactAppTestCommand, escapeFilePath } from '../src/helpers';
import { getTestCommand, isCreateReactAppTestCommand, toFilePath } from '../src/helpers';

describe('DebugConfigurationProvider', () => {
const fileName = '/a/file';
const testName = 'a test';

it('should by default return a DebugConfiguration for Jest', () => {
const folder: any = { uri: { fsPath: null } };
const sut = new DebugConfigurationProvider();
Expand Down Expand Up @@ -35,38 +38,28 @@ describe('DebugConfigurationProvider', () => {
expect(config.args[0]).toBe('test');
expect(config.args).toContain('--env=jsdom');
expect(config.args).toContain('--runInBand');
expect(config.args).toContain('--watchAll=false');
});

it.each`
debugConfigArgs | expectByPathArg | expectFileNameArg
${[]} | ${true} | ${true}
${['--runTestsByPath']} | ${false} | ${true}
${['--runTestsByPath', '${file}']} | ${false} | ${false}
`(
'should append the specified tests arguments',
({ debugConfigArgs, expectByPathArg, expectFileNameArg }) => {
((escapeFilePath as unknown) as jest.Mock<{}>).mockImplementation((s) => s);
const fileName = 'fileName';
const testNamePattern = 'testNamePattern';
const expected = [...debugConfigArgs, '--testNamePattern', testNamePattern];
debugConfigArgs | expectedArgs
${[]} | ${['--testNamePattern', testName, '--runTestsByPath', fileName]}
${['--runInBand']} | ${['--runInBand', '--testNamePattern', testName, '--runTestsByPath', fileName]}
`('should append the specified tests arguments', ({ debugConfigArgs, expectedArgs }) => {
((toFilePath as unknown) as jest.Mock<{}>).mockImplementation((s) => s);

let configuration: any = { name: 'vscode-jest-tests', args: debugConfigArgs };
let configuration: any = { name: 'vscode-jest-tests', args: debugConfigArgs };

const sut = new DebugConfigurationProvider();
sut.prepareTestRun(fileName, testNamePattern);
const sut = new DebugConfigurationProvider();
sut.prepareTestRun(fileName, testName);

configuration = sut.resolveDebugConfiguration(undefined, configuration);
configuration = sut.resolveDebugConfiguration(undefined, configuration);

expect(configuration).toBeDefined();
expect(configuration.env && configuration.env.CI).toBeTruthy();
if (expectByPathArg) {
expected.push('--runTestsByPath');
}
if (expectFileNameArg) {
expected.push(fileName);
expect(escapeFilePath).toBeCalled();
}
expect(configuration.args).toEqual(expected);
expect(configuration).toBeDefined();
expect(configuration.env && configuration.env.CI).toBeTruthy();
if (expectedArgs.includes('--runTestsByPath')) {
expect(toFilePath).toBeCalled();
}
);
expect(configuration.args).toEqual(expectedArgs);
});
});
2 changes: 1 addition & 1 deletion tests/JestProcessManagement/JestProcess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('JestProcess', () => {
${'all-tests'} | ${undefined} | ${[false, false]} | ${true} | ${undefined}
${'watch-tests'} | ${undefined} | ${[true, false]} | ${true} | ${undefined}
${'watch-all-tests'} | ${undefined} | ${[true, true]} | ${true} | ${undefined}
${'by-file'} | ${{ testFileNamePattern: '"c:\\a\\b.ts"' }} | ${[false, false]} | ${true} | ${{ args: { args: ['--findRelatedTests'] }, testFileNamePattern: '"c:\\\\a\\\\b.ts"' }}
${'by-file'} | ${{ testFileNamePattern: '"c:\\a\\b.ts"' }} | ${[false, false]} | ${true} | ${{ args: { args: ['--findRelatedTests'] }, testFileNamePattern: '"C:\\a\\b.ts"' }}
${'by-file-test'} | ${{ testFileNamePattern: '"/a/b.js"', testNamePattern: 'test' }} | ${[false, false]} | ${true} | ${{ args: { args: ['--runTestsByPath'] }, testFileNamePattern: '"/a/b.js"' }}
${'not-test'} | ${{ args: ['--listTests'] }} | ${[false, false]} | ${false} | ${{ args: { args: ['--listTests'], replace: true } }}
`(
Expand Down
13 changes: 0 additions & 13 deletions tests/TestResults/TestResult.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const {
coverageMapWithLowerCaseWindowsDriveLetters,
testResultsWithLowerCaseWindowsDriveLetters,
resultsWithoutAnsiEscapeSequence,
withLowerCaseWindowsDriveLetter,
} = TestResult;

describe('TestResult', () => {
Expand Down Expand Up @@ -137,16 +136,4 @@ describe('TestResult', () => {
});
});
});

describe('withLowerCaseDriveLetter', () => {
it('should return a new file path when provided a path with an upper case drive letter', () => {
const filePath = 'C:\\path\\file.ext';
expect(withLowerCaseWindowsDriveLetter(filePath)).toBe('c:\\path\\file.ext');
});

it('should indicate no change is required otherwise', () => {
const filePath = 'c:\\path\\file.ext';
expect(withLowerCaseWindowsDriveLetter(filePath)).toBeUndefined();
});
});
});
36 changes: 32 additions & 4 deletions tests/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ import {
testIdString,
escapeRegExp,
removeSurroundingQuote,
escapeFilePath,
toFilePath,
toLowerCaseDriveLetter,
toUpperCaseDriveLetter,
} from '../src/helpers';

// Manually (forcefully) set the executable's file extension to test its addition independendly of the operating system.
Expand Down Expand Up @@ -253,14 +255,40 @@ describe('removeSurroundingQuote', () => {
});
});

describe('escapeFilePath', () => {
describe('toFilePath', () => {
it.each`
path | expected
${'/a/b/c'} | ${'/a/b/c'}
${'C:/a/b/c.js'} | ${'C:/a/b/c.js'}
${'c:\\a\\b\\c.ts'} | ${'c:\\\\a\\\\b\\\\c.ts'}
${'c:/a/b/c.js'} | ${'c:/a/b/c.js'}
${'z:\\a\\b\\c.js'} | ${'Z:\\a\\b\\c.js'}
${'\\a\\b\\c.js'} | ${'\\a\\b\\c.js'}
${''} | ${''}
`('escape $path => $expected', ({ path, expected }) => {
expect(escapeFilePath(path)).toEqual(expected);
expect(toFilePath(path)).toEqual(expected);
});
});

describe('toLowerCaseDriveLetter', () => {
it.each`
filePath | expected
${'C:\\path\\file.ext'} | ${'c:\\path\\file.ext'}
${'c:\\path\\file.ext'} | ${undefined}
${'c:/path/file.ext'} | ${undefined}
${'/path/file.ext'} | ${undefined}
`('$filePath => $expected', ({ filePath, expected }) => {
expect(toLowerCaseDriveLetter(filePath)).toBe(expected);
});
});

describe('toUpperCaseDriveLetter', () => {
it.each`
filePath | expected
${'C:\\path\\file.ext'} | ${undefined}
${'c:\\path\\file.ext'} | ${'C:\\path\\file.ext'}
${'c:/path/file.ext'} | ${undefined}
${'/path/file.ext'} | ${undefined}
`('$filePath => $expected', ({ filePath, expected }) => {
expect(toUpperCaseDriveLetter(filePath)).toBe(expected);
});
});

0 comments on commit 584b070

Please sign in to comment.