|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @noflow |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +const { |
| 12 | + configurationResults, |
| 13 | + prepareTestState, |
| 14 | + recordConfigurationResult, |
| 15 | + reportConfigurationResults, |
| 16 | +} = require('../template-app-ci-state'); |
| 17 | +const fs = require('node:fs'); |
| 18 | +const os = require('node:os'); |
| 19 | +const path = require('node:path'); |
| 20 | + |
| 21 | +const bothFlavors = ['Debug', 'Release']; |
| 22 | + |
| 23 | +function results(outputs, flavors = bothFlavors, previousResults = {}) { |
| 24 | + return configurationResults({outputs, flavors, previousResults}); |
| 25 | +} |
| 26 | + |
| 27 | +describe('template app configuration retries', () => { |
| 28 | + it('does not retry when both configurations pass', () => { |
| 29 | + expect( |
| 30 | + results({'debug-result': 'passed', 'release-result': 'passed'}), |
| 31 | + ).toEqual({ |
| 32 | + results: {Debug: 'passed', Release: 'passed'}, |
| 33 | + failedFlavors: [], |
| 34 | + status: 'success', |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + it.each(bothFlavors)('retries only the failed %s configuration', flavor => { |
| 39 | + const outputs = {'debug-result': 'passed', 'release-result': 'passed'}; |
| 40 | + outputs[`${flavor.toLowerCase()}-result`] = 'failed'; |
| 41 | + expect(results(outputs).failedFlavors).toEqual([flavor]); |
| 42 | + }); |
| 43 | + |
| 44 | + it('retains a successful build while retrying the other configuration', () => { |
| 45 | + const first = results({ |
| 46 | + 'debug-result': 'passed', |
| 47 | + 'release-result': 'failed', |
| 48 | + }); |
| 49 | + const second = results( |
| 50 | + {'release-result': 'passed'}, |
| 51 | + first.failedFlavors, |
| 52 | + first.results, |
| 53 | + ); |
| 54 | + expect(second.status).toBe('success'); |
| 55 | + expect(second.results).toEqual({Debug: 'passed', Release: 'passed'}); |
| 56 | + }); |
| 57 | + |
| 58 | + it('keeps a configuration retryable after its result step is lost', () => { |
| 59 | + const first = results({'debug-result': 'passed'}); |
| 60 | + const second = results({}, first.failedFlavors, first.results); |
| 61 | + expect(second).toEqual({ |
| 62 | + results: {Debug: 'passed', Release: 'failed'}, |
| 63 | + failedFlavors: ['Release'], |
| 64 | + status: 'failure', |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + it('does not reuse an old pass for a configuration attempted again', () => { |
| 69 | + expect( |
| 70 | + results({}, ['Debug'], {Debug: 'passed', Release: 'passed'}), |
| 71 | + ).toEqual({ |
| 72 | + results: {Debug: 'failed', Release: 'passed'}, |
| 73 | + failedFlavors: ['Debug'], |
| 74 | + status: 'failure', |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('never treats an omitted configuration without an earlier pass as passed', () => { |
| 79 | + expect( |
| 80 | + results({'release-result': 'passed'}, ['Release']).failedFlavors, |
| 81 | + ).toEqual(['Debug']); |
| 82 | + }); |
| 83 | + |
| 84 | + it.each([[], ['Debug', 'Debug'], ['Profile'], 'Debug'])( |
| 85 | + 'rejects an invalid retry matrix %p', |
| 86 | + flavors => { |
| 87 | + expect(() => results({}, flavors)).toThrow('unique Debug/Release'); |
| 88 | + }, |
| 89 | + ); |
| 90 | + |
| 91 | + it('writes independent outputs for both matrix legs', () => { |
| 92 | + const core = {setOutput: jest.fn()}; |
| 93 | + recordConfigurationResult({core, flavor: 'Release', passed: false}); |
| 94 | + recordConfigurationResult({core, flavor: 'Debug', passed: true}); |
| 95 | + expect(core.setOutput.mock.calls).toEqual([ |
| 96 | + ['release-result', 'failed'], |
| 97 | + ['debug-result', 'passed'], |
| 98 | + ]); |
| 99 | + }); |
| 100 | + |
| 101 | + it('rejects an unknown configuration when recording results', () => { |
| 102 | + expect(() => |
| 103 | + recordConfigurationResult({ |
| 104 | + core: {setOutput: jest.fn()}, |
| 105 | + flavor: 'Profile', |
| 106 | + passed: true, |
| 107 | + }), |
| 108 | + ).toThrow('Unknown template app configuration'); |
| 109 | + }); |
| 110 | +}); |
| 111 | + |
| 112 | +describe('template app final reporting', () => { |
| 113 | + function core() { |
| 114 | + const result = { |
| 115 | + setOutput: jest.fn(), |
| 116 | + setFailed: jest.fn(), |
| 117 | + warning: jest.fn(), |
| 118 | + summary: {addRaw: jest.fn(), write: jest.fn().mockResolvedValue()}, |
| 119 | + }; |
| 120 | + result.summary.addRaw.mockReturnValue(result.summary); |
| 121 | + return result; |
| 122 | + } |
| 123 | + |
| 124 | + async function report(jobResult, outputs, failOnError = false) { |
| 125 | + const result = core(); |
| 126 | + await reportConfigurationResults({ |
| 127 | + context: { |
| 128 | + serverUrl: 'https://github.com', |
| 129 | + repo: {owner: 'react', repo: 'react-native'}, |
| 130 | + runId: 1, |
| 131 | + }, |
| 132 | + core: result, |
| 133 | + needs: {test: {result: jobResult, outputs}}, |
| 134 | + job: 'test', |
| 135 | + flavors: ['Release'], |
| 136 | + previousResults: {Debug: 'passed', Release: 'failed'}, |
| 137 | + failOnError, |
| 138 | + attempt: 1, |
| 139 | + }); |
| 140 | + return result; |
| 141 | + } |
| 142 | + |
| 143 | + it('reports success after the previously unsuccessful lane passes', async () => { |
| 144 | + const result = await report('success', {'release-result': 'passed'}, true); |
| 145 | + expect(result.setOutput).toHaveBeenCalledWith('status', 'success'); |
| 146 | + expect(result.setOutput).toHaveBeenCalledWith('failed-flavors', '[]'); |
| 147 | + expect(result.setFailed).not.toHaveBeenCalled(); |
| 148 | + }); |
| 149 | + |
| 150 | + it('reports intermediate failures without failing the report job', async () => { |
| 151 | + const result = await report('success', {'release-result': 'failed'}); |
| 152 | + expect(result.setOutput).toHaveBeenCalledWith('status', 'failure'); |
| 153 | + expect(result.setOutput).toHaveBeenCalledWith( |
| 154 | + 'failed-flavors', |
| 155 | + '["Release"]', |
| 156 | + ); |
| 157 | + expect(result.setFailed).not.toHaveBeenCalled(); |
| 158 | + expect(result.warning).toHaveBeenCalled(); |
| 159 | + }); |
| 160 | + |
| 161 | + it('fails the final attempt when a configuration still has not passed', async () => { |
| 162 | + const result = await report('success', {'release-result': 'failed'}, true); |
| 163 | + expect(result.setFailed).toHaveBeenCalled(); |
| 164 | + }); |
| 165 | + |
| 166 | + it.each(['failure', 'cancelled', 'skipped'])( |
| 167 | + 'does not hide a %s matrix job behind a passed result output', |
| 168 | + async jobResult => { |
| 169 | + const result = await report( |
| 170 | + jobResult, |
| 171 | + {'release-result': 'passed'}, |
| 172 | + true, |
| 173 | + ); |
| 174 | + expect(result.setOutput).toHaveBeenCalledWith('status', 'failure'); |
| 175 | + expect(result.setOutput).toHaveBeenCalledWith( |
| 176 | + 'failed-flavors', |
| 177 | + '["Release"]', |
| 178 | + ); |
| 179 | + expect(result.setFailed).toHaveBeenCalled(); |
| 180 | + }, |
| 181 | + ); |
| 182 | + |
| 183 | + it('treats missing app/setup results as failure on the final attempt', async () => { |
| 184 | + const result = await report('failure', {}, true); |
| 185 | + expect(result.setFailed).toHaveBeenCalledWith( |
| 186 | + expect.stringContaining('job failure'), |
| 187 | + ); |
| 188 | + }); |
| 189 | +}); |
| 190 | + |
| 191 | +describe('template app per-flow state', () => { |
| 192 | + let directory; |
| 193 | + let flowsDirectory; |
| 194 | + let statePath; |
| 195 | + let first; |
| 196 | + let second; |
| 197 | + |
| 198 | + beforeEach(() => { |
| 199 | + directory = fs.mkdtempSync(path.join(os.tmpdir(), 'template-app-ci-test-')); |
| 200 | + flowsDirectory = path.join(directory, 'flows'); |
| 201 | + fs.mkdirSync(flowsDirectory); |
| 202 | + first = path.join(flowsDirectory, 'first.yml'); |
| 203 | + second = path.join(flowsDirectory, 'second.yaml'); |
| 204 | + fs.writeFileSync(first, 'appId: x\n---\n- launchApp\n'); |
| 205 | + fs.writeFileSync(second, 'appId: x\n---\n- launchApp\n'); |
| 206 | + statePath = path.join(directory, 'results.json'); |
| 207 | + }); |
| 208 | + |
| 209 | + afterEach(() => { |
| 210 | + fs.rmSync(directory, {recursive: true, force: true}); |
| 211 | + }); |
| 212 | + |
| 213 | + function writeState(states) { |
| 214 | + const flows = Object.fromEntries( |
| 215 | + states.map(([flow, status]) => [ |
| 216 | + path.relative(process.cwd(), flow).split(path.sep).join('/'), |
| 217 | + {status, attempts: 1}, |
| 218 | + ]), |
| 219 | + ); |
| 220 | + fs.writeFileSync(statePath, JSON.stringify({version: 1, flows})); |
| 221 | + } |
| 222 | + |
| 223 | + it('runs the flows when the state artifact is missing', () => { |
| 224 | + expect(prepareTestState(statePath, flowsDirectory)).toBe(true); |
| 225 | + }); |
| 226 | + |
| 227 | + it('skips execution only when every expected flow has passed', () => { |
| 228 | + writeState([ |
| 229 | + [first, 'passed'], |
| 230 | + [second, 'passed'], |
| 231 | + ]); |
| 232 | + expect(prepareTestState(statePath, flowsDirectory)).toBe(false); |
| 233 | + }); |
| 234 | + |
| 235 | + it.each(['failed', 'pending'])( |
| 236 | + 'runs unfinished flows with status %s', |
| 237 | + status => { |
| 238 | + writeState([ |
| 239 | + [first, 'passed'], |
| 240 | + [second, status], |
| 241 | + ]); |
| 242 | + expect(prepareTestState(statePath, flowsDirectory)).toBe(true); |
| 243 | + }, |
| 244 | + ); |
| 245 | + |
| 246 | + it('does not skip a flow missing from an otherwise successful state', () => { |
| 247 | + writeState([[first, 'passed']]); |
| 248 | + expect(prepareTestState(statePath, flowsDirectory)).toBe(true); |
| 249 | + }); |
| 250 | + |
| 251 | + it('ignores helper flows, which are not independently executed', () => { |
| 252 | + const helpers = path.join(flowsDirectory, 'helpers'); |
| 253 | + fs.mkdirSync(helpers); |
| 254 | + fs.writeFileSync(path.join(helpers, 'fragment.yml'), '- tapOn: x'); |
| 255 | + writeState([ |
| 256 | + [first, 'passed'], |
| 257 | + [second, 'passed'], |
| 258 | + ]); |
| 259 | + expect(prepareTestState(statePath, flowsDirectory)).toBe(false); |
| 260 | + }); |
| 261 | + |
| 262 | + it.each(['{invalid', 'null', '{"version":2,"flows":{}}'])( |
| 263 | + 'discards invalid downloaded state %s before executing Maestro', |
| 264 | + state => { |
| 265 | + fs.writeFileSync(statePath, state); |
| 266 | + expect(prepareTestState(statePath, flowsDirectory)).toBe(true); |
| 267 | + expect(fs.existsSync(statePath)).toBe(false); |
| 268 | + }, |
| 269 | + ); |
| 270 | + |
| 271 | + it('does not interpret an empty flow directory as successful coverage', () => { |
| 272 | + fs.unlinkSync(first); |
| 273 | + fs.unlinkSync(second); |
| 274 | + writeState([]); |
| 275 | + expect(prepareTestState(statePath, flowsDirectory)).toBe(true); |
| 276 | + }); |
| 277 | +}); |
0 commit comments