bin.test.js
This file
Path
test/bin.test.js
72 lines · 2.0 KiB · JavaScript
Last changed by
Fix CLI validation of fractional and infinite ID sizes (#618)Ferit Cembefore adoption58193a1
History of this fileTake it
The walk stopped after 200 commits: lines older than cdc3edc point at it.
Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 1–13why, one step back1import { equal, match, rejects } from 'node:assert'2import child from 'node:child_process'3import { join } from 'node:path'4import { describe, test } from 'node:test'5import { fileURLToPath } from 'node:url'6import { promisify } from 'node:util'78let exec = promisify(child.exec)910const BIN = join(fileURLToPath(import.meta.url), '..', '..', 'bin', 'nanoid.js')1112describe('CLI', () => {13 test('prints unique ID', async () => {fix: quote bin path in CLI tests to support paths with spaces (#557)Mahmoud Hamdybefore adoption47cde3d
line 14why, one step back14 let { stderr, stdout } = await exec(`node "${BIN}"`)Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 15–19why, one step back15 equal(stderr, '')16 match(stdout, /^[\w-]{21}\n$/)17 })1819 test('uses size', async () => {fix: quote bin path in CLI tests to support paths with spaces (#557)Mahmoud Hamdybefore adoption47cde3d
line 20why, one step back20 let { stderr, stdout } = await exec(`node "${BIN}" --size 10`)Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 21–25why, one step back21 equal(stderr, '')22 match(stdout, /^[\w-]{10}\n$/)23 })2425 test('uses alphabet', async () => {Fix CLI support for Node.js 18Andrey Sitnikbefore adoption8239c4b
lines 26–28why, one step back26 let { stderr, stdout } = await exec(27 `node "${BIN}" --alphabet abc --size 15`28 )Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 29–33why, one step back29 equal(stderr, '')30 match(stdout, /^[abc]{15}\n$/)31 })3233 test('shows an error on unknown argument', async () => {fix: quote bin path in CLI tests to support paths with spaces (#557)Mahmoud Hamdybefore adoption47cde3d
line 34why, one step back34 await rejects(() => exec(`node "${BIN}" -test`), /Unknown argument -test/)Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 35–38why, one step back35 })3637 test('shows an error if size is not a number', async () => {38 await rejects(fix: quote bin path in CLI tests to support paths with spaces (#557)Mahmoud Hamdybefore adoption47cde3d
line 39why, one step back39 () => exec(`node "${BIN}" -s abc`),Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 40–45why, one step back40 /Size must be positive integer/41 )42 })4344 test('requires error if size is a negative number', async () => {45 await rejects(fix: quote bin path in CLI tests to support paths with spaces (#557)Mahmoud Hamdybefore adoption47cde3d
line 46why, one step back46 () => exec(`node "${BIN}" --size "-1"`),Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 47–50why, one step back47 /Size must be positive integer/48 )49 })50Fix CLI validation of fractional and infinite ID sizes (#618)Ferit Cembefore adoption58193a1
lines 51–59why, one step back51 for (let size of ['0.5', '1.5', 'Infinity']) {52 test(`rejects non-integer size ${size}`, async () => {53 await rejects(54 () => exec(`node "${BIN}" --size ${size}`),55 /Size must be positive integer/56 )57 })58 }59Update size limitAndrey Sitnikbefore adoptioncdc3edc
line 60why, one step back60 test('displays help', async () => {fix: quote bin path in CLI tests to support paths with spaces (#557)Mahmoud Hamdybefore adoption47cde3d
line 61why, one step back61 let { stderr, stdout } = await exec(`node "${BIN}" --help`)Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 62–65why, one step back62 equal(stderr, '')63 match(stdout, /Usage/)64 match(stdout, /\$ nanoid \[options]/)65 })Add --version flag to CLI (#563)Mahmoud Hamdybefore adoptione9be4aa
lines 66–71why, one step back6667 test('displays version', async () => {68 let { stderr, stdout } = await exec(`node "${BIN}" --version`)69 equal(stderr, '')70 match(stdout, /^\d+\.\d+\.\d+\n$/)71 })Update size limitAndrey Sitnikbefore adoptioncdc3edc
line 72why, one step back72})