Skip to content

Commit

Permalink
Update logs with red/green status and handle trait references gracefully
Browse files Browse the repository at this point in the history
This commit updates logs to display red or green based on run status. It also
modifies trait reference handling to return early instead of throwing an error
in both invariant and property testing.
  • Loading branch information
BowTiedRadone committed Jan 8, 2025
1 parent 012aad1 commit 32302df
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 104 deletions.
43 changes: 27 additions & 16 deletions app.tests.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { red } from "ansicolor";
import { main } from "./app";
import { version } from "./package.json";

Expand All @@ -20,8 +21,12 @@ describe("Command-line arguments handling", () => {
--runs - The runs to use for iterating over the tests. Default: 100.
--help - Show the help message.
`;
const noManifestMessage = `\nNo path to Clarinet project provided. Supply it immediately or face the relentless scrutiny of your contract's vulnerabilities.`;
const noContractNameMessage = `\nNo target contract name provided. Please provide the contract name to be fuzzed.`;
const noManifestMessage = red(
`\nNo path to Clarinet project provided. Supply it immediately or face the relentless scrutiny of your contract's vulnerabilities.`
);
const noContractNameMessage = red(
`\nNo target contract name provided. Please provide the contract name to be fuzzed.`
);

it.each([
["manifest path", ["node", "app.js"]],
Expand Down Expand Up @@ -96,56 +101,60 @@ describe("Command-line arguments handling", () => {
[
["no command-line arguments"],
["node", "app.js"],
[
`\nNo path to Clarinet project provided. Supply it immediately or face the relentless scrutiny of your contract's vulnerabilities.`,
helpMessage,
],
[noManifestMessage, helpMessage],
],
[
["manifest path"],
["node", "app.js", "example"],
[
`\nNo target contract name provided. Please provide the contract name to be fuzzed.`,
helpMessage,
],
[noContractNameMessage, helpMessage],
],
[
["manifest path", "contract name"],
["node", "app.js", "example", "counter"],
[
`\nInvalid type provided. Please provide the type of test to be executed. Possible values: test, invariant.`,
red(
`\nInvalid type provided. Please provide the type of test to be executed. Possible values: test, invariant.`
),
helpMessage,
],
],
[
["manifest path", "contract name", "seed"],
["node", "app.js", "example", "counter", "--seed=123"],
[
`\nInvalid type provided. Please provide the type of test to be executed. Possible values: test, invariant.`,
red(
`\nInvalid type provided. Please provide the type of test to be executed. Possible values: test, invariant.`
),
helpMessage,
],
],
[
["manifest path", "contract name", "seed", "path"],
["node", "app.js", "example", "counter", "--seed=123", "--path=84:0"],
[
`\nInvalid type provided. Please provide the type of test to be executed. Possible values: test, invariant.`,
red(
`\nInvalid type provided. Please provide the type of test to be executed. Possible values: test, invariant.`
),
helpMessage,
],
],
[
["manifest path", "contract name", "runs"],
["node", "app.js", "example", "counter", "--runs=10"],
[
`\nInvalid type provided. Please provide the type of test to be executed. Possible values: test, invariant.`,
red(
`\nInvalid type provided. Please provide the type of test to be executed. Possible values: test, invariant.`
),
helpMessage,
],
],
[
["manifest path", "contract name", "path"],
["node", "app.js", "example", "counter", "--path=84:0"],
[
`\nInvalid type provided. Please provide the type of test to be executed. Possible values: test, invariant.`,
red(
`\nInvalid type provided. Please provide the type of test to be executed. Possible values: test, invariant.`
),
helpMessage,
],
],
Expand All @@ -161,7 +170,9 @@ describe("Command-line arguments handling", () => {
"--runs=10",
],
[
`\nInvalid type provided. Please provide the type of test to be executed. Possible values: test, invariant.`,
red(
`\nInvalid type provided. Please provide the type of test to be executed. Possible values: test, invariant.`
),
helpMessage,
],
],
Expand Down
15 changes: 11 additions & 4 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "./shared";
import { issueFirstClassCitizenship } from "./citizen";
import { version } from "./package.json";
import { red } from "ansicolor";

const logger = (log: string, logLevel: "log" | "error" | "info" = "log") => {
console[logLevel](log);
Expand Down Expand Up @@ -42,7 +43,7 @@ const parseOptionalArgument = (argName: string) => {
export async function main() {
const radio = new EventEmitter();
radio.on("logMessage", (log) => logger(log));
radio.on("logFailure", (log) => logger(log, "error"));
radio.on("logFailure", (log) => logger(red(log), "error"));

const args = process.argv;
if (args.includes("--help")) {
Expand All @@ -55,7 +56,9 @@ export async function main() {
if (!manifestDir || manifestDir.startsWith("--")) {
radio.emit(
"logMessage",
"\nNo path to Clarinet project provided. Supply it immediately or face the relentless scrutiny of your contract's vulnerabilities."
red(
"\nNo path to Clarinet project provided. Supply it immediately or face the relentless scrutiny of your contract's vulnerabilities."
)
);
radio.emit("logMessage", helpMessage);
return;
Expand All @@ -66,7 +69,9 @@ export async function main() {
if (!sutContractName || sutContractName.startsWith("--")) {
radio.emit(
"logMessage",
"\nNo target contract name provided. Please provide the contract name to be fuzzed."
red(
"\nNo target contract name provided. Please provide the contract name to be fuzzed."
)
);
radio.emit("logMessage", helpMessage);
return;
Expand All @@ -76,7 +81,9 @@ export async function main() {
if (!type || type.startsWith("--") || !["test", "invariant"].includes(type)) {
radio.emit(
"logMessage",
"\nInvalid type provided. Please provide the type of test to be executed. Possible values: test, invariant."
red(
"\nInvalid type provided. Please provide the type of test to be executed. Possible values: test, invariant."
)
);
radio.emit("logMessage", helpMessage);
return;
Expand Down
9 changes: 2 additions & 7 deletions heatstroke.tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fc from "fast-check";
import { reporter } from "./heatstroke";
import { getContractNameFromRendezvousId } from "./invariant";
import { EventEmitter } from "events";
import { resolve } from "path";
import { initSimnet } from "@hirosystems/clarinet-sdk";
Expand Down Expand Up @@ -105,9 +104,7 @@ describe("Custom reporter logging", () => {
`\nError: Property failed after ${r.numRuns} tests.`,
`Seed : ${r.seed}`,
`\nCounterexample:`,
`- Contract : ${getContractNameFromRendezvousId(
rendezvousContractId
)}`,
`- Contract : ${rendezvousContractId.split(".")[1]}`,
`- Function : ${r.selectedFunction.name} (${r.selectedFunction.access})`,
`- Arguments: ${JSON.stringify(r.functionArgsArb)}`,
`- Caller : ${r.sutCaller[0]}`,
Expand Down Expand Up @@ -235,9 +232,7 @@ describe("Custom reporter logging", () => {
`Seed : ${r.seed}`,
`Path : ${r.path}`,
`\nCounterexample:`,
`- Contract : ${getContractNameFromRendezvousId(
rendezvousContractId
)}`,
`- Contract : ${rendezvousContractId.split(".")[1]}`,
`- Function : ${r.selectedFunction.name} (${r.selectedFunction.access})`,
`- Arguments: ${JSON.stringify(r.functionArgsArb)}`,
`- Caller : ${r.sutCaller[0]}`,
Expand Down
9 changes: 4 additions & 5 deletions heatstroke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
* @property runDetails.error - The error thrown during the test.
*/

import { EventEmitter } from "events";
import { getContractNameFromRendezvousId } from "./invariant";
import { green } from "ansicolor";
import { EventEmitter } from "events";

export function reporter(
//@ts-ignore
Expand All @@ -51,9 +50,7 @@ export function reporter(
radio.emit("logFailure", `\nCounterexample:`);
radio.emit(
"logFailure",
`- Contract : ${getContractNameFromRendezvousId(
r.rendezvousContractId
)}`
`- Contract : ${r.rendezvousContractId.split(".")[1]}`
);
radio.emit(
"logFailure",
Expand Down Expand Up @@ -138,9 +135,11 @@ export function reporter(
} else {
radio.emit(
"logMessage",
green(
`\nOK, ${
type === "invariant" ? "invariants" : "properties"
} passed after ${runDetails.numRuns} runs.\n`
)
);
}
}
31 changes: 1 addition & 30 deletions invariant.tests.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { initSimnet } from "@hirosystems/clarinet-sdk";
import {
getContractNameFromRendezvousId,
initializeClarityContext,
initializeLocalContext,
} from "./invariant";
import { initializeClarityContext, initializeLocalContext } from "./invariant";
import {
getFunctionsFromContractInterfaces,
getSimnetDeployerContractsInterfaces,
} from "./shared";
import { join } from "path";
import { issueFirstClassCitizenship } from "./citizen";
import { Cl } from "@stacks/transactions";
import fc from "fast-check";

describe("Simnet contracts operations", () => {
it("correctly initializes the local context for a given functions map", async () => {
Expand Down Expand Up @@ -97,27 +92,3 @@ describe("Simnet contracts operations", () => {
expect(actualContext).toEqual(expectedContext);
});
});

describe("Rendezvous contract name", () => {
it("gets contract name from Rendezvous contract name", () => {
const addressCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const contractNameCharset =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
fc.assert(
// Arrange
fc.property(
fc.stringOf(fc.constantFrom(...addressCharset)),
fc.stringOf(fc.constantFrom(...contractNameCharset)),
(address, contractName) => {
const rendezvousId = `${address}.${contractName}_rendezvous`;

// Act
const actual = getContractNameFromRendezvousId(rendezvousId);

// Assert
expect(actual).toBe(contractName);
}
)
);
});
});
Loading

0 comments on commit 32302df

Please sign in to comment.