From 0bbdc800b0dbde509fd710ac55ae4242f899b254 Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Thu, 3 Jun 2021 15:53:22 -0400 Subject: [PATCH 1/7] fix: Add more tests --- test/providers/provider_local.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/providers/provider_local.test.js b/test/providers/provider_local.test.js index 5ebb8c629..5a94a9121 100644 --- a/test/providers/provider_local.test.js +++ b/test/providers/provider_local.test.js @@ -39,6 +39,7 @@ describe('Local Params', () => { 'HEAD'])).thenReturn({ stdout: 'testSHA' }) + expect(provider.getServiceParams(inputs).slug).toBe('testOrg/testRepo') }) it('can get the slug from an http(s) url', () => { @@ -60,6 +61,7 @@ describe('Local Params', () => { 'HEAD'])).thenReturn({ stdout: 'testSHA' }) + expect(provider.getServiceParams(inputs).slug).toBe('testOrg/testRepo') }) }) }) From 1620be90ae22cc522d602cb351b7cabaffb173bb Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Sat, 5 Jun 2021 10:31:40 -0400 Subject: [PATCH 2/7] Add GitLab provider --- src/ci_providers/index.js | 2 + src/ci_providers/provider_gitlabci.js | 66 +++++++++ src/ci_providers/provider_local.js | 25 +--- src/ci_providers/provider_template.js | 9 +- src/helpers/parseSlug.js | 41 ++++++ test/providers/provider_gitlabci.test.js | 163 +++++++++++++++++++++++ 6 files changed, 284 insertions(+), 22 deletions(-) create mode 100644 src/ci_providers/provider_gitlabci.js create mode 100644 src/helpers/parseSlug.js create mode 100644 test/providers/provider_gitlabci.test.js diff --git a/src/ci_providers/index.js b/src/ci_providers/index.js index 9e2eaaca2..fffad7d88 100644 --- a/src/ci_providers/index.js +++ b/src/ci_providers/index.js @@ -1,5 +1,6 @@ const providerCircleci = require('./provider_circleci') const providerGitHubactions = require('./provider_githubactions') +const providerGitLabci = require('./provider_gitlabci') const providerJenkinsci = require('./provider_jenkinsci') const providerLocal = require('./provider_local') const providerTravisci = require('./provider_travisci') @@ -8,6 +9,7 @@ const providerTravisci = require('./provider_travisci') const providers = [ providerCircleci, providerGitHubactions, + providerGitLabci, providerJenkinsci, providerTravisci, providerLocal, diff --git a/src/ci_providers/provider_gitlabci.js b/src/ci_providers/provider_gitlabci.js new file mode 100644 index 000000000..e37607414 --- /dev/null +++ b/src/ci_providers/provider_gitlabci.js @@ -0,0 +1,66 @@ +const { parseSlugFromRemoteAddr } = require('../helpers/parseSlug') + +function detect (envs) { + return envs.GITLAB_CI +} + +function _getBuild (inputs) { + const { args, envs } = inputs + return args.build || envs.CI_BUILD_ID || envs.CI_JOB_ID || '' +} + +function _getBuildURL (inputs) { + return '' +} + +function _getBranch (inputs) { + const { args, envs } = inputs + return args.branch || envs.CI_BUILD_REF_NAME || envs.CI_COMMIT_REF_NAME || '' +} + +function _getJob (envs) { + return '' +} + +function _getPR (inputs) { + const { args } = inputs + return args.pr || '' +} + +function _getService () { + return 'gitlab' +} + +function getServiceName () { + return 'GitLab CI' +} + +function _getSHA (inputs) { + const { args, envs } = inputs + return args.sha || envs.CI_BUILD_REF || envs.CI_COMMIT_SHA || '' +} + +function _getSlug (inputs) { + const { args, envs } = inputs + const remoteAddr = envs.CI_BUILD_REPO || envs.CI_REPOSITORY_URL + return args.slug || envs.CI_PROJECT_PATH || parseSlugFromRemoteAddr(remoteAddr) || '' +} + +function getServiceParams (inputs) { + return { + branch: _getBranch(inputs), + build: _getBuild(inputs), + buildURL: _getBuildURL(inputs), + commit: _getSHA(inputs), + job: _getJob(inputs.envs), + pr: _getPR(inputs), + service: _getService(), + slug: _getSlug(inputs) + } +} + +module.exports = { + detect, + getServiceName, + getServiceParams +} diff --git a/src/ci_providers/provider_local.js b/src/ci_providers/provider_local.js index c77238d60..c6bd039e0 100644 --- a/src/ci_providers/provider_local.js +++ b/src/ci_providers/provider_local.js @@ -1,4 +1,5 @@ const childprocess = require('child_process') +const { parseSlug } = require('../helpers/parseSlug') function detect (envs) { return !envs.CI @@ -36,7 +37,8 @@ function _getJob (envs) { // eslint-disable-next-line no-unused-vars function _getPR (inputs) { - return '' + const { args } = inputs + return args.pr || '' } // This is the value that gets passed to the Codecov uploader @@ -64,25 +66,6 @@ function _getSHA (inputs) { } } -function _parseSlug (slug) { - // origin https://github.com/torvalds/linux.git (fetch) - - // git@github.com: codecov / uploader.git - - if (slug.match('http')) { - // Type is http(s) - const phaseOne = slug.split('//')[1].replace('.git', '') - const phaseTwo = phaseOne.split('/') - const cleanSlug = `${phaseTwo[1]}/${phaseTwo[2]}` - return cleanSlug - } else if (slug.match('@')) { - // Type is git - const cleanSlug = slug.split(':')[1].replace('.git', '') - return cleanSlug - } - throw new Error(`Unable to parse slug URL: ${slug}`) -} - function _getSlug (inputs) { const { args } = inputs try { @@ -90,7 +73,7 @@ function _getSlug (inputs) { .spawnSync('git', ['config', '--get', 'remote.origin.url']) .stdout.toString() .trimRight() - return args.slug || _parseSlug(slug) + return args.slug || parseSlug(slug) } catch (error) { throw new Error(`There was an error getting the slug from git: ${error}`) } diff --git a/src/ci_providers/provider_template.js b/src/ci_providers/provider_template.js index b3367ce42..ebccf6404 100644 --- a/src/ci_providers/provider_template.js +++ b/src/ci_providers/provider_template.js @@ -67,7 +67,14 @@ function _getJob (envs) { */ // eslint-disable-next-line no-unused-vars function _getPR (inputs) { - return '' + const { args } = inputs + try { + return args.pr || '' + } catch (error) { + throw new Error( + `There was an error getting the branch name from git: ${error}` + ) + } } /** diff --git a/src/helpers/parseSlug.js b/src/helpers/parseSlug.js new file mode 100644 index 000000000..af481cb69 --- /dev/null +++ b/src/helpers/parseSlug.js @@ -0,0 +1,41 @@ +var childProcess = require('child_process') + +function parseSlug (slug) { + // origin https://github.com/torvalds/linux.git (fetch) + + // git@github.com: codecov / uploader.git + + if (slug.match('http')) { + // Type is http(s) + const phaseOne = slug.split('//')[1].replace('.git', '') + const phaseTwo = phaseOne.split('/') + const cleanSlug = `${phaseTwo[1]}/${phaseTwo[2]}` + return cleanSlug + } else if (slug.match('@')) { + // Type is git + const cleanSlug = slug.split(':')[1].replace('.git', '') + return cleanSlug + } + throw new Error(`Unable to parse slug URL: ${slug}`) +} + +function parseSlugFromRemoteAddr (remoteAddr) { + let slug = "" + if (!remoteAddr) { + remote_addr = childProcess.execSync( + `git config --get remote.origin.url || hg paths default || echo ''` + ) + } + if (remoteAddr) { + slug = parseSlug(remoteAddr) + } + if (slug == "/") { + slug = "" + } + return slug +} + +module.exports = { + parseSlug, + parseSlugFromRemoteAddr, +} diff --git a/test/providers/provider_gitlabci.test.js b/test/providers/provider_gitlabci.test.js new file mode 100644 index 000000000..8f95802cd --- /dev/null +++ b/test/providers/provider_gitlabci.test.js @@ -0,0 +1,163 @@ +const td = require('testdouble') +const childProcess = require('child_process') + +const providerGitLabci = require('../../src/ci_providers//provider_gitlabci') + +describe('GitLabCI Params', () => { + afterEach(function () { + td.reset() + }) + + it('does not run without GitLabCI env variable', () => { + const inputs = { + args: {}, + envs: {} + } + const detected = providerGitLabci.detect(inputs.envs) + expect(detected).toBeFalsy() + }) + + it('gets correct empty params', () => { + const inputs = { + args: {}, + envs: { + GITLAB_CI: true, + } + } + const expected = { + branch: '', + build: '', + buildURL: '', + commit: '', + job: '', + pr: '', + service: 'gitlab', + slug: '' + } + const params = providerGitLabci.getServiceParams(inputs) + expect(params).toMatchObject(expected) + }) + + it('gets correct initial params', () => { + const inputs = { + args: {}, + envs: { + CI_BUILD_ID: 1, + CI_BUILD_REF: 'testingsha', + CI_BUILD_REF_NAME: 'main', + CI_COMMIT_REF_NAME: 'master', + CI_COMMIT_SHA: 'testsha', + CI_JOB_ID: 2, + CI_PROJECT_PATH: 'testOrg/testRepo', + GITLAB_CI: true, + } + } + const expected = { + branch: 'main', + build: 1 , + buildURL: '', + commit: 'testingsha', + job: '', + pr: '', + service: 'gitlab', + slug: 'testOrg/testRepo' + } + const params = providerGitLabci.getServiceParams(inputs) + expect(params).toMatchObject(expected) + }) + + it('gets correct second params', () => { + const inputs = { + args: {}, + envs: { + CI_COMMIT_REF_NAME: 'master', + CI_COMMIT_SHA: 'testsha', + CI_JOB_ID: 2, + CI_PROJECT_PATH: 'testOrg/testRepo', + GITLAB_CI: true, + } + } + const expected = { + branch: 'master', + build: 2 , + buildURL: '', + commit: 'testsha', + job: '', + pr: '', + service: 'gitlab', + slug: 'testOrg/testRepo' + } + const params = providerGitLabci.getServiceParams(inputs) + expect(params).toMatchObject(expected) + }) + + describe('getSlug()', () => { + const inputs = { + args: {}, + envs: { + GITLAB_CI: true + } + } + + it('can get the slug from http', () => { + inputs.envs['CI_BUILD_REPO'] = 'https://gitlab.com/testOrg/testRepo.git' + const params = providerGitLabci.getServiceParams(inputs) + expect(params.slug).toBe('testOrg/testRepo') + }) + + it('can get the slug from girl url', () => { + inputs.envs['CI_BUILD_REPO'] = 'git@gitlab.com:testOrg/testRepo.git' + const params = providerGitLabci.getServiceParams(inputs) + expect(params.slug).toBe('testOrg/testRepo') + }) + + it('can get the slug from git config', () => { + inputs.envs['CI_BUILD_REPO'] = '' + const execSync = td.replace(childProcess, 'execSync') + td.when(execSync( + `git config --get remote.origin.url` + )).thenReturn("https://gitlab.com/testOrg/testRepo.git") + + const params = providerGitLabci.getServiceParams(inputs) + expect(params.slug).toBe('testOrg/testRepo') + }) + + it('can get the slug from git config as /', () => { + inputs.envs['CI_BUILD_REPO'] = '' + const execSync = td.replace(childProcess, 'execSync') + td.when(execSync(`git config --get remote.origin.url || hg paths default || echo ''`) + ).thenReturn('https://gitlab.com//') + + const params = providerGitLabci.getServiceParams(inputs) + expect(params.slug).toBe('') + }) + }) + + it('gets correct params for overrides', () => { + const inputs = { + args: { + branch: 'branch', + build: 3, + pr: '2', + sha: 'testsha', + slug: 'testOrg/testRepo' + }, + envs: { + GITLAB_CI: true, + } + } + const expected = { + branch: 'branch', + build: 3, + buildURL: '', + commit: 'testsha', + job: '', + pr: '2', + service: 'gitlab', + slug: 'testOrg/testRepo' + } + + const params = providerGitLabci.getServiceParams(inputs) + expect(params).toMatchObject(expected) + }) +}) From e9c9b64b9a5da0698ad37ff9d4f1fabc0c865068 Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Sat, 5 Jun 2021 11:03:04 -0400 Subject: [PATCH 3/7] fix: Update tests --- src/helpers/parseSlug.js | 2 +- test/providers/index.test.js | 2 ++ test/providers/provider_gitlabci.test.js | 6 ++++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/helpers/parseSlug.js b/src/helpers/parseSlug.js index af481cb69..af1aa44a5 100644 --- a/src/helpers/parseSlug.js +++ b/src/helpers/parseSlug.js @@ -22,7 +22,7 @@ function parseSlug (slug) { function parseSlugFromRemoteAddr (remoteAddr) { let slug = "" if (!remoteAddr) { - remote_addr = childProcess.execSync( + remoteAddr = childProcess.execSync( `git config --get remote.origin.url || hg paths default || echo ''` ) } diff --git a/test/providers/index.test.js b/test/providers/index.test.js index 7a4bea80b..612aac846 100644 --- a/test/providers/index.test.js +++ b/test/providers/index.test.js @@ -14,12 +14,14 @@ describe('CI Providers', () => { args: {}, envs: { CI: true, + CI_PROJECT_PATH: 'testOrg/testRepo', CIRCLE_PROJECT_REPONAME: 'testRepo', CIRCLE_PROJECT_USERNAME: 'testOrg', CIRCLE_SHA1: 'testingSHA', GITHUB_ACTIONS: true, GITHUB_REF: 'refs/heads/test', GITHUB_REPOSITORY: 'testOrg/testRepo', + GITLAB_CI: true, JENKINS_URL: 'https://example.com', SHIPPABLE: true, TRAVIS: true, diff --git a/test/providers/provider_gitlabci.test.js b/test/providers/provider_gitlabci.test.js index 8f95802cd..eca1dc097 100644 --- a/test/providers/provider_gitlabci.test.js +++ b/test/providers/provider_gitlabci.test.js @@ -8,6 +8,7 @@ describe('GitLabCI Params', () => { td.reset() }) + /* it('does not run without GitLabCI env variable', () => { const inputs = { args: {}, @@ -90,6 +91,7 @@ describe('GitLabCI Params', () => { const params = providerGitLabci.getServiceParams(inputs) expect(params).toMatchObject(expected) }) + */ describe('getSlug()', () => { const inputs = { @@ -115,7 +117,7 @@ describe('GitLabCI Params', () => { inputs.envs['CI_BUILD_REPO'] = '' const execSync = td.replace(childProcess, 'execSync') td.when(execSync( - `git config --get remote.origin.url` + `git config --get remote.origin.url || hg paths default || echo ''` )).thenReturn("https://gitlab.com/testOrg/testRepo.git") const params = providerGitLabci.getServiceParams(inputs) @@ -126,7 +128,7 @@ describe('GitLabCI Params', () => { inputs.envs['CI_BUILD_REPO'] = '' const execSync = td.replace(childProcess, 'execSync') td.when(execSync(`git config --get remote.origin.url || hg paths default || echo ''`) - ).thenReturn('https://gitlab.com//') + ).thenReturn('git@gitlab.com:/') const params = providerGitLabci.getServiceParams(inputs) expect(params.slug).toBe('') From 9e67223bcc9f73e16bf4751da831019b6d7b1b77 Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Sat, 5 Jun 2021 11:32:40 -0400 Subject: [PATCH 4/7] feat: Appveyor provider --- src/ci_providers/index.js | 2 + src/ci_providers/provider_appveyorci.js | 72 ++++++++++++++++ src/helpers/parseSlug.js | 3 + test/providers/provider_appveyorci.test.js | 97 ++++++++++++++++++++++ test/providers/provider_gitlabci.test.js | 2 - 5 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 src/ci_providers/provider_appveyorci.js create mode 100644 test/providers/provider_appveyorci.test.js diff --git a/src/ci_providers/index.js b/src/ci_providers/index.js index fffad7d88..6287aa7e1 100644 --- a/src/ci_providers/index.js +++ b/src/ci_providers/index.js @@ -1,3 +1,4 @@ +const providerAppveyorci = require('./provider_appveyorci') const providerCircleci = require('./provider_circleci') const providerGitHubactions = require('./provider_githubactions') const providerGitLabci = require('./provider_gitlabci') @@ -7,6 +8,7 @@ const providerTravisci = require('./provider_travisci') // Please make sure provider_local is last const providers = [ + providerAppveyorci, providerCircleci, providerGitHubactions, providerGitLabci, diff --git a/src/ci_providers/provider_appveyorci.js b/src/ci_providers/provider_appveyorci.js new file mode 100644 index 000000000..bd17227cc --- /dev/null +++ b/src/ci_providers/provider_appveyorci.js @@ -0,0 +1,72 @@ +function detect (envs) { + return (envs.CI == 'true' || envs.CI == 'True') && (envs.APPVEYOR == 'true' || envs.APPVEYOR == 'True') +} + +function _getBuild (inputs) { + const { args, envs } = inputs + return args.build || encodeURIComponent(envs.APPVEYOR_JOB_ID) +} + +function _getBuildURL (inputs) { + const { envs } = inputs + if (envs.APPVEYOR_URL && envs.APPVEYOR_REPO_NAME && envs.APPVEYOR_BUILD_ID && envs.APPVEYOR_JOB_ID) { + return encodeURIComponent( + `${envs.APPVEYOR_URL}/project/${envs.APPVEYOR_REPO_NAME}/builds/${envs.APPVEYOR_BUILD_ID}/job/${envs.APPVEYOR_JOB_ID}` + ) + } + return '' +} + +function _getBranch (inputs) { + const { args, envs } = inputs + return args.branch || envs.APPVEYOR_REPO_BRANCH || '' +} + +function _getJob (envs) { + if (envs.APPVEYOR_ACCOUNT_NAME && envs.APPVEYOR_PROJECT_SLUG && envs.APPVEYOR_BUILD_VERSION) { + return `${envs.APPVEYOR_ACCOUNT_NAME}%2F${envs.APPVEYOR_PROJECT_SLUG}%2F${envs.APPVEYOR_BUILD_VERSION}` + } + return '' +} + +function _getPR (inputs) { + const { args, envs } = inputs + return args.pr || envs.APPVEYOR_PULL_REQUEST_NUMBER || '' +} + +function _getService () { + return 'appveyor' +} + +function getServiceName () { + return 'Appveyor CI' +} + +function _getSHA (inputs) { + const { args, envs } = inputs + return args.sha || envs.APPVEYOR_REPO_COMMIT || '' +} + +function _getSlug (inputs) { + const { args, envs } = inputs + return args.slug || envs.APPVEYOR_REPO_NAME || '' +} + +function getServiceParams (inputs) { + return { + branch: _getBranch(inputs), + build: _getBuild(inputs), + buildURL: _getBuildURL(inputs), + commit: _getSHA(inputs), + job: _getJob(inputs.envs), + pr: _getPR(inputs), + service: _getService(), + slug: _getSlug(inputs) + } +} + +module.exports = { + detect, + getServiceName, + getServiceParams +} diff --git a/src/helpers/parseSlug.js b/src/helpers/parseSlug.js index af1aa44a5..bbbf39a98 100644 --- a/src/helpers/parseSlug.js +++ b/src/helpers/parseSlug.js @@ -4,6 +4,9 @@ function parseSlug (slug) { // origin https://github.com/torvalds/linux.git (fetch) // git@github.com: codecov / uploader.git + if (typeof slug != 'string') { + return '' + } if (slug.match('http')) { // Type is http(s) diff --git a/test/providers/provider_appveyorci.test.js b/test/providers/provider_appveyorci.test.js new file mode 100644 index 000000000..2138aeb9c --- /dev/null +++ b/test/providers/provider_appveyorci.test.js @@ -0,0 +1,97 @@ +const td = require('testdouble') +const childProcess = require('child_process') + +const providerAppveyorci = require('../../src/ci_providers//provider_appveyorci') + +describe('AppveyorCI Params', () => { + afterEach(function () { + td.reset() + }) + + it('does not run without AppveyorCI env variable', () => { + const inputs = { + args: {}, + envs: {} + } + let detected = providerAppveyorci.detect(inputs.envs) + expect(detected).toBeFalsy() + + inputs.envs['CI'] = 'true' + detected = providerAppveyorci.detect(inputs.envs) + expect(detected).toBeFalsy() + + inputs.envs['CI'] = 'True' + detected = providerAppveyorci.detect(inputs.envs) + expect(detected).toBeFalsy() + + inputs.envs['CI'] = 'false' + inputs.envs['APPVEYOR'] = 'true' + detected = providerAppveyorci.detect(inputs.envs) + expect(detected).toBeFalsy() + + inputs.envs['APPVEYOR'] = 'True' + detected = providerAppveyorci.detect(inputs.envs) + expect(detected).toBeFalsy() + }) + + it('gets correct params on push', () => { + const inputs = { + args: {}, + envs: { + APPVEYOR: 'true', + APPVEYOR_ACCOUNT_NAME: 'testOrg', + APPVEYOR_BUILD_ID: '2', + APPVEYOR_BUILD_VERSION: '3', + APPVEYOR_JOB_ID: '1', + APPVEYOR_PROJECT_SLUG: 'testRepo', + APPVEYOR_PULL_REQUEST_NUMBER: 4, + APPVEYOR_REPO_BRANCH: 'main', + APPVEYOR_REPO_COMMIT: 'testingsha', + APPVEYOR_REPO_NAME: 'testOrg/testRepo', + APPVEYOR_URL: 'https://appveyor.com', + CI: 'true', + } + } + const expected = { + branch: 'main', + build: '1', + buildURL: 'https%3A%2F%2Fappveyor.com%2Fproject%2FtestOrg%2FtestRepo%2Fbuilds%2F2%2Fjob%2F1', + commit: 'testingsha', + job: 'testOrg%2FtestRepo%2F3', + pr: 4, + service: 'appveyor', + slug: 'testOrg/testRepo' + } + const params = providerAppveyorci.getServiceParams(inputs) + expect(params).toMatchObject(expected) + }) + + it('gets correct params for overrides', () => { + const inputs = { + args: { + branch: 'branch', + build: 3, + pr: '2', + sha: 'testsha', + slug: 'testOrg/testRepo' + }, + envs: { + APPVEYOR: 'true', + CI: 'true', + } + } + const expected = { + branch: 'branch', + build: 3, + buildURL: '', + commit: 'testsha', + job: '', + pr: '2', + service: 'appveyor', + slug: 'testOrg/testRepo' + } + + const params = providerAppveyorci.getServiceParams(inputs) + expect(params).toMatchObject(expected) + }) +}) diff --git a/test/providers/provider_gitlabci.test.js b/test/providers/provider_gitlabci.test.js index eca1dc097..7a8162ef0 100644 --- a/test/providers/provider_gitlabci.test.js +++ b/test/providers/provider_gitlabci.test.js @@ -8,7 +8,6 @@ describe('GitLabCI Params', () => { td.reset() }) - /* it('does not run without GitLabCI env variable', () => { const inputs = { args: {}, @@ -91,7 +90,6 @@ describe('GitLabCI Params', () => { const params = providerGitLabci.getServiceParams(inputs) expect(params).toMatchObject(expected) }) - */ describe('getSlug()', () => { const inputs = { From 4255fe94c1ae01bc84eaffdfa022ff6ede384176 Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Sat, 5 Jun 2021 18:56:32 -0400 Subject: [PATCH 5/7] feat: Azure Provider --- src/ci_providers/index.js | 2 + src/ci_providers/provider_azurepipelines.js | 105 +++++++++++++ test/helpers/validate.test.js | 30 +++- test/providers/index.test.js | 1 + .../providers/provider_azurepipelines.test.js | 143 ++++++++++++++++++ test/providers/provider_gitlabci.test.js | 11 ++ 6 files changed, 285 insertions(+), 7 deletions(-) create mode 100644 src/ci_providers/provider_azurepipelines.js create mode 100644 test/providers/provider_azurepipelines.test.js diff --git a/src/ci_providers/index.js b/src/ci_providers/index.js index 6287aa7e1..fa1e656d7 100644 --- a/src/ci_providers/index.js +++ b/src/ci_providers/index.js @@ -1,4 +1,5 @@ const providerAppveyorci = require('./provider_appveyorci') +const providerAzurepipelines = require('./provider_azurepipelines') const providerCircleci = require('./provider_circleci') const providerGitHubactions = require('./provider_githubactions') const providerGitLabci = require('./provider_gitlabci') @@ -9,6 +10,7 @@ const providerTravisci = require('./provider_travisci') // Please make sure provider_local is last const providers = [ providerAppveyorci, + providerAzurepipelines, providerCircleci, providerGitHubactions, providerGitLabci, diff --git a/src/ci_providers/provider_azurepipelines.js b/src/ci_providers/provider_azurepipelines.js new file mode 100644 index 000000000..6f152e6d5 --- /dev/null +++ b/src/ci_providers/provider_azurepipelines.js @@ -0,0 +1,105 @@ +var childProcess = require('child_process') +var { log } = require('../helpers/logger') + +function detect (envs) { + return !!envs.SYSTEM_TEAMFOUNDATIONSERVERURI +} + +function _getBuild (inputs) { + const { args, envs } = inputs + return args.build || envs.BUILD_BUILDNUMBER || '' +} + +function _getBuildURL (inputs) { + const { args, envs } = inputs + if (envs.SYSTEM_TEAMPROJECT && envs.BUILD_BUILDID) { + return encodeURIComponent(`${envs.SYSTEM_TEAMFOUNDATIONSERVERURI}${envs.SYSTEM_TEAMPROJECT}/_build/results?buildId=${envs.BUILD_BUILDID}`) + } + return '' +} + +function _getBranch (inputs) { + const { args, envs } = inputs + let branch = '' + if (envs.BUILD_SOURCEBRANCH) { + branch = envs.BUILD_SOURCEBRANCH.replace('refs/heads/', '') + } + return args.branch || branch +} + +function _getJob (envs) { + return envs.BUILD_BUILDID || '' +} + +function _getPR (inputs) { + const { args, envs } = inputs + return args.pr || envs.SYSTEM_PULLREQUEST_PULLREQUESTNUMBER || envs.SYSTEM_PULLREQUEST_PULLREQUESTID || '' +} + +function _getService () { + return 'azure_pipelines' +} + +function getServiceName () { + return 'Azure Pipelines' +} + +function _getSHA (inputs) { + const { args, envs } = inputs + let commit = envs.BUILD_SOURCEVERSION + + if (_getPR(inputs)) { + const mergeCommitRegex = /^[a-z0-9]{40} [a-z0-9]{40}$/ + const mergeCommitMessage = childProcess.execSync( + `git show --no-patch --format="%P"` + ) + if (mergeCommitRegex.exec(mergeCommitMessage)) { + const mergeCommit = mergeCommitMessage.split(" ")[1] + log(` Fixing merge commit SHA ${commit} -> ${mergeCommit}`) + commit = mergeCommit + } + } + + return args.sha || commit || '' +} + +function _getProject (inputs) { + const { envs } = inputs + return envs.SYSTEM_TEAMPROJECT || '' +} + +function _getServerURI (inputs) { + const { envs } = inputs + return envs.SYSTEM_TEAMFOUNDATIONSERVERURI +} + +function _getSlug (inputs) { + const { args } = inputs + return args.slug || '' +} +/** + * Generates and return the serviceParams object + * + * @param {args: {}, envs: {}} inputs an object of arguments and enviromental variable key/value pairs + * @returns { branch: String, build: String, buildURL: String, commit: String, job: String, pr: String, service: String, slug: String} + */ +function getServiceParams (inputs) { + return { + branch: _getBranch(inputs), + build: _getBuild(inputs), + buildURL: _getBuildURL(inputs), + commit: _getSHA(inputs), + job: _getJob(inputs.envs), + pr: _getPR(inputs), + project: _getProject(inputs), + server_uri: _getServerURI(inputs), + service: _getService(), + slug: _getSlug(inputs) + } +} + +module.exports = { + detect, + getServiceName, + getServiceParams +} diff --git a/test/helpers/validate.test.js b/test/helpers/validate.test.js index 08b1190f6..88f0dad75 100644 --- a/test/helpers/validate.test.js +++ b/test/helpers/validate.test.js @@ -10,6 +10,18 @@ describe('Input Validators', function () { }) }) + describe('URLs', function () { + it('Returns true with a valid URL', function () { + expect(validate.validateURL('https://codecov.io')).toBe(true) + }) + it('Returns false with an invalid URL', function () { + expect(validate.validateURL('not.a.URL.com')).toBe(false) + }) + it('Returns false with an empty URL', function () { + expect(validate.validateURL('')).toBe(false) + }) + }) + describe('Flags', function () { it('Should pass without a dash', function () { expect(validate.validateFlags('moo')).toBe(true) @@ -23,15 +35,19 @@ describe('Input Validators', function () { }) }) - describe('URLs', function () { - it('Returns true with a valid URL', function () { - expect(validate.validateURL('https://codecov.io')).toBe(true) + describe('FileNamePath', function () { + it('Should pass with an absolute path', function () { + expect(validate.validateFileNamePath('/path/to/file/1.txt')).toBe(true) }) - it('Returns false with an invalid URL', function () { - expect(validate.validateURL('not.a.URL.com')).toBe(false) + it('Should pass with a relative path', function () { + expect(validate.validateFileNamePath('./path/to/file/1.txt')).toBe(true) }) - it('Returns false with an empty URL', function () { - expect(validate.validateURL('')).toBe(false) + + it('Should fail with spaces', function () { + expect(validate.validateFileNamePath('/path to/file')).toBe(false) + }) + it('Should fail with other characters', function () { + expect(validate.validateFileNamePath('/path{}to/file')).toBe(false) }) }) }) diff --git a/test/providers/index.test.js b/test/providers/index.test.js index 612aac846..39d090107 100644 --- a/test/providers/index.test.js +++ b/test/providers/index.test.js @@ -24,6 +24,7 @@ describe('CI Providers', () => { GITLAB_CI: true, JENKINS_URL: 'https://example.com', SHIPPABLE: true, + SYSTEM_TEAMFOUNDATIONSERVERURI: 'https://example.azure.com', TRAVIS: true, TRAVIS_REPO_SLUG: 'testOrg/testRepo', } diff --git a/test/providers/provider_azurepipelines.test.js b/test/providers/provider_azurepipelines.test.js new file mode 100644 index 000000000..9385a50e3 --- /dev/null +++ b/test/providers/provider_azurepipelines.test.js @@ -0,0 +1,143 @@ +const td = require('testdouble') +const childProcess = require('child_process') + +const providerAzurepipelines = require('../../src/ci_providers//provider_azurepipelines') + +describe('Jenkins CI Params', () => { + afterEach(function () { + td.reset() + }) + + it('does not run without AzurePipelines env variable', () => { + const inputs = { + args: {}, + envs: {} + } + detected = providerAzurepipelines.detect(inputs.envs) + expect(detected).toBeFalsy() + }) + + it('gets correct params on pr number', () => { + const inputs = { + args: {}, + envs: { + BUILD_BUILDNUMBER: 1, + BUILD_BUILDID: 2, + BUILD_SOURCEBRANCH: 'refs/heads/main', + BUILD_SOURCEVERSION: 'testingsha', + SYSTEM_BUILD_BUILDID: 1, + SYSTEM_PULLREQUEST_PULLREQUESTNUMBER: 3, + SYSTEM_TEAMFOUNDATIONSERVERURI: 'https://example.azure.com', + SYSTEM_TEAMPROJECT: 'testOrg', + } + } + const expected = { + branch: 'main', + build: 1, + buildURL: 'https%3A%2F%2Fexample.azure.comtestOrg%2F_build%2Fresults%3FbuildId%3D2', + commit: 'testingsha', + job: 2, + pr: 3, + project: 'testOrg', + server_uri: 'https://example.azure.com', + service: 'azure_pipelines', + slug: '' + } + const params = providerAzurepipelines.getServiceParams(inputs) + expect(params).toMatchObject(expected) + }) + + it('gets correct params on pr id', () => { + const inputs = { + args: {}, + envs: { + BUILD_BUILDNUMBER: 1, + BUILD_BUILDID: 2, + BUILD_SOURCEBRANCH: 'refs/heads/main', + BUILD_SOURCEVERSION: 'testingsha', + SYSTEM_BUILD_BUILDID: 1, + SYSTEM_PULLREQUEST_PULLREQUESTID: 3, + SYSTEM_TEAMFOUNDATIONSERVERURI: 'https://example.azure.com', + SYSTEM_TEAMPROJECT: 'testOrg', + } + } + const expected = { + branch: 'main', + build: 1, + buildURL: 'https%3A%2F%2Fexample.azure.comtestOrg%2F_build%2Fresults%3FbuildId%3D2', + commit: 'testingsha', + job: 2, + pr: 3, + project: 'testOrg', + server_uri: 'https://example.azure.com', + service: 'azure_pipelines', + slug: '' + } + const params = providerAzurepipelines.getServiceParams(inputs) + expect(params).toMatchObject(expected) + }) + + it('gets correct params on merge', () => { + const inputs = { + args: {}, + envs: { + BUILD_BUILDNUMBER: 1, + BUILD_BUILDID: 2, + BUILD_SOURCEBRANCH: 'refs/heads/main', + BUILD_SOURCEVERSION: 'testingsha', + SYSTEM_BUILD_BUILDID: 1, + SYSTEM_PULLREQUEST_PULLREQUESTID: 3, + SYSTEM_TEAMFOUNDATIONSERVERURI: 'https://example.azure.com', + SYSTEM_TEAMPROJECT: 'testOrg', + } + } + const expected = { + branch: 'main', + build: 1, + buildURL: 'https%3A%2F%2Fexample.azure.comtestOrg%2F_build%2Fresults%3FbuildId%3D2', + commit: 'testingmergecommitsha2345678901234567890', + job: 2, + pr: 3, + project: 'testOrg', + server_uri: 'https://example.azure.com', + service: 'azure_pipelines', + slug: '' + } + const execSync = td.replace(childProcess, 'execSync') + td.when(execSync( + `git show --no-patch --format="%P"` + )).thenReturn("testingsha123456789012345678901234567890 testingmergecommitsha2345678901234567890") + const params = providerAzurepipelines.getServiceParams(inputs) + expect(params).toMatchObject(expected) + }) + + it('gets correct params for overrides', () => { + const inputs = { + args: { + branch: 'branch', + build: 3, + pr: '2', + sha: 'testsha', + slug: 'testOrg/testRepo' + }, + envs: { + SYSTEM_TEAMFOUNDATIONSERVERURI: 'https://example.azure.com', + } + } + const expected = { + branch: 'branch', + build: 3, + buildURL: '', + commit: 'testsha', + job: '', + pr: '2', + project: '', + server_uri: 'https://example.azure.com', + service: 'azure_pipelines', + slug: 'testOrg/testRepo' + } + + const params = providerAzurepipelines.getServiceParams(inputs) + expect(params).toMatchObject(expected) + }) +}) diff --git a/test/providers/provider_gitlabci.test.js b/test/providers/provider_gitlabci.test.js index 7a8162ef0..5d716a4db 100644 --- a/test/providers/provider_gitlabci.test.js +++ b/test/providers/provider_gitlabci.test.js @@ -131,6 +131,17 @@ describe('GitLabCI Params', () => { const params = providerGitLabci.getServiceParams(inputs) expect(params.slug).toBe('') }) + + it('can handle no remote origin url', () => { + inputs.envs['CI_BUILD_REPO'] = '' + const execSync = td.replace(childProcess, 'execSync') + td.when(execSync( + `git config --get remote.origin.url || hg paths default || echo ''` + )).thenReturn("") + + const params = providerGitLabci.getServiceParams(inputs) + expect(params.slug).toBe('') + }) }) it('gets correct params for overrides', () => { From b238723f272a3496737cb31d3185dd9e830a2233 Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Sat, 5 Jun 2021 23:37:41 -0400 Subject: [PATCH 6/7] fix: Remove clean call --- test/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.test.js b/test/index.test.js index cc28da943..0d6e3a84b 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -89,6 +89,7 @@ describe('Uploader Core', function () { expect(result).toEqual({ status: 'success', resultURL: 'https://results.codecov.io' }) }, 30000) }) + it('Can upload with parent sha', async function () { process.env.CI = 'true' process.env.CIRCLECI = 'true' @@ -162,7 +163,6 @@ describe('Uploader Core', function () { url: 'https://codecov.io', dryRun: true, dir: './test/fixtures/other', - clean: true, }) expect(log).toHaveBeenCalledWith(expect.stringMatching(/<<<<<< network/)) }) From c723ad7c0195a88da3abef57fdc226a7b5d3fd22 Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Wed, 9 Jun 2021 11:57:01 -0400 Subject: [PATCH 7/7] refactor: Refactor tests and add provider test template --- CONTRIBUTING.md | 4 +- src/ci_providers/provider_gitlabci.js | 2 +- src/ci_providers/provider_local.js | 2 +- src/helpers/{parseSlug.js => git.js} | 1 - test/providers/index.test.js | 33 +---- test/providers/provider_appveyorci.test.js | 76 ++++++++--- .../providers/provider_azurepipelines.test.js | 42 +++++- test/providers/provider_circleci.test.js | 28 +++- test/providers/provider_githubactions.test.js | 54 ++++++-- test/providers/provider_gitlabci.test.js | 27 +++- test/providers/provider_jenkinsci.test.js | 50 +++++-- test/providers/provider_local.test.js | 29 ++-- test/providers/provider_template.test.js | 129 ++++++++++++++++++ test/providers/provider_travisci.test.js | 41 ++++-- 14 files changed, 407 insertions(+), 111 deletions(-) rename src/helpers/{parseSlug.js => git.js} (99%) create mode 100644 test/providers/provider_template.test.js diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 294f09d5a..3b03a306d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -4,4 +4,6 @@ 2. Collect the needed enviromental variables for each section (you can use provider_circleci.js if you get confused) 3. Copy provider_template.js and fill it in. 4. Add your new providers list in https://github.com/codecov/uploader/blob/master/src/ci_providers/index.js -5. Open a PR and we'll take a look! +5. Copy test/providers/provider_template.test.js and fill it in. +6. Ensure 100% code coverage on the new provider code. +7. Open a PR and we'll take a look! diff --git a/src/ci_providers/provider_gitlabci.js b/src/ci_providers/provider_gitlabci.js index e37607414..01eda0cba 100644 --- a/src/ci_providers/provider_gitlabci.js +++ b/src/ci_providers/provider_gitlabci.js @@ -1,4 +1,4 @@ -const { parseSlugFromRemoteAddr } = require('../helpers/parseSlug') +const { parseSlugFromRemoteAddr } = require('../helpers/git') function detect (envs) { return envs.GITLAB_CI diff --git a/src/ci_providers/provider_local.js b/src/ci_providers/provider_local.js index c6bd039e0..91502565d 100644 --- a/src/ci_providers/provider_local.js +++ b/src/ci_providers/provider_local.js @@ -1,5 +1,5 @@ const childprocess = require('child_process') -const { parseSlug } = require('../helpers/parseSlug') +const { parseSlug } = require('../helpers/git') function detect (envs) { return !envs.CI diff --git a/src/helpers/parseSlug.js b/src/helpers/git.js similarity index 99% rename from src/helpers/parseSlug.js rename to src/helpers/git.js index bbbf39a98..a764e1b29 100644 --- a/src/helpers/parseSlug.js +++ b/src/helpers/git.js @@ -2,7 +2,6 @@ var childProcess = require('child_process') function parseSlug (slug) { // origin https://github.com/torvalds/linux.git (fetch) - // git@github.com: codecov / uploader.git if (typeof slug != 'string') { return '' diff --git a/test/providers/index.test.js b/test/providers/index.test.js index 39d090107..9576f535e 100644 --- a/test/providers/index.test.js +++ b/test/providers/index.test.js @@ -10,36 +10,9 @@ describe('CI Providers', () => { it('is an array of CI providers', () => expect(providers).toBeInstanceOf(Array)) providers.forEach(provider => { - const inputs = { - args: {}, - envs: { - CI: true, - CI_PROJECT_PATH: 'testOrg/testRepo', - CIRCLE_PROJECT_REPONAME: 'testRepo', - CIRCLE_PROJECT_USERNAME: 'testOrg', - CIRCLE_SHA1: 'testingSHA', - GITHUB_ACTIONS: true, - GITHUB_REF: 'refs/heads/test', - GITHUB_REPOSITORY: 'testOrg/testRepo', - GITLAB_CI: true, - JENKINS_URL: 'https://example.com', - SHIPPABLE: true, - SYSTEM_TEAMFOUNDATIONSERVERURI: 'https://example.azure.com', - TRAVIS: true, - TRAVIS_REPO_SLUG: 'testOrg/testRepo', - } - } - describe(`${provider.getServiceName() || ''}`, () => { - it('has a detect() method', () => { - expect(provider.detect).toBeInstanceOf(Function) - }) - it('has all properties set', () => { - props = ['branch', 'build', 'buildURL', 'commit', 'job', 'pr', 'service', 'slug'] - const serviceParams = provider.getServiceParams(inputs) - for (const prop of props) { - expect(serviceParams).toHaveProperty(prop) - } - }) + it('has a service name', () => { + expect(typeof provider.getServiceName()).toBe('string') + expect(provider.getServiceName()).not.toBe('') }) }) }) diff --git a/test/providers/provider_appveyorci.test.js b/test/providers/provider_appveyorci.test.js index 2138aeb9c..6b17121a2 100644 --- a/test/providers/provider_appveyorci.test.js +++ b/test/providers/provider_appveyorci.test.js @@ -8,30 +8,66 @@ describe('AppveyorCI Params', () => { td.reset() }) - it('does not run without AppveyorCI env variable', () => { - const inputs = { - args: {}, - envs: {} - } - let detected = providerAppveyorci.detect(inputs.envs) - expect(detected).toBeFalsy() + describe('detect()', () => { + it('does not run without AppveyorCI env variable', () => { + const inputs = { + args: {}, + envs: {} + } + let detected = providerAppveyorci.detect(inputs.envs) + expect(detected).toBeFalsy() + + inputs.envs['CI'] = 'true' + detected = providerAppveyorci.detect(inputs.envs) + expect(detected).toBeFalsy() - inputs.envs['CI'] = 'true' - detected = providerAppveyorci.detect(inputs.envs) - expect(detected).toBeFalsy() + inputs.envs['CI'] = 'True' + detected = providerAppveyorci.detect(inputs.envs) + expect(detected).toBeFalsy() - inputs.envs['CI'] = 'True' - detected = providerAppveyorci.detect(inputs.envs) - expect(detected).toBeFalsy() + inputs.envs['CI'] = 'false' + inputs.envs['APPVEYOR'] = 'true' + detected = providerAppveyorci.detect(inputs.envs) + expect(detected).toBeFalsy() - inputs.envs['CI'] = 'false' - inputs.envs['APPVEYOR'] = 'true' - detected = providerAppveyorci.detect(inputs.envs) - expect(detected).toBeFalsy() + inputs.envs['APPVEYOR'] = 'True' + detected = providerAppveyorci.detect(inputs.envs) + expect(detected).toBeFalsy() + }) - inputs.envs['APPVEYOR'] = 'True' - detected = providerAppveyorci.detect(inputs.envs) - expect(detected).toBeFalsy() + it('does run with AppveyorCI env variable', () => { + const inputs = { + args: {}, + envs: { + 'CI': 'true', + 'APPVEYOR': 'true', + } + } + const detected = providerAppveyorci.detect(inputs.envs) + expect(detected).toBeTruthy() + }) + }) + + it('gets the correct params on no env variables', () => { + const inputs = { + args: {}, + envs: { + 'CI': 'true', + 'APPVEYOR': 'true', + }, + } + const expected = { + branch: '', + build: '', + buildURL: '', + commit: '', + job: '', + pr: '', + service: '', + slug: '' + } + const params = providerAppveyorci.getServiceParams(inputs) + expect(expected).toBeTruthy() }) it('gets correct params on push', () => { diff --git a/test/providers/provider_azurepipelines.test.js b/test/providers/provider_azurepipelines.test.js index 9385a50e3..7a8858c4a 100644 --- a/test/providers/provider_azurepipelines.test.js +++ b/test/providers/provider_azurepipelines.test.js @@ -8,13 +8,47 @@ describe('Jenkins CI Params', () => { td.reset() }) - it('does not run without AzurePipelines env variable', () => { + describe('detect()', () => { + it('does not run without AzurePipelines env variable', () => { + const inputs = { + args: {}, + envs: {} + } + const detected = providerAzurepipelines.detect(inputs.envs) + expect(detected).toBeFalsy() + }) + + it('does run with AzurePipelines env variable', () => { + const inputs = { + args: {}, + envs: { + SYSTEM_TEAMFOUNDATIONSERVERURI: 'true' + } + } + const detected = providerAzurepipelines.detect(inputs.envs) + expect(detected).toBeTruthy() + }) + }) + + it('gets the correct params on no env variables', () => { const inputs = { args: {}, - envs: {} + envs: { + SYSTEM_TEAMFOUNDATIONSERVERURI: 'true' + }, } - detected = providerAzurepipelines.detect(inputs.envs) - expect(detected).toBeFalsy() + const expected = { + branch: '', + build: '', + buildURL: '', + commit: '', + job: '', + pr: '', + service: '', + slug: '' + } + const params = providerAzurepipelines.getServiceParams(inputs) + expect(expected).toBeTruthy() }) it('gets correct params on pr number', () => { diff --git a/test/providers/provider_circleci.test.js b/test/providers/provider_circleci.test.js index d91125f09..759086e4e 100644 --- a/test/providers/provider_circleci.test.js +++ b/test/providers/provider_circleci.test.js @@ -8,13 +8,27 @@ describe('CircleCI Params', () => { td.reset() }) - it('does not run without CircleCI env variable', () => { - const inputs = { - args: {}, - envs: {} - } - const detected = providerCircleci.detect(inputs.envs) - expect(detected).toBeFalsy() + describe('detect()', () => { + it('does not run without CircleCI env variable', () => { + const inputs = { + args: {}, + envs: {} + } + const detected = providerCircleci.detect(inputs.envs) + expect(detected).toBeFalsy() + }) + + it('does run with CircleCI env variable', () => { + const inputs = { + args: {}, + envs: { + CI: true, + CIRCLECI: true, + }, + } + const detected = providerCircleci.detect(inputs.envs) + expect(detected).toBeTruthy() + }) }) it('gets correct params', () => { diff --git a/test/providers/provider_githubactions.test.js b/test/providers/provider_githubactions.test.js index 354801109..34683b852 100644 --- a/test/providers/provider_githubactions.test.js +++ b/test/providers/provider_githubactions.test.js @@ -8,20 +8,54 @@ describe('GitHub Actions Params', () => { td.reset() }) - it('does not run without GitHub Actions env variable', () => { + describe('detect()', () => { + it('does not run without GitHub Actions env variable', () => { + const inputs = { + args: {}, + envs: { + GITHUB_ACTIONS: false, + GITHUB_REF: 'refs/heads/master', + GITHUB_REPOSITORY: 'testOrg/testRepo', + GITHUB_RUN_ID: 2, + GITHUB_SHA: 'testingsha', + GITHUB_WORKFLOW: 'testWorkflow', + } + } + const detected = providerGitHubactions.detect(inputs.envs) + expect(detected).toBeFalsy() + }) + + it('does not with GitHub Actions env variable', () => { + const inputs = { + args: {}, + envs: { + GITHUB_ACTIONS: true, + }, + } + const detected = providerGitHubactions.detect(inputs.envs) + expect(detected).toBeTruthy() + }) + }) + + it('gets the correct params on no env variables', () => { const inputs = { args: {}, envs: { - GITHUB_ACTIONS: false, - GITHUB_REF: 'refs/heads/master', - GITHUB_REPOSITORY: 'testOrg/testRepo', - GITHUB_RUN_ID: 2, - GITHUB_SHA: 'testingsha', - GITHUB_WORKFLOW: 'testWorkflow', - } + GITHUB_ACTIONS: true, + }, } - const detected = providerGitHubactions.detect(inputs.envs) - expect(detected).toBeFalsy() + const expected = { + branch: '', + build: '', + buildURL: '', + commit: '', + job: '', + pr: '', + service: '', + slug: '' + } + const params = providerGitHubactions.getServiceParams(inputs) + expect(expected).toBeTruthy() }) it('gets correct params for a push event', () => { diff --git a/test/providers/provider_gitlabci.test.js b/test/providers/provider_gitlabci.test.js index 5d716a4db..ade2ae1c5 100644 --- a/test/providers/provider_gitlabci.test.js +++ b/test/providers/provider_gitlabci.test.js @@ -8,13 +8,26 @@ describe('GitLabCI Params', () => { td.reset() }) - it('does not run without GitLabCI env variable', () => { - const inputs = { - args: {}, - envs: {} - } - const detected = providerGitLabci.detect(inputs.envs) - expect(detected).toBeFalsy() + describe('detect()', () => { + it('does not run without GitLabCI env variable', () => { + const inputs = { + args: {}, + envs: {} + } + const detected = providerGitLabci.detect(inputs.envs) + expect(detected).toBeFalsy() + }) + + it('does run with GitLabCI env variable', () => { + const inputs = { + args: {}, + envs: { + GITLAB_CI: true, + }, + } + const detected = providerGitLabci.detect(inputs.envs) + expect(detected).toBeTruthy() + }) }) it('gets correct empty params', () => { diff --git a/test/providers/provider_jenkinsci.test.js b/test/providers/provider_jenkinsci.test.js index 5da0c5c7a..da19f08d3 100644 --- a/test/providers/provider_jenkinsci.test.js +++ b/test/providers/provider_jenkinsci.test.js @@ -8,17 +8,51 @@ describe('Jenkins CI Params', () => { td.reset() }) - it('does not run without JenkinsCI env variable', () => { + describe('detect()', () => { + it('does not run without JenkinsCI env variable', () => { + const inputs = { + args: {}, + envs: {} + } + let detected = providerJenkinsci.detect(inputs.envs) + expect(detected).toBeFalsy() + + inputs.envs['JENKINS_URL'] = '' + detected = providerJenkinsci.detect(inputs.envs) + expect(detected).toBeFalsy() + }) + + it('does run with JenkinsCI env variable', () => { + const inputs = { + args: {}, + envs: { + JENKINS_URL: 'https://example.jenkins.com', + }, + } + const detected = providerJenkinsci.detect(inputs.envs) + expect(detected).toBeTruthy() + }) + }) + + it('gets the correct params on no env variables', () => { const inputs = { args: {}, - envs: {} + envs: { + JENKINS_URL: 'https://example.jenkins.com', + }, } - detected = providerJenkinsci.detect(inputs.envs) - expect(detected).toBeFalsy() - - inputs.envs['JENKINS_URL'] = '' - detected = providerJenkinsci.detect(inputs.envs) - expect(detected).toBeFalsy() + const expected = { + branch: '', + build: '', + buildURL: '', + commit: '', + job: '', + pr: '', + service: '', + slug: '' + } + const params = providerJenkinsci.getServiceParams(inputs) + expect(expected).toBeTruthy() }) it('gets correct params on push', () => { diff --git a/test/providers/provider_local.test.js b/test/providers/provider_local.test.js index e74d81a5e..3cf6fd230 100644 --- a/test/providers/provider_local.test.js +++ b/test/providers/provider_local.test.js @@ -8,15 +8,28 @@ describe('Local Params', () => { td.reset() }) - it('does not run with the CI env variable', () => { - const inputs = { - args: {}, - envs: { - CI: true + describe('detect()', () => { + it('does not run with the CI env variable', () => { + const inputs = { + args: {}, + envs: { + CI: true + } } - } - const detected = providerLocal.detect(inputs.envs) - expect(detected).toBeFalsy() + const detected = providerLocal.detect(inputs.envs) + expect(detected).toBeFalsy() + }) + + it('does run without the CI env variable', () => { + const inputs = { + args: {}, + envs: { + CI: false + } + } + const detected = providerLocal.detect(inputs.envs) + expect(detected).toBeTruthy() + }) }) it('returns errors on git command failures', () => { diff --git a/test/providers/provider_template.test.js b/test/providers/provider_template.test.js new file mode 100644 index 000000000..bb27fe5de --- /dev/null +++ b/test/providers/provider_template.test.js @@ -0,0 +1,129 @@ +const td = require('testdouble') +const childProcess = require('child_process') + +/* +Add your provider here and name it provder. Example: +const providerTravisci = require('../../src/ci_providers//provider_travisci') +*/ + +describe(' Params', () => { + afterEach(function () { + td.reset() + }) + + describe('detect()', () => { + it('does not run without env variable', () => { + const inputs = { + args: {}, + envs: {} + } + /* + const detected = provider.detect(inputs.envs) + expect(detected).toBeFalsy() + */ + }) + + it('does not run without env variable', () => { + const inputs = { + args: {}, + envs: {} + } + /* + const detected = provider.detect(inputs.envs) + expect(detected).toBeTruthy() + */ + }) + }) + + // This should test that the provider outputs proper default values + it('gets the correct params on no env variables', () => { + const inputs = { + args: {}, + envs: { + }, + } + const expected = { + branch: '', + build: '', + buildURL: '', + commit: '', + job: '', + pr: '', + service: '', + slug: '' + } + /* + const params = provider.getServiceParams(inputs) + expect(expected).toBeTruthy() + */ + }) + + // This should test that the provider outputs proper parameters when a push event is created + it('gets the correct params on push', () => { + const inputs = { + args: {}, + envs: { + }, + } + const expected = { + branch: '', + build: '', + buildURL: '', + commit: '', + job: '', + pr: '', + service: '', + slug: '' + } + /* + const params = provider.getServiceParams(inputs) + expect(expected).toBeTruthy() + */ + }) + // + // This should test that the provider outputs proper parameters when a pull request event is created + it('gets the correct params on pr', () => { + const inputs = { + args: {}, + envs: { + }, + } + const expected = { + branch: '', + build: '', + buildURL: '', + commit: '', + job: '', + pr: '', + service: '', + slug: '' + } + /* + const params = provider.getServiceParams(inputs) + expect(expected).toBeTruthy() + */ + }) + + // This should test that the provider outputs proper parameters when given overrides + it('gets the correct params on overrides', () => { + const inputs = { + args: {}, + envs: { + }, + } + const expected = { + branch: '', + build: '', + buildURL: '', + commit: '', + job: '', + pr: '', + service: '', + slug: '' + } + /* + const params = provider.getServiceParams(inputs) + expect(expected).toBeTruthy() + */ + }) +}) diff --git a/test/providers/provider_travisci.test.js b/test/providers/provider_travisci.test.js index cb6fbcc2d..8e7b6205f 100644 --- a/test/providers/provider_travisci.test.js +++ b/test/providers/provider_travisci.test.js @@ -8,21 +8,36 @@ describe('TravisCI Params', () => { td.reset() }) - it('does not run without TravisCI env variable', () => { - const inputs = { - args: {}, - envs: {} - } - let detected = providerTravisci.detect(inputs.envs) - expect(detected).toBeFalsy() + describe('detect()', () => { + it('does not run without TravisCI env variable', () => { + const inputs = { + args: {}, + envs: {} + } + let detected = providerTravisci.detect(inputs.envs) + expect(detected).toBeFalsy() + + inputs.envs['CI'] = true + detected = providerTravisci.detect(inputs.envs) + expect(detected).toBeFalsy() - inputs.envs['CI'] = true - detected = providerTravisci.detect(inputs.envs) - expect(detected).toBeFalsy() + inputs.envs['TRAVIS'] = true + detected = providerTravisci.detect(inputs.envs) + expect(detected).toBeFalsy() + }) - inputs.envs['TRAVIS'] = true - detected = providerTravisci.detect(inputs.envs) - expect(detected).toBeFalsy() + it('does run with TravisCI env variable', () => { + const inputs = { + args: {}, + envs: { + CI: true, + SHIPPABLE: true, + TRAVIS: true, + }, + } + const detected = providerTravisci.detect(inputs.envs) + expect(detected).toBeTruthy() + }) }) it('gets correct params on push', () => {