Skip to content

Commit 4d005d4

Browse files
committed
Unit tests & Adjustments for 3.7.0
1 parent dc667b3 commit 4d005d4

File tree

6 files changed

+143
-19
lines changed

6 files changed

+143
-19
lines changed

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ npm install @jamesives/github-pages-deploy-action
9696
It can then be imported into your project like so.
9797
9898
```javascript
99-
import run, {ActionInterface} from "github-pages-deploy-action";
99+
import run from "github-pages-deploy-action";
100100
```
101101

102102
Calling the functions directly will require you to pass in an object containing the variables found in the configuration section, you'll also need to provide a `workspace` with a path to your project.
@@ -109,11 +109,12 @@ run({
109109
branch: "gh-pages",
110110
folder: "build",
111111
repositoryName: "JamesIves/github-pages-deploy-action",
112+
silent: true,
112113
workspace: "src/project/location",
113114
});
114115
```
115116

116-
For more information regarding the [action interface please click here](https://github.com/JamesIves/github-pages-deploy-action/blob/dev/src/constants.ts#L7).
117+
For more information regarding the [action interface please click here](https://github.com/JamesIves/github-pages-deploy-action/blob/dev/src/constants.ts#L7).
117118

118119
## Configuration 📁
119120

@@ -335,8 +336,4 @@ If you'd like to enable action debugging you can set the `ACTIONS_STEP_DEBUG` en
335336

336337
This project would not be possible without all of our fantastic [contributors](https://github.com/JamesIves/github-pages-deploy-action/graphs/contributors).
337338

338-
<a href="https://github.com/JamesIves/github-pages-deploy-action/graphs/contributors">
339-
<img src="https://contributors-img.web.app/image?repo=JamesIves/github-pages-deploy-action" />
340-
</a>
341-
342339
If you'd like to support the maintenance and upkeep of this project you can [donate via GitHub Sponsors](https://github.com/sponsors/JamesIves). This project is distributed under the [MIT](https://github.com/JamesIves/github-pages-deploy-action/blob/dev/LICENSE) license.

__tests__/git.test.ts

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ jest.mock('@actions/io', () => ({
2323
}))
2424

2525
jest.mock('../src/execute', () => ({
26-
execute: jest.fn()
26+
__esModule: true,
27+
execute: jest.fn(),
2728
}))
2829

2930
describe('git', () => {
@@ -50,6 +51,33 @@ describe('git', () => {
5051
await init(action)
5152
expect(execute).toBeCalledTimes(7)
5253
})
54+
55+
it('should catch when a function throws an error', async () => {
56+
57+
(execute as jest.Mock).mockImplementationOnce(() => {
58+
throw new Error('Mocked throw');
59+
});
60+
61+
Object.assign(action, {
62+
silent: false,
63+
repositoryPath: 'JamesIves/github-pages-deploy-action',
64+
accessToken: '123',
65+
branch: 'branch',
66+
folder: '.',
67+
preserve: true,
68+
isTest: true,
69+
pusher: {
70+
name: 'asd',
71+
email: 'as@cat'
72+
}
73+
})
74+
75+
try {
76+
await init(action)
77+
} catch (error) {
78+
expect(error.message).toBe('There was an error initializing the repository: Mocked throw ❌');
79+
}
80+
})
5381
})
5482

5583
describe('generateBranch', () => {
@@ -68,6 +96,30 @@ describe('git', () => {
6896
await generateBranch(action)
6997
expect(execute).toBeCalledTimes(6)
7098
})
99+
100+
it('should catch when a function throws an error', async () => {
101+
102+
(execute as jest.Mock).mockImplementationOnce(() => {
103+
throw new Error('Mocked throw');
104+
});
105+
106+
Object.assign(action, {
107+
silent: false,
108+
accessToken: '123',
109+
branch: 'branch',
110+
folder: '.',
111+
pusher: {
112+
name: 'asd',
113+
email: 'as@cat'
114+
}
115+
})
116+
117+
try {
118+
await generateBranch(action)
119+
} catch (error) {
120+
expect(error.message).toBe('There was an error creating the deployment branch: There was an error switching to the base branch: Mocked throw ❌ ❌');
121+
}
122+
})
71123
})
72124

73125
describe('switchToBaseBranch', () => {
@@ -103,6 +155,32 @@ describe('git', () => {
103155
await switchToBaseBranch(action)
104156
expect(execute).toBeCalledTimes(1)
105157
})
158+
159+
160+
it('should catch when a function throws an error', async () => {
161+
162+
(execute as jest.Mock).mockImplementationOnce(() => {
163+
throw new Error('Mocked throw');
164+
});
165+
166+
Object.assign(action, {
167+
silent: false,
168+
baseBranch: '123',
169+
accessToken: '123',
170+
branch: 'branch',
171+
folder: '.',
172+
pusher: {
173+
name: 'asd',
174+
email: 'as@cat'
175+
}
176+
})
177+
178+
try {
179+
await switchToBaseBranch(action)
180+
} catch (error) {
181+
expect(error.message).toBe('There was an error switching to the base branch: Mocked throw ❌');
182+
}
183+
})
106184
})
107185

108186
describe('deploy', () => {
@@ -276,5 +354,30 @@ describe('git', () => {
276354
expect(rmRF).toBeCalledTimes(1)
277355
expect(response).toBe(Status.SKIPPED)
278356
})
357+
358+
it('should catch when a function throws an error', async () => {
359+
360+
(execute as jest.Mock).mockImplementationOnce(() => {
361+
throw new Error('Mocked throw');
362+
});
363+
364+
Object.assign(action, {
365+
silent: false,
366+
folder: 'assets',
367+
branch: 'branch',
368+
gitHubToken: '123',
369+
lfs: true,
370+
pusher: {
371+
name: 'asd',
372+
email: 'as@cat'
373+
}
374+
})
375+
376+
try {
377+
await deploy(action)
378+
} catch (error) {
379+
expect(error.message).toBe('The deploy step encountered an error: Mocked throw ❌');
380+
}
381+
})
279382
})
280383
})

__tests__/util.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ describe('util', () => {
321321
checkParameters(action)
322322
} catch (e) {
323323
expect(e.message).toMatch(
324-
`The ${action.folderPath} directory you're trying to deploy doesn't exist.`
324+
`The directory you're trying to deploy named notARealFolder doesn't exist. Please double check the path and any prerequisite build scripts and try again. ❗`
325325
)
326326
}
327327
})

src/constants.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface ActionInterface {
2424
email?: string
2525
/** The folder to deploy. */
2626
folder: string
27+
/** The auto generated folder path. */
2728
folderPath?: string
2829
/** GitHub deployment token. */
2930
gitHubToken?: string | null
@@ -53,6 +54,26 @@ export interface ActionInterface {
5354
workspace: string
5455
}
5556

57+
/** The minimum required values to run the action as a node module. */
58+
export interface NodeActionInterface {
59+
/** Deployment access token. */
60+
accessToken?: string | null
61+
/** The branch that the action should deploy to. */
62+
branch: string
63+
/** The folder to deploy. */
64+
folder: string
65+
/** GitHub deployment token. */
66+
gitHubToken?: string | null
67+
/** The repository path, for example JamesIves/github-pages-deploy-action. */
68+
repositoryName: string
69+
/** Determines if the action should run in silent mode or not. */
70+
silent: boolean
71+
/** Set to true if you're using an ssh client in your build step. */
72+
ssh?: boolean | null
73+
/** The folder where your deployment project lives. */
74+
workspace: string
75+
}
76+
5677
/* Required action data that gets initialized when running within the GitHub Actions environment. */
5778
export const action: ActionInterface = {
5879
accessToken: getInput('ACCESS_TOKEN'),
@@ -107,6 +128,7 @@ export const action: ActionInterface = {
107128
workspace: process.env.GITHUB_WORKSPACE || ''
108129
}
109130

131+
/** */
110132
export type RequiredActionParameters = Pick<
111133
ActionInterface,
112134
'accessToken' | 'gitHubToken' | 'ssh' | 'branch' | 'folder'

src/lib.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {exportVariable, info, setFailed} from '@actions/core'
2-
import {action, ActionInterface, Status} from './constants'
2+
import {ActionInterface, Status, NodeActionInterface} from './constants'
33
import {deploy, init} from './git'
44
import {
55
generateFolderPath,
@@ -13,7 +13,7 @@ import {
1313
* @param {object} configuration - The action configuration.
1414
*/
1515
export default async function run(
16-
configuration: ActionInterface
16+
configuration: ActionInterface | NodeActionInterface
1717
): Promise<void> {
1818
let status: Status = Status.RUNNING
1919

@@ -22,25 +22,25 @@ export default async function run(
2222
GitHub Pages Deploy Action 🚀
2323
2424
🚀 Getting Started Guide: https://github.com/marketplace/actions/deploy-to-github-pages
25-
FAQ/Wiki: https://github.com/JamesIves/github-pages-deploy-action/wiki
26-
🔧 Support: https://github.com/JamesIves/github-pages-deploy-action/issues
25+
Discussions / Q&A: https://github.com/JamesIves/github-pages-deploy-action/discussions
26+
🔧 Report a Bug: https://github.com/JamesIves/github-pages-deploy-action/issues
2727
⭐ Contribute: https://github.com/JamesIves/github-pages-deploy-action/blob/dev/CONTRIBUTING.md
28-
29-
📣 Maintained by James Ives (https://jamesiv.es)`)
28+
29+
📣 Maintained by James Ives (https://jamesiv.es)
30+
❤ Support: https://github.com/sponsors/JamesIves`)
3031

3132
info('Checking configuration and starting deployment… 🚦')
3233

33-
const settings = {
34-
...action,
34+
const settings: ActionInterface = {
3535
...configuration
3636
}
3737

38-
// Defines the folder paths
38+
// Defines the repository/folder paths and token types.
39+
// Also verifies that the action has all of the required parameters.
3940
settings.folderPath = generateFolderPath(settings)
4041

4142
checkParameters(settings)
4243

43-
// Defines the repository paths and token types.
4444
settings.repositoryPath = generateRepositoryPath(settings)
4545
settings.tokenType = generateTokenType(settings)
4646

src/util.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from 'path'
33
import {isDebug} from '@actions/core'
44
import {ActionInterface, RequiredActionParameters} from './constants'
55

6+
/* Replaces all instances of a match in a string. */
67
const replaceAll = (input: string, find: string, replace: string): string =>
78
input.split(find).join(replace)
89

@@ -49,6 +50,7 @@ const hasRequiredParameters = <K extends keyof RequiredActionParameters>(
4950
return Boolean(nonNullParams.length)
5051
}
5152

53+
/* Verifies the action has the required parameters to run, otherwise throw an error. */
5254
export const checkParameters = (action: ActionInterface): void => {
5355
if (!hasRequiredParameters(action, ['accessToken', 'gitHubToken', 'ssh'])) {
5456
throw new Error(
@@ -66,7 +68,7 @@ export const checkParameters = (action: ActionInterface): void => {
6668

6769
if (!existsSync(action.folderPath as string)) {
6870
throw new Error(
69-
`The ${action.folderPath} directory you're trying to deploy doesn't exist. ❗`
71+
`The directory you're trying to deploy named ${action.folderPath} doesn't exist. Please double check the path and any prerequisite build scripts and try again. ❗`
7072
)
7173
}
7274
}

0 commit comments

Comments
 (0)