From 8b9dfa809bd6e4dc10a74f9345b464994731a82c Mon Sep 17 00:00:00 2001 From: Jonathan Clem Date: Wed, 28 Aug 2019 22:35:27 -0400 Subject: [PATCH 1/4] Add group functions to core --- packages/core/README.md | 17 +++++++++++++++ packages/core/__tests__/lib.test.ts | 21 ++++++++++++++++++ packages/core/src/command.ts | 2 +- packages/core/src/core.ts | 33 +++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 1 deletion(-) diff --git a/packages/core/README.md b/packages/core/README.md index 4520ce2d..c68dc7ee 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -78,3 +78,20 @@ catch (err) { core.error(`Error ${err}, action may still succeed though`); } ``` + +This library can also wrap chunks of output in foldable groups. + +```js +const core = require('@actions/core') + +// Manually wrap output +core.groupStart('Do some function') +doSomeFunction() +core.groupEnd() + +// Wrap an asynchronous function call +const result = await core.group('Do something async', async () => { + const response = await doSomeHTTPRequest() + return response +}) +``` \ No newline at end of file diff --git a/packages/core/__tests__/lib.test.ts b/packages/core/__tests__/lib.test.ts index 623d4aa2..32ba6cce 100644 --- a/packages/core/__tests__/lib.test.ts +++ b/packages/core/__tests__/lib.test.ts @@ -155,6 +155,27 @@ describe('@actions/core', () => { assertWriteCalls([`##[warning]%0D%0Awarning%0A${os.EOL}`]) }) + it('startGroup starts a new group', () => { + core.startGroup('my-group') + assertWriteCalls([`##[group]my-group${os.EOL}`]) + }) + + it('endGroup ends new group', () => { + core.endGroup() + assertWriteCalls([`##[endgroup]${os.EOL}`]) + }) + + it('group wraps an async call in a group', async () => { + await core.group('mygroup', async () => { + process.stdout.write('in my group\n') + }) + assertWriteCalls([ + `##[group]mygroup${os.EOL}`, + 'in my group\n', + `##[endgroup]${os.EOL}` + ]) + }) + it('debug sets the correct message', () => { core.debug('Debug') assertWriteCalls([`##[debug]Debug${os.EOL}`]) diff --git a/packages/core/src/command.ts b/packages/core/src/command.ts index c3cb7058..4e6e9b41 100644 --- a/packages/core/src/command.ts +++ b/packages/core/src/command.ts @@ -25,7 +25,7 @@ export function issueCommand( process.stdout.write(cmd.toString() + os.EOL) } -export function issue(name: string, message: string): void { +export function issue(name: string, message: string = ''): void { issueCommand(name, {}, message) } diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index 2fa992b2..baa028bc 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -130,3 +130,36 @@ export function error(message: string): void { export function warning(message: string): void { issue('warning', message) } + +/** + * Begin an output group. + * + * Output until the next `groupEnd` will be foldable in this group + * + * @param name The name of the output group + */ +export function startGroup(name: string): void { + issue('group', name) +} + +/** + * End an output group. + */ +export function endGroup(): void { + issue('endgroup') +} + +/** + * Wrap an asynchronous function call in a group. + * + * Returns the same type as the function itself. + * + * @param name The name of the group + * @param fn The function to wrap in the group + */ +export async function group(name: string, fn: () => Promise): Promise { + startGroup(name) + const result = await fn() + endGroup() + return result +} From 80fc75ef9cc206f4aa6d820079cad12e03ceb18d Mon Sep 17 00:00:00 2001 From: Jonathan Clem Date: Wed, 28 Aug 2019 22:36:17 -0400 Subject: [PATCH 2/4] Fix readme --- packages/core/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/README.md b/packages/core/README.md index c68dc7ee..58a8287f 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -85,9 +85,9 @@ This library can also wrap chunks of output in foldable groups. const core = require('@actions/core') // Manually wrap output -core.groupStart('Do some function') +core.startGroup('Do some function') doSomeFunction() -core.groupEnd() +core.endGroup() // Wrap an asynchronous function call const result = await core.group('Do something async', async () => { From 8f9992ca1799dd193ae54508aec32378682a7bfa Mon Sep 17 00:00:00 2001 From: Jonathan Clem Date: Wed, 28 Aug 2019 22:38:30 -0400 Subject: [PATCH 3/4] Add assertion for return value of core.group --- packages/core/__tests__/lib.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/core/__tests__/lib.test.ts b/packages/core/__tests__/lib.test.ts index 32ba6cce..dc34213f 100644 --- a/packages/core/__tests__/lib.test.ts +++ b/packages/core/__tests__/lib.test.ts @@ -166,9 +166,11 @@ describe('@actions/core', () => { }) it('group wraps an async call in a group', async () => { - await core.group('mygroup', async () => { + const result = await core.group('mygroup', async () => { process.stdout.write('in my group\n') + return true }) + expect(result).toBe(true) assertWriteCalls([ `##[group]mygroup${os.EOL}`, 'in my group\n', From 92e6443cf046bcbcb6e999340932356e87d9d39d Mon Sep 17 00:00:00 2001 From: Jonathan Clem Date: Wed, 28 Aug 2019 22:47:37 -0400 Subject: [PATCH 4/4] End group in core.group regardless of error thrown --- packages/core/src/core.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index baa028bc..d98e4d7c 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -159,7 +159,14 @@ export function endGroup(): void { */ export async function group(name: string, fn: () => Promise): Promise { startGroup(name) - const result = await fn() - endGroup() + + let result: T + + try { + result = await fn() + } finally { + endGroup() + } + return result }