Skip to content

Commit

Permalink
test(cli): add tablegen tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dk1a committed Mar 2, 2023
1 parent 40416a8 commit d179c52
Show file tree
Hide file tree
Showing 9 changed files with 724 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/cli/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dist
out
test-contracts-out
broadcast
cache
7 changes: 4 additions & 3 deletions packages/cli/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ fuzz_runs = 256
optimizer = true
optimizer_runs = 3000
verbosity = 1
libs = ["../../node_modules", "../solecs", "../std-contracts"]
src = "src"
out = "out"
libs = ["../../node_modules", "../solecs", "../std-contracts", "../store"]
src = "test-contracts-src"
out = "test-contracts-out"
test = "test-contracts-test"
5 changes: 4 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
},
"scripts": {
"prepare": "yarn build && chmod u+x git-install.sh",
"codegen": "ts-node --esm --files ./scripts/codegen.ts",
"lint": "eslint . --ext .ts",
"dev": "tsup --watch",
"build": "tsup",
"link": "yarn link",
"test": "vitest typecheck --run && echo 'todo: add tests'",
"test": "vitest typecheck --run && yarn test:contracts",
"test:contracts": "yarn codegen && forge test",
"git:install": "bash git-install.sh",
"release": "npm publish || echo 'version already published'"
},
"devDependencies": {
"@latticexyz/store": "^1.34.0",
"@types/ejs": "^3.1.1",
"@types/glob": "^7.2.0",
"@types/node": "^17.0.34",
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/remappings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ ds-test/=../../node_modules/ds-test/src/
forge-std/=../../node_modules/forge-std/src/
solmate/=../../node_modules/@rari-capital/solmate/src
std-contracts/=../../node_modules/@latticexyz/std-contracts/src/
solecs/=../../node_modules/@latticexyz/solecs/src/
solecs/=../../node_modules/@latticexyz/solecs/src/
@solidstate/=../../node_modules/@solidstate/
@latticexyz/=../../node_modules/@latticexyz/
56 changes: 56 additions & 0 deletions packages/cli/scripts/codegen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { SchemaType } from "@latticexyz/schema-type";
import path from "path";
import { parseStoreConfig, StoreUserConfig } from "../src/config/index.js";
import { tablegen } from "../src/render-solidity/tablegen.js";
import { logError } from "../src/utils/errors.js";
import { getSrcDirectory } from "../src/utils/forgeConfig.js";

// This config is used only for tests
const config: StoreUserConfig = {
tables: {
Table1: {
primaryKeys: {
k1: SchemaType.UINT256,
k2: SchemaType.INT32,
k3: SchemaType.BYTES16,
k4: SchemaType.ADDRESS,
k5: SchemaType.BOOL,
k6: "Enum1",
k7: "Enum2",
},
schema: {
v1: SchemaType.UINT256,
v2: SchemaType.INT32,
v3: SchemaType.BYTES16,
v4: SchemaType.ADDRESS,
v5: SchemaType.BOOL,
v6: "Enum1",
v7: "Enum2",
},
},
},

userTypes: {
enums: {
Enum1: ["E1", "E2", "E3"],
Enum2: ["E1"],
},
},
};

// Aside from avoiding `mud.config.mts` in cli package (could cause issues),
// this also tests that tablegen can work as a standalone function
const parsedConfig = await (async () => {
try {
return await parseStoreConfig(config);
} catch (error: unknown) {
logError(error);
}
})();

const srcDirectory = await getSrcDirectory();
if (parsedConfig !== undefined) {
tablegen(parsedConfig, srcDirectory);
} else {
process.exit(1);
}
2 changes: 1 addition & 1 deletion packages/cli/src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class NotESMConfigError extends Error {
message = "MUD config must be an ES module";
}

export function logError(error: Error) {
export function logError(error: unknown) {
if (error instanceof ValidationError) {
console.log(chalk.redBright(error.message));
} else if (error instanceof ZodError) {
Expand Down
Loading

0 comments on commit d179c52

Please sign in to comment.