|  | 
|  | 1 | +import { describe, expect, it } from 'vitest'; | 
|  | 2 | +import { createCliCommand, objectToCliArgs } from './cli'; | 
|  | 3 | + | 
|  | 4 | +describe('objectToCliArgs', () => { | 
|  | 5 | +  it('should empty params', () => { | 
|  | 6 | +    const result = objectToCliArgs(); | 
|  | 7 | +    expect(result).toEqual([]); | 
|  | 8 | +  }); | 
|  | 9 | + | 
|  | 10 | +  it('should handle the "_" argument as script', () => { | 
|  | 11 | +    const params = { _: 'bin.js' }; | 
|  | 12 | +    const result = objectToCliArgs(params); | 
|  | 13 | +    expect(result).toEqual(['bin.js']); | 
|  | 14 | +  }); | 
|  | 15 | + | 
|  | 16 | +  it('should handle the "_" argument with multiple values', () => { | 
|  | 17 | +    const params = { _: ['bin.js', '--help'] }; | 
|  | 18 | +    const result = objectToCliArgs(params); | 
|  | 19 | +    expect(result).toEqual(['bin.js', '--help']); | 
|  | 20 | +  }); | 
|  | 21 | + | 
|  | 22 | +  it('should handle shorthands arguments', () => { | 
|  | 23 | +    const params = { | 
|  | 24 | +      e: `test`, | 
|  | 25 | +    }; | 
|  | 26 | +    const result = objectToCliArgs(params); | 
|  | 27 | +    expect(result).toEqual([`-e="${params.e}"`]); | 
|  | 28 | +  }); | 
|  | 29 | + | 
|  | 30 | +  it('should handle string arguments', () => { | 
|  | 31 | +    const params = { name: 'Juanita' }; | 
|  | 32 | +    const result = objectToCliArgs(params); | 
|  | 33 | +    expect(result).toEqual(['--name="Juanita"']); | 
|  | 34 | +  }); | 
|  | 35 | + | 
|  | 36 | +  it('should handle number arguments', () => { | 
|  | 37 | +    const params = { parallel: 5 }; | 
|  | 38 | +    const result = objectToCliArgs(params); | 
|  | 39 | +    expect(result).toEqual(['--parallel=5']); | 
|  | 40 | +  }); | 
|  | 41 | + | 
|  | 42 | +  it('should handle boolean arguments', () => { | 
|  | 43 | +    const params = { progress: true }; | 
|  | 44 | +    const result = objectToCliArgs(params); | 
|  | 45 | +    expect(result).toEqual(['--progress']); | 
|  | 46 | +  }); | 
|  | 47 | + | 
|  | 48 | +  it('should handle negated boolean arguments', () => { | 
|  | 49 | +    const params = { progress: false }; | 
|  | 50 | +    const result = objectToCliArgs(params); | 
|  | 51 | +    expect(result).toEqual(['--no-progress']); | 
|  | 52 | +  }); | 
|  | 53 | + | 
|  | 54 | +  it('should handle array of string arguments', () => { | 
|  | 55 | +    const params = { format: ['json', 'md'] }; | 
|  | 56 | +    const result = objectToCliArgs(params); | 
|  | 57 | +    expect(result).toEqual(['--format="json"', '--format="md"']); | 
|  | 58 | +  }); | 
|  | 59 | + | 
|  | 60 | +  it('should handle objects', () => { | 
|  | 61 | +    const params = { format: { json: 'simple' } }; | 
|  | 62 | +    const result = objectToCliArgs(params); | 
|  | 63 | +    expect(result).toStrictEqual(['--format.json="simple"']); | 
|  | 64 | +  }); | 
|  | 65 | + | 
|  | 66 | +  it('should handle objects with undefined', () => { | 
|  | 67 | +    const params = { format: undefined }; | 
|  | 68 | +    const result = objectToCliArgs(params); | 
|  | 69 | +    expect(result).toStrictEqual([]); | 
|  | 70 | +  }); | 
|  | 71 | + | 
|  | 72 | +  it('should throw error for unsupported type', () => { | 
|  | 73 | +    expect(() => objectToCliArgs({ param: Symbol('') })).toThrow( | 
|  | 74 | +      'Unsupported type', | 
|  | 75 | +    ); | 
|  | 76 | +  }); | 
|  | 77 | +}); | 
|  | 78 | + | 
|  | 79 | +describe('createCliCommand', () => { | 
|  | 80 | +  it('should create command out of command name and an object for arguments', () => { | 
|  | 81 | +    const result = createCliCommand('autorun', { verbose: true }); | 
|  | 82 | +    expect(result).toBe('npx @code-pushup/cli autorun --verbose'); | 
|  | 83 | +  }); | 
|  | 84 | +}); | 
0 commit comments