diff --git a/e2e/fabloCommands.test.ts b/e2e/fabloCommands.test.ts index b010927c..0467df2e 100644 --- a/e2e/fabloCommands.test.ts +++ b/e2e/fabloCommands.test.ts @@ -1,5 +1,7 @@ import TestCommands from "./TestCommands"; import { version as currentFabloVersion } from "../package.json"; +import { FabloConfigJson } from "../generators/types/FabloConfigJson"; +import * as fs from "fs"; const commands = new TestCommands("e2e/__tmp__/commands-tests"); @@ -104,6 +106,36 @@ describe("validate", () => { expect(commandResult.output).toContain("commands-tests/fablo-config.json does not exist\n"); expect(commands.getFiles()).toEqual([]); }); + + it("should print validation errors", () => { + // Given + const sourceConfigPath = require.resolve("../samples/fablo-config-hlf2-1org-1chaincode-raft-explorer.json"); + const samplesDir = sourceConfigPath.replace("/fablo-config-hlf2-1org-1chaincode-raft-explorer.json", ""); + const sourceConfig = require(sourceConfigPath) as FabloConfigJson; + + // old schema + sourceConfig.$schema = "https://github.com/hyperledger-labs/fablo/releases/download/1.2.0/schema.json" + + // invalid org + sourceConfig.orgs[0].organization.mspName = "some-org1"; + sourceConfig.orgs[0].organization.name = "some-org1"; + + // save updated config + fs.writeFileSync(`${samplesDir}/invalid-fablo-config.json`, JSON.stringify(sourceConfig, null, 2)); + const fabloConfig = `${commands.relativeRoot}/samples/invalid-fablo-config.json`; + + // When + const commandResult = commands.fabloExec(`validate ${fabloConfig}`); + + // Then + expect(commandResult).toEqual(TestCommands.success()); + expect(commandResult.output).toContain("Critical error occured"); + expect(commandResult.output).toContain("Json schema validation failed!"); + expect(commandResult.output).toContain("instance.$schema : does not exactly match expected constant"); + expect(commandResult.output).toContain(" instance.orgs[0].organization.name : does not match pattern"); + expect(commandResult.output).toContain(" instance.orgs[0].organization.mspName : does not match pattern"); + expect(commands.getFiles()).toEqual([]); + }); }); describe("extend config", () => { diff --git a/src/app/index.ts b/src/app/index.ts index 7548441d..e94ef2b0 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -3,8 +3,8 @@ import * as Generator from "yeoman-generator"; export default class extends Generator { displayInfo(): void { const url = "https://github.com/hyperledger-labs/fablo"; - this.log("This is main entry point for Yeoman app used in Fablo."); - this.log("Visit the project page to get more information."); - this.log(`---\n${url}\n---`); + console.log("This is main entry point for Yeoman app used in Fablo."); + console.log("Visit the project page to get more information."); + console.log(`---\n${url}\n---`); } } diff --git a/src/extend-config/index.ts b/src/extend-config/index.ts index ee099894..6c9b9275 100644 --- a/src/extend-config/index.ts +++ b/src/extend-config/index.ts @@ -22,7 +22,7 @@ class ExtendConfigGenerator extends Generator { const fabloConfigPath = `${this.env.cwd}/${this.options.fabloConfig}`; const json = parseFabloConfig(this.fs.read(fabloConfigPath)); const configExtended = extendConfig(json); - this.log(JSON.stringify(configExtended, undefined, 2)); + console.log(JSON.stringify(configExtended, undefined, 2)); } } diff --git a/src/init/index.ts b/src/init/index.ts index cb550fe6..00073054 100644 --- a/src/init/index.ts +++ b/src/init/index.ts @@ -13,7 +13,7 @@ export default class InitGenerator extends Generator { const shouldInitWithNodeChaincode = this.args.length && this.args.find((v) => v === "node"); if (shouldInitWithNodeChaincode) { - this.log("Creating sample Node.js chaincode"); + console.log("Creating sample Node.js chaincode"); this.fs.copy(this.templatePath("chaincodes"), this.destinationPath("chaincodes")); // force build on Node 12, since dev deps (@theledger/fabric-mock-stub) may not work on 16 this.fs.write(this.destinationPath("chaincodes/chaincode-kv-node/.nvmrc"), "12"); @@ -42,10 +42,10 @@ export default class InitGenerator extends Generator { this.fs.write(this.destinationPath("fablo-config.json"), JSON.stringify(fabloConfigJson, undefined, 2)); this.on("end", () => { - this.log("==========================================================="); - this.log(chalk.bold("Sample config file created! :)")); - this.log("You can start your network with 'fablo up' command"); - this.log("==========================================================="); + console.log("==========================================================="); + console.log(chalk.bold("Sample config file created! :)")); + console.log("You can start your network with 'fablo up' command"); + console.log("==========================================================="); }); } } diff --git a/src/list-compatible-updates/index.ts b/src/list-compatible-updates/index.ts index 2c97cfd5..09266028 100644 --- a/src/list-compatible-updates/index.ts +++ b/src/list-compatible-updates/index.ts @@ -14,13 +14,13 @@ export default class ListCompatibleUpdatesGenerator extends Generator { _printVersions(versionsToPrint: string[]): void { if (versionsToPrint.length > 0) { - this.log(chalk.bold("====== !Compatible Fablo versions found! :) =============")); - this.log(`${chalk.underline.bold("Compatible")} versions:`); - versionsToPrint.forEach((version) => this.log(`- ${version}`)); - this.log(""); - this.log("To update just run command:"); - this.log(`\t${chalk.bold("fablo use [version]")}`); - this.log(chalk.bold("===========================================================")); + console.log(chalk.bold("====== !Compatible Fablo versions found! :) =============")); + console.log(`${chalk.underline.bold("Compatible")} versions:`); + versionsToPrint.forEach((version) => console.log(`- ${version}`)); + console.log(""); + console.log("To update just run command:"); + console.log(`\t${chalk.bold("fablo use [version]")}`); + console.log(chalk.bold("===========================================================")); } } } diff --git a/src/list-versions/index.ts b/src/list-versions/index.ts index 143d399c..a16e7c21 100644 --- a/src/list-versions/index.ts +++ b/src/list-versions/index.ts @@ -9,6 +9,6 @@ export default class ListVersionsGenerator extends Generator { .map((v) => (v === config.fabloVersion ? `${v} <== current` : v)) .map((v) => (config.isFabloVersionSupported(v) && !v.includes("current") ? `${v} (compatible)` : v)); - versionsSortedAndMarked.forEach((version) => this.log(`- ${version}`)); + versionsSortedAndMarked.forEach((version) => console.log(`- ${version}`)); } } diff --git a/src/setup-docker/index.ts b/src/setup-docker/index.ts index 092dfe78..560eb1e8 100644 --- a/src/setup-docker/index.ts +++ b/src/setup-docker/index.ts @@ -48,9 +48,9 @@ export default class SetupDockerGenerator extends Generator { .replace(/[^0-9]+/g, ""); const composeNetworkName = `fablo_network_${dateString}`; - this.log(`Used network config: ${fabloConfigPath}`); - this.log(`Fabric version is: ${global.fabricVersion}`); - this.log(`Generating docker-compose network '${composeNetworkName}'...`); + console.log(`Used network config: ${fabloConfigPath}`); + console.log(`Fabric version is: ${global.fabricVersion}`); + console.log(`Generating docker-compose network '${composeNetworkName}'...`); // ======= fabric-config ============================================================ this._copyOrgCryptoConfig(orgs); @@ -73,9 +73,9 @@ export default class SetupDockerGenerator extends Generator { this._copyHooks(config.hooks); this.on("end", () => { - this.log("Done & done !!! Try the network out: "); - this.log("-> fablo up - to start network"); - this.log("-> fablo help - to view all commands"); + console.log("Done & done !!! Try the network out: "); + console.log("-> fablo up - to start network"); + console.log("-> fablo help - to view all commands"); }); } diff --git a/src/setup-k8s/index.ts b/src/setup-k8s/index.ts index 36a99e88..4c5a9cc8 100644 --- a/src/setup-k8s/index.ts +++ b/src/setup-k8s/index.ts @@ -32,8 +32,8 @@ export default class SetupDockerGenerator extends Generator { .replace(/[^0-9]+/g, ""); const composeNetworkName = `fablo_network_${dateString}`; - this.log(`Used network config: ${fabloConfigPath}`); - this.log(`Fabric version is: ${global.fabricVersion}`); + console.log(`Used network config: ${fabloConfigPath}`); + console.log(`Fabric version is: ${global.fabricVersion}`); this._copyGitIgnore(); @@ -49,9 +49,9 @@ export default class SetupDockerGenerator extends Generator { this._copyHooks(config.hooks); this.on("end", () => { - this.log("Done & done !!! Try the network out: "); - this.log("-> fablo up - to start network"); - this.log("-> fablo help - to view all commands"); + console.log("Done & done !!! Try the network out: "); + console.log("-> fablo up - to start network"); + console.log("-> fablo help - to view all commands"); }); } diff --git a/src/validate/index.ts b/src/validate/index.ts index d25fcf55..98c80bca 100644 --- a/src/validate/index.ts +++ b/src/validate/index.ts @@ -74,8 +74,8 @@ class ValidateGenerator extends Generator { }); this.addListener(validationErrorType.CRITICAL, (event) => { - this.log(chalk.bold.bgRed("Critical error occured:")); - this.log(chalk.bold(`- ${event.message}`)); + console.log(chalk.bold.bgRed("Critical error occured:")); + console.log(chalk.bold(`- ${event.message}`)); this._printIfNotEmpty(this.errors.getAllMessages(), chalk.red.bold("Errors found:")); process.exit(1); }); @@ -123,19 +123,19 @@ class ValidateGenerator extends Generator { } async shortSummary() { - this.log(`Validation errors count: ${this.errors.count()}`); - this.log(`Validation warnings count: ${this.warnings.count()}`); - this.log(chalk.bold("===========================================================")); + console.log(`Validation errors count: ${this.errors.count()}`); + console.log(`Validation warnings count: ${this.warnings.count()}`); + console.log(chalk.bold("===========================================================")); } async detailedSummary() { const allValidationMessagesCount = this.errors.count() + this.warnings.count(); if (allValidationMessagesCount > 0) { - this.log(chalk.bold("=================== Validation summary ====================")); + console.log(chalk.bold("=================== Validation summary ====================")); this._printIfNotEmpty(this.errors.getAllMessages(), chalk.red.bold("Errors found:")); this._printIfNotEmpty(this.warnings.getAllMessages(), chalk.yellow("Warnings found:")); - this.log(chalk.bold("===========================================================")); + console.log(chalk.bold("===========================================================")); } if (this.errors.count() > 0) { @@ -179,13 +179,13 @@ class ValidateGenerator extends Generator { _printIfNotEmpty(messages: Message[], caption: string) { if (messages.length > 0) { - this.log(caption); + console.log(caption); const grouped: Record = _.groupBy(messages, (msg) => msg.category); Object.keys(grouped).forEach((key) => { - this.log(chalk.bold(` ${key}:`)); - grouped[key].forEach((msg) => this.log(` - ${msg.message}`)); + console.log(chalk.bold(` ${key}:`)); + grouped[key].forEach((msg) => console.log(` - ${msg.message}`)); }); } } diff --git a/src/version/index.ts b/src/version/index.ts index b3cfe779..e5b1680e 100644 --- a/src/version/index.ts +++ b/src/version/index.ts @@ -12,9 +12,9 @@ export default class VersionGenerator extends Generator { async printVersion(): Promise { if (typeof this.options.verbose !== "undefined") { - this.log(JSON.stringify(fullInfo(), null, 2)); + console.log(JSON.stringify(fullInfo(), null, 2)); } else { - this.log(JSON.stringify(basicInfo(), null, 2)); + console.log(JSON.stringify(basicInfo(), null, 2)); } } }