From ae431178c17085afd420fdf7ca88b37bd32e3d7d Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Sat, 24 Apr 2021 00:44:38 +0200 Subject: [PATCH] Fix setOutput (#67) Co-authored-by: CrazyMax --- __tests__/context.test.ts | 30 ++++++++++++++++++++++++++++++ dist/index.js | 16 +++++++++++----- src/context.ts | 6 ++++++ src/main.ts | 10 +++++----- 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/__tests__/context.test.ts b/__tests__/context.test.ts index 70de571..a0aee97 100644 --- a/__tests__/context.test.ts +++ b/__tests__/context.test.ts @@ -1,4 +1,5 @@ import * as fs from 'fs'; +import * as os from 'os'; import * as path from 'path'; import * as context from '../src/context'; @@ -171,6 +172,27 @@ describe('asyncForEach', () => { }); }); +describe('setOutput', () => { + beforeEach(() => { + process.stdout.write = jest.fn(); + }); + + it('setOutput produces the correct command', () => { + context.setOutput('some output', 'some value'); + assertWriteCalls([`::set-output name=some output::some value${os.EOL}`]); + }); + + it('setOutput handles bools', () => { + context.setOutput('some output', false); + assertWriteCalls([`::set-output name=some output::false${os.EOL}`]); + }); + + it('setOutput handles numbers', () => { + context.setOutput('some output', 1.01); + assertWriteCalls([`::set-output name=some output::1.01${os.EOL}`]); + }); +}); + // See: https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67 function getInputName(name: string): string { return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`; @@ -179,3 +201,11 @@ function getInputName(name: string): string { function setInput(name: string, value: string): void { process.env[getInputName(name)] = value; } + +// Assert that process.stdout.write calls called only with the given arguments. +function assertWriteCalls(calls: string[]): void { + expect(process.stdout.write).toHaveBeenCalledTimes(calls.length); + for (let i = 0; i < calls.length; i++) { + expect(process.stdout.write).toHaveBeenNthCalledWith(i + 1, calls[i]); + } +} diff --git a/dist/index.js b/dist/index.js index 0ff34cf..9940592 100644 --- a/dist/index.js +++ b/dist/index.js @@ -39,9 +39,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.asyncForEach = exports.getInputList = exports.getInputs = exports.tmpDir = void 0; +exports.setOutput = exports.asyncForEach = exports.getInputList = exports.getInputs = exports.tmpDir = void 0; const sync_1 = __importDefault(__webpack_require__(8750)); const core = __importStar(__webpack_require__(2186)); +const command_1 = __webpack_require__(7351); const fs = __importStar(__webpack_require__(5747)); const os = __importStar(__webpack_require__(2087)); const path = __importStar(__webpack_require__(5622)); @@ -94,6 +95,11 @@ exports.asyncForEach = (array, callback) => __awaiter(void 0, void 0, void 0, fu yield callback(array[index], index, array); } }); +// FIXME: Temp fix https://github.com/actions/toolkit/issues/777 +function setOutput(name, value) { + command_1.issueCommand('set-output', { name }, value); +} +exports.setOutput = setOutput; //# sourceMappingURL=context.js.map /***/ }), @@ -292,7 +298,7 @@ function run() { core.info(version.main || ''); core.endGroup(); } - core.setOutput('version', version.main || ''); + context_1.setOutput('version', version.main || ''); // Docker tags const tags = meta.getTags(); if (tags.length == 0) { @@ -305,7 +311,7 @@ function run() { } core.endGroup(); } - core.setOutput('tags', tags.join(inputs.sepTags)); + context_1.setOutput('tags', tags.join(inputs.sepTags)); // Docker labels const labels = meta.getLabels(); core.startGroup(`Docker labels`); @@ -313,13 +319,13 @@ function run() { core.info(label); } core.endGroup(); - core.setOutput('labels', labels.join(inputs.sepLabels)); + context_1.setOutput('labels', labels.join(inputs.sepLabels)); // Bake definition file const bakeFile = meta.getBakeFile(); core.startGroup(`Bake definition file`); core.info(fs.readFileSync(bakeFile, 'utf8')); core.endGroup(); - core.setOutput('bake-file', bakeFile); + context_1.setOutput('bake-file', bakeFile); } catch (error) { core.setFailed(error.message); diff --git a/src/context.ts b/src/context.ts index 22a6e31..5306b81 100644 --- a/src/context.ts +++ b/src/context.ts @@ -1,5 +1,6 @@ import csvparse from 'csv-parse/lib/sync'; import * as core from '@actions/core'; +import {issueCommand} from '@actions/core/lib/command'; import * as fs from 'fs'; import * as os from 'os'; import * as path from 'path'; @@ -66,3 +67,8 @@ export const asyncForEach = async (array, callback) => { await callback(array[index], index, array); } }; + +// FIXME: Temp fix https://github.com/actions/toolkit/issues/777 +export function setOutput(name: string, value: any): void { + issueCommand('set-output', {name}, value); +} diff --git a/src/main.ts b/src/main.ts index cf0e7ec..a41ea4c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,5 +1,5 @@ import * as fs from 'fs'; -import {getInputs, Inputs} from './context'; +import {getInputs, Inputs, setOutput} from './context'; import * as github from './github'; import {Meta, Version} from './meta'; import * as core from '@actions/core'; @@ -36,7 +36,7 @@ async function run() { core.info(version.main || ''); core.endGroup(); } - core.setOutput('version', version.main || ''); + setOutput('version', version.main || ''); // Docker tags const tags: Array = meta.getTags(); @@ -49,7 +49,7 @@ async function run() { } core.endGroup(); } - core.setOutput('tags', tags.join(inputs.sepTags)); + setOutput('tags', tags.join(inputs.sepTags)); // Docker labels const labels: Array = meta.getLabels(); @@ -58,14 +58,14 @@ async function run() { core.info(label); } core.endGroup(); - core.setOutput('labels', labels.join(inputs.sepLabels)); + setOutput('labels', labels.join(inputs.sepLabels)); // Bake definition file const bakeFile: string = meta.getBakeFile(); core.startGroup(`Bake definition file`); core.info(fs.readFileSync(bakeFile, 'utf8')); core.endGroup(); - core.setOutput('bake-file', bakeFile); + setOutput('bake-file', bakeFile); } catch (error) { core.setFailed(error.message); }