Skip to content

Commit

Permalink
#161 Allow baseSHA to be undefined (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
GRiMe2D authored Jul 6, 2022
1 parent 25c8232 commit 88a7b15
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 12 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@del-systems/swatcher",
"version": "1.2.17",
"version": "1.3.0",
"description": "Screenshot Watcher",
"main": "src/index.js",
"scripts": {
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ describe('ci should read all available env variables', () => {
{ GITHUB_EVENT_NAME: 'push' },
{ after: '12345', before: 'first_parent' },
'first_parent'
],
[
'headSha',
{ GITHUB_EVENT_NAME: 'workflow_dispatch', GITHUB_SHA: 'work sha' },
{ },
'work sha'
],
[
'baseSha',
{ GITHUB_EVENT_NAME: 'workflow_dispatch', GITHUB_SHA: 'work sha' },
{ },
undefined
]
])('`(await CI()).%s` from %p should return %p', async (property, env, payload, expected) => {
const resetFakedEnvVariables = fakeEnv(env)
Expand Down
33 changes: 27 additions & 6 deletions src/__tests__/generate_diff_command.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import generateDiffCommand from '../generate_diff_command'
import reportChanges from '../report_changes'

jest.mock('../ci', () => ({
__esModule: true,
default: async () => ({
baseSha: 'parent-sha',
headSha: 'pull-request-sha'
const ci = require('../ci')
jest.mock('../ci', () => {
const module = {
__esModule: true,
__baseSha: '',
__headSha: ''
}

module.default = async () => ({
baseSha: module.__baseSha,
headSha: module.__headSha
})
}))

return module
})

jest.mock('../report_changes')

Expand Down Expand Up @@ -58,8 +66,21 @@ jest.mock('../compare_pngs', () => ({
}))
}))

beforeEach(jest.clearAllMocks)

it('should properly detect only updated screenshots and check for equalness', async () => {
ci.__baseSha = 'parent-sha'
ci.__headSha = 'pull-request-sha'
await generateDiffCommand()

expect(reportChanges).toHaveBeenCalledTimes(1)
})

it('should abort if base SHA cannot be detected', async () => {
ci.__baseSha = null
ci.__headSha = 'pull-request-sha'

await generateDiffCommand()

expect(reportChanges).not.toHaveBeenCalled()
})
4 changes: 3 additions & 1 deletion src/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ class GithubActionsEnvironment {
this.baseSha = githubPayload.before
this.headSha = githubPayload.after
break
case 'workflow_dispatch':
this.headSha = process.env.GITHUB_SHA
}

if (!this.baseSha || !this.headSha) throw new Error('Base sha and head sha couldn\'t be resolved')
if (!this.baseSha && !this.headSha) throw new Error('Base sha and head sha couldn\'t be resolved')
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/generate_diff_command.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export default async function () {
const s3 = new S3()
const ci = await CI()

if (!ci.baseSha) {
console.warn('Cannot generate diffs when base SHA isn\'t available')
return
}

const headPaths = await listFiles(s3, ci.headSha)
const basePaths = await listFiles(s3, ci.baseSha)
const addedPaths = headPaths.filter(item => !basePaths.find(i => i.fsPath === item.fsPath))
Expand Down
2 changes: 1 addition & 1 deletion version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default '1.2.17'
export default '1.3.0'

0 comments on commit 88a7b15

Please sign in to comment.