Skip to content

Commit

Permalink
Setup test infra - jsonc-parser is all working fine it seems
Browse files Browse the repository at this point in the history
  • Loading branch information
hamlim committed May 15, 2024
1 parent 98972fe commit ddea1b4
Show file tree
Hide file tree
Showing 10 changed files with 420 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "pkg-a",
"dependencies": {
"a": "1.0.0",
"b": "2.3.1",
"c": "^3"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "pkg-b",
"dependencies": {
"a": "1.0.0",
"b": "1.2.3",
"c": "^3"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "pkg-c",
"dependencies": {
"a": "1.0.0",
"b": "2.1.3",
"c": "^3"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"workspaces": [
"libs/*"
]
}
46 changes: 37 additions & 9 deletions packages/one-version/__tests__/jsonc-parser.test.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
import assert from "node:assert";
import { after, before, test } from "node:test";
import { after, before, describe, test } from "node:test";
import { parse } from "../utils/jsonc-parser.mjs";

test("parses plain old JSON fine", () => {
let sample = {
foo: {
bar: ["baz", 1, 2, false, true, null],
},
};
describe("jsonc-parser", () => {
test("parses plain old JSON fine", () => {
let sample = {
foo: {
bar: ["baz", 1, 2, false, true, null],
},
};

let result = parse(JSON.stringify(sample));
let result = parse(JSON.stringify(sample));

assert.deepEqual(result, sample);
assert.deepEqual(result, sample);
});

test("parses JSON with comments", () => {
let sample = {
foo: {
bar: ["baz", 1, 2, false, true, null],
},
};

let result = parse(`{
// comment
"foo": {
"bar": [
// another comment
"baz",
/* block comments too */
1,
2,
false,
true,
null
]
}
}`);

assert.deepEqual(result, sample);
});
});
172 changes: 162 additions & 10 deletions packages/one-version/__tests__/one-version.test.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,167 @@
import assert from "node:assert";
import { after, before, test } from "node:test";
import { exec as execCallback } from "node:child_process";
import { promises as fsPromises } from "node:fs";
import path from "node:path";
import { after, before, describe, test } from "node:test";
import { fileURLToPath } from "node:url";
import { promisify } from "node:util";
import { start } from "../one-version.mjs";

test("supports help command", async () => {
let logs = [];
const logger = {
log: (...args) => {
logs.push(args.join(" "));
},
};
await start({ rootDirectory: process.cwd(), logger, args: ["help"] });
describe("one-version", () => {
test("supports help command", async () => {
let logs = [];
const logger = {
log: (...args) => {
logs.push(args.join(" "));
},
};
await start({ rootDirectory: process.cwd(), logger, args: ["help"] });

assert.match(logs[0], /one-version/);
assert.match(logs[0], /one-version/);
});
});

let exec = promisify(execCallback);

let __filename = fileURLToPath(import.meta.url);
let __dirname = path.dirname(__filename);

describe("one-version integration tests", () => {
// setup
// copy the `__fixtures__/template-monorepo` into the following directories
// - `__fixtures__/npm`
// - `__fixtures__/pnpm`
// - `__fixtures__/yarn`
// - `__fixtures__/yarn-berry`
// - `__fixtures__/bun`
// - `__fixtures__/missing`
before(async () => {
let fixturesDir = path.join(__dirname, "..", "__fixtures__");
let templateDir = path.join(fixturesDir, "template-monorepo");
let targetDirs = [
"npm",
"pnpm",
"yarn",
"yarn-berry",
"bun",
"missing",
];

async function copyDirectory(src, dest) {
// Create the destination directory if it doesn't exist
await fsPromises.mkdir(dest, { recursive: true });

// Read all files and subdirectories from the source directory
let entries = await fsPromises.readdir(src, { withFileTypes: true });

// Copy each file and directory recursively
for (let entry of entries) {
let srcPath = path.join(src, entry.name);
let destPath = path.join(dest, entry.name);

if (entry.isDirectory()) {
await copyDirectory(srcPath, destPath);
} else {
await fsPromises.copyFile(srcPath, destPath);
}
}
}

let lockFile = {
npm: "package-lock.json",
pnpm: "pnpm-lock.yaml",
yarn: "yarn.lock",
"yarn-berry": "yarn.lock",
bun: "bun.lock",
};

for (const dir of targetDirs) {
const targetDir = path.join(fixturesDir, dir);
try {
await copyDirectory(templateDir, targetDir);
if (dir !== "missing") {
await fsPromises.writeFile(lockFile[dir], "");
}
if (dir === "yarn-berry") {
await fsPromises.writeFile(".yarnrc.yml", "nodeLinker: node-modules");
}
} catch (err) {
throw err;
}
}
});

after(async () => {
const fixturesDir = path.join(__dirname, "..", "__fixtures__");
const targetDirs = [
"npm",
"pnpm",
"yarn",
"yarn-berry",
"bun",
"missing",
];

async function removeDirectory(dir) {
try {
await fsPromises.rm(dir, { recursive: true, force: true });
} catch (err) {
throw err;
}
}

for (const dir of targetDirs) {
const targetDir = path.join(fixturesDir, dir);
await removeDirectory(targetDir);
}
});

test("npm", async () => {
const targetDir = path.join(__dirname, "..", "__fixtures__", "npm");
await start({ rootDirectory: targetDir, logger: console });
// let { stdout } = await exec("npm run --silent --workspaces -- lerna version --json");
// let versions = JSON.parse(stdout);
// assert.equal(versions.length, 1);
// assert.equal(versions[0].name, "one-version");
});
test("pnpm", async () => {
const targetDir = path.join(__dirname, "..", "__fixtures__", "npm");
await start({ rootDirectory: targetDir, logger: console });
// let { stdout } = await exec("npm run --silent --workspaces -- lerna version --json");
// let versions = JSON.parse(stdout);
// assert.equal(versions.length, 1);
// assert.equal(versions[0].name, "one-version");
});
test("yarn", async () => {
const targetDir = path.join(__dirname, "..", "__fixtures__", "npm");
await start({ rootDirectory: targetDir, logger: console });
// let { stdout } = await exec("npm run --silent --workspaces -- lerna version --json");
// let versions = JSON.parse(stdout);
// assert.equal(versions.length, 1);
// assert.equal(versions[0].name, "one-version");
});
test("yarn-berry", async () => {
const targetDir = path.join(__dirname, "..", "__fixtures__", "npm");
await start({ rootDirectory: targetDir, logger: console });
// let { stdout } = await exec("npm run --silent --workspaces -- lerna version --json");
// let versions = JSON.parse(stdout);
// assert.equal(versions.length, 1);
// assert.equal(versions[0].name, "one-version");
});
test("bun", async () => {
const targetDir = path.join(__dirname, "..", "__fixtures__", "npm");
await start({ rootDirectory: targetDir, logger: console });
// let { stdout } = await exec("npm run --silent --workspaces -- lerna version --json");
// let versions = JSON.parse(stdout);
// assert.equal(versions.length, 1);
// assert.equal(versions[0].name, "one-version");
});
test("missing", async () => {
const targetDir = path.join(__dirname, "..", "__fixtures__", "npm");
await start({ rootDirectory: targetDir, logger: console });
// let { stdout } = await exec("npm run --silent --workspaces -- lerna version --json");
// let versions = JSON.parse(stdout);
// assert.equal(versions.length, 1);
// assert.equal(versions[0].name, "one-version");
});
});
1 change: 1 addition & 0 deletions packages/one-version/biome.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"enabled": true
},
"linter": {
"ignore": ["jsonc-parser.mjs"],
"enabled": true,
"rules": {
"recommended": true,
Expand Down
60 changes: 50 additions & 10 deletions packages/one-version/one-version.mjs
Original file line number Diff line number Diff line change
@@ -1,21 +1,61 @@
import { exec } from "node:child_process";
import { existsSync, readFileSync } from "node:fs";
import { createReadStream, readFileSync, rmSync, writeFileSync } from "node:fs";
import path, { join as pathJoin } from "node:path";
import { promisify } from "node:util";
import { createDebug } from "./utils/create-debug.mjs";
import { parse } from "./utils/jsonc-parser.mjs";

async function _loadConfig({ rootDirectory }) {
let debug = createDebug("one-version");

function loadConfig({ rootDirectory }) {
try {
// prefer jsonc first, then fallback to .json
if (existsSync(pathJoin(rootDirectory, "one-version.config.jsonc"))) {
return parse(readFileSync(pathJoin(rootDirectory, "one-version.config.jsonc"), "utf8"));
}
return parse(readFileSync(pathJoin(rootDirectory, "one-version.config.json"), "utf8"));
} catch (error) {
debug("Error loading config", error);
return {};
}
}

export async function start({ rootDirectory, logger, args, loadConfig = _loadConfig }) {
let [firstArg] = args;
function inferPackageManager({ rootDirectory }) {
if (existsSync(pathJoin(rootDirectory, "yarn.lock"))) {
if (existsSync(pathJoin(rootDirectory, ".yarnrc.yml"))) {
return "yarn-berry";
}
return "yarn";
}
if (existsSync(pathJoin(rootDirectory, "pnpm-lock.yaml"))) {
return "pnpm";
}
if (existsSync(pathJoin(rootDirectory, "package-lock.json"))) {
return "npm";
}
if (existsSync(pathJoin(rootDirectory, "bun.lockb"))) {
return "bun";
}
throw new Error("Could not infer package manager! Please specify one in the config file.");
}

let usageLogs = [
"",
`Usage:`,
` one-version check - Check the repo to ensure all dependencies are match the expected versions`,
` one-version help - Display this help message!`,
"",
];
let usageLogs = [
"",
`Usage:`,
` one-version check - Check the repo to ensure all dependencies are match the expected versions`,
` one-version help - Display this help message!`,
"",
];
export async function start({ rootDirectory, logger, args }) {
let [firstArg] = args;

switch (firstArg) {
case "check": {
let initialConfig = loadConfig({ rootDirectory });
if (!initialConfig.packageManager) {
initialConfig.packageManager = inferPackageManager({ rootDirectory });
}
// @TODO
return;
}
Expand Down
9 changes: 9 additions & 0 deletions packages/one-version/utils/create-debug.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function createDebug(namespace) {
let enabled = process.env.DEBUG?.includes(namespace);

return function debug(...args) {
if (enabled) {
console.log(`[${namespace}]`, ...args);
}
};
}
Loading

0 comments on commit ddea1b4

Please sign in to comment.