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

Add support for using TLA to configure libtap/settings #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions coverage-map.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const glob = require('glob')
const path = require('path')

const rootFiles = ['settings', 'versions'];
const rootFiles = ['settings', 'versions', 'coverage-map'];

module.exports = t => {
const parts = path.relative(process.cwd(), path.resolve(t)).split(/\\|\//)
const ext = path.extname(t)
const unit = path.basename(parts[1], ext)

if (parts[1] === 'libtap.mjs')
return 'lib/tap.mjs'

const unit = path.basename(parts[1], '.js')
if (rootFiles.includes(unit))
return `${unit}.js`

if (unit === 'coverage-map')
return [ path.basename(__filename) ]
const cov = glob.sync(`lib/${unit}.js`)
const cov = glob.sync(`lib/${unit}${ext}`)
if (!cov.length)
return null
return cov
Expand Down
37 changes: 37 additions & 0 deletions lib/tla.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import settings from 'libtap/settings';

// Defer load of libtap itself until settings are ready
await settings.waitForReady();

const tap = await import('libtap');

// This cannot be deduplicated with `tap.mjs`
export const {
Test, Spawn, Stdin,
spawn, sub,
todo, skip, only, test,
stdinOnly, stdin,
bailout,
comment,
timeout,
main,
process,
processSubtest,
addAssert,
pragma,
plan, end,
beforeEach,
afterEach,
teardown,
autoend,
pass, fail, ok, notOk,
emits,
error, equal, not, same, notSame, strictSame, strictNotSame,
testdir, fixture,
matchSnapshot,
hasStrict, match, notMatch, type,
expectUncaughtException, throwsArgs, throws, doesNotThrow,
rejects, resolves, resolveMatch, resolveMatchSnapshot
} = tap

export default tap
5 changes: 5 additions & 0 deletions npm-run-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ async function runTests() {
const t = require('.')
const coverageMap = require('./coverage-map.js')
const testESM = semver.gte(process.versions.node, '13.10.0')
const testTLA = semver.gte(process.versions.node, '14.8.0')
const testFileGlob = testESM ? 'test/**/*.{js,mjs}' : 'test/**/*.js'
const esLoaderHook = {
NODE_OPTIONS: `${process.env.NODE_OPTIONS || ''} --experimental-loader @istanbuljs/esm-loader-hook`
Expand All @@ -18,6 +19,10 @@ async function runTests() {
t.jobs = os.cpus().length

glob.sync(testFileGlob).forEach(file => {
if (file.endsWith('tla.mjs') && !testTLA) {
return;
}

if (process.platform === 'win32' && file.includes('sigterm')) {
// TODO: investigate proper Win32 replacements for these tests
return;
Expand Down
5 changes: 4 additions & 1 deletion nyc.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

const semver = require('semver')

const excludeTLA = semver.lt(process.versions.node, '14.8.0') ? ['!lib/tla.mjs'] : []

module.exports = {
all: true,
checkCoverage: process.platform !== 'win32',
Expand All @@ -13,6 +15,7 @@ module.exports = {
include: [
'settings.js',
'versions.js',
'lib/**'
'lib/**',
...excludeTLA
]
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"default": "./lib/tap.js"
},
"./settings": "./settings.js",
"./versions": "./versions.js"
"./versions": "./versions.js",
"./tla": "./lib/tla.mjs"
},
"engines": {
"node": ">=10"
Expand Down
18 changes: 18 additions & 0 deletions settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ let rmdirRecursiveSync
let rmdirRecursive

let hasFsRm = false
let settingsResolve
let settingsPromise
function settingsPromiseInitialize() {
if (!settingsPromise) {
settingsPromise = new Promise(resolve => {
settingsResolve = resolve
})
}
}

module.exports = {
atTap: false,
Expand Down Expand Up @@ -62,6 +71,15 @@ module.exports = {
output: process.stdout,
snapshotFile: (cwd, main, argv) => {
return path.resolve(cwd, 'tap-snapshots', main + argv + '.test.cjs')
},
// This tells `libtap/tla` that settings are prepared
markAsReady() {
settingsPromiseInitialize();
settingsResolve();
},
waitForReady() {
settingsPromiseInitialize()
return settingsPromise;
}
}

Expand Down
2 changes: 2 additions & 0 deletions tap-snapshots/test/settings.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
exports[`test/settings.js TAP > must match snapshot 1`] = `
Object {
"atTap": false,
"markAsReady": Function markAsReady(),
"output": "process.stdout",
"rimrafNeeded": "version specific",
"rmdirRecursive": Function rmdirRecursive(dir, cb),
Expand All @@ -18,5 +19,6 @@ Object {
"internals": Array [],
},
"StackUtils": Function StackUtils(classStackUtils),
"waitForReady": Function waitForReady(),
}
`
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/libtap.mjs TAP libtap > must match snapshot 1`] = `
exports[`test/tap.mjs TAP libtap > must match snapshot 1`] = `
Array [
"Spawn",
"Stdin",
Expand Down
12 changes: 12 additions & 0 deletions test/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,15 @@ t.equal(settings.snapshotFile('cwd', 'main', 'args'),
settings.snapshotFile = (cwd, main, args) => [cwd, main, args].join('X')
t.equal(settings.snapshotFile('cwd', 'main', 'args'), 'cwdXmainXargs',
'can override snapshotFile setting function')

let isReady = false
settings.waitForReady().then(() => {
isReady = true;
})

t.equal(isReady, false)

settings.markAsReady()
setTimeout(() => {
t.equal(isReady, true)
})
File renamed without changes.
48 changes: 48 additions & 0 deletions test/tla.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {promisify} from 'util'

const delay = promisify(setTimeout)
const expectedLog = [
'importing settings',
'imported settings',
'watch settings.output',
'importing tla',
'flag ready',
'flaged ready',
'get settings.output',
'imported tla'
];
let log = []

log.push('importing settings')
const settings = (await import('libtap/settings')).default
log.push('imported settings')

let output = process.stdout
let outputRead = 0
Object.defineProperty(settings, 'output', {
get() {
log.push('get settings.output')
outputRead++
return output
},
set(value) {
log.push('set settings.output')
output = value
}
})
log.push('watch settings.output')

log.push('importing tla')
const libPromise = import('libtap/tla')
libPromise.then(t => {
log.push('imported tla')
t.same(log, expectedLog)
})

await delay(50)

log.push('flag ready')
settings.markAsReady()
log.push('flaged ready')

await delay(50);