Repository: crazy-max/ghaction-docker-meta
Pull request #67 (2021-04-23 22:42:11)

Fix setOutput

codecov[bot]: # [Codecov](https://codecov.io/gh/crazy-max/ghaction-docker-meta/pull/67?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CrazyMax) Report
> Merging [#67](https://codecov.io/gh/crazy-max/ghaction-docker-meta/pull/67?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CrazyMax) (a90ce06) into [master](https://codecov.io/gh/crazy-max/ghaction-docker-meta/commit/94641ff1bb3ca05cf9453545fdf8f25cb7a44147?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CrazyMax) (94641ff) will **increase** coverage by `0.20%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/crazy-max/ghaction-docker-meta/pull/67/graphs/tree.svg?width=650&height=150&src=pr&token=NgkCTCjmB1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CrazyMax)](https://codecov.io/gh/crazy-max/ghaction-docker-meta/pull/67?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CrazyMax)

```diff
@@            Coverage Diff             @@
##           master      #67      +/-   ##
==========================================
+ Coverage   75.84%   76.04%   +0.20%     
==========================================
  Files           5        5              
  Lines         356      359       +3     
  Branches       96       96              
==========================================
+ Hits          270      273       +3     
  Misses          5        5              
  Partials       81       81              
```


| [Impacted Files](https://codecov.io/gh/crazy-max/ghaction-docker-meta/pull/67?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CrazyMax) | Coverage Δ | |
|---|---|---|
| [src/context.ts](https://codecov.io/gh/crazy-max/ghaction-docker-meta/pull/67/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CrazyMax#diff-c3JjL2NvbnRleHQudHM=) | `87.09% <100.00%> (+1.38%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/crazy-max/ghaction-docker-meta/pull/67?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CrazyMax).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CrazyMax)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/crazy-max/ghaction-docker-meta/pull/67?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CrazyMax). Last update [94641ff...a90ce06](https://codecov.io/gh/crazy-max/ghaction-docker-meta/pull/67?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CrazyMax). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CrazyMax).


Patch:
diff --git a/dist/index.js b/dist/index.js
index 0ff34cf..9940592 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -39,9 +39,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.getInputs = exports.tmpDir = void 0;
+exports.setOutput = exports.asyncForEach = exports.getInputList = exports.getInputs = exports.tmpDir = void 0;
 const sync_1 = __importDefault(__webpack_require__(8750));
 const core = __importStar(__webpack_require__(2186));
+const command_1 = __webpack_require__(7351);
 const fs = __importStar(__webpack_require__(5747));
 const os = __importStar(__webpack_require__(2087));
 const path = __importStar(__webpack_require__(5622));
@@ -94,6 +95,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
 
 /***/ }),
@@ -292,7 +298,7 @@ function run() {
                 core.info(version.main || '');
                 core.endGroup();
             }
-            core.setOutput('version', version.main || '');
+            context_1.setOutput('version', version.main || '');
             // Docker tags
             const tags = meta.getTags();
             if (tags.length == 0) {
@@ -305,7 +311,7 @@ function run() {
                 }
                 core.endGroup();
             }
-            core.setOutput('tags', tags.join(inputs.sepTags));
+            context_1.setOutput('tags', tags.join(inputs.sepTags));
             // Docker labels
             const labels = meta.getLabels();
             core.startGroup(`Docker labels`);
@@ -313,13 +319,13 @@ function run() {
                 core.info(label);
             }
             core.endGroup();
-            core.setOutput('labels', labels.join(inputs.sepLabels));
+            context_1.setOutput('labels', labels.join(inputs.sepLabels));
             // Bake definition file
             const bakeFile = meta.getBakeFile();
             core.startGroup(`Bake definition file`);
             core.info(fs.readFileSync(bakeFile, 'utf8'));
             core.endGroup();
-            core.setOutput('bake-file', bakeFile);
+            context_1.setOutput('bake-file', bakeFile);
         }
         catch (error) {
             core.setFailed(error.message);
diff --git a/src/context.ts b/src/context.ts
index 22a6e31..5306b81 100644
--- a/src/context.ts
+++ b/src/context.ts
@@ -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';
@@ -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);
+}
diff --git a/src/main.ts b/src/main.ts
index cf0e7ec..a41ea4c 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -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';
@@ -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();
@@ -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();
@@ -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);
   }


Test patch:
diff --git a/__tests__/context.test.ts b/__tests__/context.test.ts
index 70de571..a0aee97 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';
@@ -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()}`;
@@ -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]);
+  }
+}