Skip to content
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
40 changes: 40 additions & 0 deletions packages/conf/src/__tests__/Ini.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, test } from "vitest";
import { INI } from "../ini.js";
import { Service } from "../service.js";

describe("INI - fromObject and toObject", () => {
test("should return same object", () => {
Expand Down Expand Up @@ -130,3 +131,42 @@ describe("INI - fromString and toString", () => {
expect(result.trim()).toStrictEqual(dataIni.trim());
});
});

describe("INI - value coercion", () => {
test("should keep octal / leading-zero values as strings", () => {
const result = INI.fromString("[X]\nUMask=0077").toObject();

expect(result).toEqual({ X: { UMask: "0077" } });
});

test("should parse 0 and 1 as numbers, not booleans", () => {
const result = INI.fromString("[X]\nA=0\nB=1\nC=200").toObject();

expect(result).toEqual({ X: { A: 0, B: 1, C: 200 } });
});

test("should keep infinity tokens as strings", () => {
const result = INI.fromString("[X]\nA=infinity\nB=Infinity\nC=-infinity").toObject();

expect(result).toEqual({ X: { A: "infinity", B: "Infinity", C: "-infinity" } });
});

test("should not throw on a unit with UMask / RestartSec=1 / OOMScoreAdjust=0", () => {
const unit = [
"[Unit]",
"Description=x",
"[Service]",
"ExecStart=/bin/true",
"UMask=0022",
"RestartSec=1",
"OOMScoreAdjust=0",
].join("\n");

const service = Service.fromINI(INI.fromString(unit));
const ini = service.toINIString();

expect(ini).toContain("UMask=0022");
expect(ini).toContain("RestartSec=1");
expect(ini).toContain("OOMScoreAdjust=0");
});
});
14 changes: 5 additions & 9 deletions packages/conf/src/ini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,18 @@ function readValue(value: string): boolean | number | string {
) {
return true;
}
if (trimmed === "1") {
console.warn("Ambiguous boolean value: 1, use yes or true instead");
return true;
}
if (
trimmed === "false" || trimmed === "no" || trimmed === "off"
) {
return false;
}
if (trimmed === "0") {
console.warn("Ambiguous boolean value: 0, use no or false instead");
return false;
}

// Only coerce when it round-trips exactly, so octal/leading-zero values
// (UMask=0077), hex, and "0"/"1" reach the Zod schema, which knows the type.
const numberValue = Number(trimmed);
return isFinite(numberValue) ? numberValue : trimmed;
return Number.isFinite(numberValue) && String(numberValue) === trimmed
? numberValue
: trimmed;
}

/**
Expand Down
Loading