Skip to content

Commit

Permalink
Fix setOutput (#67)
Browse files Browse the repository at this point in the history
Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max and crazy-max authored Apr 23, 2021
1 parent 34ebfd6 commit ae43117
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
30 changes: 30 additions & 0 deletions __tests__/context.test.ts
Original file line number Diff line number Diff line change
@@ -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 @@ -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()}`;
Expand All @@ -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]);
}
}
16 changes: 11 additions & 5 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
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
}
10 changes: 5 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<string> = meta.getTags();
Expand All @@ -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<string> = meta.getLabels();
Expand All @@ -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);
}
Expand Down

0 comments on commit ae43117

Please sign in to comment.