Skip to content

Commit

Permalink
Update tests skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
agrojean-ledger committed Nov 22, 2024
1 parent cabba3a commit e24acdb
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 82 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules/
dist/
.vscode-test/
out/
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import tsPlugin from "@typescript-eslint/eslint-plugin";

export default [
{
ignores: ["out", "dist", "**/*.d.ts"],
ignores: ["out", "dist", "**/*.d.ts", "node_modules", ".vscode-test", ".vscode"],
},
{
files: ["**/*.ts"],
Expand Down
25 changes: 3 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"type": "git",
"url": "https://github.com/LedgerHQ/ledger-vscode-extension/"
},
"type": "module",
"engines": {
"vscode": "^1.79.0"
},
Expand Down Expand Up @@ -277,7 +276,6 @@
"devDependencies": {
"@ltd/j-toml": "^1.38.0",
"@stylistic/eslint-plugin": "^2.11.0",
"@types/glob": "^8.1.0",
"@types/mocha": "^10.0.10",
"@types/node": "22.9.1",
"@types/vscode": "^1.95.0",
Expand Down
63 changes: 34 additions & 29 deletions src/appSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,19 +401,22 @@ export function getSelectedApp() {

export function setSelectedApp(app: App | undefined) {
selectedApp = app;
if (app && app.testsUseCases) {
vscode.commands.executeCommand("setContext", "ledgerDevTools.showRebuildTestUseCaseDeps", true);
if (app.testsUseCases.length > 1) {
vscode.commands.executeCommand("setContext", "ledgerDevTools.showSelectTestUseCase", true);
if (app) {
vscode.commands.executeCommand("setContext", "ledgerDevTools.showSelectTests", false);
if (app.testsUseCases) {
vscode.commands.executeCommand("setContext", "ledgerDevTools.showRebuildTestUseCaseDeps", true);
if (app.testsUseCases.length > 1) {
vscode.commands.executeCommand("setContext", "ledgerDevTools.showSelectTestUseCase", true);
}
else {
vscode.commands.executeCommand("setContext", "ledgerDevTools.showSelectTestUseCase", false);
}
}
else {
vscode.commands.executeCommand("setContext", "ledgerDevTools.showRebuildTestUseCaseDeps", false);
vscode.commands.executeCommand("setContext", "ledgerDevTools.showSelectTestUseCase", false);
}
}
else {
vscode.commands.executeCommand("setContext", "ledgerDevTools.showRebuildTestUseCaseDeps", false);
vscode.commands.executeCommand("setContext", "ledgerDevTools.showSelectTestUseCase", false);
}
}

export function getAppList() {
Expand Down Expand Up @@ -603,26 +606,28 @@ export function getAppTestsList(targetSelector: TargetSelector) {
// * Get the device option from pytest help (either --model or --device)
// * If the device option is found, run pytest with --collect-only and the device option
// * If the device option is not found, run pytest with --collect-only
let getTestsListCmd = `docker exec -u 0 ${selectedApp!.containerName} bash -c '
cd ${selectedApp.functionalTestsDir} &&
pip install -r requirements.txt > /dev/null 2>&1 &&
clear &&
device_option=$(pytest --help |
awk "/[C|c]ustom options/,/^$/" |
grep -E -- "--model|--device" |
head -n 1 |
tr " =" "\n" |
grep -v "^$" |
head -n 1
);
if [ -n "$device_option" ]; then
pytest --collect-only -q "$device_option" ${device}
else
pytest --collect-only -q
fi
'`;
let getTestsListCmd = "docker";
let getTestsListArgs = [
"exec", "-u", "0", selectedApp!.containerName, "bash", "-c",
`cd ${selectedApp.functionalTestsDir} &&
pip install -r requirements.txt > /dev/null 2>&1 &&
clear &&
device_option=$(pytest --help |
awk "/[C|c]ustom options/,/^$/" |
grep -E -- "--model|--device" |
head -n 1 |
tr " =" "\n" |
grep -v "^$" |
head -n 1
);
if [ -n "$device_option" ]; then
pytest --collect-only -q "$device_option" ${device}
else
pytest --collect-only -q
fi`,
];
// Executing the command with a callback
cp.exec(getTestsListCmd, optionsExec, (error, stdout, stderr) => {
cp.execFile(getTestsListCmd, getTestsListArgs, optionsExec, (error, stdout, stderr) => {
if (error) {
pushError(`Error while getting tests list: ${error.message}`);
return;
Expand All @@ -631,8 +636,8 @@ export function getAppTestsList(targetSelector: TargetSelector) {
stdout.split("\n").forEach((line: string) => {
if (line.includes("::")) {
let parts = line.split("::");
if (parts.length === 2) {
let testName = parts[1].split("[")[0];
if (parts.length > 0) {
let testName = parts[parts.length - 1].split("[")[0];
if (testName !== undefined && testName !== "") {
testsList.push(testName);
}
Expand Down
51 changes: 24 additions & 27 deletions src/test/suite/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
import * as path from "path";
import * as Mocha from "mocha";
import * as glob from "glob";
import Mocha = require("mocha");
import { glob } from "glob";

export function run(): Promise<void> {
// Create the mocha test
export async function run(): Promise<void> {
// Create the Mocha test instance
const mocha = new Mocha({
ui: "tdd",
color: true,
});

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

return new Promise((c, e) => {
glob("**/**.test.js", { cwd: testsRoot }, (err, files) => {
if (err) {
return e(err);
}
try {
// Use glob's promise-based API to find test files
const files = await glob("**/*.test.js", { cwd: testsRoot });

// Add files to the test suite
files.forEach(f => mocha.addFile(path.resolve(testsRoot, f)));
// Add each test file to the Mocha test suite
files.forEach(file => mocha.addFile(path.resolve(testsRoot, file)));

try {
// Run the mocha test
mocha.run((failures) => {
if (failures > 0) {
e(new Error(`${failures} tests failed.`));
}
else {
c();
}
});
}
catch (err) {
console.error(err);
e(err);
}
// Run the tests and handle results
return new Promise((resolve, reject) => {
mocha.run((failures: number) => {
if (failures > 0) {
reject(new Error(`${failures} tests failed.`));
}
else {
resolve();
}
});
});
});
}
catch (err) {
console.error("Error setting up tests:", err);
throw err;
}
}
3 changes: 2 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ const extensionConfig = {
libraryTarget: "commonjs2",
},
externals: {
vscode: "commonjs vscode", // the vscode-module is created on-the-fly and must be excluded. Add other modules that cannot be webpack'ed, 📖 -> https://webpack.js.org/configuration/externals/
"vscode": "commonjs vscode", // the vscode-module is created on-the-fly and must be excluded. Add other modules that cannot be webpack'ed, 📖 -> https://webpack.js.org/configuration/externals/
// modules added here also need to be added in the .vscodeignore file
"@ltd/j-toml": "commonjs @ltd/j-toml",
},
resolve: {
// support reading TypeScript and JavaScript files, 📖 -> https://github.com/TypeStrong/ts-loader
Expand Down

0 comments on commit e24acdb

Please sign in to comment.