Skip to content

Commit 6236d6a

Browse files
fix test env
1 parent 7cc5865 commit 6236d6a

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/bin.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ if (args.length === 0) {
138138
process.exit(0);
139139
} else if (args.some((arg) => arg === "-v" || arg === "--version")) {
140140
const version = JSON.parse(
141-
fs.readFileSync(path.resolve(process.cwd(), "..", "package.json"), "utf-8"),
141+
fs.readFileSync(path.join(__dirname, "..", "package.json"), "utf-8"),
142142
).version as string;
143143
console.log(version);
144144
process.exit(0);
@@ -148,7 +148,8 @@ if (args.length === 0) {
148148
// `deno publish` cli is under active development and args may change
149149
// frequently.
150150
if (cmd === "publish") {
151-
const binFolder = path.resolve(process.cwd(), ".download");
151+
const binFolder = path.join(__dirname, "..", ".download");
152+
152153
run(async () => {
153154
const projectInfo = await findProjectDir(process.cwd());
154155
return publish(process.cwd(), {

test/test_utils.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as fs from "node:fs";
22
import * as path from "node:path";
33
import * as os from "node:os";
4+
import * as module from "node:module";
5+
import { isMainThread } from "node:worker_threads";
46
import { exec, writeJson } from "../src/utils.ts";
57

68
export interface DenoJson {
@@ -10,6 +12,16 @@ export interface DenoJson {
1012
license: string;
1113
}
1214

15+
/**
16+
* By default when node run ts file it's in ESM context so __dirname is not defined.
17+
* But we use it in source code and `tsc` transpiles to ESM/CJS
18+
* So we need to deifnefpr test context.
19+
*/
20+
if (isMainThread && "register" in module) {
21+
const __dirname = path.dirname(new URL(import.meta.url).pathname);
22+
globalThis.__dirname = __dirname;
23+
}
24+
1325
/**
1426
* This sets the `packageManager` field in the `package.json` of the
1527
* specified directory to be the latest modern stable version of yarn.
@@ -25,10 +37,18 @@ export async function runJsr(
2537
captureOutput = false,
2638
) {
2739
const bin = path.resolve("src", "bin.ts");
40+
const testutils = path.resolve("test", "test_utils.ts");
2841

2942
return await exec(
3043
"node",
31-
["--no-warnings", "--experimental-strip-types", bin, ...args],
44+
[
45+
"--no-warnings",
46+
"--import",
47+
testutils,
48+
"--experimental-strip-types",
49+
bin,
50+
...args,
51+
],
3252
cwd,
3353
{
3454
...process.env,

0 commit comments

Comments
 (0)