Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: ensure all tests are performed #12

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
"prettier": "^3.0.0",
"progress": "^2.0.3",
"typescript": "^5.0.2",
"urun": "0.0.8",
"utest": "0.0.8"
}
}
123 changes: 117 additions & 6 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,135 @@

'use strict';

const options = {
verbose: true
const fs = require('node:fs');
const path = require('node:path');
const { spawn } = require('node:child_process');
const { EOL } = require('node:os');

const startTime = Date.now();
const filter = process.env.FILTER ? new RegExp(process.env.FILTER, 'i') : null;
Fixed Show fixed Hide fixed

const format = {
time: startTime => {
const elapsedTime = Date.now() - startTime;
const hours = Math.floor((elapsedTime / (1000 * 60 * 60)) % 24)
.toString()
.padStart(2, '0');
const minutes = Math.floor((elapsedTime / (1000 * 60)) % 60)
.toString()
.padStart(2, '0');
const seconds = Math.floor((elapsedTime / 1000) % 60)
.toString()
.padStart(2, '0');
return `${hours}:${minutes}:${seconds}`;
},
counter: (current, total, pad = ' ') => {
const totalDigits = String(total).length;
return `${String(current).padStart(totalDigits, pad)}`;
},
percent: (current, total) => {
const percentage = ((current / total) * 100).toFixed(0);
return `${format.counter(percentage, 100)}%`;
},
};

const getFiles = (dirPath, files = []) => {
const currentFiles = fs.readdirSync(dirPath);

for (const file of currentFiles) {
const fullPath = path.join(dirPath, file);

if (fs.statSync(fullPath).isDirectory()) {
getFiles(fullPath, files);
} else if (
(fullPath.endsWith('.js') || fullPath.endsWith('.mjs')) &&
(!filter || fullPath.match(filter))
) {
files.push(fullPath);
}
}

return files;
};

const runTestFile = filePath =>
new Promise(resolve => {
const child = spawn('node', [filePath], { stdio: 'inherit' });

child.on('close', code => {
if (code === 0) {
resolve(true);
} else {
resolve(false);
}
});

child.on('error', err => {
console.log(`Failed to start test: ${filePath}`, err);
resolve(false);
});
});

const runTests = async dir => {
const testDir = path.join(__dirname, dir);
const files = getFiles(testDir);
const totalTests = files.length;

let passed = true;

console.log(`${EOL}Directory: ${path.relative(process.cwd(), testDir)}${EOL}`);

for (let i = 0; i < files.length; i++) {
const filePath = files[i];
const fileRelative = path.relative(process.cwd(), filePath);
const testPassed = await runTestFile(filePath);
const testNumber = i + 1;
const elapsedTime = format.time(startTime);
const counter = format.counter(
testNumber,
totalTests,
);
const percent = format.percent(
testNumber,
totalTests,
);
const command = `node ${fileRelative}`;
const log = `${elapsedTime} ${counter}/${totalTests} ${percent} ${command}`;

if (testPassed) {
console.log(`✅ [ 0 ${log} ]`, EOL);
} else {
console.log(`❌ [ 1 ${log} ]`, EOL);
passed = false;
}
}

return passed;
};

if (process.env.FILTER) {
options.include = new RegExp(`${process.env.FILTER}.*\\.js$`);
}
(async () => {
const unitTestsPassed = await runTests('./unit');
const integrationTestsPassed = await runTests('./integration');

require('urun')(__dirname, options);
if (!unitTestsPassed || !integrationTestsPassed) {
console.log('Some tests failed.');
process.exit(1);
} else {
console.log('All tests passed.');
process.exit(0);
}
})();

process.on('exit', code => {
console.log(`About to exit with code: ${code}`);
});

process.on('unhandledRejection', reason => {
console.log('unhandledRejection', reason);
process.exit(1);
});

process.on('uncaughtException', err => {
console.log('uncaughtException', err);
process.exit(1);
});
2 changes: 1 addition & 1 deletion test/unit/test-Pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const poolConfig = {}; // config: { connectionConfig: {} } };
const pool = new mysql.createPool(poolConfig);
test('Pool', {
'exposes escape': () => {
assert.equal(pool.escape(123), '123');
assert.equal(pool.escape(123), '1234');
},

'exposes escapeId': () => {
Expand Down
12 changes: 6 additions & 6 deletions tools/create-db.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

const conn = require('../test/common.js').createConnection({ database: 'mysql' });
conn.query('CREATE DATABASE IF NOT EXISTS test', (err) => {
if (err) {
console.log(err);
return process.exit(-1);
}
conn.query('CREATE DATABASE IF NOT EXISTS test', err => {
if (err) {
console.log(err);
return process.exit(-1);
}

conn.end();
conn.end();
});
4 changes: 2 additions & 2 deletions tools/generate-charset-mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const mysql2iconv = {

const missing = {};

conn.query('show collation', function(err, res) {
conn.query('show collation', (err, res) => {
console.log(res);
res.forEach(r => {
const charset = r.Charset;
Expand All @@ -43,7 +43,7 @@ conn.query('show collation', function(err, res) {
//console.log(JSON.stringify(missing, 4, null));
//console.log(JSON.stringify(charsets, 4, null));
for (let i = 0; i < charsets.length; i += 8) {
console.log(" '" + charsets.slice(i, i + 8).join("', '") + "',");
console.log(` '${charsets.slice(i, i + 8).join("', '")}',`);
}
});

Expand Down
Loading