UNPKG

@interaktiv/dia-scripts

Version:

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

223 lines (199 loc) 7.75 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _serializers = require("../__tests__/helpers/serializers"); expect.addSnapshotSerializer(_serializers.unquoteSerializer); expect.addSnapshotSerializer(_serializers.winPathSerializer); expect.addSnapshotSerializer(_serializers.relativePathSerializer); let originalArgv = process.argv; let originalExit = process.exit; let originalConsoleLog = console.log; let crossSpawnSyncMock, originalPreCommit, originalDebug; // eslint-disable-next-line max-lines-per-function describe('lint apex', () => { beforeEach(() => { jest.mock('cross-spawn', () => ({ sync: jest.fn(command => { if (command === 'java') { return { status: 0, output: 'java version "1.8.0_51"' }; } return { status: 0 }; }) })); ({ sync: crossSpawnSyncMock } = require('cross-spawn')); jest.mock('../../utils', () => { const path = require('path'); const appDirectory = '/my/appdir'; const CACHE_DIR = path.join(appDirectory, '.cache'); const LOG_DIR = path.join(appDirectory, 'logs'); const PMD_DIR = path.join(appDirectory, '.pmd'); const TMP_DIR = path.join(appDirectory, 'tmp'); return (0, _extends2.default)({}, jest.requireActual('../../utils'), { CACHE_DIR, PMD_DIR, LOG_DIR, TMP_DIR, TMP_SFDX_DIR: path.join(TMP_DIR, 'dx'), PMD_META_DATA_BUNDLE: path.join(CACHE_DIR, 'metadata-bundle'), PMD_CACHE_FILE: path.join(CACHE_DIR, 'pmd.cache'), PMD_LOGFILE: path.join(LOG_DIR, 'pmd.log'), PMD_IGNORE_FILE: path.join(appDirectory, '.pmdignore'), createFileIfNeeded: jest.fn(), cleanDir: jest.fn(), isWindows: jest.fn(() => false), fromRoot: jest.fn((...p) => path.join(appDirectory, ...p)), createPmdMetaDataBundle: jest.fn(), createPmdMetaDataBundleFromFiles: jest.fn(), createPmdMetaDataBundleFromDir: jest.fn() }); }); originalConsoleLog = console.log; originalArgv = process.argv; originalExit = process.exit; originalPreCommit = process.env['SCRIPTS_PRE-COMMIT']; process.env['SCRIPTS_PRE-COMMIT'] = 'false'; originalDebug = process.env.DEBUG; process.exit = jest.fn(); console.log = jest.fn(); }); afterEach(() => { process.exit = originalExit; process.argv = originalArgv; console.log = originalConsoleLog; process.env['SCRIPTS_PRE-COMMIT'] = originalPreCommit; process.env.DEBUG = originalDebug; jest.resetModules(); }); describe('when NOT on PRE_COMMIT', () => { it.each` title | params ${'calls PMD CLI with default args'} | ${{}} ${'should use global pmd cmd if given'} | ${{ isPmdInPath: true }} ${'does not use built-in ruleset with -rulesets'} | ${{ args: ['-rulesets', './my-custom-ruleset.xml'] }} ${'does not use built-in config with .pmd/ruleset.xml file'} | ${{ hasFile: filename => /\.pmd\/ruleset\.xml$/i.test(filename) }} ${'does not use built-in log file with -reportfile'} | ${{ args: ['-reportfile', './my-custom-logfile'] }} ${'does not use built-in cache file with -cache'} | ${{ args: ['-cache', './my-custom.cache'] }} ${'does not use built-in log format with -format'} | ${{ args: ['-format', 'xml'] }} ${'does not use built-in suppress marker with -suppressmarker'} | ${{ args: ['-suppressmarker', 'my-custom-supress-marker'] }} ${'runs on given files, but only apex files'} | ${{ args: ['./src/index.js', './package.json', './src/index.css', 'Handler.class', 'CHANGELOG.md', './src/component.js', 'MyTrigger.cls', 'Controller.apx', 'Search.apex'] }} ${'should use ignore file with -ignorelist'} | ${{ args: ['-ignorelist', './my-custom-ignorefile'] }} ${'should use .pmdignore file if given in project root'} | ${{ hasFile: filename => /\.pmdignore$/i.test(filename) }} ${'should not show suppressed rules with --do-not-showsuppressed'} | ${{ args: ['--do-not-showsuppressed'] }} ${'should add -debug with DEBUG env set to "true"'} | ${{ args: ['--do-not-showsuppressed'], debugMode: 'true' }} ${'should add -debug with DEBUG env set to 1'} | ${{ args: ['--do-not-showsuppressed'], debugMode: 1 }} ${'should add -debug with DEBUG env set to "1"'} | ${{ args: ['--do-not-showsuppressed'], debugMode: '1' }} `('$title', ({ params = {} }) => { const path = require('path'); const utils = require('../../utils'); const { args = [], buildNumber = null, debugMode = false, hasFile = () => false, isPmdInPath = false } = params; // Before each Object.assign(utils, { hasFile, resolveBin: (modName, { executable = modName } = {}) => { if (executable === 'pmd' && isPmdInPath) { return executable; } if (executable === 'pmd') { throw new Error('pmd not in $PATH'); } return executable; }, resolvePmdBin: () => path.join('/my/appdir', '.pmd/bin/run.sh pmd') }); // Test process.argv = ['node', './apex', ...args]; process.env.DEBUG = debugMode; process.env.BUILD_NUMBER = buildNumber; require('./apex'); expect(crossSpawnSyncMock).toHaveBeenCalledTimes(2); const [, secondCall] = crossSpawnSyncMock.mock.calls; const [script, calledArgs] = secondCall; expect([script, ...calledArgs].join(' ')).toMatchSnapshot(); }); it('should validate jre if --validate-jre given', () => { const path = require('path'); const utils = require('../../utils'); // Before each Object.assign(utils, { hasFile: () => false, resolveBin: (modName, { executable = modName } = {}) => { if (executable === 'pmd') { throw new Error('pmd not in $PATH'); } return executable; }, resolvePmdBin: () => path.join('/my/appdir', '.pmd/bin/run.sh pmd') }); // Test process.argv = ['node', './apex', ...['--validate-jre']]; process.env.DEBUG = false; process.env.BUILD_NUMBER = null; require('./apex'); expect(crossSpawnSyncMock).toHaveBeenCalledTimes(3); const [firstCall,, thirdCall] = crossSpawnSyncMock.mock.calls; const [script, calledArgs] = firstCall; expect([script, ...calledArgs].join(' ')).toMatchSnapshot(); const [scriptThree, calledArgsThree] = thirdCall; expect([scriptThree, ...calledArgsThree].join(' ')).toMatchSnapshot(); }); }); describe('when on PRE-COMMIT', () => { beforeEach(() => { originalPreCommit = process.env['SCRIPTS_PRE-COMMIT']; process.env['SCRIPTS_PRE-COMMIT'] = 'true'; }); afterEach(() => { process.env['SCRIPTS_PRE-COMMIT'] = originalPreCommit; }); it('should not run without files', () => { process.argv = ['node', './apex']; require('./apex'); expect(crossSpawnSyncMock).toHaveBeenCalledTimes(0); }); }); });