Commit 2e45a01

Anton Medvedev <anton@medv.io>
2022-05-30 23:12:01
Improve tests a little
1 parent 0d3b240
test/fixtures/cd-parallel-contexts.mjs
@@ -1,34 +0,0 @@
-import { strict as assert } from 'assert'
-import { ProcessPromise } from '../../build/index.js'
-import { runInCtx, getCtx } from '../../build/experimental.js'
-
-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(!e, e)
-} finally {
-  fs.rmSync('/tmp/zx-cd-parallel', { recursive: true })
-  cd(cwd)
-}
test/fixtures/cd-relative-paths.mjs
@@ -1,33 +0,0 @@
-import { strict as assert } from 'assert'
-
-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.deepEqual(results, ['two', 'one', 'zx-cd-test'])
-} catch (e) {
-  assert(!e, e)
-} finally {
-  fs.rmSync('/tmp/zx-cd-test', { recursive: true })
-  cd(cwd)
-  assert.equal($.cwd, cwd)
-}
test/fixtures/kill-signal.mjs
@@ -1,13 +0,0 @@
-import { strict } from 'assert'
-
-let p = $`while true; do :; done`
-setTimeout(() => p.kill('SIGKILL'), 100)
-let signal
-
-try {
-  await p
-} catch (p) {
-  signal = p.signal
-}
-
-strict.equal(signal, 'SIGKILL')
test/fixtures/kill.mjs
@@ -1,5 +0,0 @@
-let p = nothrow($`sleep 9999`)
-setTimeout(() => {
-  p.kill()
-}, 100)
-await p
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 {}
+```
test/cli.test.js
@@ -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 () => {
test/index.test.js
@@ -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`
@@ -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 () => {
package.json
@@ -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"
   },
@@ -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",