-
Notifications
You must be signed in to change notification settings - Fork 0
fix: run the CLI when invoked through a symlink (npx / installed bin) #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
24ec62e
fix: run the CLI when invoked through a symlink
mpicciolli 42d4fad
fix: update CLI entry-point tests to use npm command based on platform
mpicciolli 845229d
Potential fix for pull request finding
mpicciolli 8a2ea23
fix: clarify entry point behavior in documentation comment
mpicciolli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import { execFileSync } from "node:child_process"; | ||
| import { existsSync } from "node:fs"; | ||
| import { | ||
| copyFile, | ||
| mkdir, | ||
| mkdtemp, | ||
| rm, | ||
| symlink, | ||
| writeFile, | ||
| } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join, resolve } from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import { afterAll, beforeAll, describe, expect, it } from "vitest"; | ||
|
|
||
| /** | ||
| * End-to-end guard for the CLI entry-point check. | ||
| * | ||
| * These tests must spawn the built CLI *through a symlink*, because that is how | ||
| * npm/npx invoke it (node_modules/.bin/cdb-converter -> dist/cli.mjs). Calling | ||
| * run() in-process cannot observe the bug: Node reports the symlink path in | ||
| * process.argv[1] but the real path in import.meta.url, and a comparison that | ||
| * does not dereference both makes the CLI exit 0 without doing anything. | ||
| * | ||
| * The symlink suite is skipped on Windows: fs.symlink() there needs admin | ||
| * rights or developer mode, and npm does not symlink "bin" entries anyway (it | ||
| * generates .cmd/.ps1 shims), so the scenario under test does not exist. | ||
| */ | ||
|
|
||
| const isWindows = process.platform === "win32"; | ||
| const npmCommand = isWindows ? "npm.cmd" : "npm"; | ||
| const repoRoot = resolve(fileURLToPath(import.meta.url), "../.."); | ||
| const cliPath = join(repoRoot, "dist", "cli.mjs"); | ||
| const fixture = join(repoRoot, "test", "fixtures", "OfficialRelease-2014.cdb"); | ||
|
|
||
| let workDir: string; | ||
| let linkPath: string; | ||
|
|
||
| function runCli(args: string[]): { status: number; stdout: string } { | ||
| let stdout = ""; | ||
| let status = 0; | ||
|
|
||
| try { | ||
| stdout = execFileSync(process.execPath, [linkPath, ...args], { | ||
| cwd: workDir, | ||
| encoding: "utf8", | ||
| }); | ||
| } catch (error) { | ||
| const failure = error as { status?: number; stdout?: string }; | ||
| status = failure.status ?? 1; | ||
| stdout = failure.stdout ?? ""; | ||
| } | ||
|
|
||
| return { status, stdout }; | ||
| } | ||
|
|
||
| beforeAll(async () => { | ||
| execFileSync(npmCommand, ["run", "build"], { | ||
| cwd: repoRoot, | ||
| stdio: "ignore", | ||
| }); | ||
|
|
||
| workDir = await mkdtemp(join(tmpdir(), "cdb-converter-cli-")); | ||
| await copyFile(fixture, join(workDir, "input.cdb")); | ||
| }, 120_000); | ||
|
|
||
| afterAll(async () => { | ||
| if (workDir) { | ||
| await rm(workDir, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| describe.skipIf(isWindows)("CLI invoked through a symlink", () => { | ||
| beforeAll(async () => { | ||
| await mkdir(join(workDir, "bin")); | ||
|
|
||
| // Mirrors what `npm install` creates for the "bin" entry. | ||
| linkPath = join(workDir, "bin", "cdb-converter"); | ||
| await symlink(cliPath, linkPath, "file"); | ||
| }); | ||
|
|
||
| it("converts a real .cdb and writes the output file", () => { | ||
| const { status, stdout } = runCli(["input.cdb", "output.sqlite"]); | ||
|
|
||
| expect(status).toBe(0); | ||
| expect(existsSync(join(workDir, "output.sqlite"))).toBe(true); | ||
| expect(stdout).toContain("Output :"); | ||
| expect(stdout).toMatch(/Tables : \d+/); | ||
| }, 120_000); | ||
|
|
||
| it("converts back from .sqlite to .cdb", () => { | ||
| expect(runCli(["input.cdb", "roundtrip.sqlite"]).status).toBe(0); | ||
|
|
||
| const { status } = runCli(["roundtrip.sqlite", "roundtrip.cdb"]); | ||
|
|
||
| expect(status).toBe(0); | ||
| expect(existsSync(join(workDir, "roundtrip.cdb"))).toBe(true); | ||
| }, 120_000); | ||
|
|
||
| it("prints help and version", () => { | ||
| const help = runCli(["--help"]); | ||
| expect(help.status).toBe(0); | ||
| expect(help.stdout).toContain("Usage:"); | ||
|
|
||
| const version = runCli(["--version"]); | ||
| expect(version.status).toBe(0); | ||
| expect(version.stdout.trim()).toMatch(/^\d+\.\d+\.\d+/); | ||
| }, 60_000); | ||
|
|
||
| it("exits non-zero on a missing input file", () => { | ||
| expect(runCli([]).status).toBe(1); | ||
| }, 60_000); | ||
| }); | ||
|
|
||
| describe("CLI imported as a library", () => { | ||
| it("does not run the converter on import", async () => { | ||
| const importer = join(workDir, "importer.mjs"); | ||
| await writeFile( | ||
| importer, | ||
| `import { run } from ${JSON.stringify(cliPath)};\n` + | ||
| `console.log(typeof run);\n`, | ||
| ); | ||
|
|
||
| const stdout = execFileSync( | ||
| process.execPath, | ||
| [importer, "input.cdb", "should-not-exist.sqlite"], | ||
| { cwd: workDir, encoding: "utf8" }, | ||
| ); | ||
|
|
||
| expect(stdout.trim()).toBe("function"); | ||
| expect(existsSync(join(workDir, "should-not-exist.sqlite"))).toBe(false); | ||
| }, 60_000); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.