Skip to content

Commit

Permalink
Merge pull request #116 from codecov/circleci-tests
Browse files Browse the repository at this point in the history
test: Add CircleCI tests and refactor testing
  • Loading branch information
thomasrockhu authored Jun 3, 2021
2 parents 4fec955 + f86ccd3 commit 21168e4
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 101 deletions.
19 changes: 2 additions & 17 deletions src/ci_providers/provider_circleci.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ function _getBranch (inputs) {

function _getSHA (inputs) {
const { args, envs } = inputs
try {
const sha = envs.CIRCLE_SHA1
return args.sha || sha
} catch (error) {
throw new Error(`There was an error getting the commit SHA: ${error}`)
}
return args.sha || envs.CIRCLE_SHA1
}

function _getSlug (inputs) {
Expand All @@ -38,7 +33,7 @@ function _getSlug (inputs) {
if (envs.CIRCLE_PROJECT_REPONAME !== '') {
slug = `${envs.CIRCLE_PROJECT_USERNAME}/${envs.CIRCLE_PROJECT_REPONAME}`
} else {
slug = `${envs.CIRCLE_REPOSITORY_URL}.git`
slug = `${envs.CIRCLE_REPOSITORY_URL.split(":")[1].split('.git')[0]}`
}
return args.slug || slug
}
Expand Down Expand Up @@ -71,16 +66,6 @@ function getServiceParams (inputs) {
}

module.exports = {
private: {
_getBuild,
_getBuildURL,
_getBranch,
_getJob,
_getPR,
_getService,
_getSHA,
_getSlug
},
detect,
getServiceName,
getServiceParams
Expand Down
10 changes: 0 additions & 10 deletions src/ci_providers/provider_githubactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,6 @@ function getServiceParams (inputs) {
}

module.exports = {
private: {
_getBuild,
_getBuildURL,
_getBranch,
_getJob,
_getPR,
_getService,
_getSHA,
_getSlug
},
detect,
getServiceName,
getServiceParams
Expand Down
10 changes: 0 additions & 10 deletions src/ci_providers/provider_local.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,6 @@ function getServiceParams (inputs) {
}

module.exports = {
private: {
_getBuild,
_getBuildURL,
_getBranch,
_getJob,
_getPR,
_getService,
_getSHA,
_getSlug
},
detect,
getServiceName,
getServiceParams
Expand Down
10 changes: 0 additions & 10 deletions src/ci_providers/provider_template.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,6 @@ function getServiceParams (inputs) {
}

module.exports = {
private: {
_getBuild,
_getBuildURL,
_getBranch,
_getJob,
_getPR,
_getService,
_getSHA,
_getSlug
},
detect,
getServiceName,
getServiceParams
Expand Down
83 changes: 30 additions & 53 deletions test/providers/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,59 +25,14 @@ describe('CI Providers', () => {
it('has a detect() method', () => {
expect(provider.detect).toBeInstanceOf(Function)
})
it('has a getService() method', () => {
expect(provider.private._getService).toBeInstanceOf(Function)
})
it('has a getServiceName() method', () => {
expect(provider.getServiceName).toBeInstanceOf(Function)
})
it('has a getServiceParams() method', () => {
expect(provider.getServiceParams).toBeInstanceOf(Function)
})
describe('getServiceParams()', () => {
it('has all properties set', () => {
props = ['branch', 'build', 'buildURL', 'commit', 'job', 'pr', 'service', 'slug']
const serviceParams = provider.getServiceParams(inputs)
it("has it's branch property set", () => {
expect(serviceParams.branch).toBe(
provider.private._getBranch(inputs)
)
})
it("has it's build property set", () => {
expect(serviceParams.build).toBe(
provider.private._getBuild(inputs)
)
})
it("has it's buildURL property set", () => {
expect(serviceParams.buildURL).toBe(
provider.private._getBuildURL(inputs)
)
})
it("has it's commit property set", () => {
expect(serviceParams.commit).toBe(
provider.private._getSHA(inputs)
)
})
it("has it's job property set", () => {
expect(serviceParams.job).toBe(
provider.private._getJob(inputs.envs)
)
})
it("has it's pr property set", () => {
expect(serviceParams.pr).toBe(provider.private._getPR(inputs))
})
it("has it's service property set", () => {
expect(serviceParams.service).toBe(
provider.private._getService(inputs)
)
})
it("has it's slug property set", () => {
expect(serviceParams.slug).toBe(
provider.private._getSlug(inputs)
)
})
})
it('has a getSlug() method', () => {
expect(provider.private._getSlug).toBeInstanceOf(Function)
for (const prop of props) {
expect(serviceParams).toHaveProperty(prop)
}
})

describe('getSlug()', () => {
it('can get the slug from a git url', () => {
const spawnSync = td.replace(childProcess, 'spawnSync')
Expand All @@ -87,7 +42,18 @@ describe('CI Providers', () => {
'remote.origin.url'])).thenReturn({
stdout: '[email protected]:testOrg/testRepo.git'
})
expect(provider.private._getSlug(inputs)).toBe(
td.when(spawnSync('git', [
'rev-parse',
'--abbrev-ref',
'HEAD'])).thenReturn({
stdout: 'main'
})
td.when(spawnSync('git', [
'rev-parse',
'HEAD'])).thenReturn({
stdout: 'testSHA'
})
expect(provider.getServiceParams(inputs).slug).toBe(
'testOrg/testRepo'
)
})
Expand All @@ -99,7 +65,18 @@ describe('CI Providers', () => {
'remote.origin.url'])).thenReturn({
stdout: 'http://github.com/testOrg/testRepo.git'
})
expect(provider.private._getSlug(inputs)).toEqual(
td.when(spawnSync('git', [
'rev-parse',
'--abbrev-ref',
'HEAD'])).thenReturn({
stdout: 'main'
})
td.when(spawnSync('git', [
'rev-parse',
'HEAD'])).thenReturn({
stdout: 'testSHA'
})
expect(provider.getServiceParams(inputs).slug).toBe(
'testOrg/testRepo'
)
})
Expand Down
78 changes: 78 additions & 0 deletions test/providers/provider_circleci.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const td = require('testdouble')
const childProcess = require('child_process')

const providerCircleci = require('../../src/ci_providers//provider_circleci')

describe('CircleCI Params', () => {
afterEach(function () {
td.reset()
})

it('does not run without CircleCI env variable', () => {
const inputs = {
args: {},
envs: {}
}
const detected = providerCircleci.detect(inputs.envs)
expect(detected).toBeFalsy()
})

it('gets correct params', () => {
const inputs = {
args: {},
envs: {
CI: true,
CIRCLECI: true,
CIRCLE_BRANCH: 'master',
CIRCLE_SHA1: 'testingsha',
CIRCLE_PROJECT_REPONAME: 'testRepo',
CIRCLE_PROJECT_USERNAME: 'testOrg',
CIRCLE_REPOSITORY_URL: '[email protected]:testOrg/testRepo.git',
CIRCLE_BUILD_NUM: 2,
CIRCLE_PR_NUMBER: 1,
CIRCLE_NODE_INDEX: 3
}
}
const expected = {
branch: 'master',
build: 2 ,
buildURL: '',
commit: 'testingsha',
job: 3,
pr: 1,
service: 'circleci',
slug: 'testOrg/testRepo'
}
const params = providerCircleci.getServiceParams(inputs)
expect(params).toMatchObject(expected)
})

it('gets correct slug when empty reponame', () => {
const inputs = {
args: {},
envs: {
CI: true,
CIRCLECI: true,
CIRCLE_BRANCH: 'master',
CIRCLE_SHA1: 'testingsha',
CIRCLE_PROJECT_REPONAME: '',
CIRCLE_REPOSITORY_URL: '[email protected]:testOrg/testRepo.git',
CIRCLE_BUILD_NUM: 2,
CIRCLE_PR_NUMBER: 1,
CIRCLE_NODE_INDEX: 3
}
}
const expected = {
branch: 'master',
build: 2 ,
buildURL: '',
commit: 'testingsha',
job: 3,
pr: 1,
service: 'circleci',
slug: 'testOrg/testRepo'
}
const params = providerCircleci.getServiceParams(inputs)
expect(params).toMatchObject(expected)
})
})
2 changes: 1 addition & 1 deletion test/providers/provider_githubactions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('GitHub Actions Params', () => {
}
}
const detected = providerGitHubactions.detect(inputs.envs)
expect(detected).toBe(false)
expect(detected).toBeFalsy()
})

it('gets correct params for a push event', () => {
Expand Down

0 comments on commit 21168e4

Please sign in to comment.