Skip to content

Commit 523f6ce

Browse files
authoredMay 2, 2023
Merge pull request #13365 from hasezoey/denoFixCycle6x
chore(deno): change to start mocha fixtures before mocha
2 parents 73ef135 + 48aed67 commit 523f6ce

File tree

5 files changed

+94
-42
lines changed

5 files changed

+94
-42
lines changed
 

‎.github/workflows/test.yml

+9-11
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,21 @@ jobs:
7878
test-deno:
7979
runs-on: ubuntu-20.04
8080
name: Deno tests
81+
env:
82+
MONGOMS_VERSION: 6.0.0
83+
MONGOMS_PREFER_GLOBAL_PATH: 1
8184
steps:
8285
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
8386
- name: Setup node
8487
uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
8588
with:
8689
node-version: 16
87-
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.0.2
88-
- name: Setup
89-
run: |
90-
wget -q https://downloads.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2004-6.0.0.tgz
91-
tar xf mongodb-linux-x86_64-ubuntu2004-6.0.0.tgz
92-
mkdir -p ./data/db/27017 ./data/db/27000
93-
printf "\ntimeout: 8000" >> ./.mocharc.yml
94-
./mongodb-linux-x86_64-ubuntu2004-6.0.0/bin/mongod --setParameter ttlMonitorSleepSecs=1 --fork --dbpath ./data/db/27017 --syslog --port 27017
95-
sleep 2
96-
mongod --version
97-
echo `pwd`/mongodb-linux-x86_64-ubuntu2004-6.0.0/bin >> $GITHUB_PATH
90+
- name: Load MongoDB binary cache
91+
id: cache-mongodb-binaries
92+
uses: actions/cache@v3
93+
with:
94+
path: ~/.cache/mongodb-binaries
95+
key: deno-${{ env.MONGOMS_VERSION }}
9896
- name: Setup Deno
9997
uses: denoland/setup-deno@v1
10098
with:

‎test/.eslintrc.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ rules:
44
# In `document.test.js` we sometimes use self assignment to test setters
55
no-self-assign: off
66
ignorePatterns:
7-
- deno.js
7+
- deno.js
8+
- deno*.js

‎test/deno.js

+23-30
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,39 @@
22

33
import { createRequire } from "node:module";
44
import process from "node:process";
5+
import { resolve } from "node:path";
6+
import {fileURLToPath} from "node:url";
57

6-
import { parse } from "https://deno.land/std/flags/mod.ts"
7-
const args = parse(Deno.args);
8+
import { spawn } from "node:child_process";
89

910
Error.stackTraceLimit = 100;
1011

1112
const require = createRequire(import.meta.url);
1213

13-
const Mocha = require('mocha');
14-
const fs = require('fs');
15-
const path = require('path');
14+
const fixtures = require('./mocha-fixtures.js')
1615

17-
// const fixtures = require('./mocha-fixtures.js');
16+
await fixtures.mochaGlobalSetup();
1817

19-
const mocha = new Mocha({
20-
timeout: 8000,
21-
...(args.g ? { fgrep: '' + args.g } : {})
22-
});
23-
24-
// the following is required because mocha somehow does not load "require" options and so needs to be manually set-up
25-
// mocha.globalSetup(fixtures.mochaGlobalSetup);
26-
// mocha.globalTeardown(fixtures.mochaGlobalTeardown);
18+
const child_args = [
19+
// args is required to be set manually, because there is currently no way to get all arguments from deno
20+
'--allow-env', '--allow-read', '--allow-net', '--allow-run', '--allow-sys', '--allow-write',
21+
...Deno.args,
22+
resolve(fileURLToPath(import.meta.url), '../deno_mocha.js')
23+
];
2724

28-
const testDir = 'test';
25+
const child = spawn(process.execPath, child_args, { stdio: 'inherit' });
2926

30-
const files = fs.readdirSync(testDir).
31-
concat(fs.readdirSync(path.join(testDir, 'docs')).map(file => path.join('docs', file))).
32-
concat(fs.readdirSync(path.join(testDir, 'helpers')).map(file => path.join('helpers', file)));
33-
34-
const ignoreFiles = new Set(['browser.test.js']);
27+
child.on('exit', (code, signal) => {
28+
signal ? doExit(-100) : doExit(code);
29+
});
3530

36-
for (const file of files) {
37-
if (!file.endsWith('.test.js') || ignoreFiles.has(file)) {
38-
continue;
39-
}
31+
Deno.addSignalListener("SIGINT", () => {
32+
console.log("SIGINT");
33+
child.kill("SIGINT");
34+
doExit(-2);
35+
});
4036

41-
mocha.addFile(path.join(testDir, file));
37+
async function doExit(code) {
38+
await fixtures.mochaGlobalTeardown();
39+
Deno.exit(code);
4240
}
43-
44-
mocha.run(function(failures) {
45-
process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
46-
process.exit(process.exitCode);
47-
});

‎test/deno_mocha.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
import { createRequire } from "node:module";
4+
import process from "node:process";
5+
6+
// Workaround for Mocha getting terminal width, which currently requires `--unstable`
7+
Object.defineProperty(process.stdout, 'getWindowSize', {
8+
value: function() {
9+
return [75, 40];
10+
}
11+
});
12+
13+
import { parse } from "https://deno.land/std/flags/mod.ts"
14+
const args = parse(Deno.args);
15+
16+
Error.stackTraceLimit = 100;
17+
18+
const require = createRequire(import.meta.url);
19+
20+
const Mocha = require('mocha');
21+
const fs = require('fs');
22+
const path = require('path');
23+
24+
// const fixtures = require('./mocha-fixtures.js')
25+
26+
const mocha = new Mocha({
27+
timeout: 8000,
28+
...(args.g ? { fgrep: '' + args.g } : {})
29+
});
30+
31+
// the following is required because mocha somehow does not load "require" options and so needs to be manually set-up
32+
// mocha.globalSetup(fixtures.mochaGlobalSetup);
33+
// mocha.globalTeardown(fixtures.mochaGlobalTeardown);
34+
35+
const testDir = 'test';
36+
37+
const files = fs.readdirSync(testDir).
38+
concat(fs.readdirSync(path.join(testDir, 'docs')).map(file => path.join('docs', file))).
39+
concat(fs.readdirSync(path.join(testDir, 'helpers')).map(file => path.join('helpers', file)));
40+
41+
const ignoreFiles = new Set(['browser.test.js']);
42+
43+
for (const file of files) {
44+
if (!file.endsWith('.test.js') || ignoreFiles.has(file)) {
45+
continue;
46+
}
47+
48+
mocha.addFile(path.join(testDir, file));
49+
}
50+
51+
mocha.run(function(failures) {
52+
process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
53+
process.exit(process.exitCode);
54+
});

‎test/mocha-fixtures.js

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
const mms = require('mongodb-memory-server');
44

5+
/*
6+
7+
Note: dont use any mocha-specific things in this file, it may or may not be called without mocha (see deno.js)
8+
9+
*/
10+
511
/*
612
* Default MMS mongodb version is used, unless MONGOMS_VERSION is set (which is set with the matrix in test.yml for CI)
713
*/

0 commit comments

Comments
 (0)
Please sign in to comment.