From 6c130f984609a3634df72ac9671969c74fe1804c Mon Sep 17 00:00:00 2001 From: jldec Date: Wed, 1 May 2024 10:51:26 +0100 Subject: [PATCH] improve getInlangProject test, cleanup multi-project-test --- .../src/utilities/getInlangProject.test.ts | 42 +++++++++++++------ .../sdk/multi-project-test/package.json | 2 +- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/inlang/source-code/cli/src/utilities/getInlangProject.test.ts b/inlang/source-code/cli/src/utilities/getInlangProject.test.ts index de89f6ac84..f240d8537c 100644 --- a/inlang/source-code/cli/src/utilities/getInlangProject.test.ts +++ b/inlang/source-code/cli/src/utilities/getInlangProject.test.ts @@ -1,23 +1,40 @@ -import { vi, it, describe, expect, beforeEach } from "vitest" +import { vi, it, describe, expect, beforeEach, afterEach } from "vitest" import fs from "node:fs/promises" -import { tmpdir } from "node:os" -import { join } from "node:path" +import os from "node:os" +import { dirname, join, resolve } from "node:path" +import { fileURLToPath } from "node:url" import { ProjectSettings } from "@inlang/sdk" import { getInlangProject } from "./getInlangProject.js" -process.cwd = tmpdir +const __filename = fileURLToPath(import.meta.url) +const __dirname = dirname(__filename) + +// mocks process.exit = vi.fn() console.error = vi.fn() -beforeEach(() => { +beforeEach(async () => { vi.resetAllMocks() + // mock cwd to a unique temp dir + // https://nodejs.org/docs/latest-v20.x/api/fs.html#fspromisesmkdtempprefix-options + const cwd = await fs.mkdtemp(join(os.tmpdir(), "test-cli-getInlangProject-")) + process.cwd = () => cwd +}) + +afterEach(async () => { + const cwd = process.cwd() + // cleanup temp dir carefully + if (cwd.startsWith(os.tmpdir())) { + await fs.rm(cwd, { recursive: true }) + } }) +const pluginPath = resolve(__dirname, "../../../plugins/inlang-message-format/dist/index.js") + const settings = { - $schema: "https://inlang.com/schema/project-settings", sourceLanguageTag: "en", languageTags: ["en", "de"], - modules: ["https://cdn.jsdelivr.net/npm/@inlang/plugin-message-format@latest/dist/index.js"], + modules: [pluginPath], "plugin.inlang.messageFormat": { pathPattern: "./locales/{languageTag}.json", }, @@ -27,15 +44,14 @@ describe( "getInlangProject", () => { it("does not error in a project with no git repo", async () => { - const projectPath = join( - await fs.mkdtemp(join(tmpdir(), "test-getInlangProject")), - "project.inlang" - ) - const settingsPath = join(projectPath, "settings.json") + // process.cwd() is mocked to a temp dir + const projectPath = join(process.cwd(), "project.inlang") await fs.mkdir(projectPath) + + const settingsPath = join(projectPath, "settings.json") await fs.writeFile(settingsPath, JSON.stringify(settings)) - await getInlangProject({ projectPath }) + await getInlangProject({ projectPath: "project.inlang" }) expect(process.exit).not.toHaveBeenCalled() expect(console.error).toHaveBeenCalledTimes(1) diff --git a/inlang/source-code/sdk/multi-project-test/package.json b/inlang/source-code/sdk/multi-project-test/package.json index 42aa47b860..71544a5dca 100644 --- a/inlang/source-code/sdk/multi-project-test/package.json +++ b/inlang/source-code/sdk/multi-project-test/package.json @@ -20,7 +20,7 @@ "lint2": "pnpm inlang lint --project ./project2-dir/project.inlang", "lint3": "pnpm inlang lint --project ./project3-dir/project.inlang", "lint4": "pnpm inlang lint --project ./project4-dir/project.inlang", - "test": "pnpm clean && vitest --reporter=tap-flat run" + "test": "pnpm clean && vitest --reporter=tap-flat run && pnpm clean" }, "prettier": { "semi": false,