Files
bun.sh/test/napi/napi.test.ts
Dylan Conway c7289b2cd6 fix(windows): fix a few more tests (#9550)
* fix napi tests

* only windows, update passing tests

* remove closing remainder

* fix child_process-node.test.js

* might fail in ci

* oops

* fix dns tests

* remove comment

* sometimes it is slow

* update test

* maybe fix timeout error

* one more try

* off by one, valid npm package name

* update test

* fix hot tests

* revert

* remove close
2024-03-25 13:34:08 -07:00

78 lines
2.4 KiB
TypeScript

import { it, expect, test, beforeAll, describe } from "bun:test";
import { bunExe, bunEnv } from "harness";
import { spawnSync } from "bun";
import { join } from "path";
describe("napi", () => {
beforeAll(() => {
// build gyp
const install = spawnSync({
cmd: ["bun", "install", "--verbose"],
cwd: join(__dirname, "napi-app"),
stderr: "inherit",
env: bunEnv,
stdout: "inherit",
stdin: "inherit",
});
if (!install.success) {
throw new Error("build failed");
}
});
describe("issue_7685", () => {
it("works", () => {
const args = [...Array(20).keys()];
checkSameOutput("test_issue_7685", args);
});
});
describe("napi_get_value_string_utf8 with buffer", () => {
// see https://github.com/oven-sh/bun/issues/6949
it("copies one char", () => {
const result = checkSameOutput("test_napi_get_value_string_utf8_with_buffer", ["abcdef", 2]);
expect(result).toEndWith("str: a");
});
it("copies null terminator", () => {
const result = checkSameOutput("test_napi_get_value_string_utf8_with_buffer", ["abcdef", 1]);
expect(result).toEndWith("str:");
});
it("copies zero char", () => {
const result = checkSameOutput("test_napi_get_value_string_utf8_with_buffer", ["abcdef", 0]);
expect(result).toEndWith("str: *****************************");
});
it("copies more than given len", () => {
const result = checkSameOutput("test_napi_get_value_string_utf8_with_buffer", ["abcdef", 25]);
expect(result).toEndWith("str: abcdef");
});
it("copies auto len", () => {
const result = checkSameOutput("test_napi_get_value_string_utf8_with_buffer", ["abcdef", 424242]);
expect(result).toEndWith("str:");
});
});
});
function checkSameOutput(test: string, args: any[]) {
const nodeResult = runOn("node", test, args).trim();
let bunResult = runOn(bunExe(), test, args);
// remove all debug logs
bunResult = bunResult.replaceAll(/^\[\w+\].+$/gm, "").trim();
expect(bunResult).toBe(nodeResult);
return nodeResult;
}
function runOn(executable: string, test: string, args: any[]) {
const exec = spawnSync({
cmd: [executable, join(__dirname, "napi-app/main.js"), test, JSON.stringify(args)],
env: bunEnv,
});
const errs = exec.stderr.toString();
if (errs !== "") {
throw new Error(errs);
}
expect(exec.success).toBeTrue();
return exec.stdout.toString();
}