Skip to content

Commit f53efe1

Browse files
authored
fix(fs/unstable): fix node.js test runner, fix readTextFile and copyFile in Node.js (#6441)
1 parent 71c828a commit f53efe1

File tree

5 files changed

+30
-27
lines changed

5 files changed

+30
-27
lines changed

_tools/node_test_runner/run_test.mjs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ import "../../collections/first_not_nullish_of_test.ts";
1919
import "../../collections/includes_value_test.ts";
2020
import "../../collections/intersect_test.ts";
2121
import "../../collections/invert_by_test.ts";
22-
// TODO(kt3k): Enable this
23-
// import "../../collections/invert_test.ts";
22+
import "../../collections/invert_test.ts";
2423
import "../../collections/join_to_string_test.ts";
2524
import "../../collections/map_entries_test.ts";
2625
import "../../collections/map_keys_test.ts";
@@ -40,7 +39,7 @@ import "../../collections/pick_test.ts";
4039
import "../../collections/reduce_groups_test.ts";
4140
import "../../collections/running_reduce_test.ts";
4241
import "../../collections/sample_test.ts";
43-
import "../../collections/sliding_windows.ts";
42+
import "../../collections/sliding_windows_test.ts";
4443
import "../../collections/sort_by_test.ts";
4544
import "../../collections/sum_of_test.ts";
4645
import "../../collections/take_last_while_test.ts";
@@ -49,14 +48,14 @@ import "../../collections/union_test.ts";
4948
import "../../collections/unzip_test.ts";
5049
import "../../collections/without_all_test.ts";
5150
import "../../collections/zip_test.ts";
52-
import "../../fs/unstable_copy_file.ts";
51+
import "../../fs/unstable_copy_file_test.ts";
5352
import "../../fs/unstable_link_test.ts";
5453
import "../../fs/unstable_make_temp_dir_test.ts";
5554
import "../../fs/unstable_mkdir_test.ts";
5655
import "../../fs/unstable_read_dir_test.ts";
5756
import "../../fs/unstable_read_file_test.ts";
5857
import "../../fs/unstable_read_link_test.ts";
59-
import "../../fs/unstable_read_text_file.ts";
58+
import "../../fs/unstable_read_text_file_test.ts";
6059
import "../../fs/unstable_real_path_test.ts";
6160
import "../../fs/unstable_rename_test.ts";
6261
import "../../fs/unstable_stat_test.ts";

collections/invert_test.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@ Deno.test("invert() handles duplicate values", () => {
2424
});
2525

2626
Deno.test("invert() handles nested input", () => {
27-
// @ts-ignore - testing invalid input
27+
// @ts-expect-error - testing invalid input
2828
invertTest({ a: { b: "c" } }, { "[object Object]": "a" });
29-
// @ts-ignore - testing invalid input
30-
invertTest({ a: "x", b: () => {} }, { "x": "a", "()=>{}": "b" });
31-
// @ts-ignore - testing invalid input
29+
// @ts-expect-error - testing invalid input
30+
invertTest({ a: "x", b: Object }, {
31+
"x": "a",
32+
"function Object() { [native code] }": "b",
33+
});
34+
// @ts-expect-error - testing invalid input
3235
invertTest({ a: "x", b: ["y", "z"] }, { "x": "a", "y,z": "b" });
33-
// @ts-ignore - testing invalid input
36+
// @ts-expect-error - testing invalid input
3437
invertTest({ a: "x", b: null }, { "x": "a", "null": "b" });
35-
// @ts-ignore - testing invalid input
38+
// @ts-expect-error - testing invalid input
3639
invertTest({ a: "x", b: undefined }, { "x": "a", "undefined": "b" });
3740
});

fs/unstable_copy_file.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export async function copyFile(
3232
try {
3333
await getNodeFs().promises.copyFile(from, to);
3434
} catch (error) {
35-
mapError(error);
35+
throw mapError(error);
3636
}
3737
}
3838
}
@@ -67,7 +67,7 @@ export function copyFileSync(
6767
try {
6868
getNodeFs().copyFileSync(from, to);
6969
} catch (error) {
70-
mapError(error);
70+
throw mapError(error);
7171
}
7272
}
7373
}

fs/unstable_copy_file_test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Deno.test("copyFile() copies content to a directory, which will throw an error",
3737
await writeFile(source, content);
3838

3939
try {
40-
assertRejects(async () => {
40+
await assertRejects(async () => {
4141
await copyFile(source, tmpdir());
4242
});
4343
} finally {
@@ -94,11 +94,13 @@ Deno.test("copyFileSync() copies content to a directory, which will throw an err
9494

9595
writeFileSync(source, content);
9696

97-
assertThrows(() => {
98-
copyFileSync(source, tmpdir());
99-
});
100-
101-
rmSync(tempDirPath, { recursive: true, force: true });
97+
try {
98+
assertThrows(() => {
99+
copyFileSync(source, tmpdir());
100+
});
101+
} finally {
102+
rmSync(tempDirPath, { recursive: true, force: true });
103+
}
102104
});
103105

104106
Deno.test("copyFileSync() copies content to a non existed file", () => {

fs/unstable_read_text_file.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import { mapError } from "./_map_error.ts";
44
import type { ReadFileOptions } from "./unstable_types.ts";
5-
import { isDeno } from "./_utils.ts";
6-
import { readFile, readFileSync } from "./unstable_read_file.ts";
5+
import { getNodeFs, isDeno } from "./_utils.ts";
76

87
/**
98
* Asynchronously reads and returns the entire contents of a file as an UTF-8 decoded string.
@@ -35,10 +34,12 @@ export async function readTextFile(
3534
if (isDeno) {
3635
return Deno.readTextFile(path, { ...options });
3736
} else {
37+
const { signal } = options ?? {};
3838
try {
39-
const decoder = new TextDecoder("utf-8");
40-
const data = await readFile(path, options);
41-
return decoder.decode(data);
39+
return await getNodeFs().promises.readFile(path, {
40+
encoding: "utf-8",
41+
signal,
42+
});
4243
} catch (error) {
4344
throw mapError(error);
4445
}
@@ -74,9 +75,7 @@ export function readTextFileSync(
7475
return Deno.readTextFileSync(path);
7576
} else {
7677
try {
77-
const decoder = new TextDecoder("utf-8");
78-
const data = readFileSync(path);
79-
return decoder.decode(data);
78+
return getNodeFs().readFileSync(path, "utf-8");
8079
} catch (error) {
8180
throw mapError(error);
8281
}

0 commit comments

Comments
 (0)