Skip to content

Commit

Permalink
Fix errors when yeoman is printing messages
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Dzikowski <[email protected]>
  • Loading branch information
dzikowski committed Oct 15, 2024
1 parent 548a681 commit 4292ce8
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 40 deletions.
32 changes: 32 additions & 0 deletions e2e/fabloCommands.test.ts
Original file line number Diff line number Diff line change
@@ -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");

Expand Down Expand Up @@ -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", () => {
Expand Down
6 changes: 3 additions & 3 deletions src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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---`);
}
}
2 changes: 1 addition & 1 deletion src/extend-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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("===========================================================");
});
}
}
14 changes: 7 additions & 7 deletions src/list-compatible-updates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("==========================================================="));
}
}
}
2 changes: 1 addition & 1 deletion src/list-versions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`));
}
}
12 changes: 6 additions & 6 deletions src/setup-docker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
});
}

Expand Down
10 changes: 5 additions & 5 deletions src/setup-k8s/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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");
});
}

Expand Down
20 changes: 10 additions & 10 deletions src/validate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<string, Message[]> = _.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}`));
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/version/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export default class VersionGenerator extends Generator {

async printVersion(): Promise<void> {
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));
}
}
}

0 comments on commit 4292ce8

Please sign in to comment.