Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Toolkit.run #63

Merged
merged 9 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ const tools = new Toolkit({
})
```

### Toolkit.run

Run an asynchronous function that receives an instance of `Toolkit` as its argument. If the function throws an error (or returns a rejected promise), `Toolkit.run` will log the error and exit the action with a failure status code.

The toolkit instance can be configured by passing `Toolkit` options as the second argument to `Toolkit.run`.

```js
Toolkit.run(async tools => {
// Action code
}, { event: 'push' })
```
<br>

### tools.github

Returns an [Octokit SDK](https://octokit.github.io/rest.js) client authenticated for this repository. See [https://octokit.github.io/rest.js](https://octokit.github.io/rest.js) for the API.
Expand Down
23 changes: 23 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ export interface ToolkitOptions {
}

export class Toolkit {
/**
* Run an asynchronous function that accepts a toolkit as its argument, and fail if
* an error occurs.
*
* @param func - Async function to run
* @param [opts] - Options to pass to the toolkit
*
* @example This is generally used to run a `main` async function:
*
* ```js
* Toolkit.run(async tools => {
* // Action code here.
* }, { event: 'push' })
* ```
*/
public static async run (func: (tools: Toolkit) => Promise<unknown>, opts?: ToolkitOptions) {
const tools = new Toolkit(opts)

return func(tools).catch(err => {
tools.exit.failure(err)
})
}

public context: Context

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,31 @@ describe('Toolkit', () => {
})
})

describe('.run', () => {
it('runs the async function passed to it', async () => {
JasonEtco marked this conversation as resolved.
Show resolved Hide resolved
const spy = jest.fn(() => Promise.resolve('hi'))
const actual = await Toolkit.run(spy)
// Test that the function was called
expect(spy).toHaveBeenCalled()
// Make sure it was called with a Toolkit instance
expect((spy.mock.calls as any)[0][0]).toBeInstanceOf(Toolkit)
// Check that it returned a value as an async function
expect(actual).toBe('hi')
})

it('logs and fails when the function throws an error', async () => {
const err = new Error('Whoops!')
const exitFailure = jest.fn()

await Toolkit.run(async twolkit => {
twolkit.exit.failure = exitFailure
throw err
})

expect(exitFailure).toHaveBeenCalledTimes(1)
})
})

describe('#wrapLogger', () => {
it('wraps the provided logger and allows for a callable class', () => {
const logger = new Signale() as jest.Mocked<Signale>
Expand Down