From 5e92e6623ebe2b1f76b22a7086be0e2615c44962 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Tue, 27 Apr 2021 16:30:22 +0200 Subject: [PATCH] Fix setOutput Signed-off-by: CrazyMax --- __tests__/context.test.ts | 30 ++++++++++++++++++++++++++++++ dist/index.js | 10 ++++++++-- src/context.ts | 6 ++++++ src/main.ts | 2 +- 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/__tests__/context.test.ts b/__tests__/context.test.ts index 56d93d1..55669db 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'; @@ -554,6 +555,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()}`; @@ -562,3 +584,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 7ed1c91..bd94f34 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2419,7 +2419,7 @@ function run() { if (imageID) { core.startGroup(`Extracting digest`); core.info(`${imageID}`); - core.setOutput('digest', imageID); + context.setOutput('digest', imageID); core.endGroup(); } } @@ -13004,7 +13004,7 @@ 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 = exports.tmpNameSync = exports.tmpDir = exports.defaultContext = void 0; +exports.setOutput = exports.asyncForEach = exports.getInputList = exports.getArgs = exports.getInputs = exports.tmpNameSync = exports.tmpDir = exports.defaultContext = void 0; const sync_1 = __importDefault(__webpack_require__(750)); const fs = __importStar(__webpack_require__(747)); const os = __importStar(__webpack_require__(87)); @@ -13012,6 +13012,7 @@ const path = __importStar(__webpack_require__(622)); const semver = __importStar(__webpack_require__(383)); const tmp = __importStar(__webpack_require__(517)); const core = __importStar(__webpack_require__(186)); +const command_1 = __webpack_require__(351); const github = __importStar(__webpack_require__(438)); const buildx = __importStar(__webpack_require__(295)); let _defaultContext, _tmpDir; @@ -13189,6 +13190,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 f3cfb5f..9876fec 100644 --- a/src/context.ts +++ b/src/context.ts @@ -6,6 +6,7 @@ import * as semver from 'semver'; import * as tmp from 'tmp'; import * as core from '@actions/core'; +import {issueCommand} from '@actions/core/lib/command'; import * as github from '@actions/github'; import * as buildx from './buildx'; @@ -205,3 +206,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 03fb282..ebd9f88 100644 --- a/src/main.ts +++ b/src/main.ts @@ -36,7 +36,7 @@ async function run(): Promise { if (imageID) { core.startGroup(`Extracting digest`); core.info(`${imageID}`); - core.setOutput('digest', imageID); + context.setOutput('digest', imageID); core.endGroup(); } } catch (error) {