|
| 1 | +import path from 'path' |
| 2 | +import * as utils from '../src/cli/utils' |
| 3 | + |
| 4 | +describe('cli utils', () => { |
| 5 | + const fixture = utils.readFile(path.resolve(__dirname, 'fixtures/cli-utils.js')) |
| 6 | + |
| 7 | + describe('parseCliParams', () => { |
| 8 | + it('parses CLI parameters', () => { |
| 9 | + const result = utils.parseCliParams(['a', 'b', '-c', 'd']) |
| 10 | + |
| 11 | + expect(result).toEqual(['a', 'b']) |
| 12 | + }) |
| 13 | + }) |
| 14 | + |
| 15 | + describe('parseCliOptions', () => { |
| 16 | + it('parses CLI options', () => { |
| 17 | + const result = utils.parseCliOptions(['a', '-b', 'c'], { test: ['b'] }) |
| 18 | + |
| 19 | + expect(result).toEqual({ test: ['c'] }) |
| 20 | + }) |
| 21 | + |
| 22 | + it('parses multiple types of options', () => { |
| 23 | + const result = utils.parseCliOptions(['a', '-b', 'c', '--test', 'd', '-test', 'e'], { |
| 24 | + test: ['test', 'b'], |
| 25 | + }) |
| 26 | + |
| 27 | + expect(result).toEqual({ test: ['c', 'd', 'e'] }) |
| 28 | + }) |
| 29 | + |
| 30 | + it('ignores unknown options', () => { |
| 31 | + const result = utils.parseCliOptions(['a', '-b', 'c'], {}) |
| 32 | + |
| 33 | + expect(result).toEqual({}) |
| 34 | + }) |
| 35 | + |
| 36 | + it('maps options', () => { |
| 37 | + const result = utils.parseCliOptions(['a', '-b', 'c', '-d', 'e'], { test: ['b', 'd'] }) |
| 38 | + |
| 39 | + expect(result).toEqual({ test: ['c', 'e'] }) |
| 40 | + }) |
| 41 | + |
| 42 | + it('parses undefined options', () => { |
| 43 | + const result = utils.parseCliOptions(['a'], { test: ['b'] }) |
| 44 | + |
| 45 | + expect(result).toEqual({ test: undefined }) |
| 46 | + }) |
| 47 | + |
| 48 | + it('parses flags', () => { |
| 49 | + const result = utils.parseCliOptions(['a', '-b'], { test: ['b'] }) |
| 50 | + |
| 51 | + expect(result).toEqual({ test: [] }) |
| 52 | + }) |
| 53 | + |
| 54 | + it('accepts multiple values per option', () => { |
| 55 | + const result = utils.parseCliOptions(['a', '-b', 'c', 'd', '-e', 'f', '-g', 'h'], { |
| 56 | + test: ['b', 'g'], |
| 57 | + }) |
| 58 | + |
| 59 | + expect(result).toEqual({ test: ['c', 'd', 'h'] }) |
| 60 | + }) |
| 61 | + }) |
| 62 | + |
| 63 | + describe('stripBlockComments', () => { |
| 64 | + it('does not strip code', () => { |
| 65 | + const result = utils.stripBlockComments(fixture) |
| 66 | + |
| 67 | + expect(result).toEqual(expect.stringContaining('__code_no_comment__')) |
| 68 | + expect(result).toEqual(expect.stringContaining('__code_comment_line__')) |
| 69 | + expect(result).toEqual(expect.stringContaining('__code_comment_block__')) |
| 70 | + expect(result).toEqual(expect.stringContaining('__code_comment_line_important__')) |
| 71 | + expect(result).toEqual(expect.stringContaining('__code_comment_block_important__')) |
| 72 | + }) |
| 73 | + |
| 74 | + it('strips block comments', () => { |
| 75 | + const result = utils.stripBlockComments(fixture) |
| 76 | + |
| 77 | + expect(result).not.toEqual(expect.stringContaining('__comment_block__')) |
| 78 | + expect(result).not.toEqual(expect.stringContaining('__comment_block_multiline__')) |
| 79 | + expect(result).not.toEqual(expect.stringContaining('__comment_block_code__')) |
| 80 | + }) |
| 81 | + |
| 82 | + it('strips docblock comments', () => { |
| 83 | + const result = utils.stripBlockComments(fixture) |
| 84 | + |
| 85 | + expect(result).not.toEqual(expect.stringContaining('__comment_docblock__')) |
| 86 | + }) |
| 87 | + |
| 88 | + it('does not strip line comments', () => { |
| 89 | + const result = utils.stripBlockComments(fixture) |
| 90 | + |
| 91 | + expect(result).toEqual(expect.stringContaining('__comment_line__')) |
| 92 | + expect(result).toEqual(expect.stringContaining('__comment_line_important__')) |
| 93 | + expect(result).toEqual(expect.stringContaining('__comment_line_code__')) |
| 94 | + expect(result).toEqual(expect.stringContaining('__comment_line_important_code__')) |
| 95 | + }) |
| 96 | + |
| 97 | + it('does not strip important block comments', () => { |
| 98 | + const result = utils.stripBlockComments(fixture) |
| 99 | + |
| 100 | + expect(result).toEqual(expect.stringContaining('__comment_block_important__')) |
| 101 | + expect(result).toEqual(expect.stringContaining('__comment_block_multiline_important__')) |
| 102 | + expect(result).toEqual(expect.stringContaining('__comment_block_important_code__')) |
| 103 | + }) |
| 104 | + |
| 105 | + it('does not strip important docblock comments', () => { |
| 106 | + const result = utils.stripBlockComments(fixture) |
| 107 | + |
| 108 | + expect(result).toEqual(expect.stringContaining('__comment_docblock_important__')) |
| 109 | + }) |
| 110 | + }) |
| 111 | +}) |
0 commit comments