|
| 1 | +import {spawn} from 'child_process'; |
| 2 | +import path from 'path'; |
| 3 | +import concat from 'concat-stream'; |
| 4 | + |
| 5 | +// const DOWN = '\x1B\x5B\x42' |
| 6 | +// const UP = '\x1B\x5B\x41' |
| 7 | +const ENTER = '\x0D'; |
| 8 | + |
| 9 | +async function runCli(args: string[], options: string[], timeout = 200) { |
| 10 | + const proc = spawn('node', args, { |
| 11 | + stdio: [null, null, null], |
| 12 | + env: { |
| 13 | + FORCE_COLOR: '0', |
| 14 | + }, |
| 15 | + }); |
| 16 | + proc.stdin.setDefaultEncoding('utf-8'); |
| 17 | + |
| 18 | + const loop = function (opt: string[]) { |
| 19 | + if (opt.length > 0) { |
| 20 | + setTimeout(function () { |
| 21 | + proc.stdin.write(opt[0]); |
| 22 | + loop(opt.slice(1)); |
| 23 | + }, timeout); |
| 24 | + } else { |
| 25 | + proc.stdin.end(); |
| 26 | + } |
| 27 | + }; |
| 28 | + |
| 29 | + loop(options); |
| 30 | + |
| 31 | + return new Promise<string>(function (resolve) { |
| 32 | + proc.stdout.pipe( |
| 33 | + concat(function (result) { |
| 34 | + resolve(result.toString()); |
| 35 | + }) |
| 36 | + ); |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +const cliPath = path.join(__dirname, '..', 'fixtures', 'cli.js'); |
| 41 | + |
| 42 | +test('test1', async () => { |
| 43 | + const result = await runCli( |
| 44 | + [cliPath, '--no-color'], |
| 45 | + [ |
| 46 | + 'test', |
| 47 | + ENTER, |
| 48 | + 'test', |
| 49 | + ENTER, |
| 50 | + 'test', |
| 51 | + ENTER, |
| 52 | + ':skip', |
| 53 | + ENTER, |
| 54 | + ':skip', |
| 55 | + ENTER, |
| 56 | + ] |
| 57 | + ); |
| 58 | + expect(result.trim()).toMatchInlineSnapshot(` |
| 59 | + "Please enter a type: [required] [tab-completion] [header] |
| 60 | + <type> holds information about the goal of a change. |
| 61 | +
|
| 62 | + <type>(<scope>): <subject> |
| 63 | + <body> |
| 64 | + <footer> |
| 65 | +
|
| 66 | +
|
| 67 | + ? type: |
| 68 | + 82 characters left[1A[18D[8C[1B[2K[1A[2K[G? type: t |
| 69 | + 81 characters left[1A[18D[9C[1B[2K[1A[2K[G? type: te |
| 70 | + 80 characters left[1A[18D[10C[1B[2K[1A[2K[G? type: tes |
| 71 | + 79 characters left[1A[18D[11C[1B[2K[1A[2K[G? type: test |
| 72 | + 78 characters left[1A[18D[12C[1B[2K[1A[2K[G? type: test[12D[12C |
| 73 | + Please enter a scope: [optional] [tab-completion] [header] |
| 74 | + <scope> marks which sub-component of the project is affected |
| 75 | +
|
| 76 | + test(<scope>): <subject> |
| 77 | + <body> |
| 78 | + <footer> |
| 79 | +
|
| 80 | +
|
| 81 | + ? scope: |
| 82 | + 85 characters left[1A[18D[9C[1B[2K[1A[2K[G? scope: t |
| 83 | + 84 characters left[1A[18D[10C[1B[2K[1A[2K[G? scope: te |
| 84 | + 83 characters left[1A[18D[11C[1B[2K[1A[2K[G? scope: tes |
| 85 | + 82 characters left[1A[18D[12C[1B[2K[1A[2K[G? scope: test |
| 86 | + 81 characters left[1A[18D[13C[1B[2K[1A[2K[G? scope: test[13D[13C |
| 87 | + Please enter a subject: [required] [header] |
| 88 | + <subject> is a short, high-level description of the change |
| 89 | +
|
| 90 | + test(test): <subject> |
| 91 | + <body> |
| 92 | + <footer> |
| 93 | +
|
| 94 | +
|
| 95 | + ? subject: |
| 96 | + 79 characters left[1A[18D[11C[1B[2K[1A[2K[G? subject: t |
| 97 | + 78 characters left[1A[18D[12C[1B[2K[1A[2K[G? subject: te |
| 98 | + 77 characters left[1A[18D[13C[1B[2K[1A[2K[G? subject: tes |
| 99 | + 76 characters left[1A[18D[14C[1B[2K[1A[2K[G? subject: test |
| 100 | + 75 characters left[1A[18D[15C[1B[2K[1A[2K[G? subject: test[15D[15C |
| 101 | + Please enter a body: [optional] [multi-line] |
| 102 | + <body> holds additional information about the change |
| 103 | +
|
| 104 | + test(test): test |
| 105 | + <body> |
| 106 | + <footer> |
| 107 | +
|
| 108 | +
|
| 109 | + ? body: [8D[8C[2K[G? body: :[9D[9C[2K[G? body: :s[10D[10C[2K[G? body: :sk[11D[11C[2K[G? body: :ski[12D[12C[2K[G? body: :skip[13D[13C[2K[G? body: [8D[8C |
| 110 | + Please enter a footer: [optional] [multi-line] |
| 111 | + <footer> holds further meta data, such as breaking changes and issue ids |
| 112 | +
|
| 113 | + test(test): test |
| 114 | + <footer> |
| 115 | +
|
| 116 | +
|
| 117 | + ? footer: [10D[10C[2K[G? footer: :[11D[11C[2K[G? footer: :s[12D[12C[2K[G? footer: :sk[13D[13C[2K[G? footer: :ski[14D[14C[2K[G? footer: :skip[15D[15C[2K[G? footer: [10D[10C |
| 118 | + !--------------------! |
| 119 | + test(test): test" |
| 120 | + `); |
| 121 | +}); |
| 122 | + |
| 123 | +test('test2', async () => { |
| 124 | + const result = await runCli( |
| 125 | + [cliPath, '--no-color'], |
| 126 | + ['foo', ENTER, ':skip', ENTER, 'bar', ENTER, 'baz', ENTER, 'bas', ENTER] |
| 127 | + ); |
| 128 | + expect(result.trim()).toMatchInlineSnapshot(); |
| 129 | +}); |
0 commit comments