Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than cdc3edc point at it.

Update size limitAndrey Sitnikbefore adoptioncdc3edc

lines 1–13why, one step back
1import { 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 back
14 let { stderr, stdout } = await exec(`node "${BIN}"`)

Update size limitAndrey Sitnikbefore adoptioncdc3edc

lines 15–19why, one step back
15 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 back
20 let { stderr, stdout } = await exec(`node "${BIN}" --size 10`)

Update size limitAndrey Sitnikbefore adoptioncdc3edc

lines 21–25why, one step back
21 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 back
26 let { stderr, stdout } = await exec(
27 `node "${BIN}" --alphabet abc --size 15`28 )

Update size limitAndrey Sitnikbefore adoptioncdc3edc

lines 29–33why, one step back
29 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 back
34 await rejects(() => exec(`node "${BIN}" -test`), /Unknown argument -test/)

Update size limitAndrey Sitnikbefore adoptioncdc3edc

lines 35–38why, one step back
35 })
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 back
39 () => exec(`node "${BIN}" -s abc`),

Update size limitAndrey Sitnikbefore adoptioncdc3edc

lines 40–45why, one step back
40 /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 back
46 () => exec(`node "${BIN}" --size "-1"`),

Update size limitAndrey Sitnikbefore adoptioncdc3edc

lines 47–50why, one step back
47 /Size must be positive integer/
48 )49 })50

Fix CLI validation of fractional and infinite ID sizes (#618)Ferit Cembefore adoption58193a1

lines 51–59why, one step back
51 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 }59

Update size limitAndrey Sitnikbefore adoptioncdc3edc

line 60why, one step back
60 test('displays help', async () => {

fix: quote bin path in CLI tests to support paths with spaces (#557)Mahmoud Hamdybefore adoption47cde3d

line 61why, one step back
61 let { stderr, stdout } = await exec(`node "${BIN}" --help`)

Update size limitAndrey Sitnikbefore adoptioncdc3edc

lines 62–65why, one step back
62 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 back
66
67 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 back
72})