Skip to content

Commit

Permalink
add tests to busnet get and enhance readme file (hyperledger-archives…
Browse files Browse the repository at this point in the history
…#3394)

Signed-off-by: Nick Lincoln <[email protected]>
  • Loading branch information
nklincoln authored Feb 13, 2018
1 parent 8bd462b commit 1b39b8e
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/generator-hyperledger-composer/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ env:
mocha: true
extends: 'eslint:recommended'
parserOptions:
ecmaVersion: 8
sourceType:
- script
rules:
Expand Down Expand Up @@ -31,7 +32,7 @@ rules:
dot-notation: error
no-tabs: error
no-trailing-spaces: error
# no-use-before-define: error
no-use-before-define: error
no-useless-call: error
no-with: error
operator-linebreak: error
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# <%= namespace%>
# <%= appname%>

<%= appdescription%>
Empty file.
4 changes: 1 addition & 3 deletions packages/generator-hyperledger-composer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
"yeoman-generator"
],
"scripts": {
"test": "npm run archiveTest && npm run liveNetworkTest",
"archiveTest": "mocha -t 0 test/angular-archive.js",
"liveNetworkTest": "mocha -t 0 test/angular-network.js"
"test": "mocha -t 0 test/*.js"
},
"dependencies": {
"composer-client": "0.17.5",
Expand Down
123 changes: 123 additions & 0 deletions packages/generator-hyperledger-composer/test/business-network.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
'use strict';
let path = require('path');
let fs = require('fs');
let assert = require('yeoman-assert');
let helpers = require('yeoman-test');

/**
* Get all files recursively in a directoy
* @param {*} dir directory to search
* @param {*} fileList file list to append
* @returns {*} list of files in directory
*/
function getFiles(dir, fileList){
fileList = fileList || [];
let files = fs.readdirSync(dir);
for(let i in files){
if (!files.hasOwnProperty(i)){
continue;
}
let name = dir + '/' + files[i];
if (fs.statSync(name).isDirectory()){
getFiles(name, fileList);
} else {
fileList.push(name);
}
}
return fileList;
}

describe('hyperledger-composer:businessnetwork for generating a template business network', function () {

let tmpDir;
const passedBusNetName = 'my-template-busnet';
const passedBusNetDescription = 'My busnet description';
const passedNS = 'test.template.namespace';
const passedAuthor = 'MrConga';
const passedEmail = '[email protected]';
const passedLic = 'For exclusive conga';

// Run the business network generator
before(function() {
return helpers.run(path.join(__dirname, '../generators/businessnetwork'))
.inTmpDir(function (dir) {
tmpDir = dir;
})
.withPrompts({
appname: passedBusNetName,
appdescription: passedBusNetDescription,
appauthor: passedAuthor,
appemail: passedEmail,
applicense: passedLic,
namespace: passedNS,
})
.on('error', function (error) {
console.log('Error found:', error);
})
.on('ready', function (generator) {
console.log('About to start generating files..');
console.log('Creating temporary directory:', tmpDir);

})
.on('end', function(){
console.log('Finished generating files');
});
});

it('should create all required business network files within a directory that is the passed bsuness network name', () => {
let busNetDir = tmpDir + '/' + passedBusNetName;
let myExpectedFiles = [
busNetDir + '/.eslintrc.yml',
busNetDir + '/README.md',
busNetDir + '/package.json',
busNetDir + '/models/' + passedNS +'.cto',
busNetDir + '/lib/logic.js',
busNetDir + '/test/logic.js'
];
assert.file(myExpectedFiles);
});

it('should only create required business network files', () => {
let dirFiles = getFiles(tmpDir);

let busNetDir = tmpDir + '/' + passedBusNetName;
let myExpectedFiles = [
busNetDir + '/.eslintrc.yml',
busNetDir + '/README.md',
busNetDir + '/package.json',
busNetDir + '/models/' + passedNS +'.cto',
busNetDir + '/lib/logic.js',
busNetDir + '/test/logic.js'
];

let unexpectedFiles =[];
for (let file of dirFiles){
if(myExpectedFiles.indexOf(file) === -1){
unexpectedFiles.push(file);
}
}

if(unexpectedFiles.length > 0){
assert.fail('Unexpected files generated: ', unexpectedFiles);
}

});

it('should create a README.md file that contains the BusNet name and description as content', () => {
let busNetDir = tmpDir + '/' + passedBusNetName;
let readMeContent = '# ' + passedBusNetName + '\n\n' + passedBusNetDescription;
assert.fileContent(busNetDir + '/README.md', readMeContent);
});

it('should create a package.json file that contains mapped input', () => {
let packageFile = tmpDir + '/' + passedBusNetName + '/package.json';
assert(fs.existsSync(packageFile), 'No package.json file detected in test run');

let myPackage = require(packageFile);
assert(myPackage.name === passedBusNetName, 'incorrect name in packaage file');
assert(myPackage.author === passedAuthor, 'incorrect author in packaage file');
assert(myPackage.email === passedEmail, 'incorrect email in packaage file');
assert(myPackage.license === passedLic, 'incorrect license in packaage file');
});

});

0 comments on commit 1b39b8e

Please sign in to comment.