1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

test(std/node): make tests runnable from any directory (#7397)

This makes std/node tests runnable from any directory by resolving the
testdata directory and files relative to the module directory resolved
from import.meta.url.
This commit is contained in:
Casper Beyer 2020-09-08 23:37:58 +08:00 committed by GitHub
parent ac455050ee
commit 11afd071ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 9 deletions

View file

@ -2,9 +2,8 @@ import { readFile, readFileSync } from "./_fs_readFile.ts";
import * as path from "../../path/mod.ts"; import * as path from "../../path/mod.ts";
import { assertEquals, assert } from "../../testing/asserts.ts"; import { assertEquals, assert } from "../../testing/asserts.ts";
const testData = path.resolve( const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));
path.join("node", "_fs", "testdata", "hello.txt"), const testData = path.resolve(moduleDir, "testdata", "hello.txt");
);
Deno.test("readFileSuccess", async function () { Deno.test("readFileSuccess", async function () {
const data = await new Promise((res, rej) => { const data = await new Promise((res, rej) => {

View file

@ -9,7 +9,8 @@ import { writeFile, writeFileSync } from "./_fs_writeFile.ts";
import type { TextEncodings } from "./_fs_common.ts"; import type { TextEncodings } from "./_fs_common.ts";
import * as path from "../../path/mod.ts"; import * as path from "../../path/mod.ts";
const testDataDir = path.resolve(path.join("node", "_fs", "testdata")); const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));
const testDataDir = path.resolve(moduleDir, "testdata");
const decoder = new TextDecoder("utf-8"); const decoder = new TextDecoder("utf-8");
Deno.test("Callback must be a function error", function fn() { Deno.test("Callback must be a function error", function fn() {

View file

@ -2,9 +2,8 @@ import { readFile } from "./_fs_readFile.ts";
import * as path from "../../../path/mod.ts"; import * as path from "../../../path/mod.ts";
import { assertEquals, assert } from "../../../testing/asserts.ts"; import { assertEquals, assert } from "../../../testing/asserts.ts";
const testData = path.resolve( const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));
path.join("node", "_fs", "testdata", "hello.txt"), const testData = path.resolve(moduleDir, "..", "testdata", "hello.txt");
);
Deno.test("readFileSuccess", async function () { Deno.test("readFileSuccess", async function () {
const data: Uint8Array = await readFile(testData); const data: Uint8Array = await readFile(testData);

View file

@ -4,8 +4,13 @@ import {
assert, assert,
assertStringContains, assertStringContains,
} from "../testing/asserts.ts"; } from "../testing/asserts.ts";
import * as path from "../path/mod.ts";
import { createRequire } from "./module.ts"; import { createRequire } from "./module.ts";
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));
const testdataDir = path.resolve(moduleDir, path.join("_fs", "testdata"));
const require = createRequire(import.meta.url); const require = createRequire(import.meta.url);
Deno.test("requireSuccess", function () { Deno.test("requireSuccess", function () {
@ -32,8 +37,13 @@ Deno.test("requireBuiltin", function () {
const fs = require("fs"); const fs = require("fs");
assert("readFileSync" in fs); assert("readFileSync" in fs);
const { readFileSync, isNull, extname } = require("./tests/cjs/cjs_builtin"); const { readFileSync, isNull, extname } = require("./tests/cjs/cjs_builtin");
const testData = path.relative(
Deno.cwd(),
path.join(testdataDir, "hello.txt"),
);
assertEquals( assertEquals(
readFileSync("./node/_fs/testdata/hello.txt", { encoding: "utf8" }), readFileSync(testData, { encoding: "utf8" }),
"hello world", "hello world",
); );
assert(isNull(null)); assert(isNull(null));

View file

@ -1,4 +1,5 @@
import { assert, assertThrows, assertEquals } from "../testing/asserts.ts"; import { assert, assertThrows, assertEquals } from "../testing/asserts.ts";
import * as path from "../path/mod.ts";
import * as all from "./process.ts"; import * as all from "./process.ts";
import { env, argv } from "./process.ts"; import { env, argv } from "./process.ts";
@ -27,7 +28,11 @@ Deno.test({
Deno.test({ Deno.test({
name: "process.cwd and process.chdir success", name: "process.cwd and process.chdir success",
fn() { fn() {
// this should be run like other tests from directory up assertEquals(process.cwd(), Deno.cwd());
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));
process.chdir(path.resolve(moduleDir, ".."));
assert(process.cwd().match(/\Wstd$/)); assert(process.cwd().match(/\Wstd$/));
process.chdir("node"); process.chdir("node");
assert(process.cwd().match(/\Wnode$/)); assert(process.cwd().match(/\Wnode$/));