Skip to content

Commit

Permalink
Replace path.join with path.resolve for absolute path handling
Browse files Browse the repository at this point in the history
This commit updates the codebase to use path.resolve instead of path.join for
path resolution consistency. The changes include:

- Replacing `join` with `resolve` across the app.
- Ensuring all paths, such as manifest directories, deployment plans, and
  contract sources, are resolved to absolute paths.
- Updating related variable names and comments to reflect the new behavior.

# Conflicts:
#	app.tests.ts
#	app.ts
  • Loading branch information
moodmosaic committed Jan 7, 2025
1 parent bd9c2ae commit ed940c0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 55 deletions.
68 changes: 37 additions & 31 deletions citizen.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
scheduleRendezvous,
} from "./citizen";
import { initSimnet } from "@hirosystems/clarinet-sdk";
import { join } from "path";
import { resolve } from "path";
import { cpSync, existsSync, mkdtempSync, readFileSync, rmSync } from "fs";
import { tmpdir } from "os";
import yaml from "yaml";
Expand Down Expand Up @@ -40,16 +40,16 @@ describe("Simnet deployment plan operations", () => {

it("retrieves the simnet deployment plan", async () => {
// Setup
const tempDir = mkdtempSync(join(tmpdir(), "simnet-test-"));
const tempDir = mkdtempSync(resolve(tmpdir(), "simnet-test-"));
cpSync(manifestDir, tempDir, { recursive: true });
const simnetPlanPath = join(tempDir, "deployments", simnetPlanFileName);
const simnetPlanPath = resolve(tempDir, "deployments", simnetPlanFileName);

rmSync(simnetPlanPath, { force: true });
if (existsSync(simnetPlanPath)) {
throw new Error("Simnet plan file already exists");
}

const manifestPath = join(tempDir, manifestFileName);
const manifestPath = resolve(tempDir, manifestFileName);
await initSimnet(manifestPath);

// Exercise
Expand All @@ -66,10 +66,10 @@ describe("Simnet deployment plan operations", () => {

it("groups the contracts by epoch", async () => {
// Setup
const tempDir = mkdtempSync(join(tmpdir(), "simnet-test-"));
const tempDir = mkdtempSync(resolve(tmpdir(), "simnet-test-"));
cpSync(manifestDir, tempDir, { recursive: true });
const simnetPlanPath = join(tempDir, "deployments", simnetPlanFileName);
const manifestPath = join(tempDir, manifestFileName);
const simnetPlanPath = resolve(tempDir, "deployments", simnetPlanFileName);
const manifestPath = resolve(tempDir, manifestFileName);

await initSimnet(manifestPath);
const parsedSimnetPlan = yaml.parse(
Expand Down Expand Up @@ -117,9 +117,9 @@ describe("Simnet deployment plan operations", () => {

it("retrieves the test contract source from the simnet deployment plan", () => {
// Setup
const tempDir = mkdtempSync(join(tmpdir(), "simnet-test-"));
const tempDir = mkdtempSync(resolve(tmpdir(), "simnet-test-"));
cpSync(manifestDir, tempDir, { recursive: true });
const simnetPlanPath = join(tempDir, "deployments", simnetPlanFileName);
const simnetPlanPath = resolve(tempDir, "deployments", simnetPlanFileName);

const parsedSimnetPlan = yaml.parse(
readFileSync(simnetPlanPath, { encoding: "utf-8" }).toString()
Expand All @@ -130,7 +130,7 @@ describe("Simnet deployment plan operations", () => {

// Verify
const expected = readFileSync(
join("example", "contracts", "counter.tests.clar"),
resolve("example", "contracts", "counter.tests.clar"),
{ encoding: "utf-8" }
);

Expand All @@ -142,10 +142,10 @@ describe("Simnet deployment plan operations", () => {

it("builds the rendezvous data", async () => {
// Setup
const tempDir = mkdtempSync(join(tmpdir(), "simnet-test-"));
const tempDir = mkdtempSync(resolve(tmpdir(), "simnet-test-"));
cpSync(manifestDir, tempDir, { recursive: true });
const simnetPlanPath = join(tempDir, "deployments", simnetPlanFileName);
const manifestPath = join(tempDir, manifestFileName);
const simnetPlanPath = resolve(tempDir, "deployments", simnetPlanFileName);
const manifestPath = resolve(tempDir, manifestFileName);

await initSimnet(manifestPath);

Expand All @@ -167,11 +167,11 @@ describe("Simnet deployment plan operations", () => {

// Verify
const counterSrc = readFileSync(
join("example", "contracts", "counter.clar"),
resolve("example", "contracts", "counter.clar"),
{ encoding: "utf-8" }
);
const counterTestsSrc = readFileSync(
join("example", "contracts", "counter.tests.clar"),
resolve("example", "contracts", "counter.tests.clar"),
{ encoding: "utf-8" }
);
const rendezvousSrc = scheduleRendezvous(counterSrc, counterTestsSrc);
Expand All @@ -185,10 +185,10 @@ describe("Simnet deployment plan operations", () => {

it("retrieves the contract source for a target contract", async () => {
// Setup
const tempDir = mkdtempSync(join(tmpdir(), "simnet-test-"));
const tempDir = mkdtempSync(resolve(tmpdir(), "simnet-test-"));
cpSync(manifestDir, tempDir, { recursive: true });
const simnetPlanPath = join(tempDir, "deployments", simnetPlanFileName);
const manifestPath = join(tempDir, manifestFileName);
const simnetPlanPath = resolve(tempDir, "deployments", simnetPlanFileName);
const manifestPath = resolve(tempDir, manifestFileName);

await initSimnet(manifestPath);

Expand Down Expand Up @@ -228,11 +228,11 @@ describe("Simnet deployment plan operations", () => {

// Verify
const counterSrc = readFileSync(
join("example", "contracts", "counter.clar"),
resolve("example", "contracts", "counter.clar"),
{ encoding: "utf-8" }
);
const counterTestsSrc = readFileSync(
join("example", "contracts", "counter.tests.clar"),
resolve("example", "contracts", "counter.tests.clar"),
{ encoding: "utf-8" }
);
const expected = scheduleRendezvous(counterSrc, counterTestsSrc);
Expand All @@ -245,10 +245,10 @@ describe("Simnet deployment plan operations", () => {

it("retrieves the contract source for a non-target contract", async () => {
// Setup
const tempDir = mkdtempSync(join(tmpdir(), "simnet-test-"));
const tempDir = mkdtempSync(resolve(tmpdir(), "simnet-test-"));
cpSync(manifestDir, tempDir, { recursive: true });
const simnetPlanPath = join(tempDir, "deployments", simnetPlanFileName);
const manifestPath = join(tempDir, manifestFileName);
const simnetPlanPath = resolve(tempDir, "deployments", simnetPlanFileName);
const manifestPath = resolve(tempDir, manifestFileName);

await initSimnet(manifestPath);

Expand Down Expand Up @@ -287,9 +287,12 @@ describe("Simnet deployment plan operations", () => {
);

// Verify
const expected = readFileSync(join("example", "contracts", "cargo.clar"), {
encoding: "utf-8",
});
const expected = readFileSync(
resolve("example", "contracts", "cargo.clar"),
{
encoding: "utf-8",
}
);

expect(actual).toBe(expected);

Expand All @@ -299,19 +302,22 @@ describe("Simnet deployment plan operations", () => {

it("issues first-class citizenship", async () => {
// Setup
const tempDir = mkdtempSync(join(tmpdir(), "simnet-test-"));
const tempDir = mkdtempSync(resolve(tmpdir(), "simnet-test-"));
cpSync(manifestDir, tempDir, { recursive: true });

// Exercise
const firstClassSimnet = await issueFirstClassCitizenship(tempDir, "cargo");
const actual = firstClassSimnet.getContractSource("cargo");

// Verify
const cargoSrc = readFileSync(join("example", "contracts", "cargo.clar"), {
encoding: "utf-8",
});
const cargoSrc = readFileSync(
resolve("example", "contracts", "cargo.clar"),
{
encoding: "utf-8",
}
);
const cargoTestsSrc = readFileSync(
join("example", "contracts", "cargo.tests.clar"),
resolve("example", "contracts", "cargo.tests.clar"),
{ encoding: "utf-8" }
);
const expected = scheduleRendezvous(cargoSrc, cargoTestsSrc);
Expand Down
47 changes: 25 additions & 22 deletions citizen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync } from "fs";
import { join } from "path";
import { resolve } from "path";
import yaml from "yaml";
import { initSimnet, Simnet } from "@hirosystems/clarinet-sdk";
import { EpochString } from "@hirosystems/clarinet-sdk-wasm";
Expand All @@ -18,26 +18,29 @@ import {
* - Combining the target contract with its tests and deploying all contracts
* to the simnet.
*
* @param manifestDir - The relative path to the manifest directory.
* @param absoluteManifestDir - The absolute path to the manifest directory.
* @param sutContractName - The target contract name.
* @returns The initialized simnet instance with all contracts deployed, with
* the target contract treated as a first-class citizen.
*/
export const issueFirstClassCitizenship = async (
manifestDir: string,
absoluteManifestDir: string,
sutContractName: string
): Promise<Simnet> => {
const manifestPath = join(manifestDir, "Clarinet.toml");
const manifestPath = resolve(absoluteManifestDir, "Clarinet.toml");

// Initialize the simnet, to generate the simnet plan and instance. The empty
// session will be set up, and contracts will be deployed in the correct
// order based on the simnet plan a few lines below.
const simnet = await initSimnet(manifestPath);

const simnetPlan = yaml.parse(
readFileSync(join(manifestDir, "deployments", "default.simnet-plan.yaml"), {
encoding: "utf-8",
})
readFileSync(
resolve(absoluteManifestDir, "deployments", "default.simnet-plan.yaml"),
{
encoding: "utf-8",
}
)
);

const sortedContractsByEpoch =
Expand All @@ -51,7 +54,7 @@ export const issueFirstClassCitizenship = async (
const rendezvousSources = new Map(
[sutContractName]
.map((contractName) =>
buildRendezvousData(simnetPlan, contractName, manifestDir)
buildRendezvousData(simnetPlan, contractName, absoluteManifestDir)
)
.map((rendezvousContractData) => [
rendezvousContractData.rendezvousContractName,
Expand All @@ -66,7 +69,7 @@ export const issueFirstClassCitizenship = async (
rendezvousSources,
name,
props,
manifestDir
absoluteManifestDir
)
);

Expand Down Expand Up @@ -164,7 +167,7 @@ const deployContracts = async (
* @param rendezvousMap - The rendezvous map.
* @param contractName - The contract name.
* @param contractProps - The contract properties.
* @param manifestDir - The relative path to the manifest directory.
* @param absoluteManifestDir - The absolute path to the manifest directory.
* @returns The contract source.
*/
export const getContractSource = (
Expand All @@ -175,7 +178,7 @@ export const getContractSource = (
path: string;
clarity_version: 1 | 2 | 3;
},
manifestDir: string
absoluteManifestDir: string
): string => {
if (sutContractNames.includes(contractName)) {
const contractSource = rendezvousMap.get(contractName);
Expand All @@ -184,7 +187,7 @@ export const getContractSource = (
}
return contractSource;
} else {
return readFileSync(join(manifestDir, contractProps.path), {
return readFileSync(resolve(absoluteManifestDir, contractProps.path), {
encoding: "utf-8",
});
}
Expand All @@ -194,26 +197,26 @@ export const getContractSource = (
* Build the Rendezvous data.
* @param simnetPlan The parsed simnet plan.
* @param contractName The contract name.
* @param manifestDir The relative path to the manifest directory.
* @param absoluteManifestDir The absolute path to the manifest directory.
* @returns The Rendezvous data representing an object. The returned object
* contains the Rendezvous source code and the Rendezvous contract name.
*/
export const buildRendezvousData = (
simnetPlan: SimnetPlan,
contractName: string,
manifestDir: string
absoluteManifestDir: string
) => {
try {
const sutContractSource = getSimnetPlanContractSource(
simnetPlan,
manifestDir,
absoluteManifestDir,
contractName
);

const testContractSource = getTestContractSource(
simnetPlan,
contractName,
manifestDir
absoluteManifestDir
);

const rendezvousSource = scheduleRendezvous(
Expand All @@ -235,13 +238,13 @@ export const buildRendezvousData = (
/**
* Get the contract source code from the simnet plan.
* @param simnetPlan The parsed simnet plan.
* @param manifestDir The relative path to the manifest directory.
* @param absoluteManifestDir The absolute path to the manifest directory.
* @param sutContractName The target contract name.
* @returns The contract source code.
*/
const getSimnetPlanContractSource = (
simnetPlan: SimnetPlan,
manifestDir: string,
absoluteManifestDir: string,
sutContractName: string
) => {
// Filter for transactions that contain "emulated-contract-publish".
Expand All @@ -260,7 +263,7 @@ const getSimnetPlanContractSource = (
);
}

return readFileSync(join(manifestDir, contractInfo.path), {
return readFileSync(resolve(absoluteManifestDir, contractInfo.path), {
encoding: "utf-8",
}).toString();
};
Expand All @@ -269,13 +272,13 @@ const getSimnetPlanContractSource = (
* Get the test contract source code.
* @param simnetPlan The parsed simnet plan.
* @param sutContractName The target contract name.
* @param manifestDir The relative path to the manifest directory.
* @param absoluteManifestDir The absolute path to the manifest directory.
* @returns The test contract source code.
*/
export const getTestContractSource = (
simnetPlan: SimnetPlan,
sutContractName: string,
manifestDir: string
absoluteManifestDir: string
): string => {
const contractInfo = simnetPlan.plan.batches
.flatMap((batch: Batch) => batch.transactions)
Expand All @@ -301,7 +304,7 @@ export const getTestContractSource = (
);

try {
return readFileSync(join(manifestDir, testContractPath), {
return readFileSync(resolve(absoluteManifestDir, testContractPath), {
encoding: "utf-8",
}).toString();
} catch (e: any) {
Expand Down
4 changes: 2 additions & 2 deletions invariant.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import {
getFunctionsFromContractInterfaces,
getSimnetDeployerContractsInterfaces,
} from "./shared";
import { join } from "path";
import { resolve } 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 () => {
// Arrange
const manifestPath = join("example", "Clarinet.toml");
const manifestPath = resolve("example", "Clarinet.toml");
const simnet = await initSimnet(manifestPath);
const sutContractsInterfaces = getSimnetDeployerContractsInterfaces(simnet);
const sutContractsAllFunctions = getFunctionsFromContractInterfaces(
Expand Down

0 comments on commit ed940c0

Please sign in to comment.