UNPKG

license-check-and-add

Version:

A tool to enable the checking, inserting and removal of licenses

216 lines 10.3 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var chai = __importStar(require("chai")); var mockery = __importStar(require("mockery")); var path = __importStar(require("path")); var sinon = __importStar(require("sinon")); var sinon_chai_1 = __importDefault(require("sinon-chai")); var constants_1 = require("../constants"); var config_parser_1 = require("./config-parser"); var license_manager_1 = require("./license-manager"); var expect = chai.expect; chai.use(sinon_chai_1.default); describe('#ConfigParser', function () { var sandbox; var fsReadJSONStub; var fsReadFileStub; var ConfigParser; var mockConfig; before(function () { mockery.enable({ warnOnReplace: false, warnOnUnregistered: false, }); }); beforeEach(function () { sandbox = sinon.createSandbox(); mockConfig = { license: 'LICENSE.txt', }; fsReadJSONStub = sandbox.stub().returns(mockConfig); fsReadFileStub = sandbox.stub().returns('some license'); mockery.registerMock('fs-extra', { readJSONSync: fsReadJSONStub, readFileSync: fsReadFileStub }); delete require.cache[require.resolve('./config-parser')]; ConfigParser = require('./config-parser').ConfigParser; }); afterEach(function () { sandbox.restore(); mockery.deregisterAll(); }); after(function () { mockery.disable(); }); it('should throw an error when missing license from input JSON', function () { delete mockConfig.license; expect(function () { ConfigParser.parse('some/file/path', license_manager_1.ManagementMode.CHECK); }).to.throw('Missing required field in config: license'); expect(fsReadJSONStub).to.have.been.calledOnceWithExactly('some/file/path'); }); it('should handle when minimum required fields are passed', function () { var config = ConfigParser.parse('some/file/path', license_manager_1.ManagementMode.CHECK); expect(config).deep.equal({ defaultFormat: constants_1.DEFAULT_FORMAT, ignore: [], ignoreDefaultIgnores: false, license: 'some license', licenseFormats: {}, trailingWhitespace: config_parser_1.TrailingWhitespaceMode.DEFAULT, }); expect(fsReadJSONStub).to.have.been.calledOnceWithExactly('some/file/path'); expect(fsReadFileStub).to.have.been.calledOnceWithExactly(path.resolve(process.cwd(), 'LICENSE.txt')); }); it('should handle when ignore is an array', function () { mockConfig.ignore = ['**/*.js', '**/*.html']; var config = ConfigParser.parse('some/file/path', license_manager_1.ManagementMode.CHECK); expect(config).deep.equal({ defaultFormat: constants_1.DEFAULT_FORMAT, ignore: mockConfig.ignore, ignoreDefaultIgnores: false, license: 'some license', licenseFormats: {}, trailingWhitespace: config_parser_1.TrailingWhitespaceMode.DEFAULT, }); expect(fsReadJSONStub).to.have.been.calledOnceWithExactly('some/file/path'); expect(fsReadFileStub).to.have.been.calledOnceWithExactly(path.resolve(process.cwd(), 'LICENSE.txt')); }); it('should handle when ignoreFile is passed', function () { mockConfig.ignoreFile = 'some/ignore/file'; var config = ConfigParser.parse('some/file/path', license_manager_1.ManagementMode.CHECK); expect(config).deep.equal({ defaultFormat: constants_1.DEFAULT_FORMAT, ignore: [], ignoreDefaultIgnores: false, ignoreFile: 'some/ignore/file', license: 'some license', licenseFormats: {}, trailingWhitespace: config_parser_1.TrailingWhitespaceMode.DEFAULT, }); expect(fsReadJSONStub).to.have.been.calledOnceWithExactly('some/file/path'); expect(fsReadFileStub).to.have.been.calledOnceWithExactly(path.resolve(process.cwd(), 'LICENSE.txt')); }); it('should use specified default format', function () { mockConfig.defaultFormat = { append: '###', prepend: '###', }; var config = ConfigParser.parse('some/file/path', license_manager_1.ManagementMode.CHECK); expect(config).deep.equal({ defaultFormat: mockConfig.defaultFormat, ignore: [], ignoreDefaultIgnores: false, license: 'some license', licenseFormats: {}, trailingWhitespace: config_parser_1.TrailingWhitespaceMode.DEFAULT, }); expect(fsReadJSONStub).to.have.been.calledOnceWithExactly('some/file/path'); expect(fsReadFileStub).to.have.been.calledOnceWithExactly(path.resolve(process.cwd(), 'LICENSE.txt')); }); it('should handle when trailing whitespace set but not to trim', function () { mockConfig.trailingWhitespace = 'NOT trim'; var config = ConfigParser.parse('some/file/path', license_manager_1.ManagementMode.CHECK); expect(config).deep.equal({ defaultFormat: constants_1.DEFAULT_FORMAT, ignore: [], ignoreDefaultIgnores: false, license: 'some license', licenseFormats: {}, trailingWhitespace: config_parser_1.TrailingWhitespaceMode.DEFAULT, }); expect(fsReadJSONStub).to.have.been.calledOnceWithExactly('some/file/path'); expect(fsReadFileStub).to.have.been.calledOnceWithExactly(path.resolve(process.cwd(), 'LICENSE.txt')); }); it('should handle when trailing whitespace set to trim', function () { mockConfig.trailingWhitespace = 'trIm'; var config = ConfigParser.parse('some/file/path', license_manager_1.ManagementMode.CHECK); expect(config).deep.equal({ defaultFormat: constants_1.DEFAULT_FORMAT, ignore: [], ignoreDefaultIgnores: false, license: 'some license', licenseFormats: {}, trailingWhitespace: config_parser_1.TrailingWhitespaceMode.TRIM, }); expect(fsReadJSONStub).to.have.been.calledOnceWithExactly('some/file/path'); expect(fsReadFileStub).to.have.been.calledOnceWithExactly(path.resolve(process.cwd(), 'LICENSE.txt')); }); it('should handle when output specified', function () { mockConfig.output = 'some/output/path'; var config = ConfigParser.parse('some/file/path', license_manager_1.ManagementMode.CHECK); expect(config).deep.equal({ defaultFormat: constants_1.DEFAULT_FORMAT, ignore: [], ignoreDefaultIgnores: false, license: 'some license', licenseFormats: {}, output: path.resolve(process.cwd(), 'some/output/path'), trailingWhitespace: config_parser_1.TrailingWhitespaceMode.DEFAULT, }); expect(fsReadJSONStub).to.have.been.calledOnceWithExactly('some/file/path'); expect(fsReadFileStub).to.have.been.calledOnceWithExactly(path.resolve(process.cwd(), 'LICENSE.txt')); }); it('should set regex identifier when no replacement value passed', function () { mockConfig.regexIdentifier = '#'; var config = ConfigParser.parse('some/file/path', license_manager_1.ManagementMode.CHECK); expect(config).deep.equal({ defaultFormat: constants_1.DEFAULT_FORMAT, ignore: [], ignoreDefaultIgnores: false, license: 'some license', licenseFormats: {}, regex: { identifier: '#', }, trailingWhitespace: config_parser_1.TrailingWhitespaceMode.DEFAULT, }); expect(fsReadJSONStub).to.have.been.calledOnceWithExactly('some/file/path'); expect(fsReadFileStub).to.have.been.calledOnceWithExactly(path.resolve(process.cwd(), 'LICENSE.txt')); }); it('should set regex replacement value when identifier used', function () { mockConfig.regexIdentifier = '#'; var config = ConfigParser.parse('some/file/path', license_manager_1.ManagementMode.INSERT, ['replacement']); expect(config).deep.equal({ defaultFormat: constants_1.DEFAULT_FORMAT, ignore: [], ignoreDefaultIgnores: false, license: 'some license', licenseFormats: {}, regex: { identifier: '#', replacements: ['replacement'], }, trailingWhitespace: config_parser_1.TrailingWhitespaceMode.DEFAULT, }); expect(fsReadJSONStub).to.have.been.calledOnceWithExactly('some/file/path'); expect(fsReadFileStub).to.have.been.calledOnceWithExactly(path.resolve(process.cwd(), 'LICENSE.txt')); }); it('should throw an error when regex identifier passed in INSERT mode without regex replacement', function () { mockConfig.regexIdentifier = '#'; expect(function () { ConfigParser.parse('some/file/path', license_manager_1.ManagementMode.INSERT); }).to.throw('Must supply regexReplacements option when using regexIdentifier in config when in INSERT mode'); }); }); //# sourceMappingURL=config-parser.spec.js.map