Skip to content

Commit

Permalink
Improve tests a little
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed May 30, 2022
1 parent 0d3b240 commit 2e45a01
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 110 deletions.
32 changes: 14 additions & 18 deletions package.json
Expand Up @@ -3,28 +3,28 @@
"version": "6.1.0",
"description": "A tool for writing better scripts.",
"type": "module",
"main": "build/index.js",
"types": "build/index.d.ts",
"main": "./build/index.js",
"types": "./build/index.d.ts",
"exports": {
".": {
"import": "./build/index.js",
"types": "./build/index.d.ts"
"types": "./build/index.d.ts",
"import": "./build/index.js"
},
"./globals": {
"import": "./build/globals.js",
"types": "./build/globals.d.ts"
"types": "./build/globals.d.ts",
"import": "./build/globals.js"
},
"./experimental": {
"import": "./build/experimental.js",
"types": "./build/experimental.d.ts"
"types": "./build/experimental.d.ts",
"import": "./build/experimental.js"
},
"./cli": {
"import": "./zx.js",
"types": "./zx.d.ts"
"types": "./build/cli.d.ts",
"import": "./build/cli.js"
},
"./core": {
"import": "./build/core.js",
"types": "./build/core.d.ts"
"types": "./build/core.d.ts",
"import": "./build/core.js"
},
"./package.json": "./package.json"
},
Expand All @@ -37,12 +37,8 @@
"scripts": {
"fmt": "prettier --write .",
"build": "tsc",
"test": "tsc && npm run test:unit",
"coverage": "c8 --reporter=html npm test",
"test:unit": "uvu test -i fixtures",
"test:cov": "c8 --reporter=html npm run test:unit",
"test:zx": "npm run test zx",
"test:index": "npm run test index"
"test": "tsc && uvu test -i fixtures",
"coverage": "c8 --reporter=html npm test"
},
"dependencies": {
"@types/fs-extra": "^9.0.13",
Expand Down
8 changes: 5 additions & 3 deletions test/cli.test.js
Expand Up @@ -36,9 +36,11 @@ test('supports `--experimental` flag', async () => {
await $`echo 'echo("test")' | node build/cli.js --experimental`
})

test('supports `--quiet` flag / Quiet mode is working', async () => {
let p = await $`node build/cli.js --quiet docs/markdown.md`
assert.ok(!p.stdout.includes('whoami'))
test('supports `--quiet` flag', async () => {
let p = await $`node build/cli.js test/fixtures/markdown.md`
assert.ok(!p.stdout.includes('ignore'), 'ignore was printed')
assert.ok(p.stdout.includes('hello'), 'no hello')
assert.ok(p.stdout.includes('world'), 'no world')
})

test('supports `--shell` flag ', async () => {
Expand Down
34 changes: 0 additions & 34 deletions test/fixtures/cd-parallel-contexts.mjs

This file was deleted.

33 changes: 0 additions & 33 deletions test/fixtures/cd-relative-paths.mjs

This file was deleted.

13 changes: 0 additions & 13 deletions test/fixtures/kill-signal.mjs

This file was deleted.

5 changes: 0 additions & 5 deletions test/fixtures/kill.mjs

This file was deleted.

34 changes: 34 additions & 0 deletions test/fixtures/markdown.md
@@ -0,0 +1,34 @@
# Markdown

ignore

>
> ```
> echo ignore
> ```
```js
await $`whoami`
await $`echo ${__dirname}`
```

```js
console.log(chalk.yellowBright(__filename))
```

```js
await import('chalk')
```

```bash
VAR=$(echo hello)
echo "$VAR"
```

console.log('world')

Other code blocks are ignored:

```css
.ignore {}
```
80 changes: 76 additions & 4 deletions test/index.test.js
Expand Up @@ -20,6 +20,7 @@ import { Writable } from 'node:stream'
import { Socket } from 'node:net'
import '../build/globals.js'
import { ProcessPromise } from '../build/index.js'
import {getCtx, runInCtx} from '../build/experimental.js'

test('only stdout is used during command substitution', async () => {
let hello = await $`echo Error >&2; echo Hello`
Expand Down Expand Up @@ -251,19 +252,90 @@ test('executes a script from $PATH', async () => {
})

test('cd() works with relative paths', async () => {
await $`node build/cli.js test/fixtures/cd-relative-paths.mjs`
let cwd = process.cwd()
assert.equal($.cwd, cwd)
try {
fs.mkdirpSync('/tmp/zx-cd-test/one/two')
cd('/tmp/zx-cd-test/one/two')
let p1 = $`pwd`
assert.ok($.cwd.endsWith('/two'))
assert.ok(process.cwd().endsWith('/two'))

cd('..')
let p2 = $`pwd`
assert.ok($.cwd.endsWith('/one'))
assert.ok(process.cwd().endsWith('/one'))

cd('..')
let p3 = $`pwd`
assert.ok(process.cwd().endsWith('/zx-cd-test'))
assert.ok($.cwd.endsWith('/tmp/zx-cd-test'))

let results = (await Promise.all([p1, p2, p3])).map((p) =>
path.basename(p.stdout.trim())
)

assert.equal(results, ['two', 'one', 'zx-cd-test'])
} catch (e) {
assert.ok(!e, e)
} finally {
fs.rmSync('/tmp/zx-cd-test', { recursive: true })
cd(cwd)
assert.equal($.cwd, cwd)
}
})

test('cd() does not affect parallel contexts', async () => {
await $`node build/cli.js test/fixtures/cd-parallel-contexts.mjs`
let cwd = process.cwd()
let resolve, reject
let promise = new ProcessPromise((...args) => ([resolve, reject] = args))

try {
fs.mkdirpSync('/tmp/zx-cd-parallel')
runInCtx({ ...getCtx() }, async () => {
assert.equal($.cwd, cwd)
await sleep(10)
cd('/tmp/zx-cd-parallel')
assert.ok(getCtx().cwd.endsWith('/zx-cd-parallel'))
assert.ok($.cwd.endsWith('/zx-cd-parallel'))
})

runInCtx({ ...getCtx() }, async () => {
assert.equal($.cwd, cwd)
assert.equal(getCtx().cwd, cwd)
await sleep(20)
assert.equal(getCtx().cwd, cwd)
assert.ok($.cwd.endsWith('/zx-cd-parallel'))
resolve()
})

await promise
} catch (e) {
assert.ok(!e, e)
} finally {
fs.rmSync('/tmp/zx-cd-parallel', { recursive: true })
cd(cwd)
}
})

test('kill() method works', async () => {
await $`node build/cli.js test/fixtures/kill.mjs`
let p = nothrow($`sleep 9999`)
setTimeout(() => {
p.kill()
}, 100)
await p
})

test('a signal is passed with kill() method', async () => {
await $`node build/cli.js test/fixtures/kill-signal.mjs`
let p = $`while true; do :; done`
setTimeout(() => p.kill('SIGKILL'), 100)
let signal
try {
await p
} catch (p) {
signal = p.signal
}
assert.equal(signal, 'SIGKILL')
})

test('YAML works', async () => {
Expand Down

0 comments on commit 2e45a01

Please sign in to comment.