diff options
Diffstat (limited to 'test/js')
-rw-r--r-- | test/js/bun/resolve/import-meta.test.js | 2 | ||||
-rw-r--r-- | test/js/bun/resolve/png/test-png-import.test.js | 4 | ||||
-rw-r--r-- | test/js/bun/resolve/resolve.test.ts | 143 |
3 files changed, 143 insertions, 6 deletions
diff --git a/test/js/bun/resolve/import-meta.test.js b/test/js/bun/resolve/import-meta.test.js index a940d0c87..e0e3bb11e 100644 --- a/test/js/bun/resolve/import-meta.test.js +++ b/test/js/bun/resolve/import-meta.test.js @@ -102,7 +102,7 @@ it("Module.createRequire(file://url).resolve(file://url)", () => { const createdRequire = Module.createRequire(import.meta.url); const result1 = createdRequire.resolve("./require-json.json"); - const result2 = createdRequire.resolve("file://./require-json.json"); + const result2 = createdRequire.resolve(`file://${expected}`); expect(result1).toBe(expected); expect(result2).toBe(expected); }); diff --git a/test/js/bun/resolve/png/test-png-import.test.js b/test/js/bun/resolve/png/test-png-import.test.js index f4a809e7a..ca2d0b9ce 100644 --- a/test/js/bun/resolve/png/test-png-import.test.js +++ b/test/js/bun/resolve/png/test-png-import.test.js @@ -1,7 +1,7 @@ import { expect, test } from "bun:test"; import { resolve } from "path"; -// import MyPNG from "./test-png.png"; +import MyPNG from "./test-png.png"; -test.todo("png import", () => { +test("png import", () => { expect(MyPNG).toBe(resolve(__dirname, "./test-png.png")); }); diff --git a/test/js/bun/resolve/resolve.test.ts b/test/js/bun/resolve/resolve.test.ts index a9272fb3f..217d3dc81 100644 --- a/test/js/bun/resolve/resolve.test.ts +++ b/test/js/bun/resolve/resolve.test.ts @@ -92,7 +92,6 @@ it("file url in import resolves", async () => { }); writeFileSync(`${dir}/test.js`, `import {foo} from 'file://${dir}/index.js';\nconsole.log(foo);`); - console.log("dir", dir); const { exitCode, stdout } = Bun.spawnSync({ cmd: [bunExe(), `${dir}/test.js`], env: bunEnv, @@ -102,13 +101,97 @@ it("file url in import resolves", async () => { expect(stdout.toString("utf8")).toBe("1\n"); }); +it("invalid file url in import throws error", async () => { + const dir = tempDirWithFiles("fileurl", {}); + writeFileSync(`${dir}/test.js`, `import {foo} from 'file://\0invalid url';\nconsole.log(foo);`); + + const { exitCode, stdout, stderr } = Bun.spawnSync({ + cmd: [bunExe(), `${dir}/test.js`], + env: bunEnv, + cwd: import.meta.dir, + }); + expect(exitCode).not.toBe(0); + expect(stderr.toString("utf8")).toContain("file://\0invalid url"); +}); + it("file url in await import resolves", async () => { const dir = tempDirWithFiles("fileurl", { "index.js": "export const foo = 1;", }); writeFileSync(`${dir}/test.js`, `const {foo} = await import('file://${dir}/index.js');\nconsole.log(foo);`); - console.log("dir", dir); + const { exitCode, stdout } = Bun.spawnSync({ + cmd: [bunExe(), `${dir}/test.js`], + env: bunEnv, + cwd: import.meta.dir, + }); + expect(exitCode).toBe(0); + expect(stdout.toString("utf8")).toBe("1\n"); +}); + +it("file url with special characters in await import resolves", async () => { + const filename = "🅱️ndex.js"; + const dir = tempDirWithFiles("file url", { + [filename]: "export const foo = 1;", + }); + writeFileSync( + `${dir}/test.js`, + `const {foo} = await import('file://${dir.replace(/ /g, "%20")}/${encodeURIComponent( + filename, + )}');\nconsole.log(foo);`, + ); + + const { exitCode, stdout } = Bun.spawnSync({ + cmd: [bunExe(), `${dir}/test.js`], + env: bunEnv, + cwd: import.meta.dir, + }); + expect(exitCode).toBe(0); + expect(stdout.toString("utf8")).toBe("1\n"); +}); + +it("file url with special characters not encoded in await import resolves", async () => { + const filename = "🅱️ndex.js"; + const dir = tempDirWithFiles("file url", { + [filename]: "export const foo = 1;", + }); + writeFileSync(`${dir}/test.js`, `const {foo} = await import('file://${dir}/${filename}');\nconsole.log(foo);`); + + const { exitCode, stdout } = Bun.spawnSync({ + cmd: [bunExe(), `${dir}/test.js`], + env: bunEnv, + cwd: import.meta.dir, + }); + expect(exitCode).toBe(0); + expect(stdout.toString("utf8")).toBe("1\n"); +}); + +it("file url with special characters in import statement resolves", async () => { + const filename = "🅱️ndex.js"; + const dir = tempDirWithFiles("file url", { + [filename]: "export const foo = 1;", + }); + writeFileSync( + `${dir}/test.js`, + `import {foo} from 'file://${dir.replace(/ /g, "%20")}/${encodeURIComponent(filename)}';\nconsole.log(foo);`, + ); + + const { exitCode, stdout } = Bun.spawnSync({ + cmd: [bunExe(), `${dir}/test.js`], + env: bunEnv, + cwd: import.meta.dir, + }); + expect(exitCode).toBe(0); + expect(stdout.toString("utf8")).toBe("1\n"); +}); + +it("file url with special characters not encoded in import statement resolves", async () => { + const filename = "🅱️ndex.js"; + const dir = tempDirWithFiles("file url", { + [filename]: "export const foo = 1;", + }); + writeFileSync(`${dir}/test.js`, `import {foo} from 'file://${dir}/${filename}';\nconsole.log(foo);`); + const { exitCode, stdout } = Bun.spawnSync({ cmd: [bunExe(), `${dir}/test.js`], env: bunEnv, @@ -124,7 +207,6 @@ it("file url in require resolves", async () => { }); writeFileSync(`${dir}/test.js`, `const {foo} = require('file://${dir}/index.js');\nconsole.log(foo);`); - console.log("dir", dir); const { exitCode, stdout } = Bun.spawnSync({ cmd: [bunExe(), `${dir}/test.js`], env: bunEnv, @@ -134,6 +216,61 @@ it("file url in require resolves", async () => { expect(stdout.toString("utf8")).toBe("1\n"); }); +it("file url with special characters in require resolves", async () => { + const filename = "🅱️ndex.js"; + const dir = tempDirWithFiles("file url", { + [filename]: "export const foo = 1;", + }); + writeFileSync( + `${dir}/test.js`, + `const {foo} = require('file://${dir.replace(/ /g, "%20")}/${encodeURIComponent(filename)}');\nconsole.log(foo);`, + ); + + const { exitCode, stdout } = Bun.spawnSync({ + cmd: [bunExe(), `${dir}/test.js`], + env: bunEnv, + cwd: import.meta.dir, + }); + expect(exitCode).toBe(0); + expect(stdout.toString("utf8")).toBe("1\n"); +}); + +it("file url in require.resolve resolves", async () => { + const dir = tempDirWithFiles("fileurl", { + "index.js": "export const foo = 1;", + }); + writeFileSync(`${dir}/test.js`, `const to = require.resolve('file://${dir}/index.js');\nconsole.log(to);`); + + const { exitCode, stdout } = Bun.spawnSync({ + cmd: [bunExe(), `${dir}/test.js`], + env: bunEnv, + cwd: import.meta.dir, + }); + expect(exitCode).toBe(0); + expect(stdout.toString("utf8")).toBe(`${dir}/index.js\n`); +}); + +it("file url with special characters in require resolves", async () => { + const filename = "🅱️ndex.js"; + const dir = tempDirWithFiles("file url", { + [filename]: "export const foo = 1;", + }); + writeFileSync( + `${dir}/test.js`, + `const to = require.resolve('file://${dir.replace(/ /g, "%20")}/${encodeURIComponent( + filename, + )}');\nconsole.log(to);`, + ); + + const { exitCode, stdout } = Bun.spawnSync({ + cmd: [bunExe(), `${dir}/test.js`], + env: bunEnv, + cwd: import.meta.dir, + }); + expect(exitCode).toBe(0); + expect(stdout.toString("utf8")).toBe(`${dir}/${filename}\n`); +}); + it("import long string should not segfault", async () => { try { await import("a".repeat(10000)); |