Skip to content

Commit

Permalink
Merge pull request #2686 from opral/cleaner-tests
Browse files Browse the repository at this point in the history
improve getInlangProject test, cleanup multi-project-test
  • Loading branch information
jldec authored May 1, 2024
2 parents c9d3159 + 6c130f9 commit f368886
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
42 changes: 29 additions & 13 deletions inlang/source-code/cli/src/utilities/getInlangProject.test.ts
Original file line number Diff line number Diff line change
@@ -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",
},
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion inlang/source-code/sdk/multi-project-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit f368886

Please sign in to comment.