Skip to content

Commit efbb769

Browse files
authored
Unit test suite (#30)
* Get 100% coverage for util * Add sourcemap tests * Add basic sourcemap test * Report coverage in CI run * Build WAT tools * fixup! Build WAT tools * Add example warduino test to CI * fixup! Add example warduino test to CI * Change example test * fixup! Change example test * Move submodules * Run only unit tests in CI * Remove tmp dir in tests * Test wasm mapper * Add scenario describer tests * Add MessageQueue tests
1 parent 02dfd40 commit efbb769

19 files changed

Lines changed: 779 additions & 184 deletions

File tree

.github/workflows/test.yml

Lines changed: 103 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,80 @@ name: Tests
22
on: [ push ]
33

44
jobs:
5+
build-wabt:
6+
name: Build WAT tools
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v4
10+
with:
11+
submodules: recursive
12+
13+
- name: Get WABT commit ID
14+
working-directory: tests/artifacts/wabt
15+
run: echo "WABT_VERSION=$(git rev-parse HEAD)" >> $GITHUB_ENV
16+
17+
- name: Cache wabt
18+
uses: actions/cache@v4
19+
id: cache-wabt
20+
with:
21+
key: ${{ runner.os }}-wabt-${{ env.WABT_VERSION }}
22+
path: tests/artifacts/wabt
23+
restore-keys: |
24+
${{ runner.os }}-wabt-
25+
26+
- name: Build WABT # Build latest version
27+
if: steps.cache-wabt.outputs.cache-hit == false
28+
working-directory: tests/artifacts/wabt
29+
run: |
30+
mkdir build; cd build
31+
cmake ..
32+
cmake --build .
33+
34+
- name: Upload built tools
35+
uses: actions/upload-artifact@v4
36+
with:
37+
name: wabt-build-${{ github.run_id }}
38+
path: tests/artifacts/wabt/build
39+
40+
build-wdcli:
41+
name: Build WARDuino CLI
42+
runs-on: ubuntu-latest
43+
steps:
44+
- uses: actions/checkout@v4
45+
with:
46+
submodules: recursive
47+
48+
- name: Get WDCLI commit ID
49+
working-directory: tests/artifacts/warduino
50+
run: echo "WDCLI_VERSION=$(git rev-parse HEAD)" >> $GITHUB_ENV
51+
52+
- name: Cache WARDuino CLI
53+
uses: actions/cache@v4
54+
id: cache-warduino
55+
with:
56+
key: ${{ runner.os }}-warduino-${{ env.WDCLI_VERSION }}
57+
path: tests/artifacts/warduino
58+
restore-keys: |
59+
${{ runner.os }}-warduino-
60+
61+
- name: Build WARDuino CLI # Build latest version
62+
if: steps.cache-warduino.outputs.cache-hit == false
63+
working-directory: tests/artifacts/warduino
64+
run: |
65+
mkdir build; cd build
66+
cmake .. -D BUILD_EMULATOR=ON
67+
cmake --build .
68+
69+
- name: Upload built tools
70+
uses: actions/upload-artifact@v4
71+
with:
72+
name: warduino-build-${{ github.run_id }}
73+
path: tests/artifacts/warduino/build
74+
575
unit:
676
name: Unit tests
777
runs-on: ubuntu-latest
8-
strategy:
9-
fail-fast: false
78+
needs: build-wabt
1079
if: github.event.pull_request.draft == false
1180
steps:
1281
- uses: actions/checkout@v4
@@ -16,14 +85,26 @@ jobs:
1685
- name: Prebuild files
1786
run: npm run test:prebuild
1887

88+
- name: Download WAT tools
89+
uses: actions/download-artifact@v4
90+
with:
91+
name: wabt-build-${{ github.run_id }}
92+
path: .tools
93+
94+
- name: Verify tools
95+
run: |
96+
chmod u+x $GITHUB_WORKSPACE/.tools/*
97+
$GITHUB_WORKSPACE/.tools/wasm-objdump --version
98+
1999
- name: Run ava unit tests
20100
run: npm run test:ava
101+
env:
102+
WABT: ${GITHUB_WORKSPACE}/.tools
21103

22104
coverage:
23105
name: Code coverage
24106
runs-on: ubuntu-latest
25-
strategy:
26-
fail-fast: true
107+
needs: build-wabt
27108
if: github.event.pull_request.draft == false
28109
steps:
29110
- uses: actions/checkout@v4
@@ -33,9 +114,26 @@ jobs:
33114
- name: Prebuild files
34115
run: npm run test:prebuild
35116

117+
- name: Download WAT tools
118+
uses: actions/download-artifact@v4
119+
with:
120+
name: wabt-build-${{ github.run_id }}
121+
path: .tools
122+
123+
- name: Verify tools
124+
run: |
125+
chmod u+x $GITHUB_WORKSPACE/.tools/*
126+
$GITHUB_WORKSPACE/.tools/wasm-objdump --version
127+
128+
- name: Add .tools to PATH
129+
run: echo "$GITHUB_WORKSPACE/.tools" >> $GITHUB_PATH
130+
36131
- name: Run c8 coverage
37132
run: |
38-
echo "LINE_COVERAGE=$(npm run coverage:test:ava | grep -E '^\s*Lines\s*:\s*[0-9]+(\.[0-9]+)?%' | awk '{print $3}' | tr -d '%')" >> $GITHUB_ENV
133+
echo "LINE_COVERAGE=$(npm run coverage:test:ava | grep -E 'Lines\s*:\s*[0-9]+(\.[0-9]+)?%' | awk '{print $3}' | tr -d '%')" >> $GITHUB_ENV
134+
135+
- name: Report coverage
136+
run: npx c8 report --all
39137

40138
- name: Update coverage badge
41139
uses: schneegans/dynamic-badges-action@v1.7.0

.gitmodules

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
[submodule "WABT"]
2-
path = WABT
3-
url = git@github.com:TOPLLab/wabt.git
1+
[submodule "tests/unit/artifacts/wabt"]
2+
path = tests/artifacts/wabt
3+
url = git@github.com:TOPLLab/wabt.git
4+
[submodule "tests/unit/artifacts/warduino"]
5+
path = tests/artifacts/warduino
6+
url = git@github.com:TOPLLab/WARDuino.git

package.json

Lines changed: 66 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,71 @@
11
{
2-
"name": "latch",
3-
"description": "A testing language for constraint environments",
4-
"version": "0.3.1",
5-
"type": "commonjs",
6-
"exports": {
7-
"types": "./dist/types/index.d.ts",
8-
"require": "./dist/cjs/index.cjs"
9-
},
10-
"main": "dist/cjs/index.cjs",
11-
"types": "dist/types/index.d.ts",
2+
"name": "latch",
3+
"description": "A testing language for constraint environments",
4+
"version": "0.3.1",
5+
"type": "commonjs",
6+
"exports": {
7+
"types": "./dist/types/index.d.ts",
8+
"require": "./dist/cjs/index.cjs"
9+
},
10+
"main": "dist/cjs/index.cjs",
11+
"types": "dist/types/index.d.ts",
12+
"files": [
13+
"dist",
14+
"bin"
15+
],
16+
"scripts": {
17+
"clean": "rm -rf dist",
18+
"build": "npm run build:cjs",
19+
"build:cjs": "tsc --project tsconfig.build.json",
20+
"build:tests": "tsc --outDir out --project tsconfig.tests.json",
21+
"watch": "tsc -watch -p ./",
22+
"lint": "eslint src/**/* --config .eslint.config.mjs",
23+
"test:prebuild": "npm run build && npm run build:tests",
24+
"test:all": "npm run test:prebuild && npm run test:ava",
25+
"test:ava": "ava",
26+
"test:example": "npx ts-node ./tests/examples/example.ts",
27+
"coverage:test:ava": "c8 --src src/ --all ava"
28+
},
29+
"dependencies": {
30+
"ansi-colors": "^4.1.3",
31+
"ieee754": "^1.2.1",
32+
"ora": "^8.0.1",
33+
"source-map": "^0.7.4",
34+
"ts-node": "^10.5.0",
35+
"tslib": "^2.8.1"
36+
},
37+
"devDependencies": {
38+
"@ava/typescript": "^5.0.0",
39+
"@eslint/js": "^9.16.0",
40+
"@stylistic/eslint-plugin-js": "^2.11.0",
41+
"@types/chai": "^4.3.0",
42+
"@types/mocha": "^9.1.0",
43+
"@types/node": "^20.10.4",
44+
"@types/uuid": "^9.0.0",
45+
"@types/ws": "^8.5.4",
46+
"@typescript-eslint/eslint-plugin": "^8.17.0",
47+
"ava": "^6.2.0",
48+
"c8": "^10.1.2",
49+
"convert-extension": "^0.3.0",
50+
"eslint": "^9.16.0",
51+
"globals": "^15.13.0",
52+
"mqtt": "^4.3.7",
53+
"serialport": "^10.4.0",
54+
"typescript": "^5.2.0",
55+
"typescript-eslint": "^8.17.0"
56+
},
57+
"ava": {
1258
"files": [
13-
"dist",
14-
"bin"
59+
"out/tests/unit/describers.test.js",
60+
"out/tests/unit/messaging.test.js",
61+
"out/tests/unit/sourcemap.test.js",
62+
"out/tests/unit/util.test.js"
1563
],
16-
"scripts": {
17-
"clean": "rm -rf dist",
18-
"build": "npm run build:cjs",
19-
"build:cjs": "tsc --project tsconfig.build.json",
20-
"build:tests": "tsc --outDir out --project tsconfig.tests.json",
21-
"watch": "tsc -watch -p ./",
22-
"lint": "eslint src/**/* --config .eslint.config.mjs",
23-
"test:prebuild": "npm run build && npm run build:tests",
24-
"test:all": "npm run test:prebuild && npm run test:ava",
25-
"test:ava": "ava",
26-
"test:example": "npx ts-node ./tests/examples/example.ts",
27-
"coverage:test:ava": "c8 --src src/ --all ava"
28-
},
29-
"dependencies": {
30-
"ansi-colors": "^4.1.3",
31-
"ieee754": "^1.2.1",
32-
"ora": "^8.0.1",
33-
"source-map": "^0.7.4",
34-
"ts-node": "^10.5.0",
35-
"tslib": "^2.8.1"
36-
},
37-
"devDependencies": {
38-
"@ava/typescript": "^5.0.0",
39-
"@eslint/js": "^9.16.0",
40-
"@stylistic/eslint-plugin-js": "^2.11.0",
41-
"@types/chai": "^4.3.0",
42-
"@types/mocha": "^9.1.0",
43-
"@types/node": "^20.10.4",
44-
"@types/uuid": "^9.0.0",
45-
"@types/ws": "^8.5.4",
46-
"@typescript-eslint/eslint-plugin": "^8.17.0",
47-
"ava": "^6.2.0",
48-
"c8": "^10.1.2",
49-
"convert-extension": "^0.3.0",
50-
"eslint": "^9.16.0",
51-
"globals": "^15.13.0",
52-
"mqtt": "^4.3.7",
53-
"serialport": "^10.4.0",
54-
"typescript": "^5.2.0",
55-
"typescript-eslint": "^8.17.0"
56-
},
57-
"ava": {
58-
"files": [
59-
"out/tests/unit/util.test.js"
60-
],
61-
"typescript": {
62-
"compile": false,
63-
"rewritePaths": {
64-
"src/": "dist/"
65-
}
66-
}
64+
"typescript": {
65+
"compile": false,
66+
"rewritePaths": {
67+
"src/": "dist/"
68+
}
6769
}
70+
}
6871
}

src/messaging/MessageQueue.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ export class MessageQueue implements Iterable<string> {
22
private readonly delimiter: string;
33
private queue: string[];
44

5+
/**
6+
* @param delimiter the EOM (end-of-message) token
7+
*/
58
constructor(delimiter: string) {
69
this.delimiter = delimiter;
710
this.queue = [];

src/reporter/Reporter.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,7 @@ export class Reporter {
119119
this.archiver.write();
120120
}
121121

122-
info(text: string) {
123-
this.output += `info: ${text}\n`;
124-
}
125-
126122
error(text: string) {
127123
this.output += `error: ${text}\n`;
128124
}
129-
130-
suite(title: string) {
131-
this.output += `suite: ${title}\n`;
132-
}
133-
134-
test(title: string) {
135-
this.output += ` test: ${title}\n`;
136-
}
137125
}

src/reporter/describers/SuiteDescribers.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,9 @@ export class NormalSuiteDescriber extends ShortSuiteDescriber {
6262
});
6363

6464
if (this.item.outcome === Outcome.error) {
65-
report.push(' '.repeat(2) + red(this.item.clarification.toString()));
65+
report.push(' '.repeat(2) + red(this.item.clarification));
6666
}
6767

6868
return report;
6969
}
70-
7170
}

src/util/retry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ export function retry<T>(promise: () => Promise<T>, retries: number): Promise<T>
1111
trying = ++attempt < retries;
1212
}
1313
}
14-
reject(`exhausted number of retries (${retries})`);
14+
reject(new Error(`exhausted number of retries (${retries})`));
1515
});
1616
}

src/util/util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export function getFileExtension(file: string): string {
22
const result = /(?:\.([^.]+))?$/.exec(file)
3-
if (result === null || result.length < 1) {
4-
throw Error('Could not determine file type');
3+
if (result === null || result.length < 1 || result[1] === undefined) {
4+
throw new Error('Could not determine file type');
55
}
66
return result[1];
77
}

0 commit comments

Comments
 (0)