From f503e0a38773185c9cf8087804f33c69059e2bad Mon Sep 17 00:00:00 2001 From: Mickael Jeanroy Date: Sun, 1 Dec 2019 14:44:52 +0100 Subject: [PATCH] refactor: refactor unit tests --- test/license-plugin.spec.js | 2420 ++++++++++++++--------------------- 1 file changed, 952 insertions(+), 1468 deletions(-) diff --git a/test/license-plugin.spec.js b/test/license-plugin.spec.js index cc6eb04b..a909bed1 100644 --- a/test/license-plugin.spec.js +++ b/test/license-plugin.spec.js @@ -44,80 +44,81 @@ describe('LicensePlugin', () => { tmpDir.removeCallback(); }); - it('should initialize instance', () => { - const plugin = licensePlugin(); - expect(plugin._cwd).toBeDefined(); - expect(plugin._pkg).toBeDefined(); - expect(plugin._sourcemap).toBe(true); - expect(plugin._dependencies).toEqual({}); - }); + describe('on initialization', () => { + it('should initialize instance', () => { + const plugin = licensePlugin(); + expect(plugin._cwd).toBeDefined(); + expect(plugin._pkg).toBeDefined(); + expect(plugin._sourcemap).toBe(true); + expect(plugin._dependencies).toEqual({}); + }); + + it('should initialize instance with custom cwd', () => { + const cwd = path.join(__dirname, 'fixtures', 'fake-package-1'); + const plugin = licensePlugin({ + cwd, + }); - it('should initialize instance with custom cwd', () => { - const cwd = path.join(__dirname, 'fixtures', 'fake-package-1'); - const plugin = licensePlugin({ - cwd, + expect(plugin._cwd).toBeDefined(); + expect(plugin._cwd).toBe(cwd); }); - expect(plugin._cwd).toBeDefined(); - expect(plugin._cwd).toBe(cwd); - }); + it('should initialize instance with sourcemap = false (lowercase)', () => { + const plugin = licensePlugin({ + sourcemap: false, + }); - it('should initialize instance with sourcemap = false (lowercase)', () => { - const plugin = licensePlugin({ - sourcemap: false, + expect(plugin._cwd).toBeDefined(); + expect(plugin._pkg).toBeDefined(); + expect(plugin._sourcemap).toBe(false); + expect(plugin._dependencies).toEqual({}); }); - expect(plugin._cwd).toBeDefined(); - expect(plugin._pkg).toBeDefined(); - expect(plugin._sourcemap).toBe(false); - expect(plugin._dependencies).toEqual({}); - }); + it('should print warning when plugin is used with sourceMap (camelcase)', () => { + const warn = spyOn(console, 'warn'); - it('should print warning when plugin is used with sourceMap (camelcase)', () => { - const warn = spyOn(console, 'warn'); + licensePlugin({ + sourceMap: false, + }); - licensePlugin({ - sourceMap: false, + expect(warn).toHaveBeenCalledWith( + '[rollup-plugin-license] -- "sourceMap" has been deprecated and will be removed in a future version, ' + + 'please use "sourcemap" instead.' + ); }); - expect(warn).toHaveBeenCalledWith( - '[rollup-plugin-license] -- "sourceMap" has been deprecated and will be removed in a future version, ' + - 'please use "sourcemap" instead.' - ); - }); + it('should print warning with unknown options', () => { + const warn = spyOn(console, 'warn'); - it('should print warning with unknown options', () => { - const warn = spyOn(console, 'warn'); + licensePlugin({ + foobar: false, + }); - licensePlugin({ - foobar: false, + expect(warn).toHaveBeenCalledWith( + '[rollup-plugin-license] -- Unknown property: "foobar", allowed options are: sourcemap, debug, cwd, banner, thirdParty.' + ); }); - expect(warn).toHaveBeenCalledWith( - '[rollup-plugin-license] -- Unknown property: "foobar", allowed options are: sourcemap, debug, cwd, banner, thirdParty.' - ); - }); - - it('should disable source map', () => { - const plugin = licensePlugin(); - expect(plugin._sourcemap).toBe(true); + it('should disable source map', () => { + const plugin = licensePlugin(); + expect(plugin._sourcemap).toBe(true); - plugin.disableSourceMap(); - expect(plugin._sourcemap).toBe(false); + plugin.disableSourceMap(); + expect(plugin._sourcemap).toBe(false); + }); }); - it('should load pkg', () => { - const plugin = licensePlugin(); - const id = path.join(__dirname, 'fixtures', 'fake-package-1', 'src', 'index.js'); - - spyOn(plugin, 'addDependency').and.callThrough(); + describe('when scanning dependency', () => { + let plugin; + let addDependency; + let scanDependency; + let fakePackage; - const result = plugin.scanDependency(id); - - expect(result).not.toBeDefined(); - expect(plugin.addDependency).toHaveBeenCalled(); - expect(plugin._dependencies).toEqual({ - 'fake-package': { + beforeEach(() => { + plugin = licensePlugin(); + addDependency = spyOn(plugin, 'addDependency').and.callThrough(); + scanDependency = spyOn(plugin, 'scanDependency').and.callThrough(); + fakePackage = { name: 'fake-package', version: '1.0.0', description: 'Fake package used in unit tests', @@ -133,288 +134,159 @@ describe('LicensePlugin', () => { email: 'mickael.jeanroy@gmail.com', url: null, }, - }, + }; }); - }); - it('should load pkg including license text from LICENSE.md file', () => { - const plugin = licensePlugin(); - const id = path.join(__dirname, 'fixtures', 'fake-package-2', 'src', 'index.js'); + it('should load pkg', () => { + const id = path.join(__dirname, 'fixtures', 'fake-package-1', 'src', 'index.js'); + const result = plugin.scanDependency(id); - spyOn(plugin, 'addDependency').and.callThrough(); - - const result = plugin.scanDependency(id); - - expect(result).not.toBeDefined(); - expect(plugin.addDependency).toHaveBeenCalled(); - expect(plugin._dependencies).toEqual({ - 'fake-package': { - name: 'fake-package', - version: '1.0.0', - description: 'Fake package used in unit tests', - license: 'MIT', - licenseText: 'LICENSE.md file', - private: true, - homepage: null, - repository: null, - maintainers: [], - contributors: [], - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - url: null, - }, - }, + expect(result).not.toBeDefined(); + expect(addDependency).toHaveBeenCalled(); + expect(plugin._dependencies).toEqual({ + 'fake-package': fakePackage, + }); }); - }); - it('should load pkg including license text from LICENSE.txt file', () => { - const plugin = licensePlugin(); - const id = path.join(__dirname, 'fixtures', 'fake-package-3', 'src', 'index.js'); + it('should load pkg including license text from LICENSE.md file', () => { + const id = path.join(__dirname, 'fixtures', 'fake-package-2', 'src', 'index.js'); + const result = plugin.scanDependency(id); - spyOn(plugin, 'addDependency').and.callThrough(); + expect(result).not.toBeDefined(); + expect(addDependency).toHaveBeenCalled(); + expect(plugin._dependencies).toEqual({ + 'fake-package': Object.assign(fakePackage, { + licenseText: 'LICENSE.md file', + }), + }); + }); - const result = plugin.scanDependency(id); + it('should load pkg including license text from LICENSE.txt file', () => { + const id = path.join(__dirname, 'fixtures', 'fake-package-3', 'src', 'index.js'); + const result = plugin.scanDependency(id); - expect(result).not.toBeDefined(); - expect(plugin.addDependency).toHaveBeenCalled(); - expect(plugin._dependencies).toEqual({ - 'fake-package': { - name: 'fake-package', - version: '1.0.0', - description: 'Fake package used in unit tests', - license: 'MIT', - licenseText: 'LICENSE.txt file', - private: true, - homepage: null, - repository: null, - maintainers: [], - contributors: [], - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - url: null, - }, - }, + expect(result).not.toBeDefined(); + expect(addDependency).toHaveBeenCalled(); + expect(plugin._dependencies).toEqual({ + 'fake-package': Object.assign(fakePackage, { + licenseText: 'LICENSE.txt file', + }), + }); }); - }); - it('should load pkg including license text from LICENSE file', () => { - const plugin = licensePlugin(); - const id = path.join(__dirname, 'fixtures', 'fake-package-4', 'src', 'index.js'); - spyOn(plugin, 'addDependency').and.callThrough(); + it('should load pkg including license text from LICENSE file', () => { + const id = path.join(__dirname, 'fixtures', 'fake-package-4', 'src', 'index.js'); + const result = plugin.scanDependency(id); - const result = plugin.scanDependency(id); - - expect(result).not.toBeDefined(); - expect(plugin.addDependency).toHaveBeenCalled(); - expect(plugin._dependencies).toEqual({ - 'fake-package': { - name: 'fake-package', - version: '1.0.0', - description: 'Fake package used in unit tests', - license: 'MIT', - licenseText: 'LICENSE file', - private: true, - homepage: null, - repository: null, - maintainers: [], - contributors: [], - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - url: null, - }, - }, + expect(result).not.toBeDefined(); + expect(addDependency).toHaveBeenCalled(); + expect(plugin._dependencies).toEqual({ + 'fake-package': Object.assign(fakePackage, { + licenseText: 'LICENSE file', + }), + }); }); - }); - - it('should load pkg dependencies', () => { - const plugin = licensePlugin(); - const modules = [ - path.join(__dirname, 'fixtures', 'fake-package-1', 'src', 'index.js'), - ]; - spyOn(plugin, 'scanDependency').and.callThrough(); - spyOn(plugin, 'addDependency').and.callThrough(); + it('should load pkg dependencies', () => { + const modules = [ + path.join(__dirname, 'fixtures', 'fake-package-1', 'src', 'index.js'), + ]; - const result = plugin.scanDependencies(modules); + const result = plugin.scanDependencies(modules); - expect(result).not.toBeDefined(); - expect(plugin.scanDependency).toHaveBeenCalledWith(modules[0]); - expect(plugin.addDependency).toHaveBeenCalled(); - expect(plugin._dependencies).toEqual({ - 'fake-package': { - name: 'fake-package', - version: '1.0.0', - description: 'Fake package used in unit tests', - license: 'MIT', - licenseText: null, - private: true, - repository: null, - homepage: null, - maintainers: [], - contributors: [], - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - url: null, - }, - }, + expect(result).not.toBeDefined(); + expect(scanDependency).toHaveBeenCalledWith(modules[0]); + expect(addDependency).toHaveBeenCalled(); + expect(plugin._dependencies).toEqual({ + 'fake-package': fakePackage, + }); }); - }); - - it('should load pkg and stop on cwd', () => { - const plugin = licensePlugin(); - const id = path.join(__dirname, '..', 'src', 'index.js'); - spyOn(plugin, 'addDependency').and.callThrough(); + it('should load pkg and stop on cwd', () => { + const id = path.join(__dirname, '..', 'src', 'index.js'); + const result = plugin.scanDependency(id); - const result = plugin.scanDependency(id); - - expect(result).not.toBeDefined(); - expect(plugin.addDependency).not.toHaveBeenCalled(); - expect(plugin._dependencies).toEqual({}); - }); + expect(result).not.toBeDefined(); + expect(addDependency).not.toHaveBeenCalled(); + expect(plugin._dependencies).toEqual({}); + }); - it('should load pkg and update cache', () => { - const plugin = licensePlugin(); - const fakePackage = path.join(__dirname, 'fixtures', 'fake-package-1'); - const id = path.join(fakePackage, 'src', 'index.js'); - const pkg = require(path.join(fakePackage, 'package.json')); + it('should load pkg and update cache', () => { + const pkgPath = path.join(__dirname, 'fixtures', 'fake-package-1'); + const id = path.join(pkgPath, 'src', 'index.js'); + const pkg = require(path.join(pkgPath, 'package.json')); - plugin.scanDependency(id); + plugin.scanDependency(id); - expect(plugin._dependencies).toEqual({ - 'fake-package': { - name: 'fake-package', - version: '1.0.0', - description: 'Fake package used in unit tests', - license: 'MIT', - licenseText: null, - private: true, - homepage: null, - repository: null, - maintainers: [], - contributors: [], - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - url: null, - }, - }, - }); + expect(plugin._dependencies).toEqual({ + 'fake-package': fakePackage, + }); - expect(plugin._cache).toEqual({ - [path.join(__dirname, 'fixtures', 'fake-package-1', 'src')]: pkg, - [path.join(__dirname, 'fixtures', 'fake-package-1')]: pkg, + expect(plugin._cache).toEqual({ + [path.join(__dirname, 'fixtures', 'fake-package-1', 'src')]: pkg, + [path.join(__dirname, 'fixtures', 'fake-package-1')]: pkg, + }); }); - }); - it('should load pkg and put null without package', () => { - const plugin = licensePlugin(); - const id = path.join(__dirname, '..', 'src', 'index.js'); + it('should load pkg and put null without package', () => { + const id = path.join(__dirname, '..', 'src', 'index.js'); - plugin.scanDependency(id); + plugin.scanDependency(id); - expect(plugin._dependencies).toEqual({}); - expect(plugin._cache).toEqual({ - [path.normalize(path.join(__dirname, '..', 'src'))]: null, + expect(plugin._dependencies).toEqual({}); + expect(plugin._cache).toEqual({ + [path.normalize(path.join(__dirname, '..', 'src'))]: null, + }); }); - }); - it('should try to load pkg without leading NULL character ', () => { - const plugin = licensePlugin(); - const fakePackage = path.join(__dirname, 'fixtures', 'fake-package-1'); - const idNoNull = path.join(fakePackage, 'src', 'index.js'); - const id = '\0' + idNoNull; - const pkg = require(path.join(fakePackage, 'package.json')); + it('should try to load pkg without leading NULL character ', () => { + const pkgPath = path.join(__dirname, 'fixtures', 'fake-package-1'); + const idNoNull = path.join(pkgPath, 'src', 'index.js'); + const id = '\0' + idNoNull; + const pkg = require(path.join(pkgPath, 'package.json')); - plugin.scanDependency(id); + plugin.scanDependency(id); - expect(plugin._dependencies).toEqual({ - 'fake-package': { - name: 'fake-package', - version: '1.0.0', - description: 'Fake package used in unit tests', - license: 'MIT', - licenseText: null, - private: true, - homepage: null, - repository: null, - maintainers: [], - contributors: [], - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - url: null, - }, - }, - }); + expect(plugin._dependencies).toEqual({ + 'fake-package': fakePackage, + }); - expect(plugin._cache).toEqual({ - [path.join(__dirname, 'fixtures', 'fake-package-1', 'src')]: pkg, - [path.join(__dirname, 'fixtures', 'fake-package-1')]: pkg, + expect(plugin._cache).toEqual({ + [path.join(__dirname, 'fixtures', 'fake-package-1', 'src')]: pkg, + [path.join(__dirname, 'fixtures', 'fake-package-1')]: pkg, + }); }); - }); - - it('should load pkg and use the cache if available', () => { - const plugin = licensePlugin(); - const fakePackage = path.join(__dirname, 'fixtures', 'fake-package-1'); - const id = path.join(fakePackage, 'src', 'index.js'); - plugin._cache[path.join(fakePackage, 'src')] = null; - plugin._cache[fakePackage] = null; + it('should load pkg and use the cache if available', () => { + const existsSync = spyOn(fs, 'existsSync').and.callThrough(); + const pkgPath = path.join(__dirname, 'fixtures', 'fake-package-1'); + const id = path.join(pkgPath, 'src', 'index.js'); - spyOn(fs, 'existsSync').and.callThrough(); + plugin._cache[path.join(pkgPath, 'src')] = null; + plugin._cache[pkgPath] = null; - plugin.scanDependency(id); + plugin.scanDependency(id); - expect(plugin._dependencies).toEqual({}); - expect(fs.existsSync).not.toHaveBeenCalled(); + expect(plugin._dependencies).toEqual({}); + expect(existsSync).not.toHaveBeenCalled(); + }); }); - it('should add dependency', () => { - const plugin = licensePlugin(); - const pkg = { - name: 'foo', - version: '0.0.0', - description: 'Fake Description', - main: 'src/index.js', - license: 'MIT', - homepage: 'https://www.google.fr', - private: true, - repository: { - type: 'GIT', - url: 'https://github.com/npm/npm.git', - }, - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - }, - contributors: [ - { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - }, - ], - }; - - plugin.addDependency(pkg); + describe('when adding dependencies', () => { + let plugin; + let pkg; - expect(plugin._dependencies.foo).toBeDefined(); - expect(plugin._dependencies.foo).not.toBe(pkg); - expect(plugin._dependencies).toEqual({ - foo: { + beforeEach(() => { + plugin = licensePlugin(); + pkg = { name: 'foo', version: '0.0.0', description: 'Fake Description', + main: 'src/index.js', license: 'MIT', - licenseText: null, homepage: 'https://www.google.fr', private: true, - maintainers: [], repository: { type: 'GIT', url: 'https://github.com/npm/npm.git', @@ -422,1377 +294,989 @@ describe('LicensePlugin', () => { author: { name: 'Mickael Jeanroy', email: 'mickael.jeanroy@gmail.com', - url: null, }, contributors: [ { name: 'Mickael Jeanroy', email: 'mickael.jeanroy@gmail.com', - url: null, }, ], - }, + }; }); - }); - it('should add dependency and parse author field', () => { - const plugin = licensePlugin(); - const pkg = { - name: 'foo', - version: '0.0.0', - author: 'Mickael Jeanroy (https://mjeanroy.com)', - contributors: [{name: 'Mickael Jeanroy', email: 'mickael.jeanroy@gmail.com'}], - description: 'Fake Description', - main: 'src/index.js', - license: 'MIT', - homepage: 'https://www.google.fr', - private: true, - repository: { - type: 'GIT', - url: 'https://github.com/npm/npm.git', - }, - }; - - plugin.addDependency(pkg); - - expect(plugin._dependencies.foo).toBeDefined(); - expect(plugin._dependencies.foo.author).toEqual({ - name: 'Mickael Jeanroy', - url: 'https://mjeanroy.com', - email: 'mickael.jeanroy@gmail.com', + it('should add dependency', () => { + plugin.addDependency(pkg); + + expect(plugin._dependencies.foo).toBeDefined(); + expect(plugin._dependencies.foo).not.toBe(pkg); + expect(plugin._dependencies).toEqual({ + foo: { + name: 'foo', + version: '0.0.0', + description: 'Fake Description', + license: 'MIT', + licenseText: null, + homepage: 'https://www.google.fr', + private: true, + maintainers: [], + repository: { + type: 'GIT', + url: 'https://github.com/npm/npm.git', + }, + author: { + name: 'Mickael Jeanroy', + email: 'mickael.jeanroy@gmail.com', + url: null, + }, + contributors: [ + { + name: 'Mickael Jeanroy', + email: 'mickael.jeanroy@gmail.com', + url: null, + }, + ], + }, + }); }); - }); - it('should add dependency and parse contributors field as a string', () => { - const plugin = licensePlugin(); - const pkg = { - name: 'foo', - version: '0.0.0', - author: 'Mickael Jeanroy (https://mjeanroy.com)', - contributors: 'Mickael Jeanroy (https://mjeanroy.com)', - description: 'Fake Description', - main: 'src/index.js', - license: 'MIT', - homepage: 'https://www.google.fr', - private: true, - repository: { - type: 'GIT', - url: 'https://github.com/npm/npm.git', - }, - }; - - plugin.addDependency(pkg); - - expect(plugin._dependencies.foo).toBeDefined(); - expect(plugin._dependencies.foo.contributors).toBeDefined(); - expect(plugin._dependencies.foo.contributors.length).toBe(1); - expect(plugin._dependencies.foo.contributors[0]).toEqual({ - name: 'Mickael Jeanroy', - url: 'https://mjeanroy.com', - email: 'mickael.jeanroy@gmail.com', - }); - }); + it('should add dependency and parse author field', () => { + const dependency = Object.assign(pkg, { + author: 'Mickael Jeanroy (https://mjeanroy.com)', + }); - it('should add dependency and parse contributors field', () => { - const plugin = licensePlugin(); - const pkg = { - name: 'foo', - version: '0.0.0', - author: 'Mickael Jeanroy (https://mjeanroy.com)', - description: 'Fake Description', - main: 'src/index.js', - license: 'MIT', - homepage: 'https://www.google.fr', - private: true, - repository: { - type: 'GIT', - url: 'https://github.com/npm/npm.git', - }, - contributors: [ - 'Mickael Jeanroy (https://mjeanroy.com)', - { - name: 'John Doe', - email: 'johndoe@doe.com', - }, - ], - }; + plugin.addDependency(dependency); - plugin.addDependency(pkg); + expect(plugin._dependencies.foo).toBeDefined(); + expect(plugin._dependencies.foo.author).toEqual({ + name: 'Mickael Jeanroy', + url: 'https://mjeanroy.com', + email: 'mickael.jeanroy@gmail.com', + }); + }); - expect(plugin._dependencies.foo).toBeDefined(); - expect(plugin._dependencies.foo.contributors).toBeDefined(); - expect(plugin._dependencies.foo.contributors.length).toBe(2); + it('should add dependency and parse contributors field as a string', () => { + const dependency = Object.assign(pkg, { + contributors: 'Mickael Jeanroy (https://mjeanroy.com)', + }); - expect(plugin._dependencies.foo.contributors[0]).toEqual({ - name: 'Mickael Jeanroy', - url: 'https://mjeanroy.com', - email: 'mickael.jeanroy@gmail.com', - }); + plugin.addDependency(dependency); - expect(plugin._dependencies.foo.contributors[1]).toEqual({ - name: 'John Doe', - email: 'johndoe@doe.com', - url: null, + expect(plugin._dependencies.foo).toBeDefined(); + expect(plugin._dependencies.foo.contributors).toBeDefined(); + expect(plugin._dependencies.foo.contributors.length).toBe(1); + expect(plugin._dependencies.foo.contributors[0]).toEqual({ + name: 'Mickael Jeanroy', + url: 'https://mjeanroy.com', + email: 'mickael.jeanroy@gmail.com', + }); }); - }); - it('should add dependency and parse licenses field', () => { - const plugin = licensePlugin(); - const pkg = { - name: 'foo', - version: '0.0.0', - author: 'Mickael Jeanroy (https://mjeanroy.com)', - description: 'Fake Description', - main: 'src/index.js', - homepage: 'https://www.google.fr', - private: true, - repository: { - type: 'GIT', - url: 'https://github.com/npm/npm.git', - }, - licenses: [ - { - type: 'MIT', - url: 'http://www.opensource.org/licenses/mit-license.php', - }, - { - type: 'Apache-2.0', - url: 'http://opensource.org/licenses/apache2.0.php', - }, - ], - }; + it('should add dependency and parse contributors field', () => { + const contributor1 = 'Mickael Jeanroy (https://mjeanroy.com)'; + const contributor2 = {name: 'John Doe', email: 'johndoe@doe.com'}; + const dependency = Object.assign(pkg, { + contributors: [ + contributor1, + contributor2, + ], + }); - plugin.addDependency(pkg); + plugin.addDependency(dependency); - expect(plugin._dependencies.foo).toBeDefined(); - expect(plugin._dependencies.foo.licenses).not.toBeDefined(); - expect(plugin._dependencies.foo.license).toBe('(MIT OR Apache-2.0)'); - }); + expect(plugin._dependencies.foo).toBeDefined(); + expect(plugin._dependencies.foo.contributors).toBeDefined(); + expect(plugin._dependencies.foo.contributors.length).toBe(2); - it('should not add dependency twice', () => { - const plugin = licensePlugin(); - const pkg = { - name: 'foo', - version: '0.0.0', - description: 'Fake Description', - main: 'src/index.js', - license: 'MIT', - homepage: 'https://www.google.fr', - private: true, - author: { + expect(plugin._dependencies.foo.contributors[0]).toEqual({ name: 'Mickael Jeanroy', + url: 'https://mjeanroy.com', email: 'mickael.jeanroy@gmail.com', - }, - contributors: [ - { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - }, - ], - repository: { - type: 'GIT', - url: 'https://github.com/npm/npm.git', - }, - }; - - plugin.addDependency(pkg); + }); - expect(plugin._dependencies.foo).toBeDefined(); - expect(plugin._dependencies.foo).not.toBe(pkg); - expect(plugin._dependencies).toEqual({ - foo: jasmine.anything(), + expect(plugin._dependencies.foo.contributors[1]).toEqual({ + name: 'John Doe', + email: 'johndoe@doe.com', + url: null, + }); }); - // Try to add the same pkg id + it('should add dependency and parse licenses field', () => { + const mit = {type: 'MIT', url: 'http://www.opensource.org/licenses/mit-license.php'}; + const apache2 = {type: 'Apache-2.0', url: 'http://opensource.org/licenses/apache2.0.php'}; + const dependency = Object.assign(pkg, { + license: null, + licenses: [ + mit, + apache2, + ], + }); - plugin.addDependency(pkg); + plugin.addDependency(dependency); - expect(plugin._dependencies).toEqual({ - foo: jasmine.anything(), + expect(plugin._dependencies.foo).toBeDefined(); + expect(plugin._dependencies.foo.licenses).not.toBeDefined(); + expect(plugin._dependencies.foo.license).toBe('(MIT OR Apache-2.0)'); }); - }); - it('should prepend banner to bundle from a file', () => { - const instance = licensePlugin({ - banner: { - content: { - file: path.join(__dirname, 'fixtures', 'banner.js'), - }, - }, - }); + it('should not add dependency twice', () => { + plugin.addDependency(pkg); - const code = 'var foo = 0;'; + expect(plugin._dependencies.foo).toBeDefined(); + expect(plugin._dependencies.foo).not.toBe(pkg); + expect(plugin._dependencies).toEqual({ + foo: jasmine.anything(), + }); - const result = instance.prependBanner(code); + // Try to add the same pkg id - expect(result).toBeDefined(); - expect(result.map).toBeDefined(); - expect(result.code).toEqual(join([ - '/**', - ' * Test banner.', - ' *', - ' * With a second line.', - ' */', - '', - code, - ])); - }); + plugin.addDependency(pkg); - it('should prepend banner to bundle with custom encoding', () => { - const readFileSync = spyOn(fs, 'readFileSync').and.callThrough(); - const encoding = 'ascii'; - const code = 'var foo = 0;'; - const instance = licensePlugin({ - banner: { - content: { - file: path.join(__dirname, 'fixtures', 'banner.js'), - encoding, - }, - }, - }); - - const result = instance.prependBanner(code); - - expect(readFileSync).toHaveBeenCalledWith(jasmine.any(String), encoding); - expect(result).toBeDefined(); - expect(result.map).toBeDefined(); - expect(result.code).toEqual(join([ - '/**', - ' * Test banner.', - ' *', - ' * With a second line.', - ' */', - '', - code, - ])); + expect(plugin._dependencies).toEqual({ + foo: jasmine.anything(), + }); + }); }); - it('should prepend banner to bundle from (deprecated) file option', () => { - const warn = spyOn(console, 'warn'); - const code = 'var foo = 0;'; - const instance = licensePlugin({ - banner: { - file: path.join(__dirname, 'fixtures', 'banner.js'), - }, - }); - - - const result = instance.prependBanner(code); - - expect(result).toBeDefined(); - expect(result.map).toBeDefined(); - expect(result.code).toEqual(join([ - '/**', - ' * Test banner.', - ' *', - ' * With a second line.', - ' */', - '', - code, - ])); - - expect(warn).toHaveBeenCalledWith( - '[rollup-plugin-license] -- "banner.file" has been deprecated and will be removed in a future version, ' + - 'please use "banner.content.file" instead.' - ); - }); + describe('with banner option', () => { + let warn; + let code; + let bannerJs; + let bannerTxt; - it('should prepend banner to bundle with (deprecated) custom encoding option', () => { - const readFileSync = spyOn(fs, 'readFileSync').and.callThrough(); - const warn = spyOn(console, 'warn'); - - const encoding = 'ascii'; - const instance = licensePlugin({ - banner: { - file: path.join(__dirname, 'fixtures', 'banner.js'), - encoding, - }, - }); - - const code = 'var foo = 0;'; - - const result = instance.prependBanner(code); - - expect(readFileSync).toHaveBeenCalledWith(jasmine.any(String), encoding); - expect(result).toBeDefined(); - expect(result.map).toBeDefined(); - expect(result.code).toEqual(join([ - '/**', - ' * Test banner.', - ' *', - ' * With a second line.', - ' */', - '', - code, - ])); - - expect(warn).toHaveBeenCalledWith( - '[rollup-plugin-license] -- "banner.file" has been deprecated and will be removed in a future version, ' + - 'please use "banner.content.file" instead.' - ); - }); + beforeEach(() => { + warn = spyOn(console, 'warn'); + code = 'var foo = 0;'; + bannerJs = path.join(__dirname, 'fixtures', 'banner.js'); + bannerTxt = path.join(__dirname, 'fixtures', 'banner.txt'); + }); - it('should fail to prepend banner without any content', () => { - const instance = licensePlugin({ - banner: { - commentStyle: 'regular', - content: { + it('should prepend banner to bundle from a file', () => { + const instance = licensePlugin({ + banner: { + content: { + file: bannerJs, + }, }, - }, - }); + }); - expect(() => instance.prependBanner('var foo = 0;')).toThrow(new Error( - '[rollup-plugin-license] -- Cannot find banner content, please specify an inline content, or a path to a file' - )); - }); + const result = instance.prependBanner(code); - it('should prepend banner to bundle with template', () => { - const file = path.join(__dirname, 'fixtures', 'banner.js'); - const tmpl = fs.readFileSync(file, 'utf-8'); - const instance = licensePlugin({ - banner: tmpl, + verifyResult(result); }); - const code = 'var foo = 0;'; - - const result = instance.prependBanner(code); + it('should prepend banner to bundle with custom encoding', () => { + const readFileSync = spyOn(fs, 'readFileSync').and.callThrough(); + const encoding = 'ascii'; + const instance = licensePlugin({ + banner: { + content: { + file: bannerJs, + encoding, + }, + }, + }); - expect(result).toBeDefined(); - expect(result.map).toBeDefined(); - expect(result.code).toEqual(join([ - '/**', - ' * Test banner.', - ' *', - ' * With a second line.', - ' */', - '', - code, - ])); - }); + const result = instance.prependBanner(code); - it('should prepend banner to bundle without source map', () => { - const instance = licensePlugin({ - banner: { - content: { - file: path.join(__dirname, 'fixtures', 'banner.js'), - }, - }, + verifyResult(result); + expect(readFileSync).toHaveBeenCalledWith(jasmine.any(String), encoding); }); - instance.disableSourceMap(); + it('should prepend banner to bundle from (deprecated) file option', () => { + const instance = licensePlugin({ + banner: { + file: bannerJs, + }, + }); - const code = 'var foo = 0;'; - const result = instance.prependBanner(code); + const result = instance.prependBanner(code); - expect(result).toBeDefined(); - expect(result.map).not.toBeDefined(); - expect(result.code).toEqual(join([ - '/**', - ' * Test banner.', - ' *', - ' * With a second line.', - ' */', - '', - code, - ])); - }); + verifyResult(result); + expect(warn).toHaveBeenCalledWith( + '[rollup-plugin-license] -- "banner.file" has been deprecated and will be removed in a future version, ' + + 'please use "banner.content.file" instead.' + ); + }); - it('should not prepend default banner to bundle', () => { - const instance = licensePlugin(); - const code = 'var foo = 0;'; - const result = instance.prependBanner(code); + it('should prepend banner to bundle with (deprecated) custom encoding option', () => { + const readFileSync = spyOn(fs, 'readFileSync').and.callThrough(); + const encoding = 'ascii'; + const instance = licensePlugin({ + banner: { + file: bannerJs, + encoding, + }, + }); - expect(result).toBeDefined(); - expect(result.code).toBeDefined(); - expect(result.map).toBeDefined(); - expect(result.code).toBe(code); - }); + const result = instance.prependBanner(code); - it('should fail if banner file does not exist', () => { - const file = path.join(__dirname, 'fixtures', 'dummy'); - const instance = licensePlugin({ - banner: { - content: { - file, - }, - }, + verifyResult(result); + expect(readFileSync).toHaveBeenCalledWith(jasmine.any(String), encoding); + expect(warn).toHaveBeenCalledWith( + '[rollup-plugin-license] -- "banner.file" has been deprecated and will be removed in a future version, ' + + 'please use "banner.content.file" instead.' + ); }); - expect(() => instance.prependBanner('var foo = 0;')).toThrow(new Error( - `[rollup-plugin-license] -- Template file ${file} does not exist, or cannot be read` - )); - }); - - it('should prepend banner and create block comment', () => { - const code = 'var foo = 0;'; - const instance = licensePlugin({ - banner: { - content: { - file: path.join(__dirname, 'fixtures', 'banner.txt'), + it('should fail to prepend banner without any content', () => { + const instance = licensePlugin({ + banner: { + commentStyle: 'regular', + content: {}, }, - }, + }); + + expect(() => instance.prependBanner('var foo = 0;')).toThrow(new Error( + '[rollup-plugin-license] -- Cannot find banner content, please specify an inline content, or a path to a file' + )); }); - const result = instance.prependBanner(code); + it('should prepend banner to bundle with template', () => { + const tmpl = fs.readFileSync(bannerJs, 'utf-8'); + const instance = licensePlugin({ + banner: tmpl, + }); - expect(result).toBeDefined(); - expect(result.code).toEqual(join([ - '/**', - ' * Test banner.', - ' *', - ' * With a second line.', - ' */', - '', - code, - ])); - }); + const result = instance.prependBanner(code); - it('should prepend banner and create block comment with a custom style', () => { - const instance = licensePlugin({ - banner: { - commentStyle: 'ignored', - content: { - file: path.join(__dirname, 'fixtures', 'banner.txt'), - }, - }, + verifyResult(result); }); - const code = 'var foo = 0;'; + it('should prepend banner to bundle without source map', () => { + const instance = licensePlugin({ + banner: { + content: { + file: bannerJs, + }, + }, + }); - const result = instance.prependBanner(code); + instance.disableSourceMap(); - expect(result).toBeDefined(); - expect(result.code).toEqual(join([ - '/*!', - ' * Test banner.', - ' *', - ' * With a second line.', - ' */', - '', - code, - ])); - }); + const result = instance.prependBanner(code); - it('should prepend banner and create comment with slash style', () => { - const instance = licensePlugin({ - banner: { - commentStyle: 'slash', - content: { - file: path.join(__dirname, 'fixtures', 'banner.txt'), - }, - }, + verifyBanner(result); + expect(result.map).not.toBeDefined(); }); - const code = 'var foo = 0;'; - - const result = instance.prependBanner(code); + it('should not prepend default banner to bundle', () => { + const instance = licensePlugin(); + const result = instance.prependBanner(code); - expect(result).toBeDefined(); - expect(result.code).toEqual(join([ - '//', - '// Test banner.', - '//', - '// With a second line.', - '//', - '', - code, - ])); - }); + expect(result).toBeDefined(); + expect(result.code).toBeDefined(); + expect(result.map).toBeDefined(); + expect(result.code).toBe(code); + }); - it('should prepend banner and create block comment without any style at all', () => { - const instance = licensePlugin({ - banner: { - commentStyle: 'none', - content: { - file: path.join(__dirname, 'fixtures', 'banner.txt'), + it('should fail if banner file does not exist', () => { + const file = path.join(__dirname, 'fixtures', 'dummy'); + const instance = licensePlugin({ + banner: { + content: { + file, + }, }, - }, - }); + }); - const code = 'var foo = 0;'; - - const result = instance.prependBanner(code); - - expect(result).toBeDefined(); - expect(result.code).toEqual(join([ - 'Test banner.', - '', - 'With a second line.', - '', - code, - ])); - }); - - it('should fail to prepend banner if comment style option is unknown', () => { - const instance = licensePlugin({ - banner: { - commentStyle: 'foobar', - content: { - file: path.join(__dirname, 'fixtures', 'banner.txt'), - }, - }, + expect(() => instance.prependBanner(code)).toThrow(new Error( + `[rollup-plugin-license] -- Template file ${file} does not exist, or cannot be read` + )); }); - expect(() => instance.prependBanner('var foo = 0;')).toThrow(new Error( - 'Unknown comment style foobar, please use one of: regular,ignored,slash,none' - )); - }); - - it('should prepend banner to bundle and create sourceMap', () => { - const instance = licensePlugin({ - banner: { - content: { - file: path.join(__dirname, 'fixtures', 'banner.js'), + it('should prepend banner and create block comment', () => { + const instance = licensePlugin({ + banner: { + content: { + file: bannerTxt, + }, }, - }, - }); - - const code = 'var foo = 0;'; - const result = instance.prependBanner(code); + }); - expect(result).toBeDefined(); - expect(result.code).toBeDefined(); - expect(result.map).toBeDefined(); - }); + const result = instance.prependBanner(code); - it('should prepend banner and replace moment variables', () => { - const instance = licensePlugin({ - banner: { - content: { - file: path.join(__dirname, 'fixtures', 'banner-with-moment.js'), - }, - }, + verifyBanner(result); }); - const code = 'var foo = 0;'; - - const result = instance.prependBanner(code); - - expect(result).toBeDefined(); - expect(result.code).toEqual(join([ - '/**', - ` * Date: ${moment().format('YYYY-MM-DD')}`, - ' */', - '', - 'var foo = 0;', - ])); - }); - - it('should prepend banner and replace custom data object', () => { - const instance = licensePlugin({ - banner: { - content: { - file: path.join(__dirname, 'fixtures', 'banner-with-data.js'), - }, - data: { - foo: 'bar', - bar: 'baz', + it('should prepend banner and create block comment with a custom style', () => { + const instance = licensePlugin({ + banner: { + commentStyle: 'ignored', + content: { + file: bannerTxt, + }, }, - }, - }); - - const code = 'var foo = 0;'; + }); - const result = instance.prependBanner(code); + const result = instance.prependBanner(code); - expect(result).toBeDefined(); - expect(result.code).toEqual(join([ - '/**', - ' * Foo: bar', - ' * Bar: baz', - ' */', - '', - 'var foo = 0;', - ])); - }); - - it('should prepend banner and replace custom data function', () => { - const instance = licensePlugin({ - banner: { - content: { - file: path.join(__dirname, 'fixtures', 'banner-with-data.js'), - }, - data() { - return { - foo: 'bar', - bar: 'baz', - }; - }, - }, + expect(result).toBeDefined(); + expect(result.code).toEqual(join([ + '/*!', + ' * Test banner.', + ' *', + ' * With a second line.', + ' */', + '', + code, + ])); }); - const code = 'var foo = 0;'; - - const result = instance.prependBanner(code); + it('should prepend banner and create comment with slash style', () => { + const instance = licensePlugin({ + banner: { + commentStyle: 'slash', + content: { + file: bannerTxt, + }, + }, + }); - expect(result).toBeDefined(); - expect(result.code).toEqual(join([ - '/**', - ' * Foo: bar', - ' * Bar: baz', - ' */', - '', - 'var foo = 0;', - ])); - }); + const result = instance.prependBanner(code); - it('should prepend banner and replace package variables', () => { - const instance = licensePlugin({ - banner: { - content: { - file: path.join(__dirname, 'fixtures', 'banner-with-pkg.js'), - }, - }, + expect(result).toBeDefined(); + expect(result.code).toEqual(join([ + '//', + '// Test banner.', + '//', + '// With a second line.', + '//', + '', + code, + ])); }); - const code = 'var foo = 0;'; + it('should prepend banner and create block comment without any style at all', () => { + const instance = licensePlugin({ + banner: { + commentStyle: 'none', + content: { + file: bannerTxt, + }, + }, + }); - const result = instance.prependBanner(code); + const result = instance.prependBanner(code); - expect(result).toBeDefined(); - expect(result.code).toEqual(join([ - '/**', - ' * Name: rollup-plugin-license', - ' */', - '', - 'var foo = 0;', - ])); - }); + expect(result).toBeDefined(); + expect(result.code).toEqual(join([ + 'Test banner.', + '', + 'With a second line.', + '', + code, + ])); + }); - it('should prepend banner and replace dependencies placeholders', () => { - const instance = licensePlugin({ - banner: { - content: { - file: path.join(__dirname, 'fixtures', 'banner-with-dependencies.js'), + it('should fail to prepend banner if comment style option is unknown', () => { + const instance = licensePlugin({ + banner: { + commentStyle: 'foobar', + content: { + file: bannerTxt, + }, }, - }, - }); - - // Load a dependency - instance.scanDependency(path.join(__dirname, 'fixtures', 'fake-package-1', 'src', 'index.js')); - - const code = 'var foo = 0;'; - const result = instance.prependBanner(code); - - expect(result).toBeDefined(); - expect(result.code).toEqual(join([ - '/**', - ' * Name: rollup-plugin-license', - ' *', - ' * Dependencies:', - ' * ', - ' * fake-package -- MIT', - ' * ', - ' */', - '', - 'var foo = 0;', - ])); - }); + }); - it('should export single dependency', (done) => { - const file = path.join(tmpDir.name, 'third-party.txt'); - const instance = licensePlugin({ - thirdParty: { - output: file, - }, - }); - - instance.addDependency({ - name: 'foo', - version: '1.0.0', - description: 'Foo Package', - license: 'MIT', - private: false, - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - }, + expect(() => instance.prependBanner(code)).toThrow(new Error( + 'Unknown comment style foobar, please use one of: regular,ignored,slash,none' + )); }); - const result = instance.scanThirdParties(); - - expect(result).not.toBeDefined(); - - fs.readFile(file, 'utf-8', (err, content) => { - if (err) { - done.fail(err); - return; - } - - const txt = content.toString(); - expect(txt).toBeDefined(); - expect(txt).toEqual(join([ - 'Name: foo', - 'Version: 1.0.0', - 'License: MIT', - 'Private: false', - 'Description: Foo Package', - 'Author: Mickael Jeanroy ', - ])); + it('should prepend banner to bundle and create sourceMap', () => { + const instance = licensePlugin({ + banner: { + content: { + file: bannerJs, + }, + }, + }); - done(); - }); - }); + const result = instance.prependBanner(code); - it('should export single dependency and create directory if needed', (done) => { - const file = path.join(tmpDir.name, 'output', 'third-party.txt'); - const instance = licensePlugin({ - thirdParty: { - output: file, - }, - }); - - instance.addDependency({ - name: 'foo', - version: '1.0.0', - description: 'Foo Package', - license: 'MIT', - private: false, - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - }, + verifyResult(result); }); - const result = instance.scanThirdParties(); - expect(result).not.toBeDefined(); + it('should prepend banner and replace moment variables', () => { + const instance = licensePlugin({ + banner: { + content: { + file: path.join(__dirname, 'fixtures', 'banner-with-moment.js'), + }, + }, + }); - fs.readFile(file, 'utf-8', (err, content) => { - if (err) { - console.log('Error reading file'); - done.fail(err); - return; - } + const result = instance.prependBanner(code); - const txt = content.toString(); - expect(txt).toBeDefined(); - expect(txt).toEqual(join([ - 'Name: foo', - 'Version: 1.0.0', - 'License: MIT', - 'Private: false', - 'Description: Foo Package', - 'Author: Mickael Jeanroy ', + expect(result).toBeDefined(); + expect(result.code).toEqual(join([ + '/**', + ` * Date: ${moment().format('YYYY-MM-DD')}`, + ' */', + '', + code, ])); - - done(); }); - }); - it('should export list of dependencies to given file', (done) => { - const file = path.join(tmpDir.name, 'third-party.txt'); - const instance = licensePlugin({ - thirdParty: { - output: file, - }, - }); - - instance.addDependency({ - name: 'foo', - version: '1.0.0', - description: 'Foo Package', - license: 'MIT', - private: false, - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - }, - }); - - instance.addDependency({ - name: 'bar', - version: '2.0.0', - description: 'Bar Package', - license: 'Apache 2.0', - private: false, - }); - - const result = instance.scanThirdParties(); - - expect(result).not.toBeDefined(); + it('should prepend banner and replace custom data object', () => { + const instance = licensePlugin({ + banner: { + content: { + file: path.join(__dirname, 'fixtures', 'banner-with-data.js'), + }, + data: { + foo: 'bar', + bar: 'baz', + }, + }, + }); - fs.readFile(file, 'utf-8', (err, content) => { - if (err) { - done.fail(err); - return; - } + const result = instance.prependBanner(code); - const txt = content.toString(); - expect(txt).toBeDefined(); - expect(txt).toEqual(join([ - 'Name: foo', - 'Version: 1.0.0', - 'License: MIT', - 'Private: false', - 'Description: Foo Package', - 'Author: Mickael Jeanroy ', + expect(result).toBeDefined(); + expect(result.code).toEqual(join([ + '/**', + ' * Foo: bar', + ' * Bar: baz', + ' */', '', - '---', - '', - 'Name: bar', - 'Version: 2.0.0', - 'License: Apache 2.0', - 'Private: false', - 'Description: Bar Package', + code, ])); - - done(); }); - }); - it('should export list of dependencies with custom encoding to given file', (done) => { - const file = path.join(tmpDir.name, 'third-party.txt'); - const encoding = 'base64'; - const instance = licensePlugin({ - thirdParty: { - output: { - file, - encoding, + it('should prepend banner and replace custom data function', () => { + const instance = licensePlugin({ + banner: { + content: { + file: path.join(__dirname, 'fixtures', 'banner-with-data.js'), + }, + data() { + return { + foo: 'bar', + bar: 'baz', + }; + }, }, - }, - }); - - instance.addDependency({ - name: 'foo', - version: '1.0.0', - description: 'Foo Package', - license: 'MIT', - }); + }); - const result = instance.scanThirdParties(); + const result = instance.prependBanner(code); - expect(result).not.toBeDefined(); + expect(result).toBeDefined(); + expect(result.code).toEqual(join([ + '/**', + ' * Foo: bar', + ' * Bar: baz', + ' */', + '', + code, + ])); + }); - fs.readFile(file, encoding, (err, content) => { - if (err) { - done.fail(err); - return; - } + it('should prepend banner and replace package variables', () => { + const instance = licensePlugin({ + banner: { + content: { + file: path.join(__dirname, 'fixtures', 'banner-with-pkg.js'), + }, + }, + }); - expect(content).toBeDefined(); - expect(content).toContain('foo'); + const result = instance.prependBanner(code); - done(); + expect(result).toBeDefined(); + expect(result.code).toEqual(join([ + '/**', + ' * Name: rollup-plugin-license', + ' */', + '', + code, + ])); }); - }); - it('should export list of dependencies with (deprecated) custom encoding to given file', (done) => { - const warn = spyOn(console, 'warn'); - const output = path.join(tmpDir.name, 'third-party.txt'); - const encoding = 'base64'; - const instance = licensePlugin({ - thirdParty: { - output, - encoding, - }, - }); + it('should prepend banner and replace dependencies placeholders', () => { + const instance = licensePlugin({ + banner: { + content: { + file: path.join(__dirname, 'fixtures', 'banner-with-dependencies.js'), + }, + }, + }); - instance.addDependency({ - name: 'foo', - version: '1.0.0', - description: 'Foo Package', - license: 'MIT', + // Load a dependency + instance.scanDependency(path.join(__dirname, 'fixtures', 'fake-package-1', 'src', 'index.js')); + + const result = instance.prependBanner(code); + + expect(result).toBeDefined(); + expect(result.code).toEqual(join([ + '/**', + ' * Name: rollup-plugin-license', + ' *', + ' * Dependencies:', + ' * ', + ' * fake-package -- MIT', + ' * ', + ' */', + '', + code, + ])); }); - const result = instance.scanThirdParties(); - - expect(result).not.toBeDefined(); - - fs.readFile(output, encoding, (err, content) => { - if (err) { - done.fail(err); - return; - } - - expect(content).toBeDefined(); - expect(content).toContain('foo'); - expect(warn).toHaveBeenCalledWith( - '[rollup-plugin-license] -- "thirdParty.encoding" has been deprecated and will be removed in a future version, ' + - 'please use "thirdParty.output.encoding" instead.' - ); + function verifyResult(result) { + verifyBanner(result); + expect(result.map).toBeDefined(); + } - done(); - }); + function verifyBanner(result) { + expect(result).toBeDefined(); + expect(result.code).toEqual(join([ + '/**', + ' * Test banner.', + ' *', + ' * With a second line.', + ' */', + '', + code, + ])); + } }); - it('should export default message without any dependencies', (done) => { - const file = path.join(tmpDir.name, 'third-party.txt'); - const instance = licensePlugin({ - thirdParty: { - output: file, - }, - }); - - instance._dependencies = {}; + describe('when scanning third parties', () => { + let pkg1; + let pkg2; - const result = instance.scanThirdParties(); + beforeEach(() => { + pkg1 = { + name: 'foo', + version: '1.0.0', + description: 'Foo Package', + license: 'MIT', + private: false, + author: { + name: 'Mickael Jeanroy', + email: 'mickael.jeanroy@gmail.com', + }, + }; - expect(result).not.toBeDefined(); + pkg2 = { + name: 'bar', + version: '2.0.0', + description: 'Bar Package', + license: 'Apache 2.0', + private: false, + }; + }); - fs.readFile(file, 'utf-8', (err, content) => { - if (err) { - done.fail(err); - return; - } + it('should export single dependency', (done) => { + const file = path.join(tmpDir.name, 'third-party.txt'); + const instance = licensePlugin({ + thirdParty: { + output: file, + }, + }); - const txt = content.toString(); - expect(txt).toBeDefined(); - expect(txt).toEqual('No third parties dependencies'); - done(); - }); - }); + instance.addDependency(pkg1); + instance.scanThirdParties(); - it('should not export private dependencies by default', (done) => { - const file = path.join(tmpDir.name, 'third-party.txt'); - const instance = licensePlugin({ - thirdParty: { - output: file, - }, - }); - - instance.addDependency({ - name: 'foo', - version: '1.0.0', - description: 'Foo Package', - license: 'MIT', - private: false, - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - }, + verifyFile(file, done, (content) => { + verifyDefaultOutput(content); + }); }); - instance.addDependency({ - name: 'bar', - version: '2.0.0', - description: 'Bar Package', - license: 'Apache 2.0', - private: true, - }); + it('should export single dependency and create directory if needed', (done) => { + const file = path.join(tmpDir.name, 'output', 'third-party.txt'); + const instance = licensePlugin({ + thirdParty: { + output: file, + }, + }); - const result = instance.scanThirdParties(); + instance.addDependency(pkg1); + instance.scanThirdParties(); - expect(result).not.toBeDefined(); + verifyFile(file, done, (content) => { + verifyDefaultOutput(content); + }); + }); - fs.readFile(file, 'utf-8', (err, content) => { - if (err) { - done.fail(err); - return; - } - const txt = content.toString(); - expect(txt).toBeDefined(); - expect(txt).toEqual(join([ - 'Name: foo', - 'Version: 1.0.0', - 'License: MIT', - 'Private: false', - 'Description: Foo Package', - 'Author: Mickael Jeanroy ', - ])); + it('should export list of dependencies to given file', (done) => { + const file = path.join(tmpDir.name, 'third-party.txt'); + const instance = licensePlugin({ + thirdParty: { + output: file, + }, + }); - done(); - }); - }); + instance.addDependency(pkg1); + instance.addDependency(pkg2); + instance.scanThirdParties(); - it('should export dependencies to output file if enabled', (done) => { - const file = path.join(tmpDir.name, 'third-party.txt'); - const instance = licensePlugin({ - thirdParty: { - output: file, - includePrivate: true, - }, - }); - - instance.addDependency({ - name: 'foo', - version: '1.0.0', - description: 'Foo Package', - license: 'MIT', - private: false, - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - }, + verifyFile(file, done, (content) => { + expect(content).toEqual(join([ + 'Name: foo', + 'Version: 1.0.0', + 'License: MIT', + 'Private: false', + 'Description: Foo Package', + 'Author: Mickael Jeanroy ', + '', + '---', + '', + 'Name: bar', + 'Version: 2.0.0', + 'License: Apache 2.0', + 'Private: false', + 'Description: Bar Package', + ])); + }); }); - instance.addDependency({ - name: 'bar', - version: '2.0.0', - description: 'Bar Package', - license: 'Apache 2.0', - private: true, - }); + it('should export list of dependencies with custom encoding to given file', (done) => { + const file = path.join(tmpDir.name, 'third-party.txt'); + const encoding = 'base64'; + const instance = licensePlugin({ + thirdParty: { + output: { + file, + encoding, + }, + }, + }); - const result = instance.scanThirdParties(); + instance.addDependency(pkg1); + instance.scanThirdParties(); - expect(result).not.toBeDefined(); + verifyFileWithEncoding(file, encoding, done, (content) => { + expect(content).toContain('foo'); + }); + }); - fs.readFile(file, 'utf-8', (err, content) => { - if (err) { - done.fail(err); - return; - } + it('should export list of dependencies with (deprecated) custom encoding to given file', (done) => { + const warn = spyOn(console, 'warn'); + const output = path.join(tmpDir.name, 'third-party.txt'); + const encoding = 'base64'; + const instance = licensePlugin({ + thirdParty: { + output, + encoding, + }, + }); - const txt = content.toString(); - expect(txt).toBeDefined(); - expect(txt).toEqual(join([ - 'Name: foo', - 'Version: 1.0.0', - 'License: MIT', - 'Private: false', - 'Description: Foo Package', - 'Author: Mickael Jeanroy ', - '', - '---', - '', - 'Name: bar', - 'Version: 2.0.0', - 'License: Apache 2.0', - 'Private: true', - 'Description: Bar Package', - ])); + instance.addDependency(pkg1); + instance.scanThirdParties(); - done(); + verifyFileWithEncoding(output, encoding, done, (content) => { + expect(content).toContain('foo'); + expect(warn).toHaveBeenCalledWith( + '[rollup-plugin-license] -- "thirdParty.encoding" has been deprecated and will be removed in a future version, ' + + 'please use "thirdParty.output.encoding" instead.' + ); + }); }); - }); - it('should export list of dependencies to output file using given template', (done) => { - const file = path.join(tmpDir.name, 'third-party.txt'); - const template = - '<% _.forEach(dependencies, function (dependency) {%>' + - '<%= dependency.name %> => <%= dependency.version %> (<%= dependency.license %>)' + - '<% }) %>'; - - const instance = licensePlugin({ - thirdParty: { - output: { - template, - file, + it('should export default message without any dependencies', (done) => { + const file = path.join(tmpDir.name, 'third-party.txt'); + const instance = licensePlugin({ + thirdParty: { + output: file, }, - }, - }); + }); - instance.addDependency({ - name: 'foo', - version: '1.0.0', - description: 'Foo Package', - license: 'MIT', - private: false, - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - }, - }); + instance._dependencies = {}; + instance.scanThirdParties(); - const result = instance.scanThirdParties(); + verifyFile(file, done, (content) => { + expect(content).toEqual('No third parties dependencies'); + }); + }); - expect(result).not.toBeDefined(); + it('should not export private dependencies by default', (done) => { + const file = path.join(tmpDir.name, 'third-party.txt'); + const instance = licensePlugin({ + thirdParty: { + output: file, + }, + }); - fs.readFile(file, 'utf-8', (err, content) => { - if (err) { - done.fail(err); - return; - } + instance.addDependency(pkg1); + instance.addDependency(Object.assign(pkg2, { + private: true, + })); - const txt = content.toString(); - expect(txt).toBeDefined(); - expect(txt).toEqual('foo => 1.0.0 (MIT)'); + instance.scanThirdParties(); - done(); + verifyFile(file, done, (content) => { + verifyDefaultOutput(content); + }); }); - }); - it('should export list of dependencies to output JSON file using given template', (done) => { - const file = path.join(tmpDir.name, 'third-party.json'); - const template = jasmine.createSpy('template').and.callFake((dependencies) => JSON.stringify(dependencies)); - const instance = licensePlugin({ - thirdParty: { - output: { - template, - file, + it('should export private dependencies to output file if enabled', (done) => { + const file = path.join(tmpDir.name, 'third-party.txt'); + const instance = licensePlugin({ + thirdParty: { + output: file, + includePrivate: true, }, - }, - }); + }); - instance.addDependency({ - name: 'foo', - version: '1.0.0', - description: 'Foo Package', - license: 'MIT', - private: false, - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - }, - }); + instance.addDependency(pkg1); + instance.addDependency(Object.assign(pkg2, { + private: true, + })); - const result = instance.scanThirdParties(); + instance.scanThirdParties(); - expect(result).not.toBeDefined(); + verifyFile(file, done, (content) => { + expect(content).toEqual(join([ + 'Name: foo', + 'Version: 1.0.0', + 'License: MIT', + 'Private: false', + 'Description: Foo Package', + 'Author: Mickael Jeanroy ', + '', + '---', + '', + 'Name: bar', + 'Version: 2.0.0', + 'License: Apache 2.0', + 'Private: true', + 'Description: Bar Package', + ])); + }); + }); - fs.readFile(file, 'utf-8', (err, content) => { - if (err) { - done.fail(err); - return; - } + it('should export list of dependencies to output file using given template', (done) => { + const file = path.join(tmpDir.name, 'third-party.txt'); + const template = + '<% _.forEach(dependencies, function (dependency) {%>' + + '<%= dependency.name %> => <%= dependency.version %> (<%= dependency.license %>)' + + '<% }) %>'; - const txt = content.toString(); - const json = JSON.parse(txt); + const instance = licensePlugin({ + thirdParty: { + output: { + template, + file, + }, + }, + }); - expect(json).toBeDefined(); - expect(json.length).toBe(1); - expect(json[0].name).toBe('foo'); + instance.addDependency(pkg1); + instance.scanThirdParties(); - done(); + verifyFile(file, done, (content) => { + expect(content).toEqual('foo => 1.0.0 (MIT)'); + }); }); - }); - it('should export list of dependencies to output file using given template function', (done) => { - const file = path.join(tmpDir.name, 'third-party.txt'); - const template = jasmine.createSpy('template').and.callFake((dependencies) => ( - dependencies.map((dependency) => `${dependency.name} => ${dependency.version} (${dependency.license})`).join('\n') - )); - - const instance = licensePlugin({ - thirdParty: { - output: { - template, - file, + it('should export list of dependencies to output JSON file using given template', (done) => { + const file = path.join(tmpDir.name, 'third-party.json'); + const template = jasmine.createSpy('template').and.callFake((dependencies) => JSON.stringify(dependencies)); + const instance = licensePlugin({ + thirdParty: { + output: { + template, + file, + }, }, - }, - }); + }); - instance.addDependency({ - name: 'foo', - version: '1.0.0', - description: 'Foo Package', - license: 'MIT', - private: false, - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - }, - }); + instance.addDependency(pkg1); + instance.scanThirdParties(); - const result = instance.scanThirdParties(); + verifyFile(file, done, (content) => { + const json = JSON.parse(content); + expect(json).toBeDefined(); + expect(json.length).toBe(1); + expect(json[0].name).toBe('foo'); + }); + }); - expect(result).not.toBeDefined(); + it('should export list of dependencies to output file using given template function', (done) => { + const file = path.join(tmpDir.name, 'third-party.txt'); + const template = jasmine.createSpy('template').and.callFake((dependencies) => ( + dependencies.map((dependency) => `${dependency.name} => ${dependency.version} (${dependency.license})`).join('\n') + )); - fs.readFile(file, 'utf-8', (err, content) => { - if (err) { - done.fail(err); - return; - } + const instance = licensePlugin({ + thirdParty: { + output: { + template, + file, + }, + }, + }); - const txt = content.toString(); - expect(txt).toBeDefined(); - expect(txt).toEqual('foo => 1.0.0 (MIT)'); - expect(template).toHaveBeenCalled(); + instance.addDependency(pkg1); + instance.scanThirdParties(); - done(); + verifyFile(file, done, (content) => { + expect(content).toEqual('foo => 1.0.0 (MIT)'); + expect(template).toHaveBeenCalled(); + }); }); - }); - it('should not try to export dependencies without output configuration', () => { - const instance = licensePlugin(); + it('should not try to export dependencies without output configuration', () => { + const writeFileSync = spyOn(fs, 'writeFileSync').and.callThrough(); + const instance = licensePlugin(); - instance.addDependency({ - name: 'foo', - version: '1.0.0', - description: 'Foo Package', - license: 'MIT', - private: false, - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - }, - }); + instance.addDependency(pkg1); + instance.addDependency(pkg2); + instance.scanThirdParties(); - instance.addDependency({ - name: 'bar', - version: '2.0.0', - description: 'Bar Package', - license: 'Apache 2.0', - private: true, + expect(writeFileSync).not.toHaveBeenCalled(); }); - spyOn(fs, 'writeFileSync').and.callThrough(); + it('should export list of non-private dependencies to thirdParty function', () => { + const thirdParty = jasmine.createSpy('output'); + const instance = licensePlugin({ + thirdParty, + }); - const result = instance.scanThirdParties(); + instance.addDependency(pkg1); + instance.addDependency(Object.assign(pkg2, { + private: true, + })); - expect(result).not.toBeDefined(); - expect(fs.writeFileSync).not.toHaveBeenCalled(); - }); + instance.scanThirdParties(); - it('should export list of non-private dependencies to thirdParty function', () => { - const thirdParty = jasmine.createSpy('output'); - const instance = licensePlugin({ - thirdParty, + expect(thirdParty).toHaveBeenCalledWith([ + { + name: 'foo', + version: '1.0.0', + description: 'Foo Package', + license: 'MIT', + licenseText: null, + private: false, + homepage: null, + repository: null, + maintainers: [], + contributors: [], + author: { + name: 'Mickael Jeanroy', + email: 'mickael.jeanroy@gmail.com', + url: null, + }, + }, + ]); }); - instance.addDependency({ - name: 'foo', - version: '1.0.0', - description: 'Foo Package', - license: 'MIT', - private: false, - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - }, - }); + it('should export list of non-private dependencies to output function', () => { + const output = jasmine.createSpy('output'); + const instance = licensePlugin({ + thirdParty: { + output, + }, + }); - instance.addDependency({ - name: 'bar', - version: '2.0.0', - description: 'Bar Package', - license: 'Apache 2.0', - private: true, - }); + instance.addDependency(pkg1); + instance.addDependency(Object.assign(pkg2, { + private: true, + })); - const result = instance.scanThirdParties(); + instance.scanThirdParties(); - expect(result).not.toBeDefined(); - expect(thirdParty).toHaveBeenCalledWith([ - { - name: 'foo', - version: '1.0.0', - description: 'Foo Package', - license: 'MIT', - licenseText: null, - private: false, - homepage: null, - repository: null, - maintainers: [], - contributors: [], - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - url: null, + expect(output).toHaveBeenCalledWith([ + { + name: 'foo', + version: '1.0.0', + description: 'Foo Package', + license: 'MIT', + licenseText: null, + private: false, + homepage: null, + repository: null, + maintainers: [], + contributors: [], + author: { + name: 'Mickael Jeanroy', + email: 'mickael.jeanroy@gmail.com', + url: null, + }, }, - }, - ]); - }); - - it('should export list of non-private dependencies to output function', () => { - const output = jasmine.createSpy('output'); - const instance = licensePlugin({ - thirdParty: { - output, - }, - }); - - instance.addDependency({ - name: 'foo', - version: '1.0.0', - description: 'Foo Package', - license: 'MIT', - private: false, - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - }, + ]); }); - instance.addDependency({ - name: 'bar', - version: '2.0.0', - description: 'Bar Package', - license: 'Apache 2.0', - private: true, - }); + it('should export list of dependencies to output function including private if enabled', () => { + const output = jasmine.createSpy('output'); + const includePrivate = true; + const instance = licensePlugin({ + thirdParty: { + output, + includePrivate, + }, + }); - const result = instance.scanThirdParties(); + instance.addDependency(pkg1); + instance.addDependency(Object.assign(pkg2, { + private: true, + })); - expect(result).not.toBeDefined(); - expect(output).toHaveBeenCalledWith([ - { - name: 'foo', - version: '1.0.0', - description: 'Foo Package', - license: 'MIT', - licenseText: null, - private: false, - homepage: null, - repository: null, - maintainers: [], - contributors: [], - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - url: null, - }, - }, - ]); - }); + instance.scanThirdParties(); - it('should export list of dependencies to output function including private if enabled', () => { - const output = jasmine.createSpy('output'); - const includePrivate = true; - const instance = licensePlugin({ - thirdParty: { - output, - includePrivate, - }, - }); - - instance.addDependency({ - name: 'foo', - version: '1.0.0', - description: 'Foo Package', - license: 'MIT', - private: false, - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - }, + expect(output).toHaveBeenCalledWith([ + { + name: 'foo', + version: '1.0.0', + description: 'Foo Package', + license: 'MIT', + licenseText: null, + private: false, + maintainers: [], + contributors: [], + repository: null, + homepage: null, + author: { + name: 'Mickael Jeanroy', + email: 'mickael.jeanroy@gmail.com', + url: null, + }, + }, + { + name: 'bar', + version: '2.0.0', + description: 'Bar Package', + license: 'Apache 2.0', + licenseText: null, + maintainers: [], + contributors: [], + author: null, + repository: null, + homepage: null, + private: true, + }, + ]); }); - instance.addDependency({ - name: 'bar', - version: '2.0.0', - description: 'Bar Package', - license: 'Apache 2.0', - private: true, - }); + function verifyFile(filePath, done, cb) { + verifyFileWithEncoding(filePath, 'utf-8', done, cb); + } - const result = instance.scanThirdParties(); + function verifyFileWithEncoding(filePath, encoding, done, cb) { + fs.readFile(filePath, encoding, (err, content) => { + if (err) { + done.fail(err); + return; + } - expect(result).not.toBeDefined(); - expect(output).toHaveBeenCalledWith([ - { - name: 'foo', - version: '1.0.0', - description: 'Foo Package', - license: 'MIT', - licenseText: null, - private: false, - maintainers: [], - contributors: [], - repository: null, - homepage: null, - author: { - name: 'Mickael Jeanroy', - email: 'mickael.jeanroy@gmail.com', - url: null, - }, - }, - { - name: 'bar', - version: '2.0.0', - description: 'Bar Package', - license: 'Apache 2.0', - licenseText: null, - maintainers: [], - contributors: [], - author: null, - repository: null, - homepage: null, - private: true, - }, - ]); + cb(content.toString()); + done(); + }); + } + + function verifyDefaultOutput(content) { + expect(content).toEqual(join([ + 'Name: foo', + 'Version: 1.0.0', + 'License: MIT', + 'Private: false', + 'Description: Foo Package', + 'Author: Mickael Jeanroy ', + ])); + } }); describe('with license checker', () => {