Skip to content

Commit

Permalink
Merge pull request #350 from crazy-max/fix-setoutput
Browse files Browse the repository at this point in the history
Fix setOutput
  • Loading branch information
crazy-max committed Apr 27, 2021
2 parents 3ce082a + 5e92e66 commit 17822e4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
30 changes: 30 additions & 0 deletions __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';
Expand Down Expand Up @@ -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()}`;
Expand All @@ -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]);
}
}
10 changes: 8 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/context.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion src/main.ts
Expand Up @@ -36,7 +36,7 @@ async function run(): Promise<void> {
if (imageID) {
core.startGroup(`Extracting digest`);
core.info(`${imageID}`);
core.setOutput('digest', imageID);
context.setOutput('digest', imageID);
core.endGroup();
}
} catch (error) {
Expand Down

0 comments on commit 17822e4

Please sign in to comment.