From bb74ce9b2fcc9a93e09b667c5c5ae0108cd587fc Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Fri, 30 Apr 2021 11:50:06 +0200 Subject: [PATCH] Fix setOutput --- __tests__/context.test.ts | 30 ++++++++++++++++++++++++++++++ dist/index.js | 8 +++++++- src/context.ts | 6 ++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/__tests__/context.test.ts b/__tests__/context.test.ts index bf0b2dc..c82605a 100644 --- a/__tests__/context.test.ts +++ b/__tests__/context.test.ts @@ -1,3 +1,4 @@ +import * as os from 'os'; import * as context from '../src/context'; describe('getInputList', () => { @@ -166,6 +167,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()}`; @@ -174,3 +196,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 4c3e40d..9175b69 100644 --- a/dist/index.js +++ b/dist/index.js @@ -5716,9 +5716,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.getArgs = exports.getInputs = void 0; +exports.setOutput = exports.asyncForEach = exports.getInputList = exports.getArgs = exports.getInputs = void 0; const sync_1 = __importDefault(__webpack_require__(750)); const core = __importStar(__webpack_require__(186)); +const command_1 = __webpack_require__(241); function getInputs() { return __awaiter(this, void 0, void 0, function* () { return { @@ -5806,6 +5807,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 /***/ }), diff --git a/src/context.ts b/src/context.ts index 99cb8ee..a0f65c8 100644 --- a/src/context.ts +++ b/src/context.ts @@ -1,6 +1,7 @@ import csvparse from 'csv-parse/lib/sync'; import * as buildx from './buildx'; import * as core from '@actions/core'; +import {issueCommand} from '@actions/core/lib/command'; export interface Inputs { builder: string; @@ -96,3 +97,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); +}