1
0
Files
DiAL/tests/scripts/build-common.test.ts
lanyuanxiaoyao ccd16a583e feat: 跨平台发布打包,支持 7 个目标平台交叉编译和 tar.gz 分发
- 新增 scripts/release.ts,支持 7 个编译目标(linux/darwin/windows + musl 变体)
- 从 build.ts 提取共享构建逻辑到 build-common.ts,现有 build 行为不变
- 使用 tar-stream + node:zlib 创建 tar.gz,精确控制 Unix 权限位
- SHA256 校验和文件格式兼容 sha256sum -c
- 支持 --target 参数选择特定平台编译
- 新增 devDependency: tar-stream、@types/tar-stream
- 更新 README.md 和 DEVELOPMENT.md 文档
- 同步 openspec specs
2026-05-20 23:24:36 +08:00

75 lines
2.5 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { mkdir, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join, sep } from "node:path";
import { scanDir, toImportSpecifier } from "../../scripts/build-common";
describe("scanDir", () => {
let tempDir: string;
beforeEach(async () => {
tempDir = join(tmpdir(), `build-common-test-${Date.now()}`);
await mkdir(tempDir, { recursive: true });
});
afterEach(async () => {
await rm(tempDir, { force: true, recursive: true });
});
test("扫描空目录", async () => {
const files = await scanDir(tempDir, "/");
expect(files).toEqual([]);
});
test("扫描单文件", async () => {
await writeFile(join(tempDir, "index.html"), "<html></html>");
const files = await scanDir(tempDir, "/");
expect(files).toEqual(["/index.html"]);
});
test("扫描嵌套目录", async () => {
await mkdir(join(tempDir, "assets"), { recursive: true });
await writeFile(join(tempDir, "index.html"), "");
await writeFile(join(tempDir, "assets", "style.css"), "");
await writeFile(join(tempDir, "assets", "app.js"), "");
const files = await scanDir(tempDir, "/");
expect(files.sort()).toEqual(["/assets/app.js", "/assets/style.css", "/index.html"]);
});
test("扫描多层嵌套", async () => {
await mkdir(join(tempDir, "a", "b"), { recursive: true });
await writeFile(join(tempDir, "a", "b", "deep.txt"), "");
const files = await scanDir(tempDir, "/");
expect(files).toEqual(["/a/b/deep.txt"]);
});
test("自定义前缀", async () => {
await writeFile(join(tempDir, "file.txt"), "");
const files = await scanDir(tempDir, "/static/");
expect(files).toEqual(["/static/file.txt"]);
});
});
describe("toImportSpecifier", () => {
test("同目录文件生成正确的相对路径", () => {
const result = toImportSpecifier("/project/.build", "/project/.build/file.txt");
expect(result).toBe("file.txt");
});
test("子目录文件生成正确的相对路径", () => {
const result = toImportSpecifier("/project/.build", "/project/.build/sub/file.txt");
expect(result).toBe("sub/file.txt");
});
test("Windows 路径分隔符转换为正斜杠", () => {
const fromDir = ["C:", "project", ".build"].join(sep);
const targetPath = ["C:", "project", "dist", "web", "assets", "app.js"].join(sep);
const result = toImportSpecifier(fromDir, targetPath);
expect(result).toBe("../dist/web/assets/app.js");
expect(result).not.toContain(sep);
});
});