|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { describe, it } from 'node:test'; |
| 18 | +import assert from 'node:assert'; |
| 19 | + |
| 20 | +import { getExecOutput, ExecOptions } from '@actions/exec'; |
| 21 | + |
| 22 | +const skipIfMissingEnvs = (...keys: string[]): { skip: string } | undefined => { |
| 23 | + const missingKeys: string[] = []; |
| 24 | + |
| 25 | + for (const key of keys) { |
| 26 | + if (!(key in process.env)) { |
| 27 | + missingKeys.push(key); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + if (missingKeys.length > 0) { |
| 32 | + return { skip: `missing $${missingKeys.join(', $')}` }; |
| 33 | + } |
| 34 | + return undefined; |
| 35 | +}; |
| 36 | + |
| 37 | +describe( |
| 38 | + '#run', |
| 39 | + skipIfMissingEnvs('TEST_ACCOUNT', 'TEST_PROJECT_ID', 'TEST_COMPONENTS'), |
| 40 | + async () => { |
| 41 | + const testAccount = process.env.TEST_ACCOUNT!; |
| 42 | + const testProjectID = process.env.TEST_PROJECT_ID!; |
| 43 | + const testComponents = process.env.TEST_COMPONENTS!; |
| 44 | + |
| 45 | + it('has the correct account', async () => { |
| 46 | + const raw = await gcloudRun([ |
| 47 | + '--quiet', |
| 48 | + 'auth', |
| 49 | + 'list', |
| 50 | + '--filter', |
| 51 | + 'status:ACTIVE', |
| 52 | + '--format', |
| 53 | + 'json', |
| 54 | + ]); |
| 55 | + const result = JSON.parse(raw)[0]?.['account'] || '(unset)'; |
| 56 | + assert.deepStrictEqual(result, testAccount); |
| 57 | + }); |
| 58 | + |
| 59 | + it('has the correct project_id', async () => { |
| 60 | + const raw = await gcloudRun([ |
| 61 | + '--quiet', |
| 62 | + 'config', |
| 63 | + 'list', |
| 64 | + 'core/project', |
| 65 | + '--format', |
| 66 | + 'json', |
| 67 | + ]); |
| 68 | + const result = JSON.parse(raw)['core']?.['project'] || '(unset)'; |
| 69 | + assert.deepStrictEqual(result, testProjectID); |
| 70 | + }); |
| 71 | + |
| 72 | + it('includes the given components', async () => { |
| 73 | + const raw = await gcloudRun([ |
| 74 | + '--quiet', |
| 75 | + 'components', |
| 76 | + 'list', |
| 77 | + '--only-local-state', |
| 78 | + '--format', |
| 79 | + 'json', |
| 80 | + ]); |
| 81 | + const result = JSON.parse(raw).map((entry: Record<string, any>) => entry['id']); |
| 82 | + const members = testComponents.split(',').map((component) => component.trim()); |
| 83 | + |
| 84 | + const intersection = members.filter((v) => result.includes(v)); |
| 85 | + assert.deepStrictEqual(intersection, members); |
| 86 | + }); |
| 87 | + }, |
| 88 | +); |
| 89 | + |
| 90 | +async function gcloudRun(cmd: string[], options?: ExecOptions): Promise<string> { |
| 91 | + // A workaround for https://github.com/actions/toolkit/issues/229 |
| 92 | + let toolCommand = 'gcloud'; |
| 93 | + if (process.platform == 'win32') { |
| 94 | + toolCommand = 'gcloud.cmd'; |
| 95 | + } |
| 96 | + |
| 97 | + const opts = Object.assign({}, { silent: true, ignoreReturnCode: true }, options); |
| 98 | + const commandString = `${toolCommand} ${cmd.join(' ')}`; |
| 99 | + |
| 100 | + const result = await getExecOutput(toolCommand, cmd, opts); |
| 101 | + if (result.exitCode !== 0) { |
| 102 | + const errMsg = result.stderr || `command exited ${result.exitCode}, but stderr had no output`; |
| 103 | + throw new Error(`failed to execute command \`${commandString}\`: ${errMsg}`); |
| 104 | + } |
| 105 | + return result.stdout; |
| 106 | +} |
0 commit comments