|
| 1 | +/* eslint-disable no-magic-numbers */ |
| 2 | +import path from 'path'; |
| 3 | +import { testEnv, spyOnStdout, stdoutCalledWith, spyOnExec, setChildProcessParams, execCalledWith } from '@technote-space/github-action-test-helper'; |
| 4 | +import { Logger } from '@technote-space/github-action-helper'; |
| 5 | +import { dumpDiffs, setResult, execute } from '../src/process'; |
| 6 | + |
| 7 | +const rootDir = path.resolve(__dirname, '..'); |
| 8 | +const diffs = [ |
| 9 | + {file: 'test1', insertions: 1, deletions: 100, lines: 101, filterIgnored: false, prefixMatched: true, suffixMatched: true}, |
| 10 | + {file: 'test2', insertions: 2, deletions: 200, lines: 202, filterIgnored: false, prefixMatched: true, suffixMatched: true}, |
| 11 | + {file: 'test4', insertions: 4, deletions: 400, lines: 404, filterIgnored: true, prefixMatched: true, suffixMatched: false}, |
| 12 | +]; |
| 13 | + |
| 14 | +describe('dumpDiffs', () => { |
| 15 | + testEnv(rootDir); |
| 16 | + |
| 17 | + it('should dump output', () => { |
| 18 | + const mockStdout = spyOnStdout(); |
| 19 | + |
| 20 | + dumpDiffs(diffs, new Logger()); |
| 21 | + |
| 22 | + stdoutCalledWith(mockStdout, [ |
| 23 | + '::group::Dump diffs', |
| 24 | + '[\n' + |
| 25 | + '\t{\n' + |
| 26 | + '\t\t"file": "test1",\n' + |
| 27 | + '\t\t"insertions": 1,\n' + |
| 28 | + '\t\t"deletions": 100,\n' + |
| 29 | + '\t\t"lines": 101,\n' + |
| 30 | + '\t\t"filterIgnored": false,\n' + |
| 31 | + '\t\t"prefixMatched": true,\n' + |
| 32 | + '\t\t"suffixMatched": true\n' + |
| 33 | + '\t},\n' + |
| 34 | + '\t{\n' + |
| 35 | + '\t\t"file": "test2",\n' + |
| 36 | + '\t\t"insertions": 2,\n' + |
| 37 | + '\t\t"deletions": 200,\n' + |
| 38 | + '\t\t"lines": 202,\n' + |
| 39 | + '\t\t"filterIgnored": false,\n' + |
| 40 | + '\t\t"prefixMatched": true,\n' + |
| 41 | + '\t\t"suffixMatched": true\n' + |
| 42 | + '\t},\n' + |
| 43 | + '\t{\n' + |
| 44 | + '\t\t"file": "test4",\n' + |
| 45 | + '\t\t"insertions": 4,\n' + |
| 46 | + '\t\t"deletions": 400,\n' + |
| 47 | + '\t\t"lines": 404,\n' + |
| 48 | + '\t\t"filterIgnored": true,\n' + |
| 49 | + '\t\t"prefixMatched": true,\n' + |
| 50 | + '\t\t"suffixMatched": false\n' + |
| 51 | + '\t}\n' + |
| 52 | + ']', |
| 53 | + '::endgroup::', |
| 54 | + ]); |
| 55 | + }); |
| 56 | +}); |
| 57 | + |
| 58 | +describe('setResult', () => { |
| 59 | + testEnv(rootDir); |
| 60 | + |
| 61 | + it('should set result', () => { |
| 62 | + const mockStdout = spyOnStdout(); |
| 63 | + |
| 64 | + setResult(diffs, new Logger()); |
| 65 | + |
| 66 | + stdoutCalledWith(mockStdout, [ |
| 67 | + '::group::Dump output', |
| 68 | + '::set-output name=diff::test1 test2 test4', |
| 69 | + '::set-env name=GIT_DIFF::test1 test2 test4', |
| 70 | + '"diff: test1 test2 test4"', |
| 71 | + '::set-output name=count::3', |
| 72 | + '"count: 3"', |
| 73 | + '::set-output name=insertions::3', |
| 74 | + '"insertions: 3"', |
| 75 | + '::set-output name=deletions::300', |
| 76 | + '"deletions: 300"', |
| 77 | + '::set-output name=lines::303', |
| 78 | + '"lines: 303"', |
| 79 | + '::endgroup::', |
| 80 | + ]); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should set result without env', () => { |
| 84 | + process.env.INPUT_SET_ENV_NAME = ''; |
| 85 | + process.env.INPUT_SET_ENV_NAME_COUNT = 'FILE_COUNT'; |
| 86 | + process.env.INPUT_SET_ENV_NAME_INSERTIONS = 'INSERTIONS'; |
| 87 | + process.env.INPUT_SET_ENV_NAME_DELETIONS = 'DELETIONS'; |
| 88 | + process.env.INPUT_SET_ENV_NAME_LINES = 'LINES'; |
| 89 | + const mockStdout = spyOnStdout(); |
| 90 | + |
| 91 | + setResult(diffs, new Logger()); |
| 92 | + |
| 93 | + stdoutCalledWith(mockStdout, [ |
| 94 | + '::group::Dump output', |
| 95 | + '::set-output name=diff::test1 test2 test4', |
| 96 | + '"diff: test1 test2 test4"', |
| 97 | + '::set-output name=count::3', |
| 98 | + '::set-env name=FILE_COUNT::3', |
| 99 | + '"count: 3"', |
| 100 | + '::set-output name=insertions::3', |
| 101 | + '::set-env name=INSERTIONS::3', |
| 102 | + '"insertions: 3"', |
| 103 | + '::set-output name=deletions::300', |
| 104 | + '::set-env name=DELETIONS::300', |
| 105 | + '"deletions: 300"', |
| 106 | + '::set-output name=lines::303', |
| 107 | + '::set-env name=LINES::303', |
| 108 | + '"lines: 303"', |
| 109 | + '::endgroup::', |
| 110 | + ]); |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
| 114 | +describe('execute', () => { |
| 115 | + testEnv(rootDir); |
| 116 | + |
| 117 | + it('should execute', async() => { |
| 118 | + process.env.GITHUB_WORKSPACE = '/home/runner/work/my-repo-name/my-repo-name'; |
| 119 | + process.env.GITHUB_REF = 'refs/pull/123/merge'; |
| 120 | + process.env.GITHUB_SHA = 'f01e53bb1f41af4e132326dad21e82c77ee1ff48'; |
| 121 | + process.env.GITHUB_HEAD_REF = 'release/v0.3.13'; |
| 122 | + process.env.GITHUB_BASE_REF = 'master'; |
| 123 | + |
| 124 | + const mockExec = spyOnExec(); |
| 125 | + const mockStdout = spyOnStdout(); |
| 126 | + setChildProcessParams({ |
| 127 | + stdout: (command: string): string => { |
| 128 | + if (command.startsWith('git diff')) { |
| 129 | + if (command.includes('shortstat')) { |
| 130 | + return '1 file changed, 25 insertions(+), 4 deletions(-)'; |
| 131 | + } |
| 132 | + return 'package.json\nabc/composer.json\nREADME.md\nsrc/main.ts'; |
| 133 | + } |
| 134 | + return ''; |
| 135 | + }, |
| 136 | + }); |
| 137 | + |
| 138 | + await execute(new Logger()); |
| 139 | + |
| 140 | + execCalledWith(mockExec, [ |
| 141 | + 'git fetch --no-tags origin \'+refs/pull/*/merge:refs/remotes/pull/*/merge\'', |
| 142 | + 'git fetch --no-tags origin \'+refs/heads/*:refs/remotes/origin/*\'', |
| 143 | + 'git diff "origin/${GITHUB_BASE_REF}"..."${GITHUB_REF#refs/}" \'--diff-filter=AM\' --name-only', |
| 144 | + 'git diff "origin/${GITHUB_BASE_REF}"..."${GITHUB_REF#refs/}" --shortstat \'package.json\'', |
| 145 | + 'git diff "origin/${GITHUB_BASE_REF}"..."${GITHUB_REF#refs/}" --shortstat \'abc/composer.json\'', |
| 146 | + 'git diff "origin/${GITHUB_BASE_REF}"..."${GITHUB_REF#refs/}" --shortstat \'README.md\'', |
| 147 | + 'git diff "origin/${GITHUB_BASE_REF}"..."${GITHUB_REF#refs/}" --shortstat \'src/main.ts\'', |
| 148 | + ]); |
| 149 | + stdoutCalledWith(mockStdout, [ |
| 150 | + '[command]git fetch --no-tags origin \'+refs/pull/*/merge:refs/remotes/pull/*/merge\'', |
| 151 | + '[command]git fetch --no-tags origin \'+refs/heads/*:refs/remotes/origin/*\'', |
| 152 | + '[command]git diff "origin/${GITHUB_BASE_REF}"..."${GITHUB_REF#refs/}" \'--diff-filter=AM\' --name-only', |
| 153 | + ' >> package.json', |
| 154 | + ' >> abc/composer.json', |
| 155 | + ' >> README.md', |
| 156 | + ' >> src/main.ts', |
| 157 | + '[command]git diff "origin/${GITHUB_BASE_REF}"..."${GITHUB_REF#refs/}" --shortstat \'package.json\'', |
| 158 | + ' >> 1 file changed, 25 insertions(+), 4 deletions(-)', |
| 159 | + '[command]git diff "origin/${GITHUB_BASE_REF}"..."${GITHUB_REF#refs/}" --shortstat \'abc/composer.json\'', |
| 160 | + ' >> 1 file changed, 25 insertions(+), 4 deletions(-)', |
| 161 | + '[command]git diff "origin/${GITHUB_BASE_REF}"..."${GITHUB_REF#refs/}" --shortstat \'README.md\'', |
| 162 | + ' >> 1 file changed, 25 insertions(+), 4 deletions(-)', |
| 163 | + '[command]git diff "origin/${GITHUB_BASE_REF}"..."${GITHUB_REF#refs/}" --shortstat \'src/main.ts\'', |
| 164 | + ' >> 1 file changed, 25 insertions(+), 4 deletions(-)', |
| 165 | + '::group::Dump diffs', |
| 166 | + '[\n' + |
| 167 | + '\t{\n' + |
| 168 | + '\t\t"file": "package.json",\n' + |
| 169 | + '\t\t"filterIgnored": false,\n' + |
| 170 | + '\t\t"prefixMatched": true,\n' + |
| 171 | + '\t\t"suffixMatched": true,\n' + |
| 172 | + '\t\t"insertions": 25,\n' + |
| 173 | + '\t\t"deletions": 4,\n' + |
| 174 | + '\t\t"lines": 29\n' + |
| 175 | + '\t},\n' + |
| 176 | + '\t{\n' + |
| 177 | + '\t\t"file": "abc/composer.json",\n' + |
| 178 | + '\t\t"filterIgnored": false,\n' + |
| 179 | + '\t\t"prefixMatched": true,\n' + |
| 180 | + '\t\t"suffixMatched": true,\n' + |
| 181 | + '\t\t"insertions": 25,\n' + |
| 182 | + '\t\t"deletions": 4,\n' + |
| 183 | + '\t\t"lines": 29\n' + |
| 184 | + '\t},\n' + |
| 185 | + '\t{\n' + |
| 186 | + '\t\t"file": "README.md",\n' + |
| 187 | + '\t\t"filterIgnored": false,\n' + |
| 188 | + '\t\t"prefixMatched": true,\n' + |
| 189 | + '\t\t"suffixMatched": true,\n' + |
| 190 | + '\t\t"insertions": 25,\n' + |
| 191 | + '\t\t"deletions": 4,\n' + |
| 192 | + '\t\t"lines": 29\n' + |
| 193 | + '\t},\n' + |
| 194 | + '\t{\n' + |
| 195 | + '\t\t"file": "src/main.ts",\n' + |
| 196 | + '\t\t"filterIgnored": false,\n' + |
| 197 | + '\t\t"prefixMatched": true,\n' + |
| 198 | + '\t\t"suffixMatched": true,\n' + |
| 199 | + '\t\t"insertions": 25,\n' + |
| 200 | + '\t\t"deletions": 4,\n' + |
| 201 | + '\t\t"lines": 29\n' + |
| 202 | + '\t}\n' + |
| 203 | + ']', |
| 204 | + '::endgroup::', |
| 205 | + '::group::Dump output', |
| 206 | + '::set-output name=diff::\'package.json\' \'abc/composer.json\' \'README.md\' \'src/main.ts\'', |
| 207 | + '::set-env name=GIT_DIFF::\'package.json\' \'abc/composer.json\' \'README.md\' \'src/main.ts\'', |
| 208 | + '"diff: \'package.json\' \'abc/composer.json\' \'README.md\' \'src/main.ts\'"', |
| 209 | + '::set-output name=count::4', |
| 210 | + '"count: 4"', |
| 211 | + '::set-output name=insertions::100', |
| 212 | + '"insertions: 100"', |
| 213 | + '::set-output name=deletions::16', |
| 214 | + '"deletions: 16"', |
| 215 | + '::set-output name=lines::116', |
| 216 | + '"lines: 116"', |
| 217 | + '::endgroup::', |
| 218 | + ]); |
| 219 | + }); |
| 220 | + |
| 221 | + it('should execute empty', async() => { |
| 222 | + process.env.GITHUB_WORKSPACE = '/home/runner/work/my-repo-name/my-repo-name'; |
| 223 | + process.env.GITHUB_REF = 'refs/pull/123/merge'; |
| 224 | + process.env.GITHUB_SHA = 'f01e53bb1f41af4e132326dad21e82c77ee1ff48'; |
| 225 | + process.env.GITHUB_HEAD_REF = 'release/v0.3.13'; |
| 226 | + process.env.GITHUB_BASE_REF = 'master'; |
| 227 | + |
| 228 | + const mockExec = spyOnExec(); |
| 229 | + const mockStdout = spyOnStdout(); |
| 230 | + |
| 231 | + await execute(new Logger(), []); |
| 232 | + |
| 233 | + execCalledWith(mockExec, []); |
| 234 | + stdoutCalledWith(mockStdout, [ |
| 235 | + '::group::Dump diffs', |
| 236 | + '[]', |
| 237 | + '::endgroup::', |
| 238 | + '::group::Dump output', |
| 239 | + '::set-output name=diff::', |
| 240 | + '::set-env name=GIT_DIFF::', |
| 241 | + '"diff: "', |
| 242 | + '::set-output name=count::0', |
| 243 | + '"count: 0"', |
| 244 | + '::set-output name=insertions::0', |
| 245 | + '"insertions: 0"', |
| 246 | + '::set-output name=deletions::0', |
| 247 | + '"deletions: 0"', |
| 248 | + '::set-output name=lines::0', |
| 249 | + '"lines: 0"', |
| 250 | + '::endgroup::', |
| 251 | + ]); |
| 252 | + }); |
| 253 | +}); |
0 commit comments