Skip to content

Visual Studio Code, rebuilt in Rust

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

// A repo with one crate file (`open_window`, and an unsafe block), the architecture text, the unsafe inventory and the
// crate table given.
function check(architecture: string, inventory = '| `app/src/main.rs` | libc | a fact |', crateTable = '| `app` | The binary. |', glossary = '') {
	const root = mkdtempSync(join(tmpdir(), 'docs-check-test-'));
	mkdirSync(join(root, 'native/crates/app/src'), { recursive: true });
	mkdirSync(join(root, 'docs'));
	writeFileSync(join(root, 'native/crates/app/src/main.rs'), 'fn open_window() {\n    // SAFETY: a fact.\n    unsafe { libc::getpid() };\n}\n');
	writeFileSync(join(root, 'ARCHITECTURE.md'), `${architecture}\n${crateTable}\n`);
	writeFileSync(join(root, 'GLOSSARY.md'), glossary);
	writeFileSync(join(root, 'docs/memory-safety.md'), `| File | Calls | Relies on |\n|---|---|---|\n${inventory}\n`);
	const run = Bun.spawnSync(['bun', join(import.meta.dir, 'docs-check.ts'), root]);
	rmSync(root, { recursive: true });
	return { code: run.exitCode, out: run.stdout.toString() };
}

test('live paths, symbols and links pass, from the repo root or from a crate', () => {
	const { code, out } = check('`app/src/main.rs::open_window`, `native/crates/app/src/main.rs` and [the rules](docs/memory-safety.md#rules). Upstream `src/vs/base/common/uri.ts`, `port/patches/*.patch` and the build output `native/target` are not checked.');
	expect(out).toContain('every cited path and symbol exists');
	expect(code).toBe(0);
});

test('a dead path, symbol or link fails with its line', () => {
	expect(check('ok\n`app/src/gone.rs`').out).toContain('ARCHITECTURE.md:2: `app/src/gone.rs` does not exist');
	expect(check('`app/src/main.rs::close_window`').out).toContain('ARCHITECTURE.md:1: `close_window` is not in app/src/main.rs');
	expect(check('[gone](docs/gone.md)').out).toContain('ARCHITECTURE.md:1: `docs/gone.md` does not exist');
	expect(check('`app/src/gone.rs`').code).toBe(1);
});

test('a Rust file cited from inside a crate fails: it would pass unchecked', () => {
	expect(check('`workbench/keyboard.rs`').out).toContain('`workbench/keyboard.rs` does not start at the repo root or a crate');
});

test('the unsafe inventory and the files that hold unsafe code must agree', () => {
	expect(check('', '').out).toContain('app/src/main.rs holds unsafe code and is not in the inventory');
	expect(check('', '| `app/src/main.rs` | libc | a fact |\n| `app/src/safe.rs` | none | none |').out).toContain('app/src/safe.rs is in the inventory and holds no unsafe code');
});

test('a crate without a row in the crate table fails', () => {
	expect(check('', undefined, '').out).toContain('ARCHITECTURE.md: the crate app has no row in the crate table');
});

test('GLOSSARY.md is checked as the other documents', () => {
	expect(check('', undefined, undefined, '`app/src/gone.rs`').out).toContain('GLOSSARY.md:1: `app/src/gone.rs` does not exist');
});