Add tests

This commit is contained in:
vsrs 2020-07-02 21:33:26 +03:00
parent 7b79d24ad5
commit 271abb7bc4
4 changed files with 128 additions and 10 deletions

View file

@ -5,7 +5,7 @@ export type UpdatesChannel = "stable" | "nightly";
export const NIGHTLY_TAG = "nightly"; export const NIGHTLY_TAG = "nightly";
export type RunnableEnvCfg = Record<string, string> | [{ mask?: string, env: Record<string, string>; }] export type RunnableEnvCfg = undefined | Record<string, string> | { mask?: string, env: Record<string, string>; }[];
export class Config { export class Config {
readonly extensionId = "matklad.rust-analyzer"; readonly extensionId = "matklad.rust-analyzer";
@ -117,7 +117,7 @@ export class Config {
} }
get runnableEnv() { get runnableEnv() {
return this.get<RunnableEnvCfg | undefined>("runnableEnv"); return this.get<RunnableEnvCfg>("runnableEnv");
} }
get debug() { get debug() {

View file

@ -93,7 +93,7 @@ async function getDebugConfiguration(ctx: Ctx, runnable: ra.Runnable): Promise<v
} }
const executable = await getDebugExecutable(runnable); const executable = await getDebugExecutable(runnable);
const env = prepareEnv(runnable, ctx.config); const env = prepareEnv(runnable, ctx.config.runnableEnv);
const debugConfig = knownEngines[debugEngine.id](runnable, simplifyPath(executable), env, debugOptions.sourceFileMap); const debugConfig = knownEngines[debugEngine.id](runnable, simplifyPath(executable), env, debugOptions.sourceFileMap);
if (debugConfig.type in debugOptions.engineSettings) { if (debugConfig.type in debugOptions.engineSettings) {
const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type]; const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type];

View file

@ -5,7 +5,7 @@ import * as tasks from './tasks';
import { Ctx } from './ctx'; import { Ctx } from './ctx';
import { makeDebugConfig } from './debug'; import { makeDebugConfig } from './debug';
import { Config } from './config'; import { Config, RunnableEnvCfg } from './config';
const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }]; const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }];
@ -96,22 +96,22 @@ export class RunnableQuickPick implements vscode.QuickPickItem {
} }
} }
export function prepareEnv(runnable: ra.Runnable, config: Config): Record<string, string> { export function prepareEnv(runnable: ra.Runnable, runnableEnvCfg: RunnableEnvCfg): Record<string, string> {
const env: Record<string, string> = { "RUST_BACKTRACE": "short" }; const env: Record<string, string> = { "RUST_BACKTRACE": "short" };
if (runnable.args.expectTest) { if (runnable.args.expectTest) {
env["UPDATE_EXPECT"] = "1"; env["UPDATE_EXPECT"] = "1";
} }
if (config.runnableEnv) { if (runnableEnvCfg) {
if (Array.isArray(config.runnableEnv)) { if (Array.isArray(runnableEnvCfg)) {
for (const it of config.runnableEnv) { for (const it of runnableEnvCfg) {
if (!it.mask || new RegExp(it.mask).test(runnable.label)) { if (!it.mask || new RegExp(it.mask).test(runnable.label)) {
Object.assign(env, it.env); Object.assign(env, it.env);
} }
} }
} else { } else {
Object.assign(env, config.runnableEnv as Record<string, string>); Object.assign(env, runnableEnvCfg as Record<string, string>);
} }
} }
@ -136,7 +136,7 @@ export async function createTask(runnable: ra.Runnable, config: Config): Promise
command: args[0], // run, test, etc... command: args[0], // run, test, etc...
args: args.slice(1), args: args.slice(1),
cwd: runnable.args.workspaceRoot, cwd: runnable.args.workspaceRoot,
env: prepareEnv(runnable, config), env: prepareEnv(runnable, config.runnableEnv),
}; };
const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate() const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate()

View file

@ -0,0 +1,118 @@
import * as assert from 'assert';
import { prepareEnv } from '../../src/run';
import { RunnableEnvCfg } from '../../src/config';
import * as ra from '../../src/lsp_ext';
function make_runnable(label: string): ra.Runnable {
return {
label,
kind: "cargo",
args: {
cargoArgs: [],
executableArgs: []
}
}
}
function fakePrepareEnv(runnable_name: string, config: RunnableEnvCfg) : Record<string, string> {
const runnable = make_runnable(runnable_name);
return prepareEnv(runnable, config);
}
suite('Runnable env', () => {
test('Global config works', () => {
const bin_env = fakePrepareEnv("run project_name", {"GLOBAL": "g"});
assert.equal(bin_env["GLOBAL"], "g");
const test_env = fakePrepareEnv("test some::mod::test_name", {"GLOBAL": "g"});
assert.equal(test_env["GLOBAL"], "g");
});
test('null mask works', () => {
const config = [
{
env: { DATA: "data" }
}
];
const bin_env = fakePrepareEnv("run project_name", config);
assert.equal(bin_env["DATA"], "data");
const test_env = fakePrepareEnv("test some::mod::test_name", config);
assert.equal(test_env["DATA"], "data");
});
test('order works', () => {
const config = [
{
env: { DATA: "data" }
},
{
env: { DATA: "newdata" }
}
];
const bin_env = fakePrepareEnv("run project_name", config);
assert.equal(bin_env["DATA"], "newdata");
const test_env = fakePrepareEnv("test some::mod::test_name", config);
assert.equal(test_env["DATA"], "newdata");
});
test('mask works', () => {
const config = [
{
env: { DATA: "data" }
},
{
mask: "^run",
env: { DATA: "rundata" }
},
{
mask: "special_test$",
env: { DATA: "special_test" }
}
];
const bin_env = fakePrepareEnv("run project_name", config);
assert.equal(bin_env["DATA"], "rundata");
const test_env = fakePrepareEnv("test some::mod::test_name", config);
assert.equal(test_env["DATA"], "data");
const special_test_env = fakePrepareEnv("test some::mod::special_test", config);
assert.equal(special_test_env["DATA"], "special_test");
});
test('exact test name works', () => {
const config = [
{
env: { DATA: "data" }
},
{
mask: "some::mod::test_name",
env: { DATA: "test special" }
}
];
const test_env = fakePrepareEnv("test some::mod::test_name", config);
assert.equal(test_env["DATA"], "test special");
const special_test_env = fakePrepareEnv("test some::mod::another_test", config);
assert.equal(special_test_env["DATA"], "data");
});
test('test mod name works', () => {
const config = [
{
env: { DATA: "data" }
},
{
mask: "some::mod",
env: { DATA: "mod special" }
}
];
const test_env = fakePrepareEnv("test some::mod::test_name", config);
assert.equal(test_env["DATA"], "mod special");
const special_test_env = fakePrepareEnv("test some::mod::another_test", config);
assert.equal(special_test_env["DATA"], "mod special");
});
});