From be700ab012636d3994b65535cac232cc5d65b48a Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Wed, 2 Mar 2022 14:25:56 +0000 Subject: [PATCH] cli: Add ability to override default timeout as well as ignore only tests --- appveyor.yml | 21 +++++++++++++++++++++ cli.mjs | 5 +++++ lib/cli.mjs | 11 +++++++++++ lib/eltro.mjs | 11 ++++++++--- package.json | 13 ++++++++++++- test/cli.test.mjs | 33 +++++++++++++++++++++++++++++++++ test/eltro.test.mjs | 29 +++++++++++++++++++++++++++++ 7 files changed, 119 insertions(+), 4 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index c17af0a..6196fe2 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -24,3 +24,24 @@ environment: test_script: - sh: | npm test + +on_success: + - sh: | + apk add curl jq + CURR_VER=$(cat package.json | jq -r .version) + if curl -s -X GET -H "Authorization: token $deploytoken" https://git.nfp.is/api/v1/repos/$APPVEYOR_REPO_NAME/releases | grep -o "\"name\"\:\"v${CURR_VER}\"" > /dev/null; then + echo "Release already exists, nothing to do."; + else + echo "Creating release on gitea" + RELEASE_RESULT=$(curl \ + -X POST \ + -H "Authorization: token $deploytoken" \ + -H "Content-Type: application/json" \ + https://git.nfp.is/api/v1/repos/$APPVEYOR_REPO_NAME/releases \ + -d "{\"tag_name\":\"v${CURR_VER}\",\"name\":\"v${CURR_VER}\",\"body\":\"Automatic release from Appveyor from ${APPVEYOR_REPO_COMMIT} :\n\n${APPVEYOR_REPO_COMMIT_MESSAGE}\"}") + RELEASE_ID=$(echo $RELEASE_RESULT | jq -r .id) + echo "Created release ${RELEASE_ID}" + echo '//registry.npmjs.org/:_authToken=${npmtoken}' > ~/.npmrc + echo "Publishing new version to npm" + npm publish + fi diff --git a/cli.mjs b/cli.mjs index 67a8d66..de0b729 100644 --- a/cli.mjs +++ b/cli.mjs @@ -23,6 +23,9 @@ function PrintHelp() { console.log('where can be any of the following:') console.log(' -r, --reporter - Specify the reporter to use.') console.log(' Supported reporters: list, dot') + console.log(' -t, --timeout - Specify the timeout for tests in ms.') + console.log(' Default value is 2000ms') + console.log(' --ignore-only - Specify to ignore any .only() tests found') console.log('') console.log('eltro test/mytest.mjs') console.log('eltro dot test/*.mjs') @@ -40,6 +43,8 @@ cli.processTargets().then(function() { return cli.loadFiles() .then(function() { e.reporter = cli.reporter + e.ignoreOnly = cli.ignoreOnly + e.__timeout = cli.timeout return e.run() .catch(function(err) { diff --git a/lib/cli.mjs b/lib/cli.mjs index ab57dca..cba9915 100644 --- a/lib/cli.mjs +++ b/lib/cli.mjs @@ -4,6 +4,8 @@ import fs from 'fs' export function CLI(e) { this.e = e this.reporter = 'list' + this.ignoreOnly = false + this.timeout = 2000 this.targets = ['test/**'] this.files = [] this.errored = false @@ -28,6 +30,15 @@ CLI.prototype.parseOptions = function(args) { } this.reporter = args[i + 1] i++ + } else if (args[i] === '-t' || args[i] === '--timeout') { + if (!args[i + 1] || isNaN(Number(args[i + 1]))) { + this.errored = true + return + } + this.timeout = Number(args[i + 1]) + i++ + } else if (args[i] === '--ignore-only') { + this.ignoreOnly = true } else if (args[i][0] === '-') { this.errored = true return diff --git a/lib/eltro.mjs b/lib/eltro.mjs index a6831da..fdfabfa 100644 --- a/lib/eltro.mjs +++ b/lib/eltro.mjs @@ -73,8 +73,10 @@ Test.prototype.skip = function() { } Test.prototype.only = function() { - this.isExclusive = true - this.group.__hasonly(true) + if (!this.e.ignoreOnly) { + this.isExclusive = true + this.group.__hasonly(true) + } } Test.prototype.clone = function(prefix = '') { @@ -97,6 +99,7 @@ function Eltro() { this.failedTests = [] this.hasTests = false this.starting = false + this.ignoreOnly = false this.logger = null this.filename = '' this.prefix = '' @@ -436,7 +439,9 @@ Eltro.prototype.skip = function() { } Eltro.prototype.only = function() { - this.temporary.only = true + if (!this.ignoreOnly) { + this.temporary.only = true + } return this } diff --git a/package.json b/package.json index 055bc6d..4a707cb 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,18 @@ "description": "Eltro is a tiny no-dependancy test framework for node", "main": "index.mjs", "scripts": { - "test": "node cli.mjs test/**/*.test.mjs" + "test": "node cli.mjs test/**/*.test.mjs", + "test:watch": "npm-watch test" + }, + "watch": { + "test": { + "patterns": [ + "*" + ], + "extensions": "js,mjs", + "quiet": true, + "inherit": true + } }, "repository": { "type": "git", diff --git a/test/cli.test.mjs b/test/cli.test.mjs index 7c1020f..f89a953 100644 --- a/test/cli.test.mjs +++ b/test/cli.test.mjs @@ -8,6 +8,8 @@ t.describe('CLI', function() { t.test('#constructor() give default options', function() { let cliTest = new CLI() assert.strictEqual(cliTest.reporter, 'list') + assert.strictEqual(cliTest.ignoreOnly, false) + assert.strictEqual(cliTest.timeout, 2000) assert.deepEqual(cliTest.targets, ['test/**']) assert.deepEqual(cliTest.files, []) assert.notOk(cliTest.errored) @@ -38,6 +40,13 @@ t.describe('CLI', function() { assert.strictEqual(cli.reporter, 'dot') assert.notOk(cli.errored) }) + + t.test('should support enabling ignore-only long option', function() { + cli.ignoreOnly = false + cli.parseOptions(['--ignore-only', '-r', 'dot']) + assert.strictEqual(cli.ignoreOnly, true) + assert.notOk(cli.errored) + }) t.test('should support reporter list', function() { cli.reporter = 'list' @@ -56,6 +65,30 @@ t.describe('CLI', function() { assert.ok(cli.errored) }) + t.test('should support overriding timeout with shorthand option', function() { + cli.timeout = 2000 + cli.parseOptions(['-t', '1000']) + assert.strictEqual(cli.timeout, 1000) + assert.notOk(cli.errored) + }) + + t.test('should support overriding timeout with long option', function() { + cli.timeout = 2000 + cli.parseOptions(['--timeout', '250']) + assert.strictEqual(cli.timeout, 250) + assert.notOk(cli.errored) + }) + + t.test('should mark errored if missing timeout', function() { + cli.parseOptions(['--timeout']) + assert.ok(cli.errored) + }) + + t.test('should mark errored if invalid timeout', function() { + cli.parseOptions(['--timeout', 'test']) + assert.ok(cli.errored) + }) + t.test('should add file to targets', function() { cli.parseOptions(['test']) assert.deepEqual(cli.targets, ['test']) diff --git a/test/eltro.test.mjs b/test/eltro.test.mjs index 7c6a838..7c298a9 100644 --- a/test/eltro.test.mjs +++ b/test/eltro.test.mjs @@ -268,6 +268,35 @@ e.test('Eltro should support only tests', async function() { assert.strictEqual(assertIsTrue, true) }) +e.test('Eltro should ignore only if ignoreOnly is specified', async function() { + testsWereRun = true + let assertIsRun = 0 + const t = CreateT() + + t.ignoreOnly = true + + t.begin() + t.describe('', function() { + t.test('a', function() { assertIsRun++ }) + t.test('b', function() { assertIsRun++ }).only() + t.describe('sub', function() { + t.test('d', function() { assertIsRun++ }) + t.only().test('c', function() { assertIsRun++ }) + }) + t.only().describe('sub2', function() { + t.test('d', function() { assertIsRun++ }) + t.test('c', function() { assertIsRun++ }) + }) + t.describe('sub3', function() { + t.test('d', function() { assertIsRun++ }) + t.test('c', function() { assertIsRun++ }) + }) + }) + await t.run() + assert.strictEqual(t.failedTests.length, 0) + assert.strictEqual(assertIsRun, 8) +}) + e.test('Eltro should support skipped tests in front of the test', async function() { testsWereRun = true let assertIsTrue = false