Skip to content

Commit a3faab7

Browse files
authored
Merge pull request #91 from gh640/dependabot/npm_and_yarn/actions/core-1.10.0
Bump @actions/core from 1.9.1 to 1.10.0
2 parents 808b1d7 + bf42a7b commit a3faab7

File tree

6 files changed

+170
-84
lines changed

6 files changed

+170
-84
lines changed

__tests__/main.test.ts

Lines changed: 123 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import * as process from 'process'
21
import * as cp from 'child_process'
2+
import * as fs from 'fs'
33
import * as path from 'path'
4-
import {expect, test} from '@jest/globals'
4+
import * as process from 'process'
5+
6+
import {afterAll, beforeAll, describe, expect, test} from '@jest/globals'
7+
8+
const outDirParent = path.join(__dirname, 'test')
9+
const outDirPrefix = path.join(outDirParent, 'out-')
510

611
type CommandResult = {
712
exitCode: number
@@ -46,53 +51,123 @@ const prepareEnv: prepareEnvType = vars => {
4651
}
4752
}
4853

49-
test('throws error on unknown command', async () => {
50-
const {exitCode, stdout, stderr} = await execMain(
51-
prepareEnv({INPUT_COMMAND: 'unknown'})
52-
)
53-
expect(exitCode).toEqual(1)
54-
expect(stdout).toContain('Unable to locate executable file: unknown.')
55-
expect(stderr).toContain('Error: Unable to locate executable file: unknown.')
56-
})
54+
describe('lib/main.js', () => {
55+
beforeAll(() => {
56+
if (!fs.existsSync(outDirParent)) {
57+
fs.mkdirSync(outDirParent)
58+
}
59+
})
5760

58-
test('properly handles single-line stdout', async () => {
59-
const {stdout} = await execMain(
60-
prepareEnv({INPUT_COMMAND: 'echo "Hello world"'})
61-
)
62-
63-
expect(stdout).toContain('Starting command.')
64-
expect(stdout).toContain(['echo Hello world', 'Hello world'].join('\n'))
65-
expect(stdout).toContain('Finished command.')
66-
expect(stdout).toContain('::set-output name=exitCode::0')
67-
expect(stdout).toContain('::set-output name=stdout::Hello world%0A')
68-
expect(stdout).toContain('::set-output name=stderr::')
69-
})
61+
afterAll(() => {
62+
fs.rmSync(outDirParent, {recursive: true})
63+
})
7064

71-
test('properly handles multi-line stdout (line breaks are escaped)', async () => {
72-
const {stdout} = await execMain(
73-
prepareEnv({INPUT_COMMAND: 'echo "Hello\nworld"'})
74-
)
75-
76-
expect(stdout).toContain('Starting command.')
77-
expect(stdout).toContain(['echo Hello\nworld', 'Hello\nworld'].join('\n'))
78-
expect(stdout).toContain('Finished command.')
79-
expect(stdout).toContain('::set-output name=exitCode::0')
80-
expect(stdout).toContain('::set-output name=stdout::Hello%0Aworld%0A')
81-
expect(stdout).toContain('::set-output name=stderr::')
82-
})
65+
test('properly handles single-line stdout', async () => {
66+
const [outFile, readOutFile] = tempFile()
67+
const {stdout} = await execMain(
68+
prepareEnv({
69+
INPUT_COMMAND: 'echo "Hello world"',
70+
GITHUB_OUTPUT: outFile
71+
})
72+
)
73+
const outFileContent = readOutFile()
74+
75+
expect(stdout).toContain('Starting command.')
76+
expect(stdout).toContain('echo Hello world\nHello world')
77+
expect(stdout).toContain('Finished command.')
78+
79+
// `[\w-]` matches the delimtier.
80+
expect(outFileContent).toMatch(/exitCode<<[\w-]+\n0\n/)
81+
expect(outFileContent).toMatch(/stdout<<[\w-]+\nHello world\n/)
82+
expect(outFileContent).toMatch(/stderr<<[\w-]+\n\n/)
83+
})
84+
85+
test('properly handles multi-line stdout', async () => {
86+
const [outFile, readOutFile] = tempFile()
87+
const {stdout} = await execMain(
88+
prepareEnv({
89+
INPUT_COMMAND: 'echo "Hello\nworld"',
90+
GITHUB_OUTPUT: outFile
91+
})
92+
)
93+
const outFileContent = readOutFile()
94+
95+
// `[\w-]` matches the delimtier.
96+
expect(outFileContent).toMatch(/exitCode<<[\w-]+\n0\n/)
97+
expect(outFileContent).toMatch(/stdout<<[\w-]+\nHello\nworld\n/)
98+
expect(outFileContent).toMatch(/stderr<<[\w-]+\n\n/)
99+
})
83100

84-
test('cwd', async () => {
85-
const {stdout} = await execMain(
86-
prepareEnv({
87-
INPUT_COMMAND: 'ls',
88-
INPUT_CWD: './lib'
89-
})
90-
)
91-
92-
expect(stdout).toContain('Starting command.')
93-
expect(stdout).toContain(['ls', 'main.js'].join('\n'))
94-
expect(stdout).toContain('Finished command.')
95-
expect(stdout).toContain('::set-output name=exitCode::0')
96-
expect(stdout).toContain('::set-output name=stdout::main.js%0A')
97-
expect(stdout).toContain('::set-output name=stderr::')
101+
test('works with option `cwd`', async () => {
102+
const [outFile, readOutFile] = tempFile()
103+
const {stdout} = await execMain(
104+
prepareEnv({
105+
INPUT_COMMAND: 'ls',
106+
INPUT_CWD: './lib',
107+
GITHUB_OUTPUT: outFile
108+
})
109+
)
110+
const outFileContent = readOutFile()
111+
112+
// `[\w-]` matches the delimtier.
113+
expect(outFileContent).toMatch(/exitCode<<[\w-]+\n0\n/)
114+
expect(outFileContent).toMatch(/stdout<<[\w-]+\nmain\.js\n/)
115+
expect(outFileContent).toMatch(/stderr<<[\w-]+\n\n/)
116+
})
117+
118+
test('properly captures stderr', async () => {
119+
const [outFile, readOutFile] = tempFile()
120+
const {exitCode, stdout, stderr} = await execMain(
121+
prepareEnv({
122+
INPUT_COMMAND: 'ls /unkown/path/',
123+
GITHUB_OUTPUT: outFile
124+
})
125+
)
126+
const outFileContent = readOutFile()
127+
128+
expect(exitCode).toEqual(0)
129+
expect(stdout).toContain('ls /unkown/path/')
130+
expect(stderr).toContain('')
131+
132+
// `[\w-]` matches the delimtier.
133+
// The `exitCode` can be 1 or 2 depending on the environment.
134+
expect(outFileContent).toMatch(/exitCode<<[\w-]+\n[12]\n/)
135+
expect(outFileContent).toMatch(/stdout<<[\w-]+\n\n/)
136+
expect(outFileContent).toMatch(
137+
/stderr<<[\w-]+\n.*No such file or directory\n/
138+
)
139+
})
140+
141+
test('fails with unknown command', async () => {
142+
const [outFile, readOutFile] = tempFile()
143+
const {exitCode, stdout, stderr} = await execMain(
144+
prepareEnv({
145+
INPUT_COMMAND: 'unknown',
146+
GITHUB_OUTPUT: outFile
147+
})
148+
)
149+
const outFileContent = readOutFile()
150+
151+
expect(exitCode).toEqual(1)
152+
expect(stdout).toContain('Unable to locate executable file: unknown.')
153+
expect(stderr).toContain(
154+
'Error: Unable to locate executable file: unknown.'
155+
)
156+
expect(outFileContent).toEqual('')
157+
})
98158
})
159+
160+
/**
161+
* Helper: prepare a temporary file.
162+
*/
163+
function tempFile(): [string, () => string] {
164+
const directory = fs.mkdtempSync(outDirPrefix)
165+
const file = path.join(directory, 'out')
166+
fs.appendFileSync(file, '', {encoding: 'utf8'})
167+
168+
function read(): string {
169+
return fs.readFileSync(file, {encoding: 'utf8'})
170+
}
171+
172+
return [file, read]
173+
}

dist/index.js

Lines changed: 37 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"author": "Goto Hayato <gh640@hey.com>",
2626
"license": "MIT",
2727
"dependencies": {
28-
"@actions/core": "^1.9.0",
28+
"@actions/core": "^1.10.0",
2929
"@actions/exec": "^1.1.1"
3030
},
3131
"devDependencies": {

src/main.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ async function main(): Promise<void> {
99
const command: string = core.getInput('command', {required: true})
1010
const cwd: string = core.getInput('cwd')
1111

12-
const options: exec.ExecOptions = {
13-
ignoreReturnCode: true
14-
}
12+
const options: exec.ExecOptions = {ignoreReturnCode: true}
1513
if (cwd != null) {
1614
options.cwd = cwd
1715
}

0 commit comments

Comments
 (0)