Skip to content

Commit

Permalink
Merge pull request #159 from kondratyev-nv/fix_cwd_resolution
Browse files Browse the repository at this point in the history
Fix cwd resolution
  • Loading branch information
kondratyev-nv committed Jun 11, 2020
2 parents 89b91b9 + 394ae05 commit 14b8e5f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
26 changes: 24 additions & 2 deletions src/configuration/placeholderAwareWorkspaceConfiguration.ts
Expand Up @@ -17,7 +17,7 @@ export class PlaceholderAwareWorkspaceConfiguration implements IWorkspaceConfigu
) { }

public pythonPath(): string {
return this.resolvePath(this.configuration.pythonPath());
return this.resolveExecutablePath(this.configuration.pythonPath());
}

public getCwd(): string {
Expand Down Expand Up @@ -71,11 +71,19 @@ export class PlaceholderAwareWorkspaceConfiguration implements IWorkspaceConfigu
});
}

private resolveExecutablePath(rawValue: string): string {
return this.normalizeExecutablePath(this.resolvePlaceholders(rawValue));
}

private resolvePath(rawValue: string): string {
return this.normalizePath(this.resolvePlaceholders(rawValue));
}

private normalizePath(originalValue: string): string {
// Executable can be in multiple formats:
// 1. 'command' - globally available executable (path to the file in is in PATH)
// 2. './command' - relative path to the workspace folder
// 3. '/bin/command' - absolute path
private normalizeExecutablePath(originalValue: string): string {
const value = untildify(originalValue);
if (value.includes(path.posix.sep) || value.includes(path.win32.sep)) {
const absolutePath = path.isAbsolute(value) ?
Expand All @@ -85,4 +93,18 @@ export class PlaceholderAwareWorkspaceConfiguration implements IWorkspaceConfigu
}
return value;
}

// Path to a file can be in multiple formats:
// 1. './file' - relative path to the workspace folder
// 2. '/some/path/file' - absolute path
// We do not consider 'file' as a valid path.
// For example, path in format as 'path' can not be used as a cwd in spawn,
// see https://github.com/kondratyev-nv/vscode-python-test-adapter/issues/158
private normalizePath(originalValue: string): string {
const value = untildify(originalValue);
const absolutePath = path.isAbsolute(value) ?
path.resolve(value) :
path.resolve(this.workspaceFolder.uri.fsPath, value);
return path.normalize(absolutePath);
}
}
36 changes: 36 additions & 0 deletions test/tests/placeholderAwareWorkspaceConfiguration.test.ts
Expand Up @@ -141,6 +141,42 @@ suite('Placeholder aware workspace configuration', () => {
expect(configuration.getCwd()).to.be.eq(path.normalize(path.resolve(wfPath, 'some', 'cwd', 'suffix')));
});

test('should resolve relative path from configuration', () => {
const configuration = getConfiguration({
pythonPath(): string {
return 'python';
},
getCwd(): string {
return 'some_cwd';
},
envFile(): string {
return '~/.env';
},
getUnittestConfiguration(): IUnittestConfiguration {
return {
isUnittestEnabled: true,
unittestArguments: {
startDirectory: 'test',
pattern: 'test_*.py',
},
};
},
getPytestConfiguration(): IPytestConfiguration {
return {
isPytestEnabled: true,
pytestArguments: [],
};
},
});

const wfPath = getWorkspaceFolder().uri.fsPath;
expect(configuration.pythonPath()).to.be.eq('python');
expect(configuration.getCwd()).to.be.eq(path.normalize(path.resolve(wfPath, 'some_cwd')));
expect(
configuration.getUnittestConfiguration().unittestArguments.startDirectory
).to.be.eq(path.normalize(path.resolve(wfPath, 'test')));
});

test('should resolve home path from configuration', () => {
process.env.SOME_RELATIVE_PATH_USED_IN_CWD = '../suffix';
expect(Object.keys(process.env)).to.include('SOME_RELATIVE_PATH_USED_IN_CWD');
Expand Down
28 changes: 28 additions & 0 deletions test/tests/pytestGeneral.test.ts
Expand Up @@ -104,6 +104,34 @@ suite('Pytest test discovery', async () => {
});
});

suite('Pytest test discovery with relative cwd folder', async () => {
const config: IWorkspaceConfiguration = createPytestConfiguration(
'pytest',
['--ignore=import_error_tests', '--doctest-modules'],
'test'
);
const runner = new PytestTestRunner('some-id', logger());

test('should discover tests', async () => {
const mainSuite = await runner.load(config);
expect(mainSuite).to.be.not.undefined;
expect(extractErroredTests(mainSuite!)).to.be.empty;
const expectedSuites = [
'describe_test.py',
'env_variables_test.py',
'fixture_test.py',
'generate_test.py',
'inner_fixture_test.py',
'string_test.py',
'subprocess_test.py',
'add_test.py',
'add_test.py'
];
const labels = mainSuite!.children.map(x => x.label);
expect(labels).to.have.members(expectedSuites);
});
});

suite('Run pytest tests', () => {
const config: IWorkspaceConfiguration = createPytestConfiguration(
'pytest',
Expand Down
4 changes: 2 additions & 2 deletions test/utils/helpers.ts
Expand Up @@ -79,15 +79,15 @@ export function findWorkspaceFolder(folder: string): vscode.WorkspaceFolder | un
return vscode.workspace.workspaceFolders!.find(f => f.name === folder);
}

export function createPytestConfiguration(folder: string, args?: string[]): IWorkspaceConfiguration {
export function createPytestConfiguration(folder: string, args?: string[], cwd?: string): IWorkspaceConfiguration {
const python = getPythonExecutable();
const wf = findWorkspaceFolder(folder)!;
return new PlaceholderAwareWorkspaceConfiguration({
pythonPath(): string {
return python;
},
getCwd(): string {
return wf.uri.fsPath;
return cwd || wf.uri.fsPath;
},
envFile(): string {
return path.join(wf.uri.fsPath, '..', '.env');
Expand Down

0 comments on commit 14b8e5f

Please sign in to comment.