Skip to content

Commit

Permalink
chore: remove tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoishin committed Dec 26, 2024
1 parent e8225f0 commit d1aba84
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 71 deletions.
35 changes: 0 additions & 35 deletions package-lock.json

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

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,13 @@
"@types/rimraf": "2.0.2",
"@types/semver": "5.5.0",
"@types/tar": "^6.1.3",
"@types/tmp": "^0.2.0",
"@vitest/coverage-v8": "^2.1.8",
"coveralls": "^3.1.0",
"eslint": "^9.17.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-simple-import-sort": "^12.1.1",
"npm-run-all2": "^7.0.2",
"prettier": "^3.4.2",
"tmp": "0.2.1",
"type-fest": "^0.17.0",
"typescript": "~5.7.2",
"typescript-eslint": "^8.18.2",
Expand Down
20 changes: 10 additions & 10 deletions test/commands/defaultconfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import path from "node:path";

import { Command } from "commander";
import fse from "fs-extra";
import temp from "tmp";
import { beforeEach, describe, expect, it, vi } from "vitest";

import defaultConfigCommand from "../../src/commands/defaultconfig";
import { createMockProgram, MockCommand } from "../mocks/program";
import { setupTmpDir } from "./tmp-dir";

let program: MockCommand;

beforeEach(() => {
// Set up environment.
const tempFolder = temp.dirSync();
process.chdir(tempFolder.name);
const tempFolder = setupTmpDir();
process.chdir(tempFolder);
fs.writeFileSync("package.json", JSON.stringify({ name: "nodecg" }));

// Copy fixtures.
Expand All @@ -29,7 +29,7 @@ describe("when run with a bundle argument", () => {
it("should successfully create a bundle config file when bundle has configschema.json", async () => {
await program.runWith("defaultconfig config-schema");
const config = JSON.parse(
fs.readFileSync("./cfg/config-schema.json", { encoding: "utf8" }),
fs.readFileSync("./cfg/config-schema.json", { encoding: "utf8" })
);
expect(config.username).toBe("user");
expect(config.value).toBe(5);
Expand All @@ -39,11 +39,11 @@ describe("when run with a bundle argument", () => {
it("should print an error when the target bundle does not have a configschema.json", async () => {
const spy = vi.spyOn(console, "error");
fse.mkdirpSync(
path.resolve(process.cwd(), "./bundles/missing-schema-bundle"),
path.resolve(process.cwd(), "./bundles/missing-schema-bundle")
);
await program.runWith("defaultconfig missing-schema-bundle");
expect(spy.mock.calls[0][0]).toMatchInlineSnapshot(
`"Error: Bundle %s does not have a configschema.json"`,
`"Error: Bundle %s does not have a configschema.json"`
);
spy.mockRestore();
});
Expand All @@ -52,7 +52,7 @@ describe("when run with a bundle argument", () => {
const spy = vi.spyOn(console, "error");
await program.runWith("defaultconfig not-installed");
expect(spy.mock.calls[0][0]).toMatchInlineSnapshot(
`"Error: Bundle %s does not exist"`,
`"Error: Bundle %s does not exist"`
);
spy.mockRestore();
});
Expand All @@ -62,11 +62,11 @@ describe("when run with a bundle argument", () => {
fs.mkdirSync("./cfg");
fs.writeFileSync(
"./cfg/config-schema.json",
JSON.stringify({ fake: "data" }),
JSON.stringify({ fake: "data" })
);
await program.runWith("defaultconfig config-schema");
expect(spy.mock.calls[0][0]).toMatchInlineSnapshot(
`"Error: Bundle %s already has a config file"`,
`"Error: Bundle %s already has a config file"`
);
spy.mockRestore();
});
Expand All @@ -86,7 +86,7 @@ describe("when run with no arguments", () => {
const spy = vi.spyOn(console, "error");
await program.runWith("defaultconfig");
expect(spy.mock.calls[0][0]).toMatchInlineSnapshot(
`"Error: No bundle found in the current directory!"`,
`"Error: No bundle found in the current directory!"`
);
spy.mockRestore();
});
Expand Down
14 changes: 7 additions & 7 deletions test/commands/install.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import fs from "node:fs";
import { Command } from "commander";
import rimraf from "rimraf";
import semver from "semver";
import temp from "tmp";
import { beforeEach, expect, it, vi } from "vitest";

import installCommand from "../../src/commands/install";
import { createMockProgram, MockCommand } from "../mocks/program";
import { setupTmpDir } from "./tmp-dir";

let program: MockCommand;
const tempFolder = temp.dirSync();
process.chdir(tempFolder.name);
const tempFolder = setupTmpDir();
process.chdir(tempFolder);
fs.writeFileSync("package.json", JSON.stringify({ name: "nodecg" }));

beforeEach(() => {
Expand All @@ -23,10 +23,10 @@ it("should install a bundle and its dependencies", async () => {
await program.runWith("install supportclass/lfg-streamtip");
expect(fs.existsSync("./bundles/lfg-streamtip/package.json")).toBe(true);
expect(
fs.readdirSync("./bundles/lfg-streamtip/node_modules").length,
fs.readdirSync("./bundles/lfg-streamtip/node_modules").length
).toBeGreaterThan(0);
expect(
fs.readdirSync("./bundles/lfg-streamtip/bower_components").length,
fs.readdirSync("./bundles/lfg-streamtip/bower_components").length
).toBeGreaterThan(0);
});

Expand All @@ -37,7 +37,7 @@ it("should install a version that satisfies a provided semver range", async () =
const pjson = JSON.parse(
fs.readFileSync("./bundles/lfg-nucleus/package.json", {
encoding: "utf8",
}),
})
);
expect(semver.satisfies(pjson.version, "^1.1.0")).toBe(true);
});
Expand All @@ -56,7 +56,7 @@ it("should print an error when no valid git repo is provided", async () => {
const spy = vi.spyOn(console, "error");
await program.runWith("install 123");
expect(spy).toBeCalledWith(
"Please enter a valid git repository URL or GitHub username/repo pair.",
"Please enter a valid git repository URL or GitHub username/repo pair."
);
spy.mockRestore();
});
14 changes: 7 additions & 7 deletions test/commands/schema-types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import fs from "node:fs";
import path from "node:path";

import fse from "fs-extra";
import temp from "tmp";
import { beforeEach, expect, it, vi } from "vitest";

import schemaTypesCommand from "../../src/commands/schema-types";
import { createMockProgram, MockCommand } from "../mocks/program";
import { setupTmpDir } from "./tmp-dir";

let program: MockCommand;

beforeEach(() => {
// Set up environment.
const tempFolder = temp.dirSync();
process.chdir(tempFolder.name);
const tempFolder = setupTmpDir();
process.chdir(tempFolder);
fs.writeFileSync("package.json", JSON.stringify({ name: "nodecg" }));

// Copy fixtures.
Expand Down Expand Up @@ -49,13 +49,13 @@ it("should successfully create d.ts files from the replicant schemas and create
expect(fs.existsSync(outputPath)).toBe(true);

await expect(fs.readFileSync(outputPath, "utf8")).toMatchFileSnapshot(
"../fixtures/results/schema-types/example.d.ts",
"../fixtures/results/schema-types/example.d.ts"
);

const indexPath = "./src/types/schemas/index.d.ts";
expect(fs.existsSync(indexPath)).toBe(true);
await expect(fs.readFileSync(indexPath, "utf8")).toMatchFileSnapshot(
"../fixtures/results/schema-types/index.d.ts",
"../fixtures/results/schema-types/index.d.ts"
);
});

Expand All @@ -64,7 +64,7 @@ it("should print an error when the target bundle does not have a schemas dir", a
const spy = vi.spyOn(console, "error");
await program.runWith("schema-types");
expect(spy.mock.calls[0][0]).toMatchInlineSnapshot(
`"Error: Input directory ("%s") does not exist"`,
`"Error: Input directory ("%s") does not exist"`
);
spy.mockRestore();
});
Expand All @@ -82,7 +82,7 @@ it("should successfully compile the config schema", async () => {
expect(fs.existsSync(outputPath)).toBe(true);

await expect(fs.readFileSync(outputPath, "utf8")).toMatchFileSnapshot(
"../fixtures/results/schema-types/configschema.d.ts",
"../fixtures/results/schema-types/configschema.d.ts"
);
expect(fs.readFileSync("./src/types/schemas/index.d.ts", "utf8"))
.toMatchInlineSnapshot(`
Expand Down
12 changes: 6 additions & 6 deletions test/commands/setup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import fs from "node:fs";

import { Command } from "commander";
import inquirer from "inquirer";
import temp from "tmp";
import { PackageJson } from "type-fest";
import { beforeEach, expect, test, vi } from "vitest";

import setupCommand from "../../src/commands/setup";
import { createMockProgram, MockCommand } from "../mocks/program";
import { setupTmpDir } from "./tmp-dir";

let program: MockCommand;
let currentDir = temp.dirSync();
let currentDir = setupTmpDir();
const chdir = (keepCurrentDir = false) => {
if (!keepCurrentDir) {
currentDir = temp.dirSync();
currentDir = setupTmpDir();
}

process.chdir(currentDir.name);
process.chdir(currentDir);
};

const readPackageJson = (): PackageJson => {
Expand Down Expand Up @@ -80,7 +80,7 @@ test("should print an error when the target version is the same as current", asy
test("should correctly handle and refuse when you try to downgrade from v2 to v1", async () => {
chdir();
vi.spyOn(inquirer, "prompt").mockReturnValue(
Promise.resolve({ installOlder: true }) as any,
Promise.resolve({ installOlder: true }) as any
);
await program.runWith("setup 2.0.0 --skip-dependencies");
expect(readPackageJson().version).toBe("2.0.0");
Expand All @@ -92,7 +92,7 @@ test("should print an error when the target version doesn't exist", async () =>
const spy = vi.spyOn(console, "error");
await program.runWith("setup 0.0.99 -u --skip-dependencies");
expect(spy.mock.calls[0][0]).toMatchInlineSnapshot(
`"No releases match the supplied semver range (0.0.99)"`,
`"No releases match the supplied semver range (0.0.99)"`
);
spy.mockRestore();
});
Expand Down
10 changes: 10 additions & 0 deletions test/commands/tmp-dir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { randomUUID } from "node:crypto";
import { mkdirSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";

export function setupTmpDir() {
const dir = path.join(tmpdir(), randomUUID());
mkdirSync(dir);
return dir;
}
8 changes: 4 additions & 4 deletions test/commands/uninstall.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import path from "node:path";
import { Command } from "commander";
import fse from "fs-extra";
import inquirer from "inquirer";
import temp from "tmp";
import { beforeEach, expect, it, vi } from "vitest";

import uninstallCommand from "../../src/commands/uninstall";
import { createMockProgram, MockCommand } from "../mocks/program";
import { setupTmpDir } from "./tmp-dir";

let program: MockCommand;

beforeEach(() => {
// Set up environment.
const tempFolder = temp.dirSync();
process.chdir(tempFolder.name);
const tempFolder = setupTmpDir();
process.chdir(tempFolder);
fs.writeFileSync("package.json", JSON.stringify({ name: "nodecg" }));

// Copy fixtures.
Expand All @@ -39,7 +39,7 @@ it("should print an error when the target bundle is not installed", async () =>
const spy = vi.spyOn(console, "error");
await program.runWith("uninstall not-installed");
expect(spy.mock.calls[0][0]).toBe(
"Cannot uninstall %s: bundle is not installed.",
"Cannot uninstall %s: bundle is not installed."
);
spy.mockRestore();
});

0 comments on commit d1aba84

Please sign in to comment.