|
| 1 | +/** |
| 2 | + * @license Copyright 2021 Google Inc. All Rights Reserved. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 4 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |
| 5 | + */ |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +/* eslint-env jest */ |
| 9 | + |
| 10 | +const path = require('path'); |
| 11 | +const rimraf = require('rimraf'); |
| 12 | +const fs = require('fs'); |
| 13 | + |
| 14 | +const ApiClient = require('@lhci/utils/src/api-client.js'); |
| 15 | + |
| 16 | +const { |
| 17 | + createServer: createGithubServer, |
| 18 | +} = require('./fixtures/autorun-github/mock-github-server.js'); |
| 19 | +const {runCLI, startServer, safeDeleteFile} = require('./test-utils.js'); |
| 20 | + |
| 21 | +describe('Lighthouse CI autorun CLI with GitHub status check', () => { |
| 22 | + const autorunDir = path.join(__dirname, 'fixtures/autorun-github'); |
| 23 | + const lighthouseciDir = path.join(autorunDir, '.lighthouseci'); |
| 24 | + |
| 25 | + let server; |
| 26 | + let serverBaseUrl; |
| 27 | + let apiClient; |
| 28 | + let project; |
| 29 | + |
| 30 | + let githubServer; |
| 31 | + |
| 32 | + beforeAll(async () => { |
| 33 | + if (fs.existsSync(lighthouseciDir)) rimraf.sync(lighthouseciDir); |
| 34 | + githubServer = await createGithubServer(); |
| 35 | + |
| 36 | + server = await startServer(); |
| 37 | + serverBaseUrl = `http://localhost:${server.port}/`; |
| 38 | + |
| 39 | + apiClient = new ApiClient({rootURL: serverBaseUrl}); |
| 40 | + project = await apiClient.createProject({name: 'Test'}); |
| 41 | + }); |
| 42 | + |
| 43 | + afterAll(async () => { |
| 44 | + if (server) { |
| 45 | + server.process.kill(); |
| 46 | + await safeDeleteFile(server.sqlFile); |
| 47 | + } |
| 48 | + if (githubServer) { |
| 49 | + await githubServer.close(); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + it('should submit status check to GitHub', async () => { |
| 54 | + const {stdout, stderr, status} = await runCLI( |
| 55 | + [ |
| 56 | + 'autorun', |
| 57 | + '--upload.githubToken=githubToken', |
| 58 | + `--upload.githubApiHost=http://localhost:${githubServer.port}`, |
| 59 | + `--upload.serverBaseUrl=${serverBaseUrl}`, |
| 60 | + `--upload.token=${project.token}`, |
| 61 | + ], |
| 62 | + { |
| 63 | + cwd: autorunDir, |
| 64 | + env: { |
| 65 | + LHCI_BUILD_CONTEXT__CURRENT_HASH: 'hash', |
| 66 | + LHCI_BUILD_CONTEXT__GITHUB_REPO_SLUG: 'GoogleChrome/lighthouse-ci', |
| 67 | + LHCI_NO_LIGHTHOUSERC: undefined, |
| 68 | + }, |
| 69 | + } |
| 70 | + ); |
| 71 | + |
| 72 | + expect(stdout).toMatchInlineSnapshot(` |
| 73 | + "✅ .lighthouseci/ directory writable |
| 74 | + ✅ Configuration file found |
| 75 | + ✅ Chrome installation found |
| 76 | + ⚠️ GitHub token not set |
| 77 | + Healthcheck passed! |
| 78 | +
|
| 79 | + Automatically determined ./build as \`staticDistDir\`. |
| 80 | + Set it explicitly in lighthouserc.json if incorrect. |
| 81 | +
|
| 82 | + Started a web server on port XXXX... |
| 83 | + Running Lighthouse 1 time(s) on http://localhost:XXXX/bad.html |
| 84 | + Run #1...done. |
| 85 | + Running Lighthouse 1 time(s) on http://localhost:XXXX/good.html |
| 86 | + Run #1...done. |
| 87 | + Done running Lighthouse! |
| 88 | +
|
| 89 | +
|
| 90 | + Uploading median LHR of http://localhost:XXXX/bad.html...success! |
| 91 | + Open the report at https://storage.googleapis.com/lighthouse-infrastructure.appspot.com/reports/XXXX-XXXX.report.html |
| 92 | + Uploading median LHR of http://localhost:XXXX/good.html...success! |
| 93 | + Open the report at https://storage.googleapis.com/lighthouse-infrastructure.appspot.com/reports/XXXX-XXXX.report.html |
| 94 | + GitHub token found, attempting to set status... |
| 95 | + GitHub accepted \\"failure\\" status for \\"lhci/url/bad.html\\". |
| 96 | + GitHub accepted \\"success\\" status for \\"lhci/url/good.html\\". |
| 97 | +
|
| 98 | + " |
| 99 | + `); |
| 100 | + expect(stderr).toMatchInlineSnapshot(` |
| 101 | + "Checking assertions against 2 URL(s), 2 total run(s) |
| 102 | +
|
| 103 | + 1 result(s) for [1mhttp://localhost:XXXX/bad.html[0m : |
| 104 | +
|
| 105 | + [31mX[0m [1mviewport[0m failure for [1mminScore[0m assertion |
| 106 | + Does not have a \`<meta name=\\"viewport\\">\` tag with \`width\` or \`initial-scale\` |
| 107 | + https://web.dev/viewport/ |
| 108 | +
|
| 109 | + expected: >=[32m0.9[0m |
| 110 | + found: [31m0[0m |
| 111 | + [2mall values: 0[0m |
| 112 | +
|
| 113 | + 1 result(s) for [1mhttp://localhost:XXXX/good.html[0m : |
| 114 | +
|
| 115 | + ✅ [1mviewport[0m passing for [1mminScore[0m assertion |
| 116 | + Has a \`<meta name=\\"viewport\\">\` tag with \`width\` or \`initial-scale\` |
| 117 | + https://web.dev/viewport/ |
| 118 | +
|
| 119 | + expected: >=[32m0.9[0m |
| 120 | + found: [32m1[0m |
| 121 | + [2mall values: 1[0m |
| 122 | +
|
| 123 | + Assertion failed. Exiting with status code 1. |
| 124 | + assert command failed. Exiting with status code 1. |
| 125 | + " |
| 126 | + `); |
| 127 | + expect(status).toEqual(1); |
| 128 | + }, 180000); |
| 129 | + |
| 130 | + it('should submit status check to GitHub with invalid token', async () => { |
| 131 | + const {stdout, stderr, status} = await runCLI( |
| 132 | + [ |
| 133 | + 'autorun', |
| 134 | + '--upload.githubToken=invalidToken', |
| 135 | + `--upload.githubApiHost=http://localhost:${githubServer.port}`, |
| 136 | + `--upload.serverBaseUrl=${serverBaseUrl}`, |
| 137 | + `--upload.token=${project.token}`, |
| 138 | + ], |
| 139 | + { |
| 140 | + cwd: autorunDir, |
| 141 | + env: { |
| 142 | + LHCI_BUILD_CONTEXT__CURRENT_HASH: 'hash', |
| 143 | + LHCI_BUILD_CONTEXT__GITHUB_REPO_SLUG: 'GoogleChrome/lighthouse-ci', |
| 144 | + LHCI_NO_LIGHTHOUSERC: undefined, |
| 145 | + }, |
| 146 | + } |
| 147 | + ); |
| 148 | + |
| 149 | + expect(stdout).toMatchInlineSnapshot(` |
| 150 | + "✅ .lighthouseci/ directory writable |
| 151 | + ✅ Configuration file found |
| 152 | + ✅ Chrome installation found |
| 153 | + ⚠️ GitHub token not set |
| 154 | + Healthcheck passed! |
| 155 | +
|
| 156 | + Automatically determined ./build as \`staticDistDir\`. |
| 157 | + Set it explicitly in lighthouserc.json if incorrect. |
| 158 | +
|
| 159 | + Started a web server on port XXXX... |
| 160 | + Running Lighthouse 1 time(s) on http://localhost:XXXX/bad.html |
| 161 | + Run #1...done. |
| 162 | + Running Lighthouse 1 time(s) on http://localhost:XXXX/good.html |
| 163 | + Run #1...done. |
| 164 | + Done running Lighthouse! |
| 165 | +
|
| 166 | +
|
| 167 | + Uploading median LHR of http://localhost:XXXX/bad.html...success! |
| 168 | + Open the report at https://storage.googleapis.com/lighthouse-infrastructure.appspot.com/reports/XXXX-XXXX.report.html |
| 169 | + Uploading median LHR of http://localhost:XXXX/good.html...success! |
| 170 | + Open the report at https://storage.googleapis.com/lighthouse-infrastructure.appspot.com/reports/XXXX-XXXX.report.html |
| 171 | + GitHub token found, attempting to set status... |
| 172 | + GitHub responded with 401 |
| 173 | +
|
| 174 | +
|
| 175 | + GitHub responded with 401 |
| 176 | +
|
| 177 | +
|
| 178 | +
|
| 179 | + " |
| 180 | + `); |
| 181 | + expect(stderr).toMatchInlineSnapshot(` |
| 182 | + "Checking assertions against 2 URL(s), 2 total run(s) |
| 183 | +
|
| 184 | + 1 result(s) for [1mhttp://localhost:XXXX/bad.html[0m : |
| 185 | +
|
| 186 | + [31mX[0m [1mviewport[0m failure for [1mminScore[0m assertion |
| 187 | + Does not have a \`<meta name=\\"viewport\\">\` tag with \`width\` or \`initial-scale\` |
| 188 | + https://web.dev/viewport/ |
| 189 | +
|
| 190 | + expected: >=[32m0.9[0m |
| 191 | + found: [31m0[0m |
| 192 | + [2mall values: 0[0m |
| 193 | +
|
| 194 | + 1 result(s) for [1mhttp://localhost:XXXX/good.html[0m : |
| 195 | +
|
| 196 | + ✅ [1mviewport[0m passing for [1mminScore[0m assertion |
| 197 | + Has a \`<meta name=\\"viewport\\">\` tag with \`width\` or \`initial-scale\` |
| 198 | + https://web.dev/viewport/ |
| 199 | +
|
| 200 | + expected: >=[32m0.9[0m |
| 201 | + found: [32m1[0m |
| 202 | + [2mall values: 1[0m |
| 203 | +
|
| 204 | + Assertion failed. Exiting with status code 1. |
| 205 | + assert command failed. Exiting with status code 1. |
| 206 | + " |
| 207 | + `); |
| 208 | + expect(status).toEqual(1); |
| 209 | + }, 180000); |
| 210 | +}); |
0 commit comments