|
| 1 | +/* |
| 2 | + * Copyright 2025 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, test } from 'node:test'; |
| 18 | +import assert from 'node:assert/strict'; |
| 19 | + |
| 20 | +import { promises as fs } from 'node:fs'; |
| 21 | +import os from 'node:os'; |
| 22 | +import path from 'node:path'; |
| 23 | + |
| 24 | +import { actionsGenReadme } from '../src/actions-gen-readme'; |
| 25 | +import { writeSecureFile } from '../src/fs'; |
| 26 | + |
| 27 | +describe('actions-gen-readme', { concurrency: true }, async () => { |
| 28 | + test('#actionsGenReadme', async (suite) => { |
| 29 | + let scratchDir = ''; |
| 30 | + |
| 31 | + // Ignore warnings and log messages in test output |
| 32 | + suite.mock.method(console, 'log', () => {}); |
| 33 | + suite.mock.method(console, 'warn', () => {}); |
| 34 | + |
| 35 | + suite.beforeEach(async () => { |
| 36 | + scratchDir = await fs.mkdtemp(path.join(os.tmpdir(), 'actions-gen-readme-')); |
| 37 | + }); |
| 38 | + |
| 39 | + suite.afterEach(async () => { |
| 40 | + if (scratchDir) { |
| 41 | + // For some unknown reason, Windows will sometimes fail these tests. To |
| 42 | + // make things less flakey, retry. |
| 43 | + for (let i = 0; i < 5; i++) { |
| 44 | + try { |
| 45 | + await fs.rm(scratchDir, { recursive: true, force: true }); |
| 46 | + } catch { |
| 47 | + await new Promise((r) => setTimeout(r, i * 500)); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + const cases = [ |
| 54 | + { |
| 55 | + name: 'input missing description', |
| 56 | + error: 'Input "foo" is missing a description', |
| 57 | + actionYAML: ` |
| 58 | + name: 'my-action' |
| 59 | + inputs: |
| 60 | + foo: |
| 61 | + required: true |
| 62 | + `, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: 'output missing description', |
| 66 | + error: 'Output "foo" is missing a description', |
| 67 | + actionYAML: ` |
| 68 | + name: 'my-action' |
| 69 | + outputs: |
| 70 | + foo: |
| 71 | + `, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: 'generates', |
| 75 | + actionYAML: ` |
| 76 | + name: 'my-action' |
| 77 | + inputs: |
| 78 | + foo: |
| 79 | + description: |- |
| 80 | + This is the description of foo. |
| 81 | + default: '55' |
| 82 | + required: true |
| 83 | + outputs: |
| 84 | + foo: |
| 85 | + description: |- |
| 86 | + The space between. |
| 87 | + `, |
| 88 | + expectedReadme: ` |
| 89 | + ## Inputs |
| 90 | + <!-- BEGIN_AUTOGEN_INPUTS --> |
| 91 | +
|
| 92 | + - <a name="__input_foo"></a><a href="#user-content-__input_foo"><code>foo</code></a>: _(Required, default: \`55\`)_ This is the description of foo. |
| 93 | +
|
| 94 | +
|
| 95 | + <!-- END_AUTOGEN_INPUTS --> |
| 96 | +
|
| 97 | + ## Outputs |
| 98 | + <!-- BEGIN_AUTOGEN_OUTPUTS --> |
| 99 | +
|
| 100 | + - <a name="__output_foo"></a><a href="#user-content-__output_foo"><code>foo</code></a>: The space between. |
| 101 | +
|
| 102 | +
|
| 103 | + <!-- END_AUTOGEN_OUTPUTS --> |
| 104 | + `, |
| 105 | + }, |
| 106 | + ]; |
| 107 | + |
| 108 | + for await (const tc of cases) { |
| 109 | + await suite.test(tc.name, async () => { |
| 110 | + const readmePath = path.join(scratchDir, 'README.md'); |
| 111 | + await writeSecureFile( |
| 112 | + readmePath, |
| 113 | + trimLeft( |
| 114 | + ` |
| 115 | + ## Inputs |
| 116 | + <!-- BEGIN_AUTOGEN_INPUTS --> |
| 117 | + <!-- END_AUTOGEN_INPUTS --> |
| 118 | +
|
| 119 | + ## Outputs |
| 120 | + <!-- BEGIN_AUTOGEN_OUTPUTS --> |
| 121 | + <!-- END_AUTOGEN_OUTPUTS --> |
| 122 | + `, |
| 123 | + 10, |
| 124 | + ), |
| 125 | + ); |
| 126 | + |
| 127 | + // Create the yaml contents |
| 128 | + if (tc.actionYAML) { |
| 129 | + await writeSecureFile(path.join(scratchDir, 'action.yml'), trimLeft(tc.actionYAML, 10)); |
| 130 | + } |
| 131 | + |
| 132 | + if (tc.error) { |
| 133 | + await assert.rejects(async () => { |
| 134 | + await actionsGenReadme(scratchDir); |
| 135 | + }, new RegExp(tc.error)); |
| 136 | + } else { |
| 137 | + // Success output |
| 138 | + await actionsGenReadme(scratchDir); |
| 139 | + |
| 140 | + const readmeContents = await fs.readFile(readmePath, { encoding: 'utf8' }); |
| 141 | + const expectedReadme = trimLeft(tc.expectedReadme || '', 10); |
| 142 | + assert.equal(readmeContents, expectedReadme); |
| 143 | + } |
| 144 | + }); |
| 145 | + } |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
| 149 | +function trimLeft(s: string, align: number): string { |
| 150 | + return s |
| 151 | + .split('\n') |
| 152 | + .map((l) => l.slice(align)) |
| 153 | + .join('\n') |
| 154 | + .trim(); |
| 155 | +} |
0 commit comments