Skip to content

fix(fs/unstable): fix node.js test runner, fix readTextFile and copyFile in Node.js #6441

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 1 commit into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions _tools/node_test_runner/run_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import "../../collections/first_not_nullish_of_test.ts";
import "../../collections/includes_value_test.ts";
import "../../collections/intersect_test.ts";
import "../../collections/invert_by_test.ts";
// TODO(kt3k): Enable this
// import "../../collections/invert_test.ts";
import "../../collections/invert_test.ts";
import "../../collections/join_to_string_test.ts";
import "../../collections/map_entries_test.ts";
import "../../collections/map_keys_test.ts";
Expand All @@ -40,7 +39,7 @@ import "../../collections/pick_test.ts";
import "../../collections/reduce_groups_test.ts";
import "../../collections/running_reduce_test.ts";
import "../../collections/sample_test.ts";
import "../../collections/sliding_windows.ts";
import "../../collections/sliding_windows_test.ts";
import "../../collections/sort_by_test.ts";
import "../../collections/sum_of_test.ts";
import "../../collections/take_last_while_test.ts";
Expand All @@ -49,14 +48,14 @@ import "../../collections/union_test.ts";
import "../../collections/unzip_test.ts";
import "../../collections/without_all_test.ts";
import "../../collections/zip_test.ts";
import "../../fs/unstable_copy_file.ts";
import "../../fs/unstable_copy_file_test.ts";
import "../../fs/unstable_link_test.ts";
import "../../fs/unstable_make_temp_dir_test.ts";
import "../../fs/unstable_mkdir_test.ts";
import "../../fs/unstable_read_dir_test.ts";
import "../../fs/unstable_read_file_test.ts";
import "../../fs/unstable_read_link_test.ts";
import "../../fs/unstable_read_text_file.ts";
import "../../fs/unstable_read_text_file_test.ts";
import "../../fs/unstable_real_path_test.ts";
import "../../fs/unstable_rename_test.ts";
import "../../fs/unstable_stat_test.ts";
Expand Down
15 changes: 9 additions & 6 deletions collections/invert_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ Deno.test("invert() handles duplicate values", () => {
});

Deno.test("invert() handles nested input", () => {
// @ts-ignore - testing invalid input
// @ts-expect-error - testing invalid input
invertTest({ a: { b: "c" } }, { "[object Object]": "a" });
// @ts-ignore - testing invalid input
invertTest({ a: "x", b: () => {} }, { "x": "a", "()=>{}": "b" });
// @ts-ignore - testing invalid input
// @ts-expect-error - testing invalid input
invertTest({ a: "x", b: Object }, {
"x": "a",
"function Object() { [native code] }": "b",
});
// @ts-expect-error - testing invalid input
invertTest({ a: "x", b: ["y", "z"] }, { "x": "a", "y,z": "b" });
// @ts-ignore - testing invalid input
// @ts-expect-error - testing invalid input
invertTest({ a: "x", b: null }, { "x": "a", "null": "b" });
// @ts-ignore - testing invalid input
// @ts-expect-error - testing invalid input
invertTest({ a: "x", b: undefined }, { "x": "a", "undefined": "b" });
});
4 changes: 2 additions & 2 deletions fs/unstable_copy_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
try {
await getNodeFs().promises.copyFile(from, to);
} catch (error) {
mapError(error);
throw mapError(error);

Check warning on line 35 in fs/unstable_copy_file.ts

View check run for this annotation

Codecov / codecov/patch

fs/unstable_copy_file.ts#L35

Added line #L35 was not covered by tests
}
}
}
Expand Down Expand Up @@ -67,7 +67,7 @@
try {
getNodeFs().copyFileSync(from, to);
} catch (error) {
mapError(error);
throw mapError(error);

Check warning on line 70 in fs/unstable_copy_file.ts

View check run for this annotation

Codecov / codecov/patch

fs/unstable_copy_file.ts#L70

Added line #L70 was not covered by tests
}
}
}
14 changes: 8 additions & 6 deletions fs/unstable_copy_file_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Deno.test("copyFile() copies content to a directory, which will throw an error",
await writeFile(source, content);

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

writeFileSync(source, content);

assertThrows(() => {
copyFileSync(source, tmpdir());
});

rmSync(tempDirPath, { recursive: true, force: true });
try {
assertThrows(() => {
copyFileSync(source, tmpdir());
});
} finally {
rmSync(tempDirPath, { recursive: true, force: true });
}
});

Deno.test("copyFileSync() copies content to a non existed file", () => {
Expand Down
15 changes: 7 additions & 8 deletions fs/unstable_read_text_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import { mapError } from "./_map_error.ts";
import type { ReadFileOptions } from "./unstable_types.ts";
import { isDeno } from "./_utils.ts";
import { readFile, readFileSync } from "./unstable_read_file.ts";
import { getNodeFs, isDeno } from "./_utils.ts";

/**
* Asynchronously reads and returns the entire contents of a file as an UTF-8 decoded string.
Expand Down Expand Up @@ -35,10 +34,12 @@
if (isDeno) {
return Deno.readTextFile(path, { ...options });
} else {
const { signal } = options ?? {};

Check warning on line 37 in fs/unstable_read_text_file.ts

View check run for this annotation

Codecov / codecov/patch

fs/unstable_read_text_file.ts#L37

Added line #L37 was not covered by tests
try {
const decoder = new TextDecoder("utf-8");
const data = await readFile(path, options);
return decoder.decode(data);
return await getNodeFs().promises.readFile(path, {
encoding: "utf-8",
signal,
});

Check warning on line 42 in fs/unstable_read_text_file.ts

View check run for this annotation

Codecov / codecov/patch

fs/unstable_read_text_file.ts#L39-L42

Added lines #L39 - L42 were not covered by tests
} catch (error) {
throw mapError(error);
}
Expand Down Expand Up @@ -74,9 +75,7 @@
return Deno.readTextFileSync(path);
} else {
try {
const decoder = new TextDecoder("utf-8");
const data = readFileSync(path);
return decoder.decode(data);
return getNodeFs().readFileSync(path, "utf-8");

Check warning on line 78 in fs/unstable_read_text_file.ts

View check run for this annotation

Codecov / codecov/patch

fs/unstable_read_text_file.ts#L78

Added line #L78 was not covered by tests
} catch (error) {
throw mapError(error);
}
Expand Down
Loading