-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from UUX-Brasil/dev
Dev > master release v3.0.0
- Loading branch information
Showing
7 changed files
with
211 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"header": "THIRD-PARTY SOFTWARE NOTICES AND INFORMATION\n\nNote: OTTN is not the author of the files below.\n\nThe notices below are provided for informational purposes only and are not\nthe license terms under which OTTN distributes these files.\n\n", | ||
"file": "LICENSE-3RD-PARTY.txt", | ||
"skipPrefix": "spotlight@", | ||
"allowedLicenseList": | ||
[ | ||
"MIT", "MIT*", "Apache-2.0", "Apache-2.0*", "BSD-3-Clause", "BSD-3-Clause*", | ||
"BSD", "BSD*", "ISC", "ISC*", "AFLv2.1", "Unlicense", "WTFPL", "BSD-2-Clause", | ||
"(MIT AND CC-BY-3.0)", "Public Domain", "Apache License, Version 2.0" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
|
||
class LicenseFileHandler | ||
{ | ||
static getMissingFields(obj) | ||
{ | ||
let missingFields = []; | ||
|
||
if(!obj.header) missingFields.push('header'); | ||
if(!obj.file) missingFields.push('file'); | ||
if(!obj.skipPrefix) missingFields.push('skipPrefix'); | ||
if(!obj.allowedLicenseList) missingFields.push('allowedLicenseList'); | ||
|
||
return missingFields; | ||
} | ||
|
||
static getFile(filename) | ||
{ | ||
return new Promise((resolve, reject) => { | ||
return fs.readFile(filename, 'utf8', (err, data) => { | ||
if (err) | ||
return reject( | ||
{ | ||
'errorCode': 'LICENSE_CONFIG_NOT_FOUND', | ||
'errorMessage': err.toString() | ||
}); | ||
return resolve(data); | ||
}); | ||
}); | ||
} | ||
|
||
static parseFile(arg) | ||
{ | ||
return new Promise((resolve, reject) => { | ||
let object = null; | ||
|
||
try { | ||
object = JSON.parse(arg); | ||
} catch (e) { | ||
return reject( | ||
{ | ||
'errorCode': 'LICENSE_CONFIG_CORRUPTED', | ||
'errorMessage': 'License config file is not a valid JSON.' | ||
}); | ||
} | ||
|
||
let missingFields = LicenseFileHandler.getMissingFields(object); | ||
|
||
if (missingFields.length === 0) | ||
return resolve(object); | ||
return reject( | ||
{ | ||
'errorCode': 'LICENSE_CONFIG_CORRUPTED', | ||
'errorMessage': 'License config file does not have the parameters: ' | ||
+ missingFields.toString() | ||
+ '.' | ||
}); | ||
}); | ||
} | ||
|
||
static getConfig(filename) | ||
{ | ||
return LicenseFileHandler.getFile(filename ? filename : '.license-config'); | ||
} | ||
} | ||
|
||
module.exports = LicenseFileHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"header": "MY SUPER DUPER LICENSE 3000", | ||
"file": "ULTRA-LICENSE-DO-MILHAO.txt", | ||
"skipPrefix": "prefix@", | ||
"allowedLicenseList": | ||
[ | ||
"MIT", "MIT*", "Apache-2.0", "Apache-2.0*", "BSD-3-Clause", "BSD-3-Clause*", | ||
"BSD", "BSD*", "ISC", "ISC*", "AFLv2.1", "Unlicense", "WTFPL", "BSD-2-Clause", | ||
"(MIT AND CC-BY-3.0)", "Public Domain" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
'use strict'; | ||
/* global describe, it */ | ||
|
||
const should = require('should'); // eslint-disable-line no-unused-vars | ||
const LicenseFileHandler = require('../../../lib/cmd/license-file-handler'); | ||
|
||
const default_config = '{\n\ | ||
\"header\": \"THIRD-PARTY SOFTWARE NOTICES AND INFORMATION\\n\\n\ | ||
Note: OTTN is not the author of the files below.\\n\\n\ | ||
The notices below are provided for informational purposes only and are not\\n\ | ||
the license terms under which OTTN distributes these files.\\n\\n\",\n\ | ||
\"file\": \"LICENSE-3RD-PARTY.txt\",\n\ | ||
\"skipPrefix\": \"spotlight@\",\n\ | ||
\"allowedLicenseList\":\n\ | ||
[\n\ | ||
\"MIT\", \"MIT*\", \"Apache-2.0\", \"Apache-2.0*\", \"BSD-3-Clause\", \"BSD-3-Clause*\",\n\ | ||
\"BSD\", \"BSD*\", \"ISC\", \"ISC*\", \"AFLv2.1\", \"Unlicense\", \"WTFPL\", \"BSD-2-Clause\",\n\ | ||
\"(MIT AND CC-BY-3.0)\", \"Public Domain\", "Apache License, Version 2.0"\n\ | ||
]\n\ | ||
}\n'; | ||
|
||
const custom_config = '{\n\ | ||
\"header\": \"MY SUPER DUPER LICENSE 3000\",\n\ | ||
\"file\": \"ULTRA-LICENSE-DO-MILHAO.txt\",\n\ | ||
\"skipPrefix\": \"prefix@\",\n\ | ||
\"allowedLicenseList\":\n\ | ||
[\n\ | ||
\"MIT\", \"MIT*\", \"Apache-2.0\", \"Apache-2.0*\", \"BSD-3-Clause\", \"BSD-3-Clause*\",\n\ | ||
\"BSD\", \"BSD*\", \"ISC\", \"ISC*\", \"AFLv2.1\", \"Unlicense\", \"WTFPL\", \"BSD-2-Clause\",\n\ | ||
\"(MIT AND CC-BY-3.0)\", \"Public Domain\"\n\ | ||
]\n\ | ||
}\n'; | ||
|
||
|
||
describe('LicenseFileHandler', function() { | ||
describe('.getFile', function() { | ||
it('get default license file (/.license-config)', function() { | ||
return LicenseFileHandler | ||
.getConfig() | ||
.then((content) => { | ||
content.should.be.eql(default_config); | ||
}); | ||
}); | ||
|
||
it('get custom license file (/mylicense.json)', function() { | ||
return LicenseFileHandler | ||
.getFile('mylicense.json') | ||
.then((content) => { | ||
content.should.be.eql(custom_config); | ||
}); | ||
}); | ||
|
||
it('get and parse default license file (/.license-config)', function() { | ||
let test = null; | ||
return LicenseFileHandler | ||
.getConfig() | ||
.then((content) => { | ||
return LicenseFileHandler.parseFile(content); | ||
}) | ||
.then((obj) => { | ||
test = obj; | ||
return LicenseFileHandler.parseFile(default_config); | ||
}) | ||
.then((comparison) => { | ||
test.should.be.eql(comparison); | ||
}); | ||
}); | ||
|
||
it('get and parse custom license file (/mylicense.json)', function() { | ||
return LicenseFileHandler | ||
.getFile('mylicense.json') | ||
.then((content) => { | ||
let test = LicenseFileHandler.parseFile(content); | ||
let comparison = LicenseFileHandler.parseFile(custom_config); | ||
|
||
test.should.be.eql(comparison); | ||
}); | ||
}); | ||
|
||
it('fails when given an invalid file path', function() { | ||
return LicenseFileHandler | ||
.getFile('aaaaaaaa') | ||
.should.be.rejectedWith({'errorCode': 'LICENSE_CONFIG_NOT_FOUND'}); | ||
}); | ||
|
||
it('fails when given an invalid JSON object', function() { | ||
return LicenseFileHandler | ||
.parseFile('aaaaaaaa') | ||
.should.be.rejectedWith({'errorCode': 'LICENSE_CONFIG_CORRUPTED'}); | ||
}); | ||
|
||
it('fails when given a valid JSON object without the needed parameters', function() { | ||
return LicenseFileHandler | ||
.parseFile('{\"header\":\"asas\"}') | ||
.should.be.rejectedWith({'errorCode': 'LICENSE_CONFIG_CORRUPTED'}); | ||
}); | ||
}); | ||
}); |