mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 10:58:56 +00:00
* fix napi_get_value_string_utf8 to match node closes #6949 * [autofix.ci] apply automated fixes * Update test/napi/napi.test.ts Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> * Update test/napi/napi.test.ts Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
67 lines
2.2 KiB
TypeScript
67 lines
2.2 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 build = spawnSync({
|
|
cmd: ["yarn", "build"],
|
|
cwd: join(__dirname, "napi-app"),
|
|
});
|
|
if (!build.success) {
|
|
console.error(build.stderr.toString());
|
|
throw new Error("build failed");
|
|
}
|
|
});
|
|
|
|
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(join(__dirname, "../../build/bun-debug"), 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();
|
|
expect(errs).toBe("");
|
|
expect(exec.success).toBeTrue();
|
|
return exec.stdout.toString();
|
|
}
|