|
1 | | -import * as process from 'process' |
2 | 1 | import * as cp from 'child_process' |
| 2 | +import * as fs from 'fs' |
3 | 3 | import * as path from 'path' |
4 | | -import {expect, test} from '@jest/globals' |
| 4 | +import * as process from 'process' |
| 5 | + |
| 6 | +import {afterAll, beforeAll, describe, expect, test} from '@jest/globals' |
| 7 | + |
| 8 | +const outDirParent = path.join(__dirname, 'test') |
| 9 | +const outDirPrefix = path.join(outDirParent, 'out-') |
5 | 10 |
|
6 | 11 | type CommandResult = { |
7 | 12 | exitCode: number |
@@ -46,53 +51,123 @@ const prepareEnv: prepareEnvType = vars => { |
46 | 51 | } |
47 | 52 | } |
48 | 53 |
|
49 | | -test('throws error on unknown command', async () => { |
50 | | - const {exitCode, stdout, stderr} = await execMain( |
51 | | - prepareEnv({INPUT_COMMAND: 'unknown'}) |
52 | | - ) |
53 | | - expect(exitCode).toEqual(1) |
54 | | - expect(stdout).toContain('Unable to locate executable file: unknown.') |
55 | | - expect(stderr).toContain('Error: Unable to locate executable file: unknown.') |
56 | | -}) |
| 54 | +describe('lib/main.js', () => { |
| 55 | + beforeAll(() => { |
| 56 | + if (!fs.existsSync(outDirParent)) { |
| 57 | + fs.mkdirSync(outDirParent) |
| 58 | + } |
| 59 | + }) |
57 | 60 |
|
58 | | -test('properly handles single-line stdout', async () => { |
59 | | - const {stdout} = await execMain( |
60 | | - prepareEnv({INPUT_COMMAND: 'echo "Hello world"'}) |
61 | | - ) |
62 | | - |
63 | | - expect(stdout).toContain('Starting command.') |
64 | | - expect(stdout).toContain(['echo Hello world', 'Hello world'].join('\n')) |
65 | | - expect(stdout).toContain('Finished command.') |
66 | | - expect(stdout).toContain('::set-output name=exitCode::0') |
67 | | - expect(stdout).toContain('::set-output name=stdout::Hello world%0A') |
68 | | - expect(stdout).toContain('::set-output name=stderr::') |
69 | | -}) |
| 61 | + afterAll(() => { |
| 62 | + fs.rmSync(outDirParent, {recursive: true}) |
| 63 | + }) |
70 | 64 |
|
71 | | -test('properly handles multi-line stdout (line breaks are escaped)', async () => { |
72 | | - const {stdout} = await execMain( |
73 | | - prepareEnv({INPUT_COMMAND: 'echo "Hello\nworld"'}) |
74 | | - ) |
75 | | - |
76 | | - expect(stdout).toContain('Starting command.') |
77 | | - expect(stdout).toContain(['echo Hello\nworld', 'Hello\nworld'].join('\n')) |
78 | | - expect(stdout).toContain('Finished command.') |
79 | | - expect(stdout).toContain('::set-output name=exitCode::0') |
80 | | - expect(stdout).toContain('::set-output name=stdout::Hello%0Aworld%0A') |
81 | | - expect(stdout).toContain('::set-output name=stderr::') |
82 | | -}) |
| 65 | + test('properly handles single-line stdout', async () => { |
| 66 | + const [outFile, readOutFile] = tempFile() |
| 67 | + const {stdout} = await execMain( |
| 68 | + prepareEnv({ |
| 69 | + INPUT_COMMAND: 'echo "Hello world"', |
| 70 | + GITHUB_OUTPUT: outFile |
| 71 | + }) |
| 72 | + ) |
| 73 | + const outFileContent = readOutFile() |
| 74 | + |
| 75 | + expect(stdout).toContain('Starting command.') |
| 76 | + expect(stdout).toContain('echo Hello world\nHello world') |
| 77 | + expect(stdout).toContain('Finished command.') |
| 78 | + |
| 79 | + // `[\w-]` matches the delimtier. |
| 80 | + expect(outFileContent).toMatch(/exitCode<<[\w-]+\n0\n/) |
| 81 | + expect(outFileContent).toMatch(/stdout<<[\w-]+\nHello world\n/) |
| 82 | + expect(outFileContent).toMatch(/stderr<<[\w-]+\n\n/) |
| 83 | + }) |
| 84 | + |
| 85 | + test('properly handles multi-line stdout', async () => { |
| 86 | + const [outFile, readOutFile] = tempFile() |
| 87 | + const {stdout} = await execMain( |
| 88 | + prepareEnv({ |
| 89 | + INPUT_COMMAND: 'echo "Hello\nworld"', |
| 90 | + GITHUB_OUTPUT: outFile |
| 91 | + }) |
| 92 | + ) |
| 93 | + const outFileContent = readOutFile() |
| 94 | + |
| 95 | + // `[\w-]` matches the delimtier. |
| 96 | + expect(outFileContent).toMatch(/exitCode<<[\w-]+\n0\n/) |
| 97 | + expect(outFileContent).toMatch(/stdout<<[\w-]+\nHello\nworld\n/) |
| 98 | + expect(outFileContent).toMatch(/stderr<<[\w-]+\n\n/) |
| 99 | + }) |
83 | 100 |
|
84 | | -test('cwd', async () => { |
85 | | - const {stdout} = await execMain( |
86 | | - prepareEnv({ |
87 | | - INPUT_COMMAND: 'ls', |
88 | | - INPUT_CWD: './lib' |
89 | | - }) |
90 | | - ) |
91 | | - |
92 | | - expect(stdout).toContain('Starting command.') |
93 | | - expect(stdout).toContain(['ls', 'main.js'].join('\n')) |
94 | | - expect(stdout).toContain('Finished command.') |
95 | | - expect(stdout).toContain('::set-output name=exitCode::0') |
96 | | - expect(stdout).toContain('::set-output name=stdout::main.js%0A') |
97 | | - expect(stdout).toContain('::set-output name=stderr::') |
| 101 | + test('works with option `cwd`', async () => { |
| 102 | + const [outFile, readOutFile] = tempFile() |
| 103 | + const {stdout} = await execMain( |
| 104 | + prepareEnv({ |
| 105 | + INPUT_COMMAND: 'ls', |
| 106 | + INPUT_CWD: './lib', |
| 107 | + GITHUB_OUTPUT: outFile |
| 108 | + }) |
| 109 | + ) |
| 110 | + const outFileContent = readOutFile() |
| 111 | + |
| 112 | + // `[\w-]` matches the delimtier. |
| 113 | + expect(outFileContent).toMatch(/exitCode<<[\w-]+\n0\n/) |
| 114 | + expect(outFileContent).toMatch(/stdout<<[\w-]+\nmain\.js\n/) |
| 115 | + expect(outFileContent).toMatch(/stderr<<[\w-]+\n\n/) |
| 116 | + }) |
| 117 | + |
| 118 | + test('properly captures stderr', async () => { |
| 119 | + const [outFile, readOutFile] = tempFile() |
| 120 | + const {exitCode, stdout, stderr} = await execMain( |
| 121 | + prepareEnv({ |
| 122 | + INPUT_COMMAND: 'ls /unkown/path/', |
| 123 | + GITHUB_OUTPUT: outFile |
| 124 | + }) |
| 125 | + ) |
| 126 | + const outFileContent = readOutFile() |
| 127 | + |
| 128 | + expect(exitCode).toEqual(0) |
| 129 | + expect(stdout).toContain('ls /unkown/path/') |
| 130 | + expect(stderr).toContain('') |
| 131 | + |
| 132 | + // `[\w-]` matches the delimtier. |
| 133 | + // The `exitCode` can be 1 or 2 depending on the environment. |
| 134 | + expect(outFileContent).toMatch(/exitCode<<[\w-]+\n[12]\n/) |
| 135 | + expect(outFileContent).toMatch(/stdout<<[\w-]+\n\n/) |
| 136 | + expect(outFileContent).toMatch( |
| 137 | + /stderr<<[\w-]+\n.*No such file or directory\n/ |
| 138 | + ) |
| 139 | + }) |
| 140 | + |
| 141 | + test('fails with unknown command', async () => { |
| 142 | + const [outFile, readOutFile] = tempFile() |
| 143 | + const {exitCode, stdout, stderr} = await execMain( |
| 144 | + prepareEnv({ |
| 145 | + INPUT_COMMAND: 'unknown', |
| 146 | + GITHUB_OUTPUT: outFile |
| 147 | + }) |
| 148 | + ) |
| 149 | + const outFileContent = readOutFile() |
| 150 | + |
| 151 | + expect(exitCode).toEqual(1) |
| 152 | + expect(stdout).toContain('Unable to locate executable file: unknown.') |
| 153 | + expect(stderr).toContain( |
| 154 | + 'Error: Unable to locate executable file: unknown.' |
| 155 | + ) |
| 156 | + expect(outFileContent).toEqual('') |
| 157 | + }) |
98 | 158 | }) |
| 159 | + |
| 160 | +/** |
| 161 | + * Helper: prepare a temporary file. |
| 162 | + */ |
| 163 | +function tempFile(): [string, () => string] { |
| 164 | + const directory = fs.mkdtempSync(outDirPrefix) |
| 165 | + const file = path.join(directory, 'out') |
| 166 | + fs.appendFileSync(file, '', {encoding: 'utf8'}) |
| 167 | + |
| 168 | + function read(): string { |
| 169 | + return fs.readFileSync(file, {encoding: 'utf8'}) |
| 170 | + } |
| 171 | + |
| 172 | + return [file, read] |
| 173 | +} |
0 commit comments