Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests(cli): increasing unit test coverage #87

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion apps/cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
*/
const DEFAULT_FORMAT = "ext2";
const DEFAULT_RAM = "128Mi";
const DEFAULT_RAM_IMAGE_DOCKER = "/usr/share/cartesi-machine/images/linux.bin";

Check warning on line 77 in apps/cli/src/config.ts

View workflow job for this annotation

GitHub Actions / lint

'DEFAULT_RAM_IMAGE_DOCKER' is assigned a value but never used
const DEFAULT_RAM_IMAGE_LINUX = "/usr/share/cartesi-machine/images/linux.bin";
const DEFAULT_RAM_IMAGE_MAC =
"/opt/homebrew/share/cartesi-machine/images/linux.bin";
Expand Down Expand Up @@ -292,7 +292,10 @@
} else if (typeof value === "bigint") {
return Number(value);
} else if (typeof value === "number" || typeof value === "string") {
return bytes.parse(value);
const output = bytes.parse(value);
if (output !== null) {
return output;
}
}
throw new InvalidBytesValueError(value);
};
Expand Down
279 changes: 181 additions & 98 deletions apps/cli/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import {
defaultMachineConfig,
InvalidBooleanValueError,
InvalidBuilderError,
InvalidBytesValueError,
InvalidDriveFormatError,
InvalidEmptyDriveFormatError,
InvalidNumberValueError,
InvalidStringValueError,
parse,
RequiredFieldError,
} from "../src/config.js";

describe("config", () => {
it("default config", () => {
describe("when parsing a cartesi.toml config", () => {
it("should load the default config when file is empty", () => {
const config = parse("");
expect(config).toEqual(defaultConfig());
});
Expand Down Expand Up @@ -43,114 +45,195 @@ shared = true`);
});
});

it("invalid drive", () => {
expect(parse("drives = 42")).toEqual(defaultConfig());
expect(parse("drives.root = true")).toEqual(defaultConfig());
expect(parse("drives.root = 42")).toEqual(defaultConfig());
/**
* [machine]
*/
describe("when parsing [machine]", () => {
const config = `
[machine]
no-rollup = true
`;
it("machine-config", () => {
expect(parse(config)).toEqual({
...defaultConfig(),
machine: {
...defaultMachineConfig(),
noRollup: true,
},
});
});
it("should fail for invalid bootargs", () => {
const invalidConfig = `
${config}
bootargs = ["no4lvl", "quiet", false]
`;
expect(() => parse(invalidConfig)).toThrowError(
new InvalidStringValueError(false),
);
});
});

it("invalid drive: invalid builder", () => {
expect(() => parse('[drives.root]\nbuilder = "invalid"')).toThrowError(
new InvalidBuilderError("invalid"),
);
expect(() => parse("[drives.root]\nbuilder = true")).toThrowError(
new InvalidBuilderError(true),
);
expect(() => parse("[drives.root]\nbuilder = 10")).toThrowError(
new InvalidBuilderError(10),
);
expect(() => parse("[drives.root]\nbuilder = {}")).toThrowError(
new InvalidBuilderError({}),
);
});
/**
* [drives]
*/
describe("when parsing [drives]", () => {
it("should fail for invalid configuration", () => {
expect(parse("drives = 42")).toEqual(defaultConfig());
expect(parse("drives.root = true")).toEqual(defaultConfig());
expect(parse("drives.root = 42")).toEqual(defaultConfig());
});

it("invalid drive: invalid format", () => {
expect(() => parse('[drives.root]\nformat = "invalid"')).toThrowError(
new InvalidDriveFormatError("invalid"),
);
expect(() => parse("[drives.root]\nformat = true")).toThrowError(
new InvalidDriveFormatError(true),
);
expect(() => parse("[drives.root]\nformat = 10")).toThrowError(
new InvalidDriveFormatError(10),
);
expect(() => parse("[drives.root]\nformat = {}")).toThrowError(
new InvalidDriveFormatError({}),
);
});
it("should fail for invalid builder", () => {
expect(() =>
parse('[drives.root]\nbuilder = "invalid"'),
).toThrowError(new InvalidBuilderError("invalid"));
expect(() => parse("[drives.root]\nbuilder = true")).toThrowError(
new InvalidBuilderError(true),
);
expect(() => parse("[drives.root]\nbuilder = 10")).toThrowError(
new InvalidBuilderError(10),
);
expect(() => parse("[drives.root]\nbuilder = {}")).toThrowError(
new InvalidBuilderError({}),
);
});

it("invalid drive: invalid extension", () => {
const builderNone = `
[drives.none]
builder = "none"
filename = "./games/doom.xyzfs"
mount = "/usr/local/games/doom"
`;
expect(() => parse(builderNone)).toThrowError(
new InvalidDriveFormatError(".xyzfs"),
);
});
it("should fail for invalid format", () => {
expect(() =>
parse('[drives.root]\nformat = "invalid"'),
).toThrowError(new InvalidDriveFormatError("invalid"));
expect(() => parse("[drives.root]\nformat = true")).toThrowError(
new InvalidDriveFormatError(true),
);
expect(() => parse("[drives.root]\nformat = 10")).toThrowError(
new InvalidDriveFormatError(10),
);
expect(() => parse("[drives.root]\nformat = {}")).toThrowError(
new InvalidDriveFormatError({}),
);
});

it("invalid drive: invalid mount", () => {
expect(() => parse("[drives.data]\nmount = 42")).toThrowError(
new InvalidStringValueError(42),
);
});
it("should fail for invalid filename extension", () => {
const builderNone = `
[drives.none]
builder = "none"
filename = "./games/doom.xyzfs"
mount = "/usr/local/games/doom"
`;
expect(() => parse(builderNone)).toThrowError(
new InvalidDriveFormatError(".xyzfs"),
);
});

it("invalid empty drive: invalid fomat", () => {
expect(() =>
parse("[drives.data]\nbuilder = 'empty'\nformat = 42"),
).toThrowError(new InvalidEmptyDriveFormatError(42));
});
it("should fail for invalid mount", () => {
expect(() => parse("[drives.data]\nmount = 42")).toThrowError(
new InvalidStringValueError(42),
);
});

it("invalid boolean value", () => {
expect(() => parse("[machine]\nno-rollup = 42")).toThrowError(
new InvalidBooleanValueError(42),
);
it("should fail for invalid empty drive format", () => {
expect(() =>
parse("[drives.data]\nbuilder = 'empty'\nformat = 42"),
).toThrowError(new InvalidEmptyDriveFormatError(42));
});
});

it("invalid string value", () => {
const invalidTarDrive = `
[drives.data]
builder = "tar"
filename = 42 # invalid
format = "ext2"
`;
expect(() => parse(invalidTarDrive)).toThrowError(
new InvalidStringValueError(42),
);
});
/**
* field types
*/
describe("when parsing fields types", () => {
it("should fail for invalid boolean value", () => {
expect(() => parse("[machine]\nno-rollup = 42")).toThrowError(
new InvalidBooleanValueError(42),
);
});

it("required field", () => {
const invalidDirectoryDrive = `
[drives.data]
builder = "directory"
# directory = '' # required
`;
expect(() => parse(invalidDirectoryDrive)).toThrowError(
new RequiredFieldError("directory"), //XXX: how to know which field was required
);
});
it("should fail for invalid number value", () => {
expect(() => parse("[machine]\nmax-mcycle = 'abc'")).toThrowError(
new InvalidNumberValueError("abc"),
);
});

it("machine-config", () => {
const config = `
[machine]
no-rollup = true
`;
expect(parse(config)).toEqual({
...defaultConfig(),
machine: {
...defaultMachineConfig(),
noRollup: true,
},
it("should fail for invalid string value", () => {
const invalidTarDrive = `
[drives.data]
builder = "tar"
filename = 42 # invalid
format = "ext2"
`;
expect(() => parse(invalidTarDrive)).toThrowError(
new InvalidStringValueError(42),
);
});

it("should fail for invalid bytes value", () => {
const invalidTarDrive = `
[drives.data]
builder = "tar"
extraSize = "abc"
filename = "data.tar"
format = "ext2"
`;
expect(() => parse(invalidTarDrive)).toThrowError(
new InvalidBytesValueError("abc"),
);
});

const invalidConfig = `
${config}
bootargs = ["no4lvl", "quiet", false]
`;
expect(() => parse(invalidConfig)).toThrowError(
new InvalidStringValueError(false),
);
it("should pass for valid bytes value", () => {
// nukmber
expect(() =>
parse(
`[drives.data]
builder = "directory"
directory = "/data"
extra-size = 128
`,
),
).not.toThrow();
// string
expect(() =>
parse(
`[drives.data]
builder = "directory"
directory = "/data"
extra-size = "128MB"
`,
),
).not.toThrow();
// bigint
const bigInt = BigInt(128);
expect(() =>
parse(
`[drives.data]
builder = "directory"
directory = "/data"
extra-size = ${bigInt}
`,
),
).not.toThrow();
});

it("should fail for invalid boolean value", () => {
expect(() => parse("[machine]\nfinal-hash = 42")).toThrowError(
new InvalidBooleanValueError(42),
);
});

it("should fail for invalid optional boolean value", () => {
expect(() =>
parse("[machine]\nassert-rolling-template = 42"),
).toThrowError(new InvalidBooleanValueError(42));
});

it("should fail when required field is not defined", () => {
const invalidDirectoryDrive = `
[drives.data]
builder = "directory"
# directory = '' # required
`;
expect(() => parse(invalidDirectoryDrive)).toThrowError(
new RequiredFieldError("directory"), //XXX: how to know which field was required
);
});
});
});
2 changes: 1 addition & 1 deletion apps/cli/test/configs/full.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# runtime = "lambada"

# [machine]
# assert_rolling_update = true
# assert-rolling-update = true
# bootargs = ["no4lvl", "quiet", "earlycon=sbi", "console=hvc0", "rootfstype=ext2", "root=/dev/pmem0", "rw", "init=/usr/sbin/cartesi-init"]
# entrypoint = "/usr/local/bin/app"
# final-hash = true
Expand Down
Loading