Skip to content

Commit

Permalink
feat(ci): added e2e test for gha event data
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelangarano committed Jan 5, 2024
1 parent e81f441 commit e47947d
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 33 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"pull_request": {
"head": {
"ref": "test-ref",
"sha": "test-sha"
},
"base": {
"ref": "test-ref",
"sha": "test-sha"
},
"issue_url": "test-issue",
"html_url": "test-html",
"title": "test-title"
},
"sender": {
"avatar_url": "test-avatar",
"html_url": "test-html"
}
}
14 changes: 13 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,16 @@ jobs:
- run: npm ci
- run: npm test
# - if: github.ref == 'refs/heads/master'
# run: npm run semantic-release
# run: npm run semantic-release
gha:
runs-on: ubuntu-latest
steps:
- name: Copy event file
- run: cp ./event.json /home/runner/work/_temp/_github_workflow/event.json

- name: Run gha e2e
uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
- run: npm run gha-e2e
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";",
"test": "npm run unit",
"unit": "mocha src/*-spec.js",
"gha-e2e": "mocha src/utils-e2e.js",
"semantic-release": "semantic-release pre && npm publish --access public && semantic-release post"
},
"release": {
Expand Down
51 changes: 51 additions & 0 deletions src/utils-e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict'

const la = require('lazy-ass')

/* eslint-env mocha */
describe('utils', () => {
const event = {
pull_request: {
head: {
ref: 'test-ref',
sha: 'test-sha'
},
base: {
ref: 'test-ref',
sha: 'test-sha'
},
issue_url: 'test-issue',
html_url: 'test-html',
title: 'test-title'
},
sender: {
avatar_url: 'test-avatar',
html_url: 'test-html'
}
}

const eventResult = {
headRef: event.pull_request.head.ref,
headhSha: event.pull_request.head.sha,
baseRef: event.pull_request.base.ref,
baseSha: event.pull_request.base.sha,
issueUrl: event.pull_request.issue_url,
htmlUrl: event.pull_request.html_url,
prTitle: event.pull_request.title,
senderAvatarUrl: event.sender.avatar_url,
senderHtmlUrl: event.sender.html_url
}

describe('E2E gha event data', () => {
const { getGhaEventData } = require('./utils')

it('returns event data if file path and gha env are truthy', () => {
const eventData = getGhaEventData(
process.env.GITHUB_EVENT_PATH,
process.env.GITHUB_ACTIONS
)

la(JSON.stringify(eventData) === JSON.stringify(eventResult), eventData)
})
})
})
64 changes: 32 additions & 32 deletions src/utils-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,38 @@ const fs = require('fs')

/* eslint-env mocha */
describe('utils', () => {
const event = {
pull_request: {
head: {
ref: 'test-ref',
sha: 'test-sha'
},
base: {
ref: 'test-ref',
sha: 'test-sha'
},
issue_url: 'test-issue',
html_url: 'test-html',
title: 'test-title'
},
sender: {
avatar_url: 'test-avatar',
html_url: 'test-html'
}
}

const eventResult = {
headRef: event.pull_request.head.ref,
headhSha: event.pull_request.head.sha,
baseRef: event.pull_request.base.ref,
baseSha: event.pull_request.base.sha,
issueUrl: event.pull_request.issue_url,
htmlUrl: event.pull_request.html_url,
prTitle: event.pull_request.title,
senderAvatarUrl: event.sender.avatar_url,
senderHtmlUrl: event.sender.html_url
}

describe('getFields', () => {
const { getFields } = require('./utils')

Expand Down Expand Up @@ -65,38 +97,6 @@ describe('utils', () => {
let readFileStub
const { getGhaEventData } = require('./utils')

const event = {
pull_request: {
head: {
ref: 'test-ref',
sha: 'test-sha'
},
base: {
ref: 'test-ref',
sha: 'test-sha'
},
issue_url: 'test-issue',
html_url: 'test-html',
title: 'test-title'
},
sender: {
avatar_url: 'test-avatar',
html_url: 'test-html'
}
}

const eventResult = {
headRef: event.pull_request.head.ref,
headhSha: event.pull_request.head.sha,
baseRef: event.pull_request.base.ref,
baseSha: event.pull_request.base.sha,
issueUrl: event.pull_request.issue_url,
htmlUrl: event.pull_request.html_url,
prTitle: event.pull_request.title,
senderAvatarUrl: event.sender.avatar_url,
senderHtmlUrl: event.sender.html_url
}

beforeEach(() => {
readFileStub = sinon
.stub(fs, 'readFileSync')
Expand Down

0 comments on commit e47947d

Please sign in to comment.