From f86ccd3e89f7720e0132f647e681c90fcffb873d Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Thu, 3 Jun 2021 12:33:50 -0400 Subject: [PATCH] Add CircleCI tests and refactor testing --- src/ci_providers/provider_circleci.js | 19 +---- src/ci_providers/provider_githubactions.js | 10 --- src/ci_providers/provider_local.js | 10 --- src/ci_providers/provider_template.js | 10 --- test/providers/index.test.js | 83 +++++++------------ test/providers/provider_circleci.test.js | 78 +++++++++++++++++ test/providers/provider_githubactions.test.js | 2 +- 7 files changed, 111 insertions(+), 101 deletions(-) create mode 100644 test/providers/provider_circleci.test.js diff --git a/src/ci_providers/provider_circleci.js b/src/ci_providers/provider_circleci.js index 1aa4b4137..b3d5f4c94 100644 --- a/src/ci_providers/provider_circleci.js +++ b/src/ci_providers/provider_circleci.js @@ -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) { @@ -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 } @@ -71,16 +66,6 @@ function getServiceParams (inputs) { } module.exports = { - private: { - _getBuild, - _getBuildURL, - _getBranch, - _getJob, - _getPR, - _getService, - _getSHA, - _getSlug - }, detect, getServiceName, getServiceParams diff --git a/src/ci_providers/provider_githubactions.js b/src/ci_providers/provider_githubactions.js index 7a9bf0d38..c304208a3 100644 --- a/src/ci_providers/provider_githubactions.js +++ b/src/ci_providers/provider_githubactions.js @@ -97,16 +97,6 @@ function getServiceParams (inputs) { } module.exports = { - private: { - _getBuild, - _getBuildURL, - _getBranch, - _getJob, - _getPR, - _getService, - _getSHA, - _getSlug - }, detect, getServiceName, getServiceParams diff --git a/src/ci_providers/provider_local.js b/src/ci_providers/provider_local.js index a97de296b..c77238d60 100644 --- a/src/ci_providers/provider_local.js +++ b/src/ci_providers/provider_local.js @@ -110,16 +110,6 @@ function getServiceParams (inputs) { } module.exports = { - private: { - _getBuild, - _getBuildURL, - _getBranch, - _getJob, - _getPR, - _getService, - _getSHA, - _getSlug - }, detect, getServiceName, getServiceParams diff --git a/src/ci_providers/provider_template.js b/src/ci_providers/provider_template.js index c70fd3a07..80d4786e9 100644 --- a/src/ci_providers/provider_template.js +++ b/src/ci_providers/provider_template.js @@ -137,16 +137,6 @@ function getServiceParams (inputs) { } module.exports = { - private: { - _getBuild, - _getBuildURL, - _getBranch, - _getJob, - _getPR, - _getService, - _getSHA, - _getSlug - }, detect, getServiceName, getServiceParams diff --git a/test/providers/index.test.js b/test/providers/index.test.js index 32e51e0e9..dcca6887d 100644 --- a/test/providers/index.test.js +++ b/test/providers/index.test.js @@ -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') @@ -87,7 +42,18 @@ describe('CI Providers', () => { 'remote.origin.url'])).thenReturn({ stdout: 'git@github.com: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' ) }) @@ -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' ) }) diff --git a/test/providers/provider_circleci.test.js b/test/providers/provider_circleci.test.js new file mode 100644 index 000000000..d91125f09 --- /dev/null +++ b/test/providers/provider_circleci.test.js @@ -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: 'git@github.com: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: 'git@github.com: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) + }) +}) diff --git a/test/providers/provider_githubactions.test.js b/test/providers/provider_githubactions.test.js index 202501e25..354801109 100644 --- a/test/providers/provider_githubactions.test.js +++ b/test/providers/provider_githubactions.test.js @@ -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', () => {