|
| 1 | +/* eslint-disable no-magic-numbers */ |
| 2 | +import { |
| 3 | + testChildProcess, |
| 4 | + setChildProcessParams, |
| 5 | + spyOnSpawn, |
| 6 | + execCalledWith, |
| 7 | + spyOnStdout, |
| 8 | + stdoutCalledWith, |
| 9 | +} from '@technote-space/github-action-test-helper'; |
| 10 | +import { Logger, Command } from '../src'; |
| 11 | + |
| 12 | +describe('execAsync', () => { |
| 13 | + testChildProcess(); |
| 14 | + beforeEach(() => { |
| 15 | + Logger.resetForTesting(); |
| 16 | + }); |
| 17 | + |
| 18 | + const command = new Command(new Logger()); |
| 19 | + |
| 20 | + it('should run command', async() => { |
| 21 | + const mockExec = spyOnSpawn(); |
| 22 | + const mockStdout = spyOnStdout(); |
| 23 | + |
| 24 | + expect(await command.execAsync({command: 'test'})).toEqual({stdout: 'stdout', stderr: '', command: 'test'}); |
| 25 | + |
| 26 | + execCalledWith(mockExec, [ |
| 27 | + 'test', |
| 28 | + ]); |
| 29 | + stdoutCalledWith(mockStdout, [ |
| 30 | + '[command]test', |
| 31 | + ' >> stdout', |
| 32 | + ]); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should run command with cwd, altCommand', async() => { |
| 36 | + setChildProcessParams({stderr: 'stderr'}); |
| 37 | + const mockExec = spyOnSpawn(); |
| 38 | + const mockStdout = spyOnStdout(); |
| 39 | + |
| 40 | + expect(await command.execAsync({command: 'test', cwd: 'dir', altCommand: 'alt'})).toEqual({stdout: 'stdout', stderr: 'stderr', command: 'alt'}); |
| 41 | + |
| 42 | + execCalledWith(mockExec, [ |
| 43 | + ['test', [], {'cwd': 'dir', shell: true}], |
| 44 | + ]); |
| 45 | + stdoutCalledWith(mockStdout, [ |
| 46 | + '[command]alt', |
| 47 | + ' >> stdout', |
| 48 | + '::warning:: >> stderr', |
| 49 | + ]); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should run command with args', async() => { |
| 53 | + const mockExec = spyOnSpawn(); |
| 54 | + const mockStdout = spyOnStdout(); |
| 55 | + |
| 56 | + expect(await command.execAsync({command: 'test', args: ['hello!', 'how are you doing $USER', '"double"', '\'single\'']})).toEqual({ |
| 57 | + stdout: 'stdout', |
| 58 | + stderr: '', |
| 59 | + command: 'test \'hello!\' \'how are you doing $USER\' \'"double"\' \\\'\'single\'\\\'', |
| 60 | + }); |
| 61 | + |
| 62 | + execCalledWith(mockExec, [ |
| 63 | + 'test \'hello!\' \'how are you doing $USER\' \'"double"\' \\\'\'single\'\\\'', |
| 64 | + ]); |
| 65 | + stdoutCalledWith(mockStdout, [ |
| 66 | + '[command]test \'hello!\' \'how are you doing $USER\' \'"double"\' \\\'\'single\'\\\'', |
| 67 | + ' >> stdout', |
| 68 | + ]); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should run command with args, altCommand', async() => { |
| 72 | + const mockExec = spyOnSpawn(); |
| 73 | + const mockStdout = spyOnStdout(); |
| 74 | + |
| 75 | + expect(await command.execAsync({command: 'test', args: ['hello!', 'how are you doing $USER', '"double"', '\'single\''], altCommand: 'alt'})).toEqual({ |
| 76 | + stdout: 'stdout', |
| 77 | + stderr: '', |
| 78 | + command: 'alt', |
| 79 | + }); |
| 80 | + |
| 81 | + execCalledWith(mockExec, [ |
| 82 | + 'test \'hello!\' \'how are you doing $USER\' \'"double"\' \\\'\'single\'\\\'', |
| 83 | + ]); |
| 84 | + stdoutCalledWith(mockStdout, [ |
| 85 | + '[command]alt', |
| 86 | + ' >> stdout', |
| 87 | + ]); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should not output empty stdout', async() => { |
| 91 | + setChildProcessParams({stdout: ' \n\n \n'}); |
| 92 | + const mockExec = spyOnSpawn(); |
| 93 | + const mockStdout = spyOnStdout(); |
| 94 | + |
| 95 | + expect(await command.execAsync({command: 'test'})).toEqual({stdout: '', stderr: '', command: 'test'}); |
| 96 | + |
| 97 | + execCalledWith(mockExec, [ |
| 98 | + 'test', |
| 99 | + ]); |
| 100 | + stdoutCalledWith(mockStdout, [ |
| 101 | + '[command]test', |
| 102 | + ]); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should not output empty stderr', async() => { |
| 106 | + setChildProcessParams({stderr: ' \n\n \n'}); |
| 107 | + const mockExec = spyOnSpawn(); |
| 108 | + const mockStdout = spyOnStdout(); |
| 109 | + |
| 110 | + expect(await command.execAsync({command: 'test'})).toEqual({stdout: 'stdout', stderr: '', command: 'test'}); |
| 111 | + |
| 112 | + execCalledWith(mockExec, [ |
| 113 | + 'test', |
| 114 | + ]); |
| 115 | + stdoutCalledWith(mockStdout, [ |
| 116 | + '[command]test', |
| 117 | + ' >> stdout', |
| 118 | + ]); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should catch error 1', async() => { |
| 122 | + const error = new Error('test message'); |
| 123 | + error['code'] = 123; |
| 124 | + setChildProcessParams({error: error}); |
| 125 | + |
| 126 | + await expect(command.execAsync({ |
| 127 | + command: 'test', |
| 128 | + })).rejects.toThrow('command [test] exited with code 123. message: test message'); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should catch error 2', async() => { |
| 132 | + const error = new Error('test message'); |
| 133 | + error['code'] = 123; |
| 134 | + setChildProcessParams({error: error}); |
| 135 | + |
| 136 | + await expect(command.execAsync({ |
| 137 | + command: 'test', |
| 138 | + altCommand: 'alt', |
| 139 | + })).rejects.toThrow('command [alt] exited with code 123. message: test message'); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should catch error 3', async() => { |
| 143 | + const error = new Error('test message'); |
| 144 | + error['code'] = 123; |
| 145 | + setChildProcessParams({error: error}); |
| 146 | + |
| 147 | + await expect(command.execAsync({ |
| 148 | + command: 'test', |
| 149 | + altCommand: 'alt', |
| 150 | + quiet: true, |
| 151 | + })).rejects.toThrow('command [alt] exited with code 123.'); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should catch error 4', async() => { |
| 155 | + const error = new Error('test message'); |
| 156 | + error['code'] = 123; |
| 157 | + setChildProcessParams({error: error}); |
| 158 | + |
| 159 | + await expect(command.execAsync({ |
| 160 | + command: 'test', |
| 161 | + quiet: true, |
| 162 | + })).rejects.toThrow('command exited with code 123.'); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should suppress stdout', async() => { |
| 166 | + const mockExec = spyOnSpawn(); |
| 167 | + const mockStdout = spyOnStdout(); |
| 168 | + |
| 169 | + await command.execAsync({ |
| 170 | + command: 'test', |
| 171 | + suppressOutput: true, |
| 172 | + }); |
| 173 | + |
| 174 | + execCalledWith(mockExec, [ |
| 175 | + 'test', |
| 176 | + ]); |
| 177 | + stdoutCalledWith(mockStdout, [ |
| 178 | + '[command]test', |
| 179 | + ]); |
| 180 | + }); |
| 181 | + |
| 182 | + it('should output stdout instead of stderr', async() => { |
| 183 | + setChildProcessParams({stderr: 'stderr'}); |
| 184 | + const mockExec = spyOnSpawn(); |
| 185 | + const mockStdout = spyOnStdout(); |
| 186 | + |
| 187 | + await command.execAsync({ |
| 188 | + command: 'test', |
| 189 | + stderrToStdout: true, |
| 190 | + }); |
| 191 | + |
| 192 | + execCalledWith(mockExec, [ |
| 193 | + 'test', |
| 194 | + ]); |
| 195 | + stdoutCalledWith(mockStdout, [ |
| 196 | + '[command]test', |
| 197 | + ' >> stdout', |
| 198 | + ' >> stderr', |
| 199 | + ]); |
| 200 | + }); |
| 201 | + |
| 202 | + it('should not output stdout', async() => { |
| 203 | + setChildProcessParams({stdout: ''}); |
| 204 | + const mockExec = spyOnSpawn(); |
| 205 | + const mockStdout = spyOnStdout(); |
| 206 | + |
| 207 | + await command.execAsync({ |
| 208 | + command: 'test', |
| 209 | + }); |
| 210 | + |
| 211 | + execCalledWith(mockExec, [ |
| 212 | + 'test', |
| 213 | + ]); |
| 214 | + stdoutCalledWith(mockStdout, [ |
| 215 | + '[command]test', |
| 216 | + ]); |
| 217 | + }); |
| 218 | + |
| 219 | + it('should run suppress error command', async() => { |
| 220 | + const mockExec = spyOnSpawn(); |
| 221 | + const mockStdout = spyOnStdout(); |
| 222 | + |
| 223 | + await command.execAsync({ |
| 224 | + command: 'test', |
| 225 | + suppressError: true, |
| 226 | + }); |
| 227 | + |
| 228 | + execCalledWith(mockExec, [ |
| 229 | + 'test || :', |
| 230 | + ]); |
| 231 | + stdoutCalledWith(mockStdout, [ |
| 232 | + '[command]test', |
| 233 | + ' >> stdout', |
| 234 | + ]); |
| 235 | + }); |
| 236 | +}); |
0 commit comments