Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Release-Candidate committed Feb 17, 2023
1 parent fc6f761 commit 1b46f67
Show file tree
Hide file tree
Showing 14 changed files with 6,319 additions and 455 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ out/
alcotest_*.txt
yarn-error.log
vscode-ocaml-alcotest-test-adapter-*.vsix
.vscode-test
7 changes: 6 additions & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
"problemMatcher": [],
"label": "yarn: publish"
},

{
"type": "yarn",
"task": "test",
"problemMatcher": [],
"label": "yarn: test"
}
]
}
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ All yarn commands add `--ignore-engines` to not get a spurious warning:

- `yarn --ignore-engines clean` - deletes the directory `./out`
- `yarn --ignore-engines compile` - compiles the Typescript sources to `./out/` and generates the source maps
- `yarn --ignore-engines test` - compiles the extension and runs the tests
- `yarn --ignore-engines esbuild` - compiles the Typescript sources and bundles them to `./out/extension.js` and adds a source map `./out/extension.js.map`. This is used for testing.
- `yarn --ignore-engines esbuild-watch` - runs the same commands as `yarn --ignore-engines esbuild-watch` in watch mode, that is, it re-bundles everything if a file has been changed
- `yarn --ignore-engines bundle` - compiles and minifies the Typescript sources and bundles them to `./out/extension.js`, no source maps are generated. This is used for releases.
Expand Down
4,953 changes: 4,953 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,36 @@
}
},
"devDependencies": {
"@types/chai": "^4.3.4",
"@types/glob": "^8.0.1",
"@types/mocha": "^10.0.1",
"@types/node": "^18.13.0",
"@types/vscode": "1.59.0",
"@typescript-eslint/eslint-plugin": "^5.52.0",
"@typescript-eslint/parser": "^5.52.0",
"@vscode/test-electron": "^2.2.3",
"@vscode/vsce": "^2.17.0",
"chai": "^4.3.7",
"del-cli": "^5.0.0",
"esbuild": "^0.17.8",
"eslint": "^8.34.0",
"eslint-plugin-chai-friendly": "^0.7.2",
"eslint-plugin-mocha": "^10.1.0",
"glob": "^8.1.0",
"mocha": "^10.2.0",
"source-map-support": "^0.5.21",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.1.2",
"typescript": "^4.9.5"
},
"dependencies": {},
"vsce": {
"dependencies": true,
"yarn": true
},
"scripts": {
"clean": "del-cli -- out",
"compile": "tsc -p ./",
"test": "yarn --ignore-engines run compile && node out/test/runner.js",
"esbuild-base": "esbuild ./src/extension.ts --bundle --outfile=out/extension.js --external:vscode --format=cjs --platform=node",
"esbuild": "yarn --ignore-engines run esbuild-base --sourcemap",
"esbuild-watch": "yarn --ignore-engines run esbuild-base --sourcemap --watch",
Expand Down
1 change: 0 additions & 1 deletion src/extension_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import * as vscode from "vscode";
* @returns The list ('or' an empty list `[]`) of currently opened workspace
* folders.
*/

export function workspaceFolders() {
return vscode.workspace.workspaceFolders || [];
}
2 changes: 1 addition & 1 deletion src/osInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export async function filterExistingDirs(
}

// eslint-disable-next-line no-unused-vars
return statsAndPaths.filter(([f, _]) => filterDirs(f)).map(([_, d]) => d);
return statsAndPaths.filter(([f]) => filterDirs(f)).map(([_, d]) => d);
}

/**
Expand Down
13 changes: 0 additions & 13 deletions src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import * as h from "./extension_helpers";
import * as io from "./osInteraction";
import * as vscode from "vscode";

// eslint-disable-next-line max-statements
export async function addTests(env: {
config: vscode.WorkspaceConfiguration;
controller: vscode.TestController;
Expand All @@ -34,18 +33,6 @@ export async function addTests(env: {
);
env.outChannel.appendLine(`Found dune files: ${uris}`);
}
const out = await io.runCommand("ls", ["-l", "./"]);
env.outChannel.appendLine(
`out: ${out.stdout} stderr: ${out.stderr} err: ${out.error}`
);
const out2 = await io.runCommand("ls", ["-l", "./hugo"]);
env.outChannel.appendLine(
`out: ${out2.stdout} stderr: ${out2.stderr} err: ${out2.error}`
);
const out3 = await io.runCommand("hugo", ["-l", "./"]);
env.outChannel.appendLine(
`out: ${out3.stdout} stderr: ${out3.stderr} err: ${out3.error}`
);
}

/**
Expand Down
47 changes: 47 additions & 0 deletions test/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* SPDX-License-Identifier: MIT
* Copyright (C) 2023 Roland Csaszar
*
* Project: vscode-ocaml-alcotest-test-adapter
* File: main.ts
* Date: 17.Feb.2023
*
* ==============================================================================
* The mocha test runner.
*/

import * as Mocha from "mocha";
import * as glob from "glob";
import * as path from "path";

export function run(): Promise<void> {
const mocha = new Mocha({
ui: "tdd",
});

const testsRoot = path.resolve(__dirname, "..");

return new Promise((c, e) => {
// eslint-disable-next-line consistent-return
glob("**/**-test.js", { cwd: testsRoot }, (err, files) => {
if (err) {
return e(err);
}

files.forEach((f) => mocha.addFile(path.resolve(testsRoot, f)));

try {
mocha.run((failures) => {
if (failures > 0) {
e(new Error(`${failures} tests failed.`));
} else {
c();
}
});
} catch (error) {
console.error(error);
e(error);
}
});
});
}
197 changes: 197 additions & 0 deletions test/osInteraction-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/* eslint-disable max-lines-per-function */
/*
* SPDX-License-Identifier: MIT
* Copyright (C) 2023 Roland Csaszar
*
* Project: vscode-ocaml-alcotest-test-adapter
* File: osInteraction-test.ts
* Date: 17.Feb.2023
*
* ==============================================================================
* Tests for the `odInteraction` module.
*/

import * as chai from "chai";
import * as h from "../src/extension_helpers";
import * as io from "../src/osInteraction";
import * as mocha from "mocha";
import * as vscode from "vscode";

/**
* *****************************************************************************
* Helper functions and constants.
*/

const roots = h.workspaceFolders();
// The first workspace, that is the extension's workspace.
const [root] = roots;

/**
* *****************************************************************************
* Tests
*/
mocha.describe("I/O Functions", () => {
//==========================================================================
mocha.describe("filterExistingDirs", () => {
mocha.it("No directory exists", async () => {
chai.assert.deepEqual(
await io.filterExistingDirs(root, [
"IdoNotExist",
"IDontEither",
"NeitherMe",
]),
[],
"No existing directory!"
);
});
mocha.it("Empty directory list", async () => {
chai.assert.deepEqual(
await io.filterExistingDirs(root, []),
[],
"Empty list passed"
);
});
mocha.it("Some directories exist", async () => {
chai.assert.deepEqual(
await io.filterExistingDirs(root, [
"IdoNotExist",
"test",
"IDontEither",
"src",
"NeitherMe",
"images",
"node_modules",
]),
["test", "src", "images", "node_modules"],
"Should be test, src, images, node_modules"
);
});
});
//==========================================================================
mocha.describe("existsIsFile", () => {
mocha.it("No file exists", async () => {
chai.assert.isFalse(
await io.existsIsFile(
vscode.Uri.joinPath(root.uri, "IdoNotExist")
),
"File should not exist in workspace"
);
});
mocha.it("Is directory, not file", async () => {
chai.assert.isFalse(
await io.existsIsFile(vscode.Uri.joinPath(root.uri, "src")),
"Directory is not a file"
);
});
mocha.it("Is existing file", async () => {
chai.assert.isTrue(
await io.existsIsFile(
vscode.Uri.joinPath(root.uri, "src/constants.ts")
),
"File src/constants.ts should exist"
);
});
});
//==========================================================================
mocha.describe("findFilesRelative", () => {
mocha.it("No file exists", async () => {
chai.assert.deepEqual(
await io.findFilesRelative(root, "IdoNotExistInThisDirectory"),
[],
"None of the files exist"
);
});
mocha.it("Exactly one file exists", async () => {
const [{ path: result }] = await io.findFilesRelative(
root,
"**/parsing.ts"
);
const { path: should } = vscode.Uri.joinPath(
root.uri,
"src/parsing.ts"
);
chai.assert.strictEqual(result, should, "parsing.ts exists");
});
mocha.it("Many .md files exist", async () => {
const result = await io.findFilesRelative(root, "*.md");
const should = [
vscode.Uri.joinPath(root.uri, "CONTRIBUTING.md"),
vscode.Uri.joinPath(root.uri, "README.md"),
vscode.Uri.joinPath(root.uri, "CHANGELOG.md"),
];
chai.assert.deepEqual(
result
.map((e) => {
const { path } = e;
return path;
})
.sort(),
should
.map((e) => {
const { path } = e;
return path;
})
.sort(),
"CONTRIBUTING.md, CHANGELOG.md and README.md exist"
);
});
});
//==========================================================================
mocha.describe("runCommand", () => {
mocha.it("Cmd does not exist", async () => {
const out = await io.runCommand("does-not-exist", []);
chai.assert.strictEqual(
out.error,
"spawn does-not-exist ENOENT",
"Not existing command name."
);
chai.assert.isUndefined(
out.stdout,
// eslint-disable-next-line no-undefined
"No stdout, command not found"
);
chai.assert.isUndefined(
out.stderr,
// eslint-disable-next-line no-undefined
"No stderr, command not found"
);
});
mocha.it("Run ls on non existing file", async () => {
const out = await io.runCommand("ls", [
"-l",
"does_not_exist_I_hope",
]);
chai.assert.strictEqual(
out.stderr,
"ls: does_not_exist_I_hope: No such file or directory\n",
"ls on non existing file."
);
chai.assert.strictEqual(out.stdout, "", "No output at stdout!");
chai.assert.isUndefined(
out.error,
// eslint-disable-next-line no-undefined
"No error calling ls!"
);
});
mocha.it("Run ls on existing file LICENSE", async () => {
const out = await io.runCommand("ls", ["LICENSE"]);
chai.assert.strictEqual(out.stdout, "LICENSE\n", "ls of LICENSE");
chai.assert.strictEqual(out.stderr, "", "No output at stderr!");
chai.assert.isUndefined(
out.error,
// eslint-disable-next-line no-undefined
"No error calling ls!"
);
});
mocha.it("Output of `dune --version`", async () => {
const out = await io.runCommand("dune", ["--version"]);
chai.assert.isString(out.stdout, "Something like `3.6.2`");
chai.assert.strictEqual(out.stderr, "", "No output at stderr!");
chai.assert.isUndefined(
out.error,
// eslint-disable-next-line no-undefined
"No error calling dune!"
);
});
});
});
Loading

0 comments on commit 1b46f67

Please sign in to comment.