feat: few more tests

This commit is contained in:
Jozef Steinhübl 2024-11-16 12:10:36 +01:00
parent c12caeb448
commit 211eb136ac
No known key found for this signature in database
GPG Key ID: E6BC90C91973B08F
2 changed files with 73 additions and 3 deletions

View File

@ -15,7 +15,7 @@ if (registryUrl) {
registries.push({
url: registryUrl,
scope: scope,
token: "$$BUN_AUTH_TOKEN",
token: "$BUN_AUTH_TOKEN",
});
}

View File

@ -1,5 +1,5 @@
import { afterEach, describe, expect, it } from "bun:test";
import { unlink } from "fs";
import { unlinkSync } from "node:fs";
import { writeBunfig } from "../src/bunfig";
import { EOL } from "os";
@ -14,7 +14,8 @@ describe("writeBunfig", () => {
}
afterEach(() => {
unlink(filePath, () => console.log(`${filePath} was deleted`));
unlinkSync(filePath);
console.log(`${filePath} was deleted`);
});
describe("when no bunfig.toml file exists", () => {
@ -43,6 +44,32 @@ describe("writeBunfig", () => {
expect(contents.length).toBe(expectedContents.length);
});
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);
});
});
describe("when local bunfig.toml file exists", () => {
@ -154,5 +181,48 @@ describe("writeBunfig", () => {
expect(contents.length).toBe(expectedContents.length);
});
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]",
'\'@foo-bar\' = { token = "$BUN_AUTH_TOKEN", url = "https://npm.pkg.github.com/" }',
'\'@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);
});
});
});