1import { 2 _resolveStringOrBooleanArgs, 3 assertDuplicateArgs, 4 assertUnknownArgs, 5 collapseAliases, 6 resolveStringOrBooleanArgsAsync, 7} from '../args'; 8 9describe(collapseAliases, () => { 10 it(`will collapse aliases into arguments`, () => { 11 const arg = { 12 '--basic': Boolean, 13 '--help': Boolean, 14 '-h': '--help', 15 }; 16 const args = ['--basic', '-h']; 17 const actual = collapseAliases(arg, args); 18 expect(actual).toEqual(['--basic', '--help']); 19 }); 20}); 21 22describe(_resolveStringOrBooleanArgs, () => { 23 it(`resolves string or boolean arguments to boolean`, () => { 24 expect( 25 _resolveStringOrBooleanArgs( 26 { 27 '--basic': Boolean, 28 // This will be omitted since we should handle collapsing aliases earlier. 29 '-b': 'foobar', 30 }, 31 ['--basic'] 32 ) 33 ).toEqual({ 34 args: { '--basic': true }, 35 projectRoot: '', 36 }); 37 }); 38 it(`resolves to string`, () => { 39 expect(_resolveStringOrBooleanArgs({ '--basic': Boolean }, ['--basic', 'foobar'])).toEqual({ 40 args: { '--basic': 'foobar' }, 41 // foobar will be used as the string value for basic and not as the project root 42 projectRoot: '', 43 }); 44 }); 45 it(`asserts dangling arguments`, () => { 46 expect(() => 47 _resolveStringOrBooleanArgs({ '--basic': Boolean }, [ 48 'possibleProjectRoot', 49 '--basic', 50 'foobar', 51 'anotherProjectRoot', 52 ]) 53 ).toThrow(/Unknown argument: possibleProjectRoot/); 54 }); 55 it(`resolves to string with custom project root`, () => { 56 expect( 57 _resolveStringOrBooleanArgs({ '--basic': Boolean }, ['--basic', 'foobar', 'root']) 58 ).toEqual({ 59 args: { '--basic': 'foobar' }, 60 projectRoot: 'root', 61 }); 62 }); 63 it(`asserts invalid arguments`, () => { 64 expect(() => 65 _resolveStringOrBooleanArgs({ '--basic': Boolean }, ['foobar', 'root', '--basic']) 66 ).toThrow(/Unknown argument: root/); 67 }); 68}); 69 70describe(assertUnknownArgs, () => { 71 it(`asserts unknown arguments`, () => { 72 expect(() => assertUnknownArgs({}, ['--foo', '--bar'])).toThrowError( 73 `Unknown arguments: --foo, --bar` 74 ); 75 }); 76 it(`does not throw when there are no unknown arguments`, () => { 77 expect(() => 78 assertUnknownArgs( 79 { 80 '--foo': Boolean, 81 '--bar': Boolean, 82 }, 83 ['--foo', '--bar'] 84 ) 85 ).not.toThrow(); 86 }); 87}); 88 89describe(resolveStringOrBooleanArgsAsync, () => { 90 it(`parses arguments`, async () => { 91 await expect( 92 resolveStringOrBooleanArgsAsync( 93 ['-y', '--no-install'], 94 { 95 // Types 96 '--yes': Boolean, 97 '--no-install': Boolean, 98 // Aliases 99 '-y': '--yes', 100 }, 101 { 102 '--template': Boolean, 103 '-t': '--template', 104 } 105 ) 106 ).resolves.toEqual({ 107 args: { 108 '--yes': true, 109 '--no-install': true, 110 }, 111 projectRoot: '', 112 }); 113 }); 114 it(`parses complex arguments`, async () => { 115 await expect( 116 resolveStringOrBooleanArgsAsync( 117 ['--no-bundler', '--scheme', '-d', 'my-device', 'custom-root'], 118 { 119 '--no-bundler': Boolean, 120 }, 121 { 122 '--scheme': Boolean, 123 '--device': Boolean, 124 '-d': '--device', 125 } 126 ) 127 ).resolves.toEqual({ 128 args: { 129 '--device': 'my-device', 130 '--no-bundler': true, 131 '--scheme': true, 132 }, 133 projectRoot: 'custom-root', 134 }); 135 }); 136}); 137 138describe(assertDuplicateArgs, () => { 139 it(`does not assert`, () => { 140 assertDuplicateArgs([], []); 141 assertDuplicateArgs(['--foo', '--bar'], [['--device', '-d']]); 142 }); 143 it(`asserts duplicate arguments`, () => { 144 expect(() => 145 assertDuplicateArgs(['--device', '--bar', '--device'], [['--device', '-d']]) 146 ).toThrowErrorMatchingInlineSnapshot(`"Can only provide one instance of --device or -d"`); 147 }); 148}); 149