UNPKG

@interaktiv/dia-scripts

Version:

CLI toolbox with common scripts for most sort of projects at DIA

227 lines (176 loc) 6.48 kB
"use strict"; var _serializers = require("./__tests__/helpers/serializers"); expect.addSnapshotSerializer(_serializers.unquoteSerializer); // eslint-disable-next-line max-lines-per-function describe('pipelines-after-success', () => { let crossSpawnSyncMock, originalEnvs, originalLog, originalExit; beforeEach(() => { ({ sync: crossSpawnSyncMock } = require('cross-spawn')); originalLog = console.log; console.log = jest.fn(); originalExit = process.exit; process.exit = jest.fn(); originalEnvs = process.env; }); afterEach(() => { console.log = originalLog; process.env = originalEnvs; process.exit = originalExit; jest.resetModules(); }); it('should not do the codecov script by default', () => { const env = { CI: 'true', BITBUCKET_BRANCH: 'master', BITBUCKET_PR_ID: 'null', REPO_ORIGIN: 'git@bitbucket.org:wurstladen/salat.git' }; Object.keys(env).forEach(envKey => { process.env[envKey] = env[envKey]; }); const utils = require('../utils'); utils.resolveBin = (modName, { executable = modName } = {}) => executable; utils.hasFile = () => true; utils.isOptedIn = optIn => optIn === 'reportcoverage' && false; process.env.SKIP_CODECOV = true; require('./pipelines-after-success'); expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1); const [firstCall] = crossSpawnSyncMock.mock.calls; const [scriptOne, calledArgsOne] = firstCall; expect([scriptOne, ...calledArgsOne].join(' ')).toMatchSnapshot(); }); it('should not auto release when running on pipelines but in a pull request', () => { const env = { CI: 'true', BITBUCKET_BRANCH: 'master', BITBUCKET_PR_ID: 'null', REPO_ORIGIN: 'git@bitbucket.org:wurstladen/salat.git' }; Object.keys(env).forEach(envKey => { process.env[envKey] = env[envKey]; }); const utils = require('../utils'); utils.resolveBin = (modName, { executable = modName } = {}) => executable; utils.hasFile = () => true; process.env.SKIP_CODECOV = true; require('./pipelines-after-success'); expect(console.log.mock.calls).toMatchSnapshot(); }); it('should no do the codecov script when there is no coverage directory', () => { const env = { CI: 'true', BITBUCKET_BRANCH: 'master', BITBUCKET_PR_ID: 'null', REPO_ORIGIN: 'git@bitbucket.org:wurstladen/salat.git' }; Object.keys(env).forEach(envKey => { process.env[envKey] = env[envKey]; }); const utils = require('../utils'); utils.resolveBin = (modName, { executable = modName } = {}) => executable; utils.hasFile = () => false; process.env.SKIP_CODECOV = true; require('./pipelines-after-success'); expect(console.log.mock.calls).toMatchSnapshot(); const [firstCall] = crossSpawnSyncMock.mock.calls; const [scriptOne, calledArgsOne] = firstCall; expect([scriptOne, ...calledArgsOne].join(' ')).toMatchSnapshot(); }); it('should do the codecov script when opted in (via CI)', () => { const env = { CI: 'true', BITBUCKET_BRANCH: 'master', BITBUCKET_PR_ID: 'null', REPO_ORIGIN: 'git@bitbucket.org:wurstladen/salat.git' }; Object.keys(env).forEach(envKey => { process.env[envKey] = env[envKey]; }); const utils = require('../utils'); utils.resolveBin = (modName, { executable = modName } = {}) => executable; utils.hasFile = () => true; utils.isOptedIn = optIn => optIn === 'reportcoverage' && false; process.env.SKIP_CODECOV = false; require('./pipelines-after-success'); expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1); const [firstCall] = crossSpawnSyncMock.mock.calls; const [scriptOne, calledArgsOne] = firstCall; expect([scriptOne, ...calledArgsOne].join(' ')).toMatchSnapshot(); }); it('should do the codecov script when opted in (via .opt-in)', () => { const env = { CI: 'true', BITBUCKET_BRANCH: 'master', BITBUCKET_PR_ID: 'null', REPO_ORIGIN: 'git@bitbucket.org:wurstladen/salat.git' }; Object.keys(env).forEach(envKey => { process.env[envKey] = env[envKey]; }); const utils = require('../utils'); utils.resolveBin = (modName, { executable = modName } = {}) => executable; utils.hasFile = () => true; utils.isOptedIn = optIn => optIn === 'reportcoverage' && true; process.env.SKIP_CODECOV = true; require('./pipelines-after-success'); expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1); const [firstCall] = crossSpawnSyncMock.mock.calls; const [scriptOne, calledArgsOne] = firstCall; expect([scriptOne, ...calledArgsOne].join(' ')).toMatchSnapshot(); }); it('should use REPO_ORIGIN env var if given', () => { const env = { CI: 'true', BITBUCKET_BRANCH: 'master', BITBUCKET_PR_ID: 'null', REPO_ORIGIN: 'git@bitbucket.org:wurstladen/salat.git' }; Object.keys(env).forEach(envKey => { process.env[envKey] = env[envKey]; }); const utils = require('../utils'); utils.resolveBin = (modName, { executable = modName } = {}) => executable; utils.hasFile = () => true; utils.isOptedIn = optIn => optIn === 'reportcoverage' && false; process.env.SKIP_CODECOV = true; require('./pipelines-after-success'); expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1); }); it('should use repo origin from git config if REPO_ORIGIN env var is not set', () => { const env = { CI: 'true', BITBUCKET_BRANCH: 'master', BITBUCKET_PR_ID: 'null', REPO_ORIGIN: null }; Object.keys(env).forEach(envKey => { process.env[envKey] = env[envKey]; }); const utils = require('../utils'); utils.resolveBin = (modName, { executable = modName } = {}) => executable; utils.hasFile = () => true; utils.isOptedIn = optIn => optIn === 'reportcoverage' && false; utils.getGitOriginUrl = () => 'https://x-token-auth:ACCESS_TOKEN@bitbucket.org:wurstladen/salat.git'; process.env.SKIP_CODECOV = true; require('./pipelines-after-success'); expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1); const [firstCall] = crossSpawnSyncMock.mock.calls; const [scriptOne, calledArgsOne] = firstCall; expect([scriptOne, ...calledArgsOne].join(' ')).toMatchSnapshot(); }); });