From a64653ca627b2b19277635a9cca9f66b4ab93846 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Wed, 30 Oct 2024 22:39:02 +0300 Subject: [PATCH] style: use let for dynamic values only --- src/goods.ts | 2 +- test/cli.test.js | 48 ++++++++--------- test/core.test.js | 85 +++++++++++++++--------------- test/deps.test.js | 2 +- test/fixtures/interactive.mjs | 2 +- test/fixtures/require.mjs | 2 +- test/fixtures/ts-project/script.ts | 2 +- test/global.test.js | 2 +- test/goods.test.js | 14 ++--- 9 files changed, 79 insertions(+), 80 deletions(-) diff --git a/src/goods.ts b/src/goods.ts index d429598890..a26d0901a5 100644 --- a/src/goods.ts +++ b/src/goods.ts @@ -29,7 +29,7 @@ export * as os from 'node:os' export const argv = minimist(process.argv.slice(2)) export function updateArgv(args: string[]) { - for (var k in argv) delete argv[k] + for (const k in argv) delete argv[k] Object.assign(argv, minimist(args)) } diff --git a/test/cli.test.js b/test/cli.test.js index 576ec624d4..fb1ad0aa08 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -48,14 +48,14 @@ describe('cli', () => { }) test('prints help', async () => { - let p = $`node build/cli.js -h` + const p = $`node build/cli.js -h` p.stdin.end() - let help = await p + const help = await p assert.match(help.stdout, /zx/) }) test('zx prints usage if no param passed', async () => { - let p = $`node build/cli.js` + const p = $`node build/cli.js` p.stdin.end() try { await p @@ -67,62 +67,62 @@ describe('cli', () => { }) test('starts repl with --repl', async () => { - let p = $`node build/cli.js --repl` + const p = $`node build/cli.js --repl` p.stdin.write('await $`echo f"o"o`\n') p.stdin.write('"b"+"ar"\n') p.stdin.end() - let out = await p + const out = await p assert.match(out.stdout, /foo/) assert.match(out.stdout, /bar/) }) test('starts repl with verbosity off', async () => { - let p = $`node build/cli.js --repl` + const p = $`node build/cli.js --repl` p.stdin.write('"verbose" + " is " + $.verbose\n') p.stdin.end() - let out = await p + const out = await p assert.match(out.stdout, /verbose is false/) }) test('supports `--quiet` flag', async () => { - let p = await $`node build/cli.js --quiet test/fixtures/markdown.md` + const p = await $`node build/cli.js --quiet test/fixtures/markdown.md` assert.ok(!p.stderr.includes('ignore'), 'ignore was printed') assert.ok(!p.stderr.includes('hello'), 'no hello') assert.ok(p.stdout.includes('world'), 'no world') }) test('supports `--shell` flag ', async () => { - let shell = $.shell - let p = + const shell = $.shell + const p = await $`node build/cli.js --verbose --shell=${shell} <<< '$\`echo \${$.shell}\`'` assert.ok(p.stderr.includes(shell)) }) test('supports `--prefix` flag ', async () => { - let prefix = 'set -e;' - let p = + const prefix = 'set -e;' + const p = await $`node build/cli.js --verbose --prefix=${prefix} <<< '$\`echo \${$.prefix}\`'` assert.ok(p.stderr.includes(prefix)) }) test('supports `--postfix` flag ', async () => { - let postfix = '; exit 0' - let p = + const postfix = '; exit 0' + const p = await $`node build/cli.js --verbose --postfix=${postfix} <<< '$\`echo \${$.postfix}\`'` assert.ok(p.stderr.includes(postfix)) }) test('supports `--cwd` option ', async () => { - let cwd = path.resolve(fileURLToPath(import.meta.url), '../../temp') + const cwd = path.resolve(fileURLToPath(import.meta.url), '../../temp') fs.mkdirSync(cwd, { recursive: true }) - let p = + const p = await $`node build/cli.js --verbose --cwd=${cwd} <<< '$\`echo \${$.cwd}\`'` assert.ok(p.stderr.endsWith(cwd + '\n')) }) test('scripts from https', async () => { const server = $`cat ${path.resolve('test/fixtures/echo.http')} | nc -l 8080` - let out = + const out = await $`node build/cli.js --verbose http://127.0.0.1:8080/echo.mjs` assert.match(out.stderr, /test/) await server.kill() @@ -130,7 +130,7 @@ describe('cli', () => { test('scripts from https not ok', async () => { const server = $`echo $'HTTP/1.1 500\n\n' | nc -l 8081` - let out = await $`node build/cli.js http://127.0.0.1:8081`.nothrow() + const out = await $`node build/cli.js http://127.0.0.1:8081`.nothrow() assert.match(out.stderr, /Error: Can't get/) await server.kill() }) @@ -145,7 +145,7 @@ describe('cli', () => { }) test('require() is working from stdin', async () => { - let out = + const out = await $`node build/cli.js <<< 'console.log(require("./package.json").name)'` assert.match(out.stdout, /zx/) }) @@ -167,14 +167,14 @@ describe('cli', () => { }) test('markdown scripts are working for CRLF', async () => { - let p = await $`node build/cli.js test/fixtures/markdown-crlf.md` + const p = await $`node build/cli.js test/fixtures/markdown-crlf.md` assert.ok(p.stdout.includes('Hello, world!')) }) test('exceptions are caught', async () => { - let out1 = await $`node build/cli.js <<<${'await $`wtf`'}`.nothrow() + const out1 = await $`node build/cli.js <<<${'await $`wtf`'}`.nothrow() + const out2 = await $`node build/cli.js <<<'throw 42'`.nothrow() assert.match(out1.stderr, /Error:/) - let out2 = await $`node build/cli.js <<<'throw 42'`.nothrow() assert.match(out2.stderr, /42/) }) @@ -184,7 +184,7 @@ describe('cli', () => { }) test('eval works with stdin', async () => { - let p = $`(printf foo; sleep 0.1; printf bar) | node build/cli.js --eval 'echo(await stdin())'` + const p = $`(printf foo; sleep 0.1; printf bar) | node build/cli.js --eval 'echo(await stdin())'` assert.equal((await p).stdout, 'foobar\n') }) @@ -231,7 +231,7 @@ describe('cli', () => { }) test('exit code can be set', async () => { - let p = await $`node build/cli.js test/fixtures/exit-code.mjs`.nothrow() + const p = await $`node build/cli.js test/fixtures/exit-code.mjs`.nothrow() assert.equal(p.exitCode, 42) }) diff --git a/test/core.test.js b/test/core.test.js index f786b5d2a4..715a90e5cf 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -26,21 +26,21 @@ describe('core', () => { describe('$', () => { test('is a regular function', async () => { const _$ = $.bind(null) - let foo = await _$`echo foo` + const foo = await _$`echo foo` assert.equal(foo.stdout, 'foo\n') assert.ok(typeof $.call === 'function') assert.ok(typeof $.apply === 'function') }) test('only stdout is used during command substitution', async () => { - let hello = await $`echo Error >&2; echo Hello` - let len = +(await $`echo ${hello} | wc -c`) + const hello = await $`echo Error >&2; echo Hello` + const len = +(await $`echo ${hello} | wc -c`) assert.equal(len, 6) }) test('env vars works', async () => { process.env.ZX_TEST_FOO = 'foo' - let foo = await $`echo $ZX_TEST_FOO` + const foo = await $`echo $ZX_TEST_FOO` assert.equal(foo.stdout, 'foo\n') }) @@ -51,7 +51,7 @@ describe('core', () => { }) test('arguments are quoted', async () => { - let bar = 'bar"";baz!$#^$\'&*~*%)({}||\\/' + const bar = 'bar"";baz!$#^$\'&*~*%)({}||\\/' assert.equal((await $`echo ${bar}`).stdout.trim(), bar) }) @@ -83,7 +83,7 @@ describe('core', () => { }) test('can create a dir with a space in the name', async () => { - let name = 'foo bar' + const name = 'foo bar' try { await $`mkdir /tmp/${name}` } catch { @@ -104,13 +104,13 @@ describe('core', () => { }) test('toString() is called on arguments', async () => { - let foo = 0 - let p = await $`echo ${foo}` + const foo = 0 + const p = await $`echo ${foo}` assert.equal(p.stdout, '0\n') }) test('can use array as an argument', async () => { - let args = ['-n', 'foo'] + const args = ['-n', 'foo'] assert.equal((await $`echo ${args}`).toString(), 'foo') }) @@ -128,9 +128,9 @@ describe('core', () => { test('snapshots works', async () => { await within(async () => { $.prefix += 'echo success;' - let p = $`:` + const p = $`:` $.prefix += 'echo fail;' - let out = await p + const out = await p assert.equal(out.stdout, 'success\n') assert.doesNotMatch(out.stdout, /fail/) }) @@ -328,26 +328,26 @@ describe('core', () => { }) test('stdio() works', async () => { - let p = $`printf foo` - await p + const p1 = $`printf foo` + await p1 // assert.throws(() => p.stdin) - assert.equal((await p).stdout, 'foo') - let b = $`read; printf $REPLY` - b.stdin.write('bar\n') - assert.equal((await b).stdout, 'bar') + assert.equal((await p1).stdout, 'foo') + const p2 = $`read; printf $REPLY` + p2.stdin.write('bar\n') + assert.equal((await p2).stdout, 'bar') }) describe('pipe() API', () => { test('accepts Writable', async () => { let contents = '' - let stream = new Writable({ + const stream = new Writable({ write: function (chunk, encoding, next) { contents += chunk.toString() next() }, }) - let p1 = $`echo 'test'` - let p2 = p1.pipe(stream) + const p1 = $`echo 'test'` + const p2 = p1.pipe(stream) await p2 assert.ok(p1._piped) assert.ok(p1.stderr instanceof Socket) @@ -360,7 +360,7 @@ describe('core', () => { await $`echo foo`.pipe(fs.createWriteStream(file)) assert.equal((await fs.readFile(file)).toString(), 'foo\n') - let r = $`cat` + const r = $`cat` fs.createReadStream(file).pipe(r.stdin) assert.equal((await r).stdout, 'foo\n') } finally { @@ -397,19 +397,19 @@ describe('core', () => { ) }) - describe('is chainable', () => { - test('$ to $', async () => { - let { stdout: o1 } = await $`echo "hello"` + describe('supports chaining', () => { + test('$ > $', async () => { + const { stdout: o1 } = await $`echo "hello"` .pipe($`awk '{print $1" world"}'`) .pipe($`tr '[a-z]' '[A-Z]'`) assert.equal(o1, 'HELLO WORLD\n') - let { stdout: o2 } = await $`echo "hello"` + const { stdout: o2 } = await $`echo "hello"` .pipe`awk '{print $1" world"}'`.pipe`tr '[a-z]' '[A-Z]'` assert.equal(o2, 'HELLO WORLD\n') }) - test('Transform/Duplex', async () => { + test('$ > stream', async () => { const file = tempfile() const p = $`echo "hello"` .pipe( @@ -428,7 +428,7 @@ describe('core', () => { }) }) - it('supports multipiping', async () => { + it('supports delayed piping', async () => { const result = $`echo 1; sleep 1; echo 2; sleep 1; echo 3` const piped1 = result.pipe`cat` let piped2 @@ -521,8 +521,8 @@ describe('core', () => { describe('handles halt option', () => { test('just works', async () => { - let filepath = `/tmp/${Math.random().toString()}` - let p = $({ halt: true })`touch ${filepath}` + const filepath = `${tmpdir()}/${Math.random().toString()}` + const p = $({ halt: true })`touch ${filepath}` await sleep(1) assert.ok( !fs.existsSync(filepath), @@ -533,7 +533,7 @@ describe('core', () => { }) test('await on halted throws', async () => { - let p = $({ halt: true })`sleep 1` + const p = $({ halt: true })`sleep 1` let ok = true try { await p @@ -602,7 +602,7 @@ describe('core', () => { }) test('a signal is passed with kill() method', async () => { - let p = $`while true; do :; done` + const p = $`while true; do :; done` setTimeout(() => p.kill('SIGKILL'), 100) let signal try { @@ -615,8 +615,8 @@ describe('core', () => { }) test('quiet() mode is working', async () => { + const log = console.log let stdout = '' - let log = console.log console.log = (...args) => { stdout += args.join(' ') } @@ -626,7 +626,6 @@ describe('core', () => { { // Deprecated. let stdout = '' - let log = console.log console.log = (...args) => { stdout += args.join(' ') } @@ -648,11 +647,11 @@ describe('core', () => { }) test('nothrow() does not throw', async () => { - let { exitCode } = await $`exit 42`.nothrow() + const { exitCode } = await $`exit 42`.nothrow() assert.equal(exitCode, 42) { // Deprecated. - let { exitCode } = await nothrow($`exit 42`) + const { exitCode } = await nothrow($`exit 42`) assert.equal(exitCode, 42) } }) @@ -771,21 +770,21 @@ describe('core', () => { describe('cd()', () => { test('works with relative paths', async () => { - let cwd = process.cwd() + const cwd = process.cwd() try { fs.mkdirpSync('/tmp/zx-cd-test/one/two') cd('/tmp/zx-cd-test/one/two') - let p1 = $`pwd` + const p1 = $`pwd` assert.equal($.cwd, undefined) assert.ok(process.cwd().endsWith('/two')) cd('..') - let p2 = $`pwd` + const p2 = $`pwd` assert.equal($.cwd, undefined) assert.ok(process.cwd().endsWith('/one')) cd('..') - let p3 = $`pwd` + const p3 = $`pwd` assert.equal($.cwd, undefined) assert.ok(process.cwd().endsWith('/tmp/zx-cd-test')) @@ -859,7 +858,7 @@ describe('core', () => { describe('within()', () => { test('just works', async () => { let resolve, reject - let promise = new Promise((...args) => ([resolve, reject] = args)) + const promise = new Promise((...args) => ([resolve, reject] = args)) function yes() { assert.equal($.verbose, true) @@ -884,9 +883,9 @@ describe('core', () => { test('keeps the cwd ref for internal $ calls', async () => { let resolve, reject - let promise = new Promise((...args) => ([resolve, reject] = args)) - let cwd = process.cwd() - let pwd = await $`pwd` + const promise = new Promise((...args) => ([resolve, reject] = args)) + const cwd = process.cwd() + const pwd = await $`pwd` within(async () => { cd('/tmp') diff --git a/test/deps.test.js b/test/deps.test.js index a493832a52..823a60275d 100644 --- a/test/deps.test.js +++ b/test/deps.test.js @@ -28,7 +28,7 @@ describe('deps', () => { }) test('installDeps() loader works via CLI', async () => { - let out = + const out = await $`node build/cli.js --install <<< 'import _ from "lodash" /* @4.17.15 */; console.log(_.VERSION)'` assert.match(out.stdout, /4.17.15/) }) diff --git a/test/fixtures/interactive.mjs b/test/fixtures/interactive.mjs index a9484b3247..cb37f418b7 100755 --- a/test/fixtures/interactive.mjs +++ b/test/fixtures/interactive.mjs @@ -14,7 +14,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -let out = fs.createWriteStream('log.txt') // Record program output to this file. +const out = fs.createWriteStream('log.txt') // Record program output to this file. process.stdin.pipe(out) // Record user input as well. await $`npm init`.pipe(out) // Process will be interactive. diff --git a/test/fixtures/require.mjs b/test/fixtures/require.mjs index a300c1686d..530af9391d 100644 --- a/test/fixtures/require.mjs +++ b/test/fixtures/require.mjs @@ -13,6 +13,6 @@ // limitations under the License. import { strict as assert } from 'assert' -let data = require('../../package.json') +const data = require('../../package.json') assert.equal(data.name, 'zx') assert.equal(data, require('zx/package.json')) diff --git a/test/fixtures/ts-project/script.ts b/test/fixtures/ts-project/script.ts index dfd90ab246..3bb86690e2 100644 --- a/test/fixtures/ts-project/script.ts +++ b/test/fixtures/ts-project/script.ts @@ -14,5 +14,5 @@ import { $, ProcessPromise } from 'zx' -let p: ProcessPromise = $({ verbose: true })`echo ts-script` +const p: ProcessPromise = $({ verbose: true })`echo ts-script` await p diff --git a/test/global.test.js b/test/global.test.js index 946f72514f..94bfdd04d8 100644 --- a/test/global.test.js +++ b/test/global.test.js @@ -27,7 +27,7 @@ describe('global', () => { }) test('injects zx index to global', () => { - for (let [key, value] of Object.entries(index)) { + for (const [key, value] of Object.entries(index)) { assert.equal(global[key], value) } }) diff --git a/test/goods.test.js b/test/goods.test.js index 6c520311b9..07b7d11664 100644 --- a/test/goods.test.js +++ b/test/goods.test.js @@ -23,7 +23,7 @@ describe('goods', () => { } test('question() works', async () => { - let p = $`node build/cli.js --eval " + const p = $`node build/cli.js --eval " let answer = await question('foo or bar? ', { choices: ['foo', 'bar'] }) echo('Answer is', answer) "` @@ -52,8 +52,8 @@ describe('goods', () => { }) test('echo() works', async () => { + const log = console.log let stdout = '' - let log = console.log console.log = (...args) => { stdout += args.join(' ') } @@ -103,7 +103,7 @@ describe('goods', () => { test('retry() works', async () => { const now = Date.now() - let p = await zx(` + const p = await zx(` try { await retry(5, '50ms', () => $\`exit 123\`) } catch (e) { @@ -119,7 +119,7 @@ describe('goods', () => { test('retry() with expBackoff() works', async () => { const now = Date.now() - let p = await zx(` + const p = await zx(` try { await retry(5, expBackoff('60s', 0), () => $\`exit 123\`) } catch (e) { @@ -133,7 +133,7 @@ describe('goods', () => { }) test('spinner() works', async () => { - let out = await zx(` + const out = await zx(` echo(await spinner(async () => { await sleep(100) await $\`echo hidden\` @@ -146,14 +146,14 @@ describe('goods', () => { }) test('spinner() with title works', async () => { - let out = await zx(` + const out = await zx(` await spinner('processing', () => sleep(100)) `) assert.match(out.stderr, /processing/) }) test('spinner() stops on throw', async () => { - let out = await zx(` + const out = await zx(` await spinner('processing', () => $\`wtf-cmd\`) `) assert.match(out.stderr, /Error:/)