Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Unit tests for loading/downloading a config file
Browse files Browse the repository at this point in the history
When we support the configuration file feature we have to
execute one more step in our workflow - checking if a Precaution
configuration file exists and downloading it from the repository.

Also, when we add new config options we should test them if we
use them correctly in our workflow.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
  • Loading branch information
MVrachev committed May 20, 2019
1 parent 18e3162 commit 47a40dc
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
3 changes: 2 additions & 1 deletion filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function isExcluded (filePath, excludes) {
/**
* Filters the listed files and returns only the files relevant to us
* @param {*} rawData the raw output from list files API call
* @param {String[]} excludes excludes globs provided by the user to filter files/folders
* @param {String[]} excludes array of exclusion rules in glob format provided by the user
* @return {Promise[]<any>} the filtered GitHub response
*/
function filterData (rawData, excludes) {
Expand All @@ -45,3 +45,4 @@ function filterData (rawData, excludes) {
}

module.exports.filterData = filterData
module.exports.isExcluded = isExcluded
5 changes: 3 additions & 2 deletions github_api_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ function inProgressAPIresponse (context, headSha) {
* Get Precaution config file contents as raw data,
* if that file exists.
* @param {import('probot').Context} context Probot context
* @param {String} customConfigPath Optional (default value config.configFilePath)
* @return {Promise<any>} GitHub response
* See https://developer.github.com/v3/repos/contents/#get-contents
*/
async function getConfigFile (context) {
async function getConfigFile (context, customConfigPath = config.configFilePath) {
let { owner, repo } = context.repo()
// GitHub doesn't provide "checkIfFileExists" API endpoint.
// That's why we should try to download the file and if we have
Expand All @@ -43,7 +44,7 @@ async function getConfigFile (context) {
return await context.github.repos.getContents({
owner,
repo,
path: config.configFilePath,
path: customConfigPath,
headers: { accept: rawMediaType }
})
} catch (error) {
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/pull_request.files.mix.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
{
"filename": "executable",
"status": "added"
},
{
"filename":"tests/func/funcTest.go"
},
{
"filename":"src/commands/command_test.go"
},
{
"filename":"src/cmd/main.py"
}
]
}
43 changes: 43 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,40 @@ describe('Precaution workflow', () => {
}))
})

test('Checks the downloading of a config file', async () => {
await app.receive(pullRequestOpenedEvent)

expect(github.repos.getContents).toHaveBeenCalledWith(expect.objectContaining({
owner: 'owner_login',
repo: 'repo_name',
path: config.configFilePath
}))
})

test('Check for a repo without config file', async () => {
const { getConfigFile } = require('../github_api_helper')
let context = {
repo: jest.fn().mockResolvedValue({
owner: 'owner_login',
repo: 'repo_name'
}),
github: {
repos: {
getContents: jest.fn(({ path }) => {
if (mockFiles.hasOwnProperty(path)) {
return Promise.resolve({ data: mockFiles[path] })
} else {
let error = new Error(path + ' fixture does not exist')
error.code = 404
throw error
}
})
}
}
}
expect(await getConfigFile(context, 'customConfig.json')).toEqual(null)
})

test('Check run rerequest event', async () => {
await app.receive(checkRunRerequestEvent)

Expand Down Expand Up @@ -230,6 +264,15 @@ describe('Precaution workflow', () => {
expect(github.repos.getContents).not.toHaveBeenCalledWith(expect.objectContaining({
path: 'executable'
}))
expect(github.repos.getContents).not.toHaveBeenCalledWith(expect.objectContaining({
path: 'tests/func/funcTest.go'
}))
expect(github.repos.getContents).not.toHaveBeenCalledWith(expect.objectContaining({
path: 'src/commands/command_test.go'
}))
expect(github.repos.getContents).not.toHaveBeenCalledWith(expect.objectContaining({
path: 'src/cmd/main.py'
}))
})

test('handles PRs with only deletions', async () => {
Expand Down

0 comments on commit 47a40dc

Please sign in to comment.