Skip to content

Commit

Permalink
....
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 21, 2023
1 parent f2470f0 commit 61dc1ed
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 102 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@types/unist": "^3.0.0",
"c8": "^8.0.0",
"concat-stream": "^2.0.0",
"execa": "^8.0.0",
"prettier": "^3.0.0",
"remark": "^14.0.0",
"remark-cli": "^11.0.0",
Expand Down
203 changes: 101 additions & 102 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* @typedef {import('node:child_process').ExecException} ExecExceptionBasic
* @typedef {import('execa').ExecaError<string>} ExecaError
*/

/**
Expand All @@ -15,15 +16,16 @@
*/

import assert from 'node:assert/strict'
import {spawn} from 'node:child_process'
// . import {spawn} from 'node:child_process'
// . exec as execCb
import fs from 'node:fs/promises'
// . import {sep} from 'node:path'
// import {platform} from 'node:process'
import {sep} from 'node:path'
// . import {platform} from 'node:process'
import test from 'node:test'
import {fileURLToPath} from 'node:url'
import {promisify} from 'node:util'
import concatStream from 'concat-stream'
// . import {promisify} from 'node:util'
import {execa} from 'execa'
// . import concatStream from 'concat-stream'
import stripAnsi from 'strip-ansi'

// . const exec = promisify(execCb)
Expand All @@ -46,67 +48,64 @@ test('args', async function (t) {
// const longFlag = String(
// await fs.readFile(new URL('LONG_FLAG', base))
// ).replace(/\r\n/g, '\n')

// .
// await t.test('should expose the public api', async function () {
// assert.deepEqual(Object.keys(await import('../index.js')).sort(), ['args'])
// })
// . const shortFlag = String(
// const shortFlag = String(
// await fs.readFile(new URL('SHORT_FLAG', base))
// ).replace(/\r\n/g, '\n')

// await t.test('should fail on missing files', async function () {
// try {
// await exec(binPath + ' missing.txt', {cwd: base})
// assert.fail()
// } catch (error) {
// const result = /** @type {ExecException} */ (error)
// assert.deepEqual(
// [result.code, cleanError(result.stderr)],
// [
// 1,
// [
// 'missing.txt',
// ' error No such file or folder',
// ' [cause]:',
// ' Error: ENOENT:…',
// '',
// '✖ 1 error',
// ''
// ].join('\n')
// ]
// )
// }
// })
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('../index.js')).sort(), ['args'])
})

// .
// await t.test('should accept a path to a file', async function () {
// const result = await exec(binPath + ' one.txt')
await t.test('should fail on missing files', async function () {
try {
await execa(binPath, ['missing.txt'])
assert.fail()
} catch (error) {
const result = /** @type {ExecaError} */ (error)
assert.deepEqual(
[result.exitCode, cleanError(result.stderr)],
[
1,
[
'missing.txt',
' error No such file or folder',
' [cause]:',
' Error: ENOENT:…',
'',
'✖ 1 error'
].join('\n')
]
)
}
})

// assert.deepEqual(
// [result.stdout, cleanError(result.stderr)],
// ['one\n', 'one.txt: no issues found\n']
// )
// })
await t.test('should accept a path to a file', async function () {
const result = await execa(binPath, ['one.txt'])

// await t.test('should accept a path to a directory', async function () {
// const result = await exec(binPath + ' .')
assert.deepEqual(
[result.stdout, cleanError(result.stderr)],
['one', 'one.txt: no issues found']
)
})

// assert.deepEqual(
// [result.stdout, cleanError(result.stderr)],
// [
// '',
// [
// 'one.txt: no issues found',
// 'three' + sep + 'five.txt: no issues found',
// 'three' + sep + 'four.txt: no issues found',
// 'two.txt: no issues found',
// ''
// ].join('\n')
// ]
// )
// })
await t.test('should accept a path to a directory', async function () {
const result = await execa(binPath, ['.'])

assert.deepEqual(
[result.stdout, cleanError(result.stderr)],
[
'',
[
'one.txt: no issues found',
'three' + sep + 'five.txt: no issues found',
'three' + sep + 'four.txt: no issues found',
'two.txt: no issues found'
].join('\n')
]
)
})

// .
// await t.test('should accept a glob to files', async function () {
// const result = await exec(binPath + ' "*.txt"')

Expand Down Expand Up @@ -682,58 +681,58 @@ test('args', async function (t) {
// )
// })

await t.test('should support `--watch`', async function () {
// Ignore checking watch mode on Windows.
// if (platform === 'win32') {
// console.warn('Not checking watch mode on windows…')
// return
// }
// await t.test('should support `--watch`', async function () {
// // Ignore checking watch mode on Windows.
// // if (platform === 'win32') {
// // console.warn('Not checking watch mode on windows…')
// // return
// // }

const url = new URL('watch.txt', base)
await fs.writeFile(url, 'alpha')
// const url = new URL('watch.txt', base)
// await fs.writeFile(url, 'alpha')

await new Promise(function (resolve) {
const delay = 500
// await new Promise(function (resolve) {
// const delay = 500

const proc = spawn(binPath, ['watch.txt', '-w'])
// const proc = spawn(binPath, ['watch.txt', '-w'])

setTimeout(seeYouLaterAlligator, delay)
// setTimeout(seeYouLaterAlligator, delay)

proc.on('close', async function (code) {
assert.equal(code, 0)
await fs.unlink(url)
resolve(undefined)
})
// proc.on('close', async function (code) {
// assert.equal(code, 0)
// await fs.unlink(url)
// resolve(undefined)
// })

proc.stdout.pipe(
concatStream(function (d) {
assert.deepEqual(String(d), '')
})
)
proc.stderr.pipe(
concatStream(function (d) {
assert.deepEqual(
cleanError(String(d)),
[
'Watching... (press CTRL+C to exit)',
'watch.txt: no issues found',
'watch.txt: no issues found',
'\n'
].join('\n')
)
})
)
// proc.stdout.pipe(
// concatStream(function (d) {
// assert.deepEqual(String(d), '')
// })
// )
// proc.stderr.pipe(
// concatStream(function (d) {
// assert.deepEqual(
// cleanError(String(d)),
// [
// 'Watching... (press CTRL+C to exit)',
// 'watch.txt: no issues found',
// 'watch.txt: no issues found',
// '\n'
// ].join('\n')
// )
// })
// )

async function seeYouLaterAlligator() {
await fs.writeFile(url, 'bravo')
setTimeout(afterAWhileCrocodile, delay)
}
// async function seeYouLaterAlligator() {
// await fs.writeFile(url, 'bravo')
// setTimeout(afterAWhileCrocodile, delay)
// }

function afterAWhileCrocodile() {
proc.kill('SIGINT')
}
})
})
// function afterAWhileCrocodile() {
// proc.kill('SIGINT')
// }
// })
// })

// .
// await t.test('should not regenerate when watching', async function () {
Expand Down

0 comments on commit 61dc1ed

Please sign in to comment.