diff options
author | vsrs <[email protected]> | 2020-07-02 19:33:26 +0100 |
---|---|---|
committer | vsrs <[email protected]> | 2020-07-03 12:23:51 +0100 |
commit | 271abb7bc43f11c9b9e9c1353b162d9d267b1d21 (patch) | |
tree | 5bf0cbdcbfa09535dca9db7cc02cbc4d6153889a /editors/code | |
parent | 7b79d24ad5b251c0806a07aa7769e824f3c37fec (diff) |
Add tests
Diffstat (limited to 'editors/code')
-rw-r--r-- | editors/code/src/config.ts | 4 | ||||
-rw-r--r-- | editors/code/src/debug.ts | 2 | ||||
-rw-r--r-- | editors/code/src/run.ts | 14 | ||||
-rw-r--r-- | editors/code/tests/unit/runnable_env.test.ts | 118 |
4 files changed, 128 insertions, 10 deletions
diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index a317aabcb..3257275c5 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts | |||
@@ -5,7 +5,7 @@ export type UpdatesChannel = "stable" | "nightly"; | |||
5 | 5 | ||
6 | export const NIGHTLY_TAG = "nightly"; | 6 | export const NIGHTLY_TAG = "nightly"; |
7 | 7 | ||
8 | export type RunnableEnvCfg = Record<string, string> | [{ mask?: string, env: Record<string, string>; }] | 8 | export type RunnableEnvCfg = undefined | Record<string, string> | { mask?: string, env: Record<string, string>; }[]; |
9 | 9 | ||
10 | export class Config { | 10 | export class Config { |
11 | readonly extensionId = "matklad.rust-analyzer"; | 11 | readonly extensionId = "matklad.rust-analyzer"; |
@@ -117,7 +117,7 @@ export class Config { | |||
117 | } | 117 | } |
118 | 118 | ||
119 | get runnableEnv() { | 119 | get runnableEnv() { |
120 | return this.get<RunnableEnvCfg | undefined>("runnableEnv"); | 120 | return this.get<RunnableEnvCfg>("runnableEnv"); |
121 | } | 121 | } |
122 | 122 | ||
123 | get debug() { | 123 | get debug() { |
diff --git a/editors/code/src/debug.ts b/editors/code/src/debug.ts index 525d26923..bd92c5b6d 100644 --- a/editors/code/src/debug.ts +++ b/editors/code/src/debug.ts | |||
@@ -93,7 +93,7 @@ async function getDebugConfiguration(ctx: Ctx, runnable: ra.Runnable): Promise<v | |||
93 | } | 93 | } |
94 | 94 | ||
95 | const executable = await getDebugExecutable(runnable); | 95 | const executable = await getDebugExecutable(runnable); |
96 | const env = prepareEnv(runnable, ctx.config); | 96 | const env = prepareEnv(runnable, ctx.config.runnableEnv); |
97 | const debugConfig = knownEngines[debugEngine.id](runnable, simplifyPath(executable), env, debugOptions.sourceFileMap); | 97 | const debugConfig = knownEngines[debugEngine.id](runnable, simplifyPath(executable), env, debugOptions.sourceFileMap); |
98 | if (debugConfig.type in debugOptions.engineSettings) { | 98 | if (debugConfig.type in debugOptions.engineSettings) { |
99 | const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type]; | 99 | const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type]; |
diff --git a/editors/code/src/run.ts b/editors/code/src/run.ts index d7c7c489c..4a5c6ad41 100644 --- a/editors/code/src/run.ts +++ b/editors/code/src/run.ts | |||
@@ -5,7 +5,7 @@ import * as tasks from './tasks'; | |||
5 | 5 | ||
6 | import { Ctx } from './ctx'; | 6 | import { Ctx } from './ctx'; |
7 | import { makeDebugConfig } from './debug'; | 7 | import { makeDebugConfig } from './debug'; |
8 | import { Config } from './config'; | 8 | import { Config, RunnableEnvCfg } from './config'; |
9 | 9 | ||
10 | const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }]; | 10 | const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }]; |
11 | 11 | ||
@@ -96,22 +96,22 @@ export class RunnableQuickPick implements vscode.QuickPickItem { | |||
96 | } | 96 | } |
97 | } | 97 | } |
98 | 98 | ||
99 | export function prepareEnv(runnable: ra.Runnable, config: Config): Record<string, string> { | 99 | export function prepareEnv(runnable: ra.Runnable, runnableEnvCfg: RunnableEnvCfg): Record<string, string> { |
100 | const env: Record<string, string> = { "RUST_BACKTRACE": "short" }; | 100 | const env: Record<string, string> = { "RUST_BACKTRACE": "short" }; |
101 | 101 | ||
102 | if (runnable.args.expectTest) { | 102 | if (runnable.args.expectTest) { |
103 | env["UPDATE_EXPECT"] = "1"; | 103 | env["UPDATE_EXPECT"] = "1"; |
104 | } | 104 | } |
105 | 105 | ||
106 | if (config.runnableEnv) { | 106 | if (runnableEnvCfg) { |
107 | if (Array.isArray(config.runnableEnv)) { | 107 | if (Array.isArray(runnableEnvCfg)) { |
108 | for (const it of config.runnableEnv) { | 108 | for (const it of runnableEnvCfg) { |
109 | if (!it.mask || new RegExp(it.mask).test(runnable.label)) { | 109 | if (!it.mask || new RegExp(it.mask).test(runnable.label)) { |
110 | Object.assign(env, it.env); | 110 | Object.assign(env, it.env); |
111 | } | 111 | } |
112 | } | 112 | } |
113 | } else { | 113 | } else { |
114 | Object.assign(env, config.runnableEnv as Record<string, string>); | 114 | Object.assign(env, runnableEnvCfg as Record<string, string>); |
115 | } | 115 | } |
116 | } | 116 | } |
117 | 117 | ||
@@ -136,7 +136,7 @@ export async function createTask(runnable: ra.Runnable, config: Config): Promise | |||
136 | command: args[0], // run, test, etc... | 136 | command: args[0], // run, test, etc... |
137 | args: args.slice(1), | 137 | args: args.slice(1), |
138 | cwd: runnable.args.workspaceRoot, | 138 | cwd: runnable.args.workspaceRoot, |
139 | env: prepareEnv(runnable, config), | 139 | env: prepareEnv(runnable, config.runnableEnv), |
140 | }; | 140 | }; |
141 | 141 | ||
142 | const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate() | 142 | const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate() |
diff --git a/editors/code/tests/unit/runnable_env.test.ts b/editors/code/tests/unit/runnable_env.test.ts new file mode 100644 index 000000000..979d497dd --- /dev/null +++ b/editors/code/tests/unit/runnable_env.test.ts | |||
@@ -0,0 +1,118 @@ | |||
1 | import * as assert from 'assert'; | ||
2 | import { prepareEnv } from '../../src/run'; | ||
3 | import { RunnableEnvCfg } from '../../src/config'; | ||
4 | import * as ra from '../../src/lsp_ext'; | ||
5 | |||
6 | function make_runnable(label: string): ra.Runnable { | ||
7 | return { | ||
8 | label, | ||
9 | kind: "cargo", | ||
10 | args: { | ||
11 | cargoArgs: [], | ||
12 | executableArgs: [] | ||
13 | } | ||
14 | } | ||
15 | } | ||
16 | |||
17 | function fakePrepareEnv(runnable_name: string, config: RunnableEnvCfg) : Record<string, string> { | ||
18 | const runnable = make_runnable(runnable_name); | ||
19 | return prepareEnv(runnable, config); | ||
20 | } | ||
21 | |||
22 | suite('Runnable env', () => { | ||
23 | test('Global config works', () => { | ||
24 | const bin_env = fakePrepareEnv("run project_name", {"GLOBAL": "g"}); | ||
25 | assert.equal(bin_env["GLOBAL"], "g"); | ||
26 | |||
27 | const test_env = fakePrepareEnv("test some::mod::test_name", {"GLOBAL": "g"}); | ||
28 | assert.equal(test_env["GLOBAL"], "g"); | ||
29 | }); | ||
30 | |||
31 | test('null mask works', () => { | ||
32 | const config = [ | ||
33 | { | ||
34 | env: { DATA: "data" } | ||
35 | } | ||
36 | ]; | ||
37 | const bin_env = fakePrepareEnv("run project_name", config); | ||
38 | assert.equal(bin_env["DATA"], "data"); | ||
39 | |||
40 | const test_env = fakePrepareEnv("test some::mod::test_name", config); | ||
41 | assert.equal(test_env["DATA"], "data"); | ||
42 | }); | ||
43 | |||
44 | test('order works', () => { | ||
45 | const config = [ | ||
46 | { | ||
47 | env: { DATA: "data" } | ||
48 | }, | ||
49 | { | ||
50 | env: { DATA: "newdata" } | ||
51 | } | ||
52 | ]; | ||
53 | const bin_env = fakePrepareEnv("run project_name", config); | ||
54 | assert.equal(bin_env["DATA"], "newdata"); | ||
55 | |||
56 | const test_env = fakePrepareEnv("test some::mod::test_name", config); | ||
57 | assert.equal(test_env["DATA"], "newdata"); | ||
58 | }); | ||
59 | |||
60 | test('mask works', () => { | ||
61 | const config = [ | ||
62 | { | ||
63 | env: { DATA: "data" } | ||
64 | }, | ||
65 | { | ||
66 | mask: "^run", | ||
67 | env: { DATA: "rundata" } | ||
68 | }, | ||
69 | { | ||
70 | mask: "special_test$", | ||
71 | env: { DATA: "special_test" } | ||
72 | } | ||
73 | ]; | ||
74 | const bin_env = fakePrepareEnv("run project_name", config); | ||
75 | assert.equal(bin_env["DATA"], "rundata"); | ||
76 | |||
77 | const test_env = fakePrepareEnv("test some::mod::test_name", config); | ||
78 | assert.equal(test_env["DATA"], "data"); | ||
79 | |||
80 | const special_test_env = fakePrepareEnv("test some::mod::special_test", config); | ||
81 | assert.equal(special_test_env["DATA"], "special_test"); | ||
82 | }); | ||
83 | |||
84 | test('exact test name works', () => { | ||
85 | const config = [ | ||
86 | { | ||
87 | env: { DATA: "data" } | ||
88 | }, | ||
89 | { | ||
90 | mask: "some::mod::test_name", | ||
91 | env: { DATA: "test special" } | ||
92 | } | ||
93 | ]; | ||
94 | const test_env = fakePrepareEnv("test some::mod::test_name", config); | ||
95 | assert.equal(test_env["DATA"], "test special"); | ||
96 | |||
97 | const special_test_env = fakePrepareEnv("test some::mod::another_test", config); | ||
98 | assert.equal(special_test_env["DATA"], "data"); | ||
99 | }); | ||
100 | |||
101 | test('test mod name works', () => { | ||
102 | const config = [ | ||
103 | { | ||
104 | env: { DATA: "data" } | ||
105 | }, | ||
106 | { | ||
107 | mask: "some::mod", | ||
108 | env: { DATA: "mod special" } | ||
109 | } | ||
110 | ]; | ||
111 | const test_env = fakePrepareEnv("test some::mod::test_name", config); | ||
112 | assert.equal(test_env["DATA"], "mod special"); | ||
113 | |||
114 | const special_test_env = fakePrepareEnv("test some::mod::another_test", config); | ||
115 | assert.equal(special_test_env["DATA"], "mod special"); | ||
116 | }); | ||
117 | |||
118 | }); | ||