Skip to content

Visual Studio Code, rebuilt in Rust

Rust
4.2 KB
Raw Blame
import { expect, test } from 'bun:test';
import { chmodSync, cpSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';

// Runs port/tools/cargo-test with a stand-in cargo that prints `log`, exits with `status` and records its arguments.
function cargoTest(log: string, status: number, args: string[] = []) {
	const root = mkdtempSync(join(tmpdir(), 'cargo-test-test-'));
	mkdirSync(join(root, 'port/tools'), { recursive: true });
	mkdirSync(join(root, 'native'));
	mkdirSync(join(root, 'bin'));
	cpSync(join(import.meta.dir, 'cargo-test'), join(root, 'port/tools/cargo-test'));
	writeFileSync(join(root, 'log'), log);
	writeFileSync(join(root, 'bin/cargo'), `#!/bin/bash\n[[ $1 == gc ]] || exit 1\nshift\necho "$@" > ${root}/args\ncat ${root}/log\nexit ${status}\n`);
	chmodSync(join(root, 'bin/cargo'), 0o755);
	// An exported bash function named cargo (BASH_FUNC_cargo%%) would win over the stand-in on PATH.
	const env = Object.fromEntries(Object.entries(process.env).filter(([name]) => !name.startsWith('BASH_FUNC_')));
	const run = Bun.spawnSync(['bash', join(root, 'port/tools/cargo-test'), ...args], { env: { ...env, PATH: `${join(root, 'bin')}:${process.env.PATH}` } });
	const result = { exitCode: run.exitCode, stdout: run.stdout.toString(), stderr: run.stderr.toString(), args: readFileSync(join(root, 'args'), 'utf8').trim() };
	rmSync(root, { recursive: true });
	return result;
}

const compiling = '   Compiling app v0.1.0\n    Finished `test` profile\n     Running unittests src/main.rs\n';

test('a passing run prints only the summary of the targets that ran tests', () => {
	const run = cargoTest(`${compiling}running 2 tests\ntest a ... ok\ntest b ... ok\n\ntest result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 9 filtered out; finished in 0.01s\n\nrunning 0 tests\n\ntest result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 3 filtered out; finished in 0.00s\n`, 0, ['-p', 'config', 'menus']);
	expect(run.exitCode).toBe(0);
	expect(run.stdout).toBe('test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 9 filtered out; finished in 0.01s\n');
	expect(run.args).toBe('test -p config -- menus');
});

test('a filter that matches no test fails', () => {
	const run = cargoTest(`${compiling}running 0 tests\n\ntest result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 12 filtered out; finished in 0.00s\n`, 0, ['typo']);
	expect(run.exitCode).toBe(1);
	expect(run.stderr).toContain('no test of app matches: typo');
	expect(run.args).toBe('test -p app -- typo');
});

test('a failing test prints its failures block and keeps cargo\'s exit code', () => {
	const failures = "failures:\n\n---- a stdout ----\nthread 'a' panicked at src/a.rs:3:5:\nleft: 1\nright: 2\n\n\nfailures:\n    a\n\n";
	const run = cargoTest(`${compiling}running 2 tests\ntest a ... FAILED\ntest b ... ok\n\n${failures}test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s\n\nerror: test failed, to rerun pass \`-p app --bin vsrustium\`\n`, 101);
	expect(run.exitCode).toBe(101);
	expect(run.stdout).toBe(`${failures}test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s\nerror: test failed, to rerun pass \`-p app --bin vsrustium\`\n`);
});

test('a compile error prints its diagnostic with the location', () => {
	const run = cargoTest('   Compiling app v0.1.0\nwarning: unused import\n --> src/b.rs:1:5\n\nerror[E0425]: cannot find function `x`\n --> src/a.rs:4:5\n  |\n4 |     x();\n  |     ^ not found\n\nerror: could not compile `app` (bin "vsrustium" test) due to 1 previous error\n', 101);
	expect(run.exitCode).toBe(101);
	expect(run.stdout).toBe('error[E0425]: cannot find function `x`\n --> src/a.rs:4:5\n  |\n4 |     x();\n  |     ^ not found\n\nerror: could not compile `app` (bin "vsrustium" test) due to 1 previous error\n');
});

test('a failure without an error or failures block shows the end of the log', () => {
	const run = cargoTest(`${compiling}running 3 tests\nprocess didn't exit successfully (signal: 9, SIGKILL: kill)\n`, 101);
	expect(run.exitCode).toBe(101);
	expect(run.stdout).toBe(`${compiling}running 3 tests\nprocess didn't exit successfully (signal: 9, SIGKILL: kill)\n`);
});