Skip to content

Commit

Permalink
test: check entry points exports
Browse files Browse the repository at this point in the history
closes #1020
  • Loading branch information
antongolub committed Dec 22, 2024
1 parent 9179bc7 commit 6aac978
Show file tree
Hide file tree
Showing 10 changed files with 687 additions and 329 deletions.
69 changes: 34 additions & 35 deletions scripts/build-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,47 @@
import fs from 'node:fs'
import path from 'node:path'
import * as vendor from '../build/vendor.js'
import * as core from '../build/core.js'
import * as cli from '../build/cli.js'
import * as index from '../build/index.js'

const root = path.resolve(new URL(import.meta.url).pathname, '../..')
const apis = [
'chalk',
'depseek',
'fs',
'glob',
'minimist',
'ps',
'which',
'YAML',
// prettier-ignore
const modules = [
['core', core],
['cli', cli],
['index', index],
['vendor', vendor, ['chalk', 'depseek', 'fs', 'glob', 'minimist', 'ps', 'which', 'YAML',]],
]
const root = path.resolve(new URL(import.meta.url).pathname, '../..')
const filePath = path.resolve(root, `test/export.test.js`)

const copyright = await fs.readFileSync(
path.resolve(root, 'test/fixtures/copyright.txt'),
'utf8'
)

const filePath = path.resolve(root, `test/vendor-export.test.js`)
let fileContents = `${copyright.replace('YEAR', new Date().getFullYear())}
let head = `${copyright.replace('YEAR', new Date().getFullYear())}
import assert from 'node:assert'
import { test, describe } from 'node:test'
import {
${apis.map((v) => ' ' + v).join(',\n')},
} from '../build/vendor.js'
`
import { test, describe } from 'node:test'`
let body = '\n'

for (const [name, ref, apis = Object.keys(ref).sort()] of modules) {
head += `\nimport * as ${name} from '../build/${name}.js'`
body += `\n//prettier-ignore\ndescribe('${name}', () => {\n`
body += ` test('exports', () => {\n`
for (const r of apis) {
const api = ref[r]
body += ` assert.equal(typeof ${name}.${r}, '${typeof api}', '${name}.${r}')\n`
if (typeof api !== 'function' && typeof api !== 'object') continue
for (const k of Object.keys(api).sort()) {
const v = api[k]
body += ` assert.equal(typeof ${name}.${r}.${k}, '${typeof v}', '${name}.${r}.${k}')\n`
}
}
body += ' })\n'
body += '})\n'
}

apis.forEach((name) => {
const api = vendor[name]
const methods = Object.entries(api)
const formatAssert = (k, v, prefix = ' ') =>
`${prefix}assert.equal(typeof ${name}.${k}, '${typeof v}', '${name}.${k}')`
const methodChecks = methods.length
? '\n' + methods.map(([k, v]) => formatAssert(k, v)).join('\n')
: ''
fileContents += `
describe('vendor ${name} API ', () => {
// prettier-ignore
test('exports', () => {
assert.equal(typeof ${name}, '${typeof api}')${methodChecks}
})
})
`
})
const contents = head + body

fs.writeFileSync(filePath, fileContents)
fs.writeFileSync(filePath, contents)
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export async function main() {
await runScript(argv.eval, argv.ext)
return
}
const firstArg = argv._[0]
const [firstArg] = argv._
updateArgv(argv._.slice(firstArg === undefined ? 0 : 1))
if (!firstArg || firstArg === '-') {
const success = await scriptFromStdin(argv.ext)
Expand Down
3 changes: 2 additions & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const CWD = Symbol('processCwd')
const SYNC = Symbol('syncExec')
const EOL = Buffer.from(_EOL)
const SIGTERM = 'SIGTERM'
const ENV_PREFIX = 'ZX_'
const storage = new AsyncLocalStorage<Options>()

function getStore() {
Expand Down Expand Up @@ -857,7 +858,7 @@ const promisifyStream = <S extends Writable>(

export function resolveDefaults(
defs: Options,
prefix: string = 'ZX_',
prefix: string = ENV_PREFIX,
env = process.env
) {
const allowed = new Set([
Expand Down
4 changes: 2 additions & 2 deletions test/all.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import './goods.test.js'
import './index.test.js'
import './package.test.js'
import './util.test.js'
import './vendor-yaml.test.js'
import './vendor-export.test.js'
import './yaml.test.js'
import './export.test.js'
17 changes: 15 additions & 2 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,20 @@ import { fileURLToPath } from 'node:url'
import net from 'node:net'
import getPort from 'get-port'
import '../build/globals.js'
import { isMain, normalizeExt, transformMarkdown } from '../build/cli.js'
import {
argv,
importPath,
injectGlobalRequire,
isMain,
main,
normalizeExt,
runScript,
printUsage,
scriptFromStdin,
scriptFromHttp,
transformMarkdown,
writeAndImport,
} from '../build/cli.js'

const __filename = fileURLToPath(import.meta.url)
const spawn = $.spawn
Expand All @@ -38,7 +51,7 @@ const getServer = (resp = [], log = console.log) => {
}

describe('cli', () => {
// Helps detect unresolved ProcessPromise.
// Helps to detect unresolved ProcessPromise.
before(() => {
const spawned = []
$.spawn = (...args) => {
Expand Down
13 changes: 11 additions & 2 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,20 @@ import { WriteStream } from 'node:fs'
import { Readable, Transform, Writable } from 'node:stream'
import { Socket } from 'node:net'
import {
$,
ProcessPromise,
ProcessOutput,
resolveDefaults,
} from '../build/index.js'
import '../build/globals.js'
cd,
syncProcessCwd,
log,
kill,
defaults,
within,
usePowerShell,
usePwsh,
useBash,
} from '../build/core.js'

describe('core', () => {
describe('resolveDefaults()', () => {
Expand Down
Loading

0 comments on commit 6aac978

Please sign in to comment.