setup-bun/tests/bunfig.spec.ts

229 lines
6.8 KiB
TypeScript
Raw Permalink Normal View History

2024-11-16 11:56:08 +01:00
import { afterEach, describe, expect, it } from "bun:test";
2024-11-16 12:10:36 +01:00
import { unlinkSync } from "node:fs";
2024-11-16 11:56:08 +01:00
import { writeBunfig } from "../src/bunfig";
import { EOL } from "os";
describe("writeBunfig", () => {
const filePath = "bunfig.toml";
async function getFileAndContents() {
const file = Bun.file(filePath);
const contents = (await file.text()).split(EOL);
return { file, contents };
}
afterEach(() => {
2024-11-16 12:10:36 +01:00
unlinkSync(filePath);
console.log(`${filePath} was deleted`);
2024-11-16 11:56:08 +01:00
});
describe("when no bunfig.toml file exists", () => {
it("should create a new file with scopes content", async () => {
writeBunfig(filePath, [
{
url: "https://npm.pkg.github.com",
scope: "foo-bar",
token: "$BUN_AUTH_TOKEN",
},
]);
const { file, contents } = await getFileAndContents();
expect(file.exists()).resolves.toBeTrue();
const expectedContents = [
"[install.scopes]",
2024-12-08 19:11:17 +01:00
'\'@foo-bar\' = { url = "https://npm.pkg.github.com/", token = "$BUN_AUTH_TOKEN" }',
2024-11-16 11:56:08 +01:00
"",
];
contents.forEach((content, index) =>
expect(content).toBe(expectedContents[index])
);
expect(contents.length).toBe(expectedContents.length);
});
2024-11-16 12:10:36 +01:00
it("should create a new file with global registry", async () => {
writeBunfig(filePath, [
{
url: "https://npm.pkg.github.com",
scope: "",
token: "$BUN_AUTH_TOKEN",
},
]);
const { file, contents } = await getFileAndContents();
expect(file.exists()).resolves.toBeTrue();
const expectedContents = [
"[install]",
'registry = "https://npm.pkg.github.com/"',
"",
];
contents.forEach((content, index) =>
expect(content).toBe(expectedContents[index])
);
expect(contents.length).toBe(expectedContents.length);
});
2024-11-16 11:56:08 +01:00
});
describe("when local bunfig.toml file exists", () => {
it("and no [install.scopes] exists, should concatenate file correctly", async () => {
const bunfig = `[install]${EOL}optional = true${EOL}${EOL}[install.cache]${EOL}disable = true`;
await Bun.write(filePath, bunfig);
writeBunfig(filePath, [
{
url: "https://npm.pkg.github.com",
scope: "foo-bar",
token: "$BUN_AUTH_TOKEN",
},
]);
const { file, contents } = await getFileAndContents();
expect(file.exists()).resolves.toBeTrue();
const expectedContents = [
"[install]",
"optional = true",
"",
"[install.cache]",
"disable = true",
"[install.scopes]",
2024-12-08 19:11:17 +01:00
'\'@foo-bar\' = { url = "https://npm.pkg.github.com/", token = "$BUN_AUTH_TOKEN" }',
2024-11-16 11:56:08 +01:00
"",
];
contents.forEach((content, index) =>
expect(content).toBe(expectedContents[index])
);
expect(contents.length).toBe(expectedContents.length);
});
it("and [install.scopes] exists and it's not the same registry, should concatenate file correctly", async () => {
const bunfig = `[install]${EOL}optional = true${EOL}${EOL}[install.scopes]${EOL}'@bla-ble' = { token = "$BUN_AUTH_TOKEN", url = "https://npm.pkg.github.com/" }${EOL}${EOL}[install.cache]${EOL}disable = true`;
await Bun.write(filePath, bunfig);
writeBunfig(filePath, [
{
url: "https://npm.pkg.github.com",
scope: "foo-bar",
token: "$BUN_AUTH_TOKEN",
},
]);
const { file, contents } = await getFileAndContents();
expect(file.exists()).resolves.toBeTrue();
const expectedContents = [
"[install]",
"optional = true",
"",
"[install.scopes]",
2024-12-08 19:11:17 +01:00
'\'@foo-bar\' = { url = "https://npm.pkg.github.com/", token = "$BUN_AUTH_TOKEN" }',
2024-11-16 11:56:08 +01:00
'\'@bla-ble\' = { token = "$BUN_AUTH_TOKEN", url = "https://npm.pkg.github.com/" }',
"",
"[install.cache]",
"disable = true",
"",
];
contents.forEach((content, index) =>
expect(content).toBe(expectedContents[index])
);
expect(contents.length).toBe(expectedContents.length);
});
it("and [install.scopes] exists and it's the same registry, should concatenate file correctly", async () => {
const bunfig = `[install]${EOL}optional = true${EOL}${EOL}[install.scopes]${EOL}'@foo-bar' = { token = "$BUN_AUTH_TOKEN", url = "https://npm.pkg.github.com/" }${EOL}'@bla-ble' = { token = "$BUN_AUTH_TOKEN", url = "https://npm.pkg.github.com/" }${EOL}${EOL}[install.cache]${EOL}disable = true`;
await Bun.write(filePath, bunfig);
writeBunfig(filePath, [
{
url: "https://npm.pkg.github.com",
scope: "foo-bar",
token: "$BUN_AUTH_TOKEN",
},
]);
const { file, contents } = await getFileAndContents();
expect(file.exists()).resolves.toBeTrue();
const expectedContents = [
"[install]",
"optional = true",
"",
"[install.scopes]",
2024-12-08 19:11:17 +01:00
'\'@foo-bar\' = { url = "https://npm.pkg.github.com/", token = "$BUN_AUTH_TOKEN" }',
2024-11-16 11:56:08 +01:00
'\'@bla-ble\' = { token = "$BUN_AUTH_TOKEN", url = "https://npm.pkg.github.com/" }',
"",
"[install.cache]",
"disable = true",
"",
];
contents.forEach((content, index) =>
expect(content).toBe(expectedContents[index])
);
expect(contents.length).toBe(expectedContents.length);
});
2024-11-16 12:10:36 +01:00
it("and [install.scopes] and [install] exists, should concantenate file correctly", async () => {
const bunfig = `[install]${EOL}optional = true${EOL}${EOL}[install.scopes]${EOL}'@foo-bar' = { token = "$BUN_AUTH_TOKEN", url = "https://npm.pkg.github.com/" }${EOL}'@bla-ble' = { token = "$BUN_AUTH_TOKEN", url = "https://npm.pkg.github.com/" }${EOL}${EOL}[install.cache]${EOL}disable = true`;
await Bun.write(filePath, bunfig);
writeBunfig(filePath, [
{
url: "https://npm.pkg.github.com",
scope: "foo-bar",
token: "$BUN_AUTH_TOKEN",
},
{
url: "https://bun.sh",
scope: "",
token: "$BUN_AUTH_TOKEN",
}, // global registry
]);
const { file, contents } = await getFileAndContents();
expect(file.exists()).resolves.toBeTrue();
const expectedContents = [
"[install]",
'registry = "https://bun.sh/"',
"optional = true",
"",
"[install.scopes]",
2024-12-08 19:11:17 +01:00
'\'@foo-bar\' = { url = "https://npm.pkg.github.com/", token = "$BUN_AUTH_TOKEN" }',
2024-11-16 12:10:36 +01:00
'\'@bla-ble\' = { token = "$BUN_AUTH_TOKEN", url = "https://npm.pkg.github.com/" }',
"",
"[install.cache]",
"disable = true",
"",
];
contents.forEach((content, index) =>
expect(content).toBe(expectedContents[index])
);
expect(contents.length).toBe(expectedContents.length);
});
2024-11-16 11:56:08 +01:00
});
});