|
| 1 | +let { is, match } = require('uvu/assert') |
| 2 | +let { promisify } = require('util') |
| 3 | +let { test } = require('uvu') |
| 4 | +let { join } = require('path') |
| 5 | +let child = require('child_process') |
| 6 | + |
| 7 | +let exec = promisify(child.exec) |
| 8 | + |
| 9 | +const BIN = join(__dirname, '..', 'bin', 'nanoid.cjs') |
| 10 | + |
| 11 | +test('prints unique ID', async () => { |
| 12 | + let { stdout, stderr } = await exec('node ' + BIN) |
| 13 | + is(stderr, '') |
| 14 | + match(stdout, /^[\w-]{21}\n$/) |
| 15 | +}) |
| 16 | + |
| 17 | +test('uses size', async () => { |
| 18 | + let { stdout, stderr } = await exec('node ' + BIN + ' --size 10') |
| 19 | + is(stderr, '') |
| 20 | + match(stdout, /^[\w-]{10}\n$/) |
| 21 | +}) |
| 22 | + |
| 23 | +test('uses alphabet', async () => { |
| 24 | + let { stdout, stderr } = await exec( |
| 25 | + 'node ' + BIN + ' --alphabet abc --size 15' |
| 26 | + ) |
| 27 | + is(stderr, '') |
| 28 | + match(stdout, /^[abc]{15}\n$/) |
| 29 | +}) |
| 30 | + |
| 31 | +test('shows an error on unknown argument', async () => { |
| 32 | + try { |
| 33 | + await exec('node ' + BIN + ' -test') |
| 34 | + } catch (e) { |
| 35 | + match(e, /Unknown argument -test/) |
| 36 | + } |
| 37 | +}) |
| 38 | + |
| 39 | +test('shows an error if size is not a number', async () => { |
| 40 | + try { |
| 41 | + await exec('node ' + BIN + ' -s abc') |
| 42 | + } catch (e) { |
| 43 | + match(e, /Size must be positive integer/) |
| 44 | + } |
| 45 | +}) |
| 46 | + |
| 47 | +test('shows an error on no size with custom alphabet', async () => { |
| 48 | + try { |
| 49 | + await exec('node ' + BIN + ' --alphabet abc') |
| 50 | + } catch (e) { |
| 51 | + match(e, /You must also specify size option, when using custom alphabet/) |
| 52 | + } |
| 53 | +}) |
| 54 | + |
| 55 | +test('requires error if size is a negative number', async () => { |
| 56 | + try { |
| 57 | + await exec('node ' + BIN + ' --size "-1"') |
| 58 | + } catch (e) { |
| 59 | + match(e, /Size must be positive integer/) |
| 60 | + } |
| 61 | +}) |
| 62 | + |
| 63 | +test('displays help', async () => { |
| 64 | + let { stdout, stderr } = await exec('node ' + BIN + ' --help') |
| 65 | + is(stderr, '') |
| 66 | + match(stdout, /Usage/) |
| 67 | + match(stdout, /\$ nanoid \[options]/) |
| 68 | +}) |
| 69 | + |
| 70 | +test.run() |
0 commit comments