Skip to content

Commit

Permalink
test: separate tests for experimental api
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Mar 18, 2022
1 parent 24c4950 commit 813066a
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 52 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -28,8 +28,8 @@
"scripts": {
"test:cov": "c8 --reporter=html npm run test",
"test": "node zx.mjs test/full.test.mjs",
"test:cli": "node zx.mjs test/zx.test.mjs",
"test:index": "node zx.mjs test/index.test.mjs"
"test:zx": "npm run test zx",
"test:index": "npm run test index"
},
"dependencies": {
"@types/fs-extra": "^9.0.13",
Expand Down
57 changes: 57 additions & 0 deletions test/experimental.test.mjs
@@ -0,0 +1,57 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {echo, retry, startSpinner, withTimeout} from '../src/experimental.mjs'
import {assert, test as t} from './test-utils.mjs'
import chalk from 'chalk'

const test = t.bind(null, 'experimental')

if (test('Retry works')) {
let exitCode = 0
let now = Date.now()
try {
await retry(5, 50)`exit 123`
} catch (p) {
exitCode = p.exitCode
}
assert.equal(exitCode, 123)
assert(Date.now() >= now + 50 * (5 - 1))
}

if (test('withTimeout works')) {
let exitCode = 0
let signal
try {
await withTimeout(100, 'SIGKILL')`sleep 9999`
} catch (p) {
exitCode = p.exitCode
signal = p.signal
}
assert.equal(exitCode, null)
assert.equal(signal, 'SIGKILL')
}

if (test('echo works')) {
echo(chalk.red('foo'), chalk.green('bar'), chalk.bold('baz'))
echo`${chalk.red('foo')} ${chalk.green('bar')} ${chalk.bold('baz')}`
echo(await $`echo ${chalk.red('foo')}`, await $`echo ${chalk.green('bar')}`, await $`echo ${chalk.bold('baz')}`)
}

if (test('spinner works')) {
let s = startSpinner('waiting')

await sleep(1000)
s()
}
6 changes: 5 additions & 1 deletion test/full.test.mjs
Expand Up @@ -12,5 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import './index.test.mjs'
import {printTestDigest} from './test-utils.mjs'
import './zx.test.mjs'
import './index.test.mjs'
import './experimental.test.mjs'

printTestDigest()
46 changes: 3 additions & 43 deletions test/index.test.mjs
Expand Up @@ -12,14 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.


import {inspect} from 'util'
import chalk from 'chalk'
import {Writable} from 'stream'
import {Socket} from 'net'

import {assert, printTestDigest, test} from './test-utils.mjs'
import {retry, echo, startSpinner, withTimeout} from '../src/experimental.mjs'
import {assert, test as t} from './test-utils.mjs'

const test = t.bind(null, 'index')

if (test('Only stdout is used during command substitution')) {
let hello = await $`echo Error >&2; echo Hello`
Expand Down Expand Up @@ -287,48 +287,8 @@ if (test('which available')) {
assert.equal(which.sync('npm'), await which('npm'))
}

if (test('Retry works (experimental)')) {
let exitCode = 0
let now = Date.now()
try {
await retry(5, 50)`exit 123`
} catch (p) {
exitCode = p.exitCode
}
assert.equal(exitCode, 123)
assert(Date.now() >= now + 50 * (5 - 1))
}

if (test('withTimeout works (experimental)')) {
let exitCode = 0
let signal
try {
await withTimeout(100, 'SIGKILL')`sleep 9999`
} catch (p) {
exitCode = p.exitCode
signal = p.signal
}
assert.equal(exitCode, null)
assert.equal(signal, 'SIGKILL')
}

if (test('echo works (experimental)')) {
echo(chalk.red('foo'), chalk.green('bar'), chalk.bold('baz'))
echo`${chalk.red('foo')} ${chalk.green('bar')} ${chalk.bold('baz')}`
echo(await $`echo ${chalk.red('foo')}`, await $`echo ${chalk.green('bar')}`, await $`echo ${chalk.bold('baz')}`)
}

if (test('spinner works (experimental)')) {
let s = startSpinner('waiting')

await sleep(1000)
s()
}

if (test('require() is working in ESM')) {
let data = require('../package.json')
assert.equal(data.name, 'zx')
assert.equal(data, require('zx/package.json'))
}

printTestDigest()
6 changes: 3 additions & 3 deletions test/test-utils.mjs
Expand Up @@ -18,10 +18,10 @@ export {strict as assert} from 'assert'

let всегоТестов = 0

export function test(name) {
export function test(group, name) {
let фильтр = process.argv[3] || '.'
if (RegExp(фильтр).test(name)) {
console.log('\n' + chalk.bgGreenBright.black(` ${name} `))
if (RegExp(фильтр).test(name) || RegExp(фильтр).test(group)) {
console.log('\n' + chalk.bgGreenBright.black(`${chalk.inverse(' ' + group + ' ')} ${name} `))
всегоТестов++
return true
}
Expand Down
6 changes: 3 additions & 3 deletions test/zx.test.mjs
Expand Up @@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {assert, printTestDigest, test} from './test-utils.mjs'
import {assert, test as t} from './test-utils.mjs'

const test = t.bind(null, 'zx')

if (test('supports `-v` flag / prints version')) {
let v = (await $`node zx.mjs -v`).toString().trim()
Expand Down Expand Up @@ -55,5 +57,3 @@ if (test('The require() is working from stdin')) {
if (test('Markdown scripts are working')) {
await $`node zx.mjs docs/markdown.md`
}

printTestDigest()

0 comments on commit 813066a

Please sign in to comment.