Skip to content

Commit

Permalink
Tests: added support for testing against writes too
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiospampinato committed Sep 15, 2024
1 parent b565238 commit 3f2339f
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 4 deletions.
64 changes: 64 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@
"jest": "^29.7.0",
"jest-snapshot-serializer-ansi": "^2.1.0",
"jest-snapshot-serializer-raw": "^2.0.0",
"json-archive": "^2.1.0",
"nanoexec": "^1.1.0",
"prettier": "3.3.3",
"radix64-encoding": "^2.0.1",
"tiny-dirname": "^1.0.1",
"typescript": "^5.4.5"
}
Expand Down
4 changes: 2 additions & 2 deletions test/__tests__/cursor-offset.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { runCli } from "../utils";

describe("write cursorOffset to stderr with --cursor-offset <int>", () => {
runCli("/", [
runCli("", [
"--cursor-offset",
"2",
"--parser",
Expand All @@ -14,7 +14,7 @@ describe("write cursorOffset to stderr with --cursor-offset <int>", () => {
});

describe("cursorOffset should not be affected by full-width character", () => {
runCli("/", [
runCli("", [
"--cursor-offset",
"21",
"--parser",
Expand Down
2 changes: 1 addition & 1 deletion test/__tests__/plugin-precedence.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { runCli } from "../utils";

describe("json-stringify takes precedence over json for package.json", () => {
runCli("/", [
runCli("", [
"--stdin-filepath=package.json",
], {
input:
Expand Down
51 changes: 50 additions & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import once from "function-once";
import * as Archive from "json-archive";
import exec from "nanoexec";
import fs from "node:fs/promises";
import path from "node:path";
import process from "node:process";
import Base64 from "radix64-encoding";

import { expect, test } from "@jest/globals";
import serializerRaw from "jest-snapshot-serializer-raw";
Expand All @@ -13,6 +17,48 @@ const ROOT_PATH = process.cwd();
const BIN_PATH = path.join(ROOT_PATH, "dist", "bin.js");
const FIXTURES_PATH = path.join(ROOT_PATH, "test", "__fixtures__");

async function getArchive(folderPath) {
const packPrev = await Archive.pack(folderPath);
const archive = {
getPack: once(() => {
return Archive.pack(folderPath);
}),
getChanged: once(async () => {
const packNext = await archive.getPack();
const changed = [];
for (const fileName in packNext) {
const filePrev = packPrev[fileName];
const fileNext = packNext[fileName];
if (filePrev.contents === fileNext.contents) continue;
changed.push(fileName);
}
return changed;
}),
getDiff: async () => {
const packNext = await archive.getPack();
const changed = await archive.getChanged();
const diff = [];
for (const fileName of changed) {
const fileNext = packNext[fileName];
diff.push({
filename: fileName,
content: Base64.decodeStr(fileNext.contents),
});
}
return diff;
},
reset: async () => {
const changed = await archive.getChanged();
for (const fileName of changed) {
const filePath = path.join(folderPath, fileName);
const filePrev = packPrev[fileName];
await fs.writeFile(filePath, filePrev.contents, filePrev.encoding);
}
},
};
return archive;
}

function getFixturesPath(dir) {
return path.join(FIXTURES_PATH, dir);
}
Expand All @@ -28,6 +74,7 @@ function getNormalizedOutput(output, options) {

async function runCommand(dir, args, options) {
const cwd = getFixturesPath(dir);
const archive = dir ? await getArchive(cwd) : undefined;
const result = exec("node", [BIN_PATH, ...args], { cwd, stdio: "pipe" });

if (options.input) {
Expand All @@ -38,7 +85,9 @@ async function runCommand(dir, args, options) {
const status = await result.code;
const stdout = getNormalizedOutput((await result.stdout).toString());
const stderr = getNormalizedOutput((await result.stderr).toString());
const write = []; //TODO
const write = (await archive?.getDiff()) || [];

await archive?.reset();

return { status, stdout, stderr, write };
}
Expand Down

0 comments on commit 3f2339f

Please sign in to comment.