Skip to content

Commit c64340c

Browse files
committed
refactor helper
1 parent 0d3e576 commit c64340c

4 files changed

Lines changed: 85 additions & 62 deletions

File tree

src/test/managers/conda/condaEnvManager.findEnvironmentByPath.unit.test.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,11 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import assert from 'assert';
33
import * as sinon from 'sinon';
4-
import { Uri } from 'vscode';
54
import { PythonEnvironment, PythonEnvironmentApi } from '../../../api';
65
import { isWindows } from '../../../common/utils/platformUtils';
7-
import { PythonEnvironmentImpl } from '../../../internal.api';
86
import { CondaEnvManager } from '../../../managers/conda/condaEnvManager';
97
import { NativePythonFinder } from '../.././../managers/common/nativePythonFinder';
10-
11-
/**
12-
* Helper to create a minimal PythonEnvironment stub with required fields.
13-
* Only `name`, `environmentPath`, and `version` matter for findEnvironmentByPath.
14-
*/
15-
function makeEnv(name: string, envPath: string, version: string = '3.12.0'): PythonEnvironment {
16-
return new PythonEnvironmentImpl(
17-
{ id: `${name}-test`, managerId: 'ms-python.python:conda' },
18-
{
19-
name,
20-
displayName: `${name} (${version})`,
21-
displayPath: envPath,
22-
version,
23-
environmentPath: Uri.file(envPath),
24-
sysPrefix: envPath,
25-
execInfo: {
26-
run: { executable: 'python' },
27-
},
28-
},
29-
);
30-
}
8+
import { makeMockPythonEnvironment as makeEnv } from '../../mocks/pythonEnvironment';
319

3210
/**
3311
* Creates a CondaEnvManager with a given collection, bypassing initialization.

src/test/managers/conda/condaEnvManager.setEvents.unit.test.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,12 @@
22
import assert from 'assert';
33
import * as sinon from 'sinon';
44
import { Uri } from 'vscode';
5-
import { DidChangeEnvironmentEventArgs, PythonEnvironment, PythonEnvironmentApi, PythonProject } from '../../../api';
5+
import { DidChangeEnvironmentEventArgs, PythonEnvironmentApi, PythonProject } from '../../../api';
66
import { normalizePath } from '../../../common/utils/pathUtils';
7-
import { PythonEnvironmentImpl } from '../../../internal.api';
87
import { CondaEnvManager } from '../../../managers/conda/condaEnvManager';
98
import * as condaUtils from '../../../managers/conda/condaUtils';
109
import { NativePythonFinder } from '../../../managers/common/nativePythonFinder';
11-
12-
function makeEnv(name: string, envPath: string, version: string = '3.12.0'): PythonEnvironment {
13-
return new PythonEnvironmentImpl(
14-
{ id: `${name}-test`, managerId: 'ms-python.python:conda' },
15-
{
16-
name,
17-
displayName: `${name} (${version})`,
18-
displayPath: envPath,
19-
version,
20-
environmentPath: Uri.file(envPath),
21-
sysPrefix: envPath,
22-
execInfo: {
23-
run: { executable: 'python' },
24-
},
25-
},
26-
);
27-
}
10+
import { makeMockPythonEnvironment as makeEnv } from '../../mocks/pythonEnvironment';
2811

2912
function createManager(apiOverrides?: Partial<PythonEnvironmentApi>): CondaEnvManager {
3013
const api = {

src/test/managers/conda/condaEnvManager.setGlobal.unit.test.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,11 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import assert from 'assert';
33
import * as sinon from 'sinon';
4-
import { Uri } from 'vscode';
5-
import { PythonEnvironment, PythonEnvironmentApi } from '../../../api';
6-
import { PythonEnvironmentImpl } from '../../../internal.api';
4+
import { PythonEnvironmentApi } from '../../../api';
75
import { CondaEnvManager } from '../../../managers/conda/condaEnvManager';
86
import * as condaUtils from '../../../managers/conda/condaUtils';
97
import { NativePythonFinder } from '../../../managers/common/nativePythonFinder';
10-
11-
function makeEnv(name: string, envPath: string, version: string = '3.12.0'): PythonEnvironment {
12-
return new PythonEnvironmentImpl(
13-
{ id: `${name}-test`, managerId: 'ms-python.python:conda' },
14-
{
15-
name,
16-
displayName: `${name} (${version})`,
17-
displayPath: envPath,
18-
version,
19-
environmentPath: Uri.file(envPath),
20-
sysPrefix: envPath,
21-
execInfo: {
22-
run: { executable: 'python' },
23-
},
24-
},
25-
);
26-
}
8+
import { makeMockPythonEnvironment as makeEnv } from '../../mocks/pythonEnvironment';
279

2810
function createManager(): CondaEnvManager {
2911
const manager = new CondaEnvManager(
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import { Uri } from 'vscode';
5+
import { PythonEnvironment } from '../../api';
6+
import { PythonEnvironmentImpl } from '../../internal.api';
7+
8+
/**
9+
* Options for {@link createMockPythonEnvironment}.
10+
*/
11+
export interface MockPythonEnvironmentOptions {
12+
/** Environment name, e.g. `myenv`. Defaults to `test-env`. */
13+
name?: string;
14+
/** Filesystem path for `environmentPath`, `displayPath`, and `sysPrefix`. */
15+
envPath: string;
16+
/** Version string. Defaults to `3.12.0`. */
17+
version?: string;
18+
/** Manager id. Defaults to `ms-python.python:conda`. */
19+
managerId?: string;
20+
/** Environment id. Defaults to `<name>-test`. */
21+
id?: string;
22+
/** Optional description. */
23+
description?: string;
24+
/** Optional display name. Defaults to `<name> (<version>)`. */
25+
displayName?: string;
26+
/** If true, includes an `activation` entry in `execInfo`. */
27+
hasActivation?: boolean;
28+
}
29+
30+
/**
31+
* Create a minimal {@link PythonEnvironment} for use in unit tests.
32+
*
33+
* Shared across manager and view tests so they agree on the shape of a mock
34+
* environment. Only fields that tests commonly need are populated; extend this
35+
* helper if additional fields become required.
36+
*/
37+
export function createMockPythonEnvironment(options: MockPythonEnvironmentOptions): PythonEnvironment {
38+
const {
39+
name = 'test-env',
40+
envPath,
41+
version = '3.12.0',
42+
managerId = 'ms-python.python:conda',
43+
id = `${name}-test`,
44+
description,
45+
displayName = `${name} (${version})`,
46+
hasActivation = false,
47+
} = options;
48+
49+
return new PythonEnvironmentImpl(
50+
{ id, managerId },
51+
{
52+
name,
53+
displayName,
54+
displayPath: envPath,
55+
version,
56+
description,
57+
environmentPath: Uri.file(envPath),
58+
sysPrefix: envPath,
59+
execInfo: {
60+
run: { executable: 'python' },
61+
...(hasActivation && {
62+
activation: [{ executable: envPath.replace('python', 'activate') }],
63+
}),
64+
},
65+
},
66+
);
67+
}
68+
69+
/**
70+
* Positional shorthand for {@link createMockPythonEnvironment} used by conda
71+
* manager unit tests. Prefer {@link createMockPythonEnvironment} for new tests
72+
* that need to customize additional fields.
73+
*/
74+
export function makeMockPythonEnvironment(
75+
name: string,
76+
envPath: string,
77+
version: string = '3.12.0',
78+
): PythonEnvironment {
79+
return createMockPythonEnvironment({ name, envPath, version });
80+
}

0 commit comments

Comments
 (0)