Skip to content

Commit

Permalink
fix: use output environment files
Browse files Browse the repository at this point in the history
  • Loading branch information
rlespinasse committed Oct 20, 2022
1 parent 7ab2386 commit c4fe109
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 30 deletions.
32 changes: 28 additions & 4 deletions .github/actions/operations/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const { add, subtract, multiply } = require("./math");
const process = require('process');
const cp = require('child_process');
const path = require('path');
const fs = require('fs');
const os = require('os');

test('make sure it returns something', async () => {
const core = {
Expand All @@ -29,17 +31,39 @@ test('make sure it returns something', async () => {

describe("simple test", () => {
it('test runs', () => {
const filePath = path.join(__dirname, `test_outputs.log`)
setupGitHubOutput(process, filePath);

process.env['INPUT_INPUT1'] = 9;
process.env['INPUT_INPUT2'] = 5;

const ip = path.join(__dirname, 'index.js');
const result = cp.execSync(`node ${ip}`, { env: process.env }).toString();
expect(result).toContain("::set-output name=addition::14");
expect(result).toContain("::set-output name=subtraction::4");
expect(result).toContain("::set-output name=multiplication::45");
cp.execSync(`node ${ip}`, { env: process.env });

const contents = fs.readFileSync(filePath, 'utf8')
try {
verifyGitHubOutput(contents, "addition", "14");
verifyGitHubOutput(contents, "subtraction", "4");
verifyGitHubOutput(contents, "multiplication", "45");
} finally {
fs.unlinkSync(filePath)
}
})
});


function setupGitHubOutput(process, filePath) {
process.env['GITHUB_OUTPUT'] = filePath;
fs.appendFileSync(filePath, '', {
encoding: 'utf8'
})
}

function verifyGitHubOutput(contents, expectedKey, expectedValue) {
const regex = `${expectedKey}<<ghadelimiter_.*${os.EOL}${expectedValue}${os.EOL}ghadelimiter_.*${os.EOL}`
expect(contents).toMatch(new RegExp(regex));
}

describe("simple arithmetic", () => {
describe("addition", () => {
test("expect 9 + 5 = 14", () => {
Expand Down
2 changes: 1 addition & 1 deletion .github/actions/operations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "jest"
},
"dependencies": {
"@actions/core": "1.2.5"
"@actions/core": "^1.10.0"
},
"devDependencies": {
"@vercel/ncc": "^0.31.1",
Expand Down
7 changes: 4 additions & 3 deletions .github/actions/testable-action/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ inputs:
outputs:
value:
description: output value
value: '${{ steps.final-output-value.outputs.outputValue }}'
value: "${{ steps.final-output-value.outputs.outputValue }}"
runs:
using: 'composite'
using: "composite"
steps:
- run: |
echo "OUTPUT_VALUE=output-${{ inputs.value }}" >> $GITHUB_ENV
Expand All @@ -23,5 +23,6 @@ runs:
if: ${{ inputs.value == '' }}
- id: final-output-value
run: echo "::set-output name=outputValue::${{ env.OUTPUT_VALUE }}"
run: |
echo "outputValue=${{ env.OUTPUT_VALUE }}" >> $GITHUB_OUTPUT
shell: bash
8 changes: 4 additions & 4 deletions docs/markdown/10-syntax/01-outputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ runs:
image: 'Dockerfile'
```

To define an output value, you need to print the `set-output` command.
To define an output value, you can use the [GITHUB_OUTPUT](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter) environment file.

**main.sh**

```sh
echo "::set-output name=someOutput::Some Value"
echo "someOutput=Some Value" >> $GITHUB_ENV
```

Notes:
Expand Down Expand Up @@ -82,11 +82,11 @@ runs:
using: 'composite'
steps:
- id: some-output-step
run: echo "::set-output name=someOutput::Some Value"
run: echo "someOutput=Some Value" >> $GITHUB_ENV
shell: bash
```

To define an output value, a step need to print the `set-output` command and you need to map it as value of the output.
To define an output value, a step need to use the [GITHUB_OUTPUT](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter) environment file and then map it as value of the output of the action.

Notes:

Expand Down
1 change: 1 addition & 0 deletions docs/markdown/20-interactions/02-communicate.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Notes:
Speaker **Romain**

##--##

# Communicate

## Logging and annotations
Expand Down
23 changes: 16 additions & 7 deletions docs/markdown/30-testing/01-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Speaker **Thibauld**

**Test suite**

```js
```js [1,19|2-6|8-12|14-18]
describe("simple arithmetic", () => {
describe("addition", () => {
test("expect 9 + 5 = 14", () => {
Expand Down Expand Up @@ -90,17 +90,26 @@ For the operations **math.js** file, we can test its exposed function.

**Run** the main file

```js
```js [9-10|6-7|3-4|12-19]
describe("simple test", () => {
it('test runs', () => {
const filePath = path.join(__dirname, `test_outputs.log`)
setupGitHubOutput(process, filePath);

process.env['INPUT_INPUT1'] = 9;
process.env['INPUT_INPUT2'] = 5;

const ip = path.join(__dirname, 'index.js');
const result = cp.execSync(`node ${ip}`, { env: process.env }).toString();
expect(result).toContain("::set-output name=addition::14");
expect(result).toContain("::set-output name=subtraction::4");
expect(result).toContain("::set-output name=multiplication::45");
cp.execSync(`node ${ip}`, { env: process.env });

const contents = fs.readFileSync(filePath, 'utf8')
try {
verifyGitHubOutput(contents, "addition", "14");
verifyGitHubOutput(contents, "subtraction", "4");
verifyGitHubOutput(contents, "multiplication", "45");
} finally {
fs.unlinkSync(filePath)
}
})
});
```
Expand All @@ -117,7 +126,7 @@ Speaker **Thibauld**

**Mock** some methods and test their usage.

```js
```js [13|2-9|10-12|14-20]
test('make sure it returns something', async () => {
const core = {
getInput: jest.fn().mockResolvedValue(parseInt(43)),
Expand Down
8 changes: 4 additions & 4 deletions steps/10-syntax-lab2-action-output-solution/action.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 'Syntax:Lab2'
description: 'Action of Lab2 in Syntax section'
name: "Syntax:Lab2"
description: "Action of Lab2 in Syntax section"
inputs:
text:
description: text
Expand All @@ -9,11 +9,11 @@ outputs:
description: text in UPPER CASE
value: ${{ steps.upper_step.outputs.upper_text }}
runs:
using: 'composite'
using: "composite"
steps:
- id: upper_step
run: |
echo "::set-output name=upper_text::${INPUT_TEXT^^}"
echo "upper_text=${INPUT_TEXT^^}" >> $GITHUB_OUTPUT
env:
INPUT_TEXT: ${{ inputs.text }}
shell: bash
2 changes: 1 addition & 1 deletion steps/20-interactions-lab1-warning-solution/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"prepare": "ncc build index.js -o dist --source-map"
},
"dependencies": {
"@actions/core": "^1.2.5",
"@actions/core": "^1.10.0",
"@actions/glob": "^0.3.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion steps/20-interactions-lab1-warning/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"prepare": "ncc build index.js -o dist --source-map"
},
"dependencies": {
"@actions/core": "^1.2.5"
"@actions/core": "^1.10.0"
},
"devDependencies": {
"@vercel/ncc": "^0.31.1"
Expand Down
2 changes: 1 addition & 1 deletion steps/20-interactions-lab2-summary/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"prepare": "ncc build index.js -o dist --source-map"
},
"dependencies": {
"@actions/core": "^1.2.5",
"@actions/core": "^1.10.0",
"@actions/glob": "^0.3.0"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions steps/30-testing-lab2-action-workflow-testing/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ runs:
ADD2: ${{ inputs.addition-input-2 }}
run: |
OUTPUT_VALUE=$(sh scripts/addition.sh $ADD1 $ADD2)
echo "::set-output name=outputAdditionValue::$OUTPUT_VALUE"
echo "outputAdditionValue=$OUTPUT_VALUE" >> $GITHUB_OUTPUT
shell: bash

- name: run hello-world
id: hello-world
run: |
OUTPUT_VALUE=$(sh scripts/hello-world.sh)
echo "::set-output name=outputHelloWorldValue::$OUTPUT_VALUE"
echo "outputHelloWorldValue=$OUTPUT_VALUE" >> $GITHUB_OUTPUT
shell: bash

- name: run palindrome
Expand All @@ -53,5 +53,5 @@ runs:
PAL: ${{ inputs.palindrome-input }}
run: |
OUTPUT_VALUE=$(sh scripts/palindrome.sh $PAL)
echo "::set-output name=outputPalindromeValue::$OUTPUT_VALUE"
echo "outputPalindromeValue=$OUTPUT_VALUE" >> $GITHUB_OUTPUT
shell: bash
2 changes: 1 addition & 1 deletion steps/50-lifecycle-lab2-dist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"prepare": "ncc build index.js -o dist --source-map"
},
"dependencies": {
"@actions/core": "^1.2.5"
"@actions/core": "^1.10.0"
},
"devDependencies": {
"@vercel/ncc": "^0.31.1"
Expand Down

0 comments on commit c4fe109

Please sign in to comment.