Skip to content

Commit

Permalink
Refactor the VS Code highlighting testsuite and add GPR tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eliericha committed Aug 20, 2024
1 parent 6f0cde8 commit 86e5bfc
Show file tree
Hide file tree
Showing 5 changed files with 1,135 additions and 83 deletions.
2 changes: 1 addition & 1 deletion integration/vscode/ada/.vscode-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default defineConfig(
// This may prevent running locally on Linux and having the test
// windows visible, but we consider this a minor use case for
// now. A workaround is to remove this line.
DISPLAY: ':99',
DISPLAY: process.env.DISPLAY ?? ':99',
},
launchArgs: [
// It's important to use the --user-data-dir=<path> form. The
Expand Down
171 changes: 89 additions & 82 deletions integration/vscode/ada/test/suite/general/highlighting.test.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,41 @@
import assert from 'assert';
import * as vscode from 'vscode';
import { spawnSync } from 'child_process';
import { existsSync, opendirSync, renameSync } from 'fs';
import path, { basename, dirname } from 'path';
import fs, { existsSync, lstatSync, opendirSync, readdirSync, renameSync } from 'fs';
import path from 'path';
import { SemanticTokensParams, SemanticTokensRequest, integer } from 'vscode-languageclient';
import { adaExtState } from '../../../src/extension';
import { assertEqualToFileContent, update, activate } from '../utils';

let adaFilePaths: string[] = [];
const extensionRootPath = path.join(__dirname, '..', '..', '..', '..');
const testWsPath = path.join(extensionRootPath, 'test', 'workspaces', 'general');
const adaTestsPath = path.join(testWsPath, 'src', 'highlighting');

suite('Highlighting', function () {
this.beforeAll(async function () {
await activate();
});

const highlightingTestRoot = getDocUri('src/highlighting').fsPath;
adaFilePaths = [];

function walk(dir: string) {
const openDir = opendirSync(dir);
try {
let child;
while ((child = openDir.readSync()) != null) {
const childPath = path.join(dir, child.name);
if (child.isDirectory()) {
walk(childPath);
} else if (child.isFile()) {
if (child.name.match(/\.ad[bs]$/)) {
adaFilePaths.push(childPath);
}
}
}
} finally {
openDir.closeSync();
}
}

walk(highlightingTestRoot);
assert.notStrictEqual(adaFilePaths, []);
const adaTestPaths = [
'objects/objects.ads',
'unknown_imports/pkg.ads',
'hello/hello.adb',
'nesting/main.adb',
'invalid_ada/invalid.adb',
'types/types.ads',
'subprograms/subprograms.adb',
'pkgs-and-specs/pkgbodynospec.adb',
'pkgs-and-specs/pkgbodywithspec.ads',
'pkgs-and-specs/pkgbodywithspec.adb',
'lsp-ada_handlers/lsp-ada_handlers.adb',
'lsp-ada_handlers/lsp.ads',
'lsp-ada_handlers/lsp-ada_handlers.ads',
];

for (const relPath of adaTestPaths) {
suite(relPath, function () {
const absPath = path.join(adaTestsPath, relPath);

for (const absPath of adaFilePaths) {
const testName = `${basename(dirname(absPath))}/${basename(absPath)}`;
const absFileUri = vscode.Uri.file(absPath);

suite(testName, function () {
this.afterAll(async function () {
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
});
Expand All @@ -57,10 +49,22 @@ suite('Highlighting', function () {
});

test('semantic', async function () {
const absFileUri = vscode.Uri.file(absPath);
await testSemanticHighlighting(absFileUri);
});
});
}

const gprTests = ['prj.gpr', 'src/test.gpr'];

for (const relPath of gprTests) {
const gprSyntaxPath = path.join(extensionRootPath, 'syntaxes', 'gpr.tmLanguage.json');

test(relPath, function () {
const gprPath = path.join(testWsPath, relPath);
testSyntax(gprSyntaxPath, gprPath, 'source.gpr');
});
}
});

/**
Expand Down Expand Up @@ -186,8 +190,6 @@ function getDocUri(p: string): vscode.Uri {
}
}

const extensionRootPath = path.resolve(__dirname, '../../../../');

/**
* A type representing the two TextMate grammars available in the repository.
* The values match directory names in the extension source directory. The
Expand Down Expand Up @@ -234,58 +236,63 @@ function testSyntaxHighlighting(absFilePath: string, syntax: Syntaxes) {
);
}

const cmd = [
// Use npx to avoid sensitivity to PATH env var. On Windows, the
// Node installation provides a 'npx' executable file which is a
// Bash script which doesn't work on Windows. Instead on Windows,
// the 'npx.cmd' file should be used.
process.platform == 'win32' ? 'npx.cmd' : 'npx',
'vscode-tmgrammar-snap',
// We pass a non-existing language configuration, otherwise the tool
// picks up the package.json file and always loads the grammar in
// use.
'--config',
'none',
// Show diffs on separate lines because color coding isn't visible
// in the VS Code debug console.
'--expandDiff',
'-g',
syntaxPath,
'-s',
'source.ada',
absFilePath,
];

if (update()) {
cmd.push('--updateSnapshot');
}

const proc = spawnSync(cmd[0], cmd.slice(1), { cwd: workDirPath });

if (proc.error) {
// proc.error is set if we fail to spawn the child process
throw proc.error;
}

if (proc.status === null) {
const msg =
`Null return code for command: ${cmd.join(' ')}\n` +
String(proc.stdout) +
String(proc.stderr);
assert.fail(msg);
} else if (proc.status != 0) {
const msg =
`Return code ${proc.status.toString()} for command: cd ${workDirPath}; ${cmd.join(
' '
)}\n` +
String(proc.stdout) +
String(proc.stderr);
assert.fail(msg);
}
testSyntax(syntaxPath, absFilePath, 'source.ada');
} finally {
if (existsSync(workSnapPath)) {
// Rename .snap --> .snap.<syntax>
renameSync(workSnapPath, refSnapPath);
}
}
}

function testSyntax(syntaxPath: string, absFilePath: string, languageId: string) {
const workDirPath = path.dirname(syntaxPath);
const cmd = [
// Use npx to avoid sensitivity to PATH env var. On Windows, the
// Node installation provides a 'npx' executable file which is a
// Bash script which doesn't work on Windows. Instead on Windows,
// the 'npx.cmd' file should be used.
process.platform == 'win32' ? 'npx.cmd' : 'npx',
'vscode-tmgrammar-snap',
// We pass a non-existing language configuration, otherwise the tool
// picks up the package.json file and always loads the grammar in
// use.
'--config',
'none',
// Show diffs on separate lines because color coding isn't visible
// in the VS Code debug console.
'--expandDiff',
'-g',
syntaxPath,
'-s',
languageId,
absFilePath,
];

if (update()) {
cmd.push('--updateSnapshot');
}

const proc = spawnSync(cmd[0], cmd.slice(1), { cwd: workDirPath });

if (proc.error) {
// proc.error is set if we fail to spawn the child process
throw proc.error;
}

if (proc.status === null) {
const msg =
`Null return code for command: ${cmd.join(' ')}\n` +
String(proc.stdout) +
String(proc.stderr);
assert.fail(msg);
} else if (proc.status != 0) {
const msg =
`Return code ${proc.status.toString()} for command: cd ${workDirPath}; ${cmd.join(
' '
)}\n` +
String(proc.stdout) +
String(proc.stderr);
assert.fail(msg);
}
}
Loading

0 comments on commit 86e5bfc

Please sign in to comment.