Skip to content

Commit

Permalink
Merge pull request #19 from crazy-max/fix-setoutput
Browse files Browse the repository at this point in the history
Fix setOutput
  • Loading branch information
crazy-max authored Apr 30, 2021
2 parents f845825 + bb74ce9 commit 1882cef
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
30 changes: 30 additions & 0 deletions __tests__/context.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as os from 'os';
import * as context from '../src/context';

describe('getInputList', () => {
Expand Down Expand Up @@ -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()}`;
Expand All @@ -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]);
}
}
8 changes: 7 additions & 1 deletion 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
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}

0 comments on commit 1882cef

Please sign in to comment.