From c723ad7c0195a88da3abef57fdc226a7b5d3fd22 Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Wed, 9 Jun 2021 11:57:01 -0400 Subject: [PATCH] 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', () => {