Skip to content

Commit 2c9498e

Browse files
segevfinerlgeiger
authored andcommitted
Split detectVirtualEnv to a separate file and add specs
1 parent d0a5a87 commit 2c9498e

File tree

8 files changed

+99
-44
lines changed

8 files changed

+99
-44
lines changed

lib/main.js

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
const cp = require("child_process");
22
const { shell } = require("electron");
33
const { AutoLanguageClient } = require("atom-languageclient");
4-
const { Directory } = require("atom");
4+
const { detectVirtualEnv } = require("./utils");
55

66
// Ref: https://github.com/nteract/hydrogen/blob/master/lib/autocomplete-provider.js#L33
77
// adapted from http://stackoverflow.com/q/5474008
88
const PYTHON_REGEX = /(([^\d\W]|[\u00A0-\uFFFF])[\w.\u00A0-\uFFFF]*)|\.$/;
99

10-
const VIRTUAL_ENV_BIN_DIRS = ["bin", "Scripts"];
11-
const VIRTUAL_ENV_EXECUTABLES = ["python", "python.exe"];
12-
1310
class PythonLanguageClient extends AutoLanguageClient {
1411
getGrammarScopes() {
1512
return ["source.python"];
@@ -31,49 +28,10 @@ class PythonLanguageClient extends AutoLanguageClient {
3128
return { pyls: { plugins: configuration } }
3229
}
3330

34-
async detectVirtualEnv(path) {
35-
const entries = await new Promise(resolve =>
36-
new Directory(path).getEntries((error, entries) => {
37-
if (error === null) {
38-
resolve(entries);
39-
} else {
40-
resolve(null);
41-
}
42-
})
43-
);
44-
45-
if (entries) {
46-
for (let entry of entries) {
47-
if (entry.isDirectory()) {
48-
if (VIRTUAL_ENV_BIN_DIRS.indexOf(entry.getBaseName()) !== -1) {
49-
for (let executable of VIRTUAL_ENV_EXECUTABLES) {
50-
if (await entry.getFile(executable).exists()) {
51-
return path;
52-
}
53-
}
54-
} else {
55-
for (let dir_name of VIRTUAL_ENV_BIN_DIRS) {
56-
for (let executable of VIRTUAL_ENV_EXECUTABLES) {
57-
if (
58-
await entry
59-
.getSubdirectory(dir_name)
60-
.getFile(executable)
61-
.exists()
62-
) {
63-
return entry.getPath();
64-
}
65-
}
66-
}
67-
}
68-
}
69-
}
70-
}
71-
}
72-
7331
async startServerProcess(projectPath) {
7432
await new Promise(resolve => atom.whenShellEnvironmentLoaded(resolve));
7533

76-
const venvPath = await this.detectVirtualEnv(projectPath)
34+
const venvPath = await detectVirtualEnv(projectPath)
7735

7836
const pylsEnvironment = Object.assign({}, process.env);
7937

lib/utils.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const { Directory } = require("atom");
2+
3+
const VIRTUAL_ENV_BIN_DIRS = ["bin", "Scripts"];
4+
const VIRTUAL_ENV_EXECUTABLES = ["python", "python.exe"];
5+
6+
async function detectVirtualEnv(path) {
7+
const entries = await new Promise(resolve =>
8+
new Directory(path).getEntries((error, entries) => {
9+
if (error === null) {
10+
resolve(entries);
11+
} else {
12+
resolve(null);
13+
}
14+
})
15+
);
16+
17+
if (entries) {
18+
for (let entry of entries) {
19+
if (entry.isDirectory()) {
20+
if (VIRTUAL_ENV_BIN_DIRS.indexOf(entry.getBaseName()) !== -1) {
21+
for (let executable of VIRTUAL_ENV_EXECUTABLES) {
22+
if (await entry.getFile(executable).exists()) {
23+
return path;
24+
}
25+
}
26+
} else {
27+
for (let dir_name of VIRTUAL_ENV_BIN_DIRS) {
28+
for (let executable of VIRTUAL_ENV_EXECUTABLES) {
29+
if (
30+
await entry
31+
.getSubdirectory(dir_name)
32+
.getFile(executable)
33+
.exists()
34+
) {
35+
return entry.getPath();
36+
}
37+
}
38+
}
39+
}
40+
}
41+
}
42+
}
43+
}
44+
45+
exports.detectVirtualEnv = detectVirtualEnv;

spec/fixtures/venv/none/.gitignore

Whitespace-only changes.

spec/fixtures/venv/unix-subdir/venv/bin/python

Whitespace-only changes.

spec/fixtures/venv/unix/bin/python

Whitespace-only changes.

spec/fixtures/venv/windows-subdir/venv/Scripts/python

Whitespace-only changes.

spec/fixtures/venv/windows/Scripts/python.exe

Whitespace-only changes.

spec/utils-spec.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const path = require("path");
2+
const { detectVirtualEnv } = require("../lib/utils");
3+
4+
const venvFixturesDir = path.join(__dirname, "fixtures", "venv");
5+
6+
describe("detectVirtualEnv", () => {
7+
it("detects no virtual env when there isn't any", () => {
8+
waitsForPromise(() => {
9+
return detectVirtualEnv(path.join(venvFixturesDir, "none")).then(venv => {
10+
expect(venv).toBeUndefined();
11+
});
12+
});
13+
});
14+
15+
it("detects a Unix virtual env at the root", () => {
16+
const projectPath = path.join(venvFixturesDir, "unix");
17+
waitsForPromise(() => {
18+
return detectVirtualEnv(projectPath).then(venv => {
19+
expect(venv).toEqual(projectPath);
20+
});
21+
});
22+
});
23+
24+
it("detects a Windows virtual env at the root", () => {
25+
const projectPath = path.join(venvFixturesDir, "windows");
26+
waitsForPromise(() => {
27+
return detectVirtualEnv(projectPath).then(venv => {
28+
expect(venv).toEqual(projectPath);
29+
});
30+
});
31+
});
32+
33+
it("detects a Unix virtual env in a subdirectory", () => {
34+
const projectPath = path.join(venvFixturesDir, "unix-subdir");
35+
const venvPath = path.join(projectPath, "venv");
36+
waitsForPromise(() => {
37+
return detectVirtualEnv(projectPath).then(venv => {
38+
expect(venv).toEqual(venvPath);
39+
});
40+
});
41+
});
42+
43+
it("detects a Windows virtual env in a subdirectory", () => {
44+
const projectPath = path.join(venvFixturesDir, "windows-subdir");
45+
const venvPath = path.join(projectPath, "venv");
46+
waitsForPromise(() => {
47+
return detectVirtualEnv(projectPath).then(venv => {
48+
expect(venv).toEqual(venvPath);
49+
});
50+
});
51+
});
52+
});

0 commit comments

Comments
 (0)