-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(bunfig): fix and test preloads #16329
Draft
DonIsaac
wants to merge
9
commits into
main
Choose a base branch
from
don/fix/bunfig-preload
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+199
−0
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e7ae340
fix(toml): cursor pos for duplicate key errors
DonIsaac eea292b
more test cases
DonIsaac 1b505d5
test: add plugin preload test case
DonIsaac ade532d
add parent dir test
DonIsaac 563602e
test fixes
DonIsaac 9b4e336
test: add cli arg merging case
DonIsaac aed5920
test: `--preload <file>` merging
DonIsaac f8e71bc
cli merge testing
DonIsaac 58d60bc
test: disable currently-failing test cases
DonIsaac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
preload = "./preload-run.ts" | ||
|
||
[test] | ||
preload = "./preload-test.ts" |
5 changes: 5 additions & 0 deletions
5
test/config/bunfig/fixtures/preload/mixed/index.fixture-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
it("the correct file was preloaded", () => { | ||
expect(globalThis.preload).toBeDefined(); | ||
expect(globalThis.preload).toBeArrayOfSize(1); | ||
expect(globalThis.preload[0]).toEqual("mixed/preload-test.ts"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import assert from "node:assert"; | ||
assert.deepStrictEqual(globalThis.preload, ["mixed/preload-run.ts"]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// this file's name so we know the right file was loaded | ||
(globalThis.preload ??= []).push("mixed/preload-run.ts"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// this file's name so we know the right file was loaded | ||
(globalThis.preload ??= []).push("mixed/preload-test.ts"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
preloads in `[test]` should not be loaded by `bun run`. `bun test` should use both | ||
top-level and `[test]` preloads. |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
preload = ["./preload1.ts", "./preload2.ts"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import assert from "node:assert"; | ||
assert.deepStrictEqual(globalThis.preload, ["multi/preload1.ts", "multi/preload2.ts", "multi/preload3.ts"]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// `bun --config=bunfig.empty.toml run index.ts` | ||
import assert from "node:assert"; | ||
assert.strictEqual(globalThis.preload, undefined); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import assert from "node:assert"; | ||
assert.deepStrictEqual(globalThis.preload, ["multi/preload1.ts", "multi/preload2.ts"]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(globalThis.preload ??= []).push("multi/preload1.ts"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(globalThis.preload ??= []).push("multi/preload2.ts"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(globalThis.preload ??= []).push("multi/preload3.ts"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Used to test 3 behaviors: | ||
|
||
1. `preload` can be an array | ||
2. When a bunfig is specified via `--config=<path>`, the "default" bunfig (i.e. | ||
`bunfig.toml` in the same dir as cwd) is not loaded. | ||
3. Using `--preload <file>` adds `<file>` to the preload list without clobbering | ||
existing preloads. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
preload = "../preload.ts" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import assert from "node:assert"; | ||
assert.equal(globalThis.preload, "parent/preload.ts"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
globalThis.preload = "parent/preload.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
preload = "bun-plugin-yaml" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
foo: | ||
bar: "baz" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import assert from "node:assert"; | ||
import foo from "./foo.yaml" with { type: "yaml" }; | ||
assert(foo); | ||
assert.equal(typeof foo, "object"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# no "./", should be treated as a relative path | ||
preload = "preload.ts" |
3 changes: 3 additions & 0 deletions
3
test/config/bunfig/fixtures/preload/relative/index.fixture-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
it("the correct file was preloaded", () => { | ||
expect(globalThis.preload).toBe("simple/preload.ts"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import assert from "node:assert"; | ||
assert.strictEqual(globalThis.preload, "simple/preload.ts"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// this file's name so we know the right file was loaded | ||
globalThis.preload = "simple/preload.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
preload = "./preload.ts" |
3 changes: 3 additions & 0 deletions
3
test/config/bunfig/fixtures/preload/simple/index.fixture-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
it("the correct file was preloaded", () => { | ||
expect(globalThis.preload).toBe("simple/preload.ts"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import assert from "node:assert"; | ||
assert.strictEqual(globalThis.preload, "simple/preload.ts"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// this file's name so we know the right file was loaded | ||
globalThis.preload = "simple/preload.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { join, resolve } from "path"; | ||
import { bunExe, bunEnv } from "harness"; | ||
import type { SpawnOptions } from "bun"; | ||
|
||
const fixturePath = (...segs: string[]) => resolve(import.meta.dirname, "fixtures", "preload", ...segs); | ||
|
||
type Opts = { | ||
args?: string[]; | ||
cwd?: string; | ||
}; | ||
type Out = [stdout: string, stderr: string, exitCode: number]; | ||
const run = (file: string, { args = [], cwd }: Opts = {}): Promise<Out> => { | ||
const res = Bun.spawn([bunExe(), ...args, file], { | ||
cwd, | ||
stdio: ["ignore", "pipe", "pipe"], | ||
env: bunEnv, | ||
} satisfies SpawnOptions.OptionsObject<"ignore", "pipe", "pipe">); | ||
|
||
return Promise.all([ | ||
new Response(res.stdout).text().then(s => s.trim()), | ||
new Response(res.stderr).text().then(s => s.trim()), | ||
res.exited, | ||
]); | ||
}; | ||
|
||
describe("Given a single universal preload", () => { | ||
const dir = fixturePath("simple"); | ||
|
||
// `bun run` looks for a `bunfig.toml` in the current directory by default | ||
it("When `bun run` is run and `bunfig.toml` is implicitly loaded, preloads are run", async () => { | ||
// `bun run index.ts` | ||
const [out, err, code] = await run("index.ts", { cwd: dir }); | ||
expect(err).toEqual(""); | ||
expect(out).toEqual(""); | ||
expect(code).toBe(0); | ||
}); | ||
|
||
// FIXME: relative paths are being resolved to cwd, not the file's directory | ||
it.skip("When `bun run` is run from a different directory but bunfig.toml is explicitly used, preloads are run", async () => { | ||
// `bun run index.ts` | ||
const [out, err, code] = await run(join(dir, "index.ts"), { | ||
args: [`--config=${join(dir, "bunfig.toml")}`], | ||
cwd: process.cwd(), | ||
}); | ||
expect(err).toEqual(""); | ||
expect(out).toEqual(""); | ||
expect(code).toBe(0); | ||
}); | ||
}); // </given a single universal preload> | ||
|
||
describe("Given a bunfig.toml with both universal and test-only preloads", () => { | ||
const dir = fixturePath("mixed"); | ||
|
||
it("`bun run index.ts` only loads the universal preload", async () => { | ||
const [out, err, code] = await run("index.ts", { cwd: dir }); | ||
expect(err).toEqual(""); | ||
expect(out).toEqual(""); | ||
expect(code).toBe(0); | ||
}); | ||
|
||
it("`bun test` only loads test-only preloads, clobbering the universal ones", async () => { | ||
const [out, err, code] = await run("./index.fixture-test.ts", { args: ["test"], cwd: dir }); | ||
// note: err has test report, out has "bun test <version>" | ||
|
||
expect(code).toBe(0); | ||
}); | ||
}); // </given a bunfig.toml with both universal and test-only preloads> | ||
|
||
describe("Given a `bunfig.toml` with a list of preloads", () => { | ||
const dir = fixturePath("multi"); | ||
|
||
it("When `bun run` is run, preloads are run", async () => { | ||
const [out, err, code] = await run("index.ts", { cwd: dir }); | ||
expect(err).toEqual(""); | ||
expect(out).toEqual(""); | ||
expect(code).toBe(0); | ||
}); | ||
|
||
it("when passed `--config=bunfig.empty.toml`, preloads are not run", async () => { | ||
const [out, err, code] = await run("empty.ts", { args: ["--config=bunfig.empty.toml"], cwd: dir }); | ||
expect(err).toEqual(""); | ||
expect(out).toEqual(""); | ||
expect(code).toBe(0); | ||
}); | ||
|
||
it.each([ | ||
// | ||
"--preload ./preload3.ts", | ||
"--preload=./preload3.ts", | ||
// FIXME: Tests are failing due to active bugs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are bugs. The last pair suffers the same problem as |
||
// "--preload ./preload3.ts run", | ||
// "--preload=./preload3.ts run", | ||
// "run --preload ./preload3.ts", | ||
// "run --preload=./preload3.ts", | ||
])("When `bun %s cli-merge.ts` is run, `--preload` adds the target file to the list of preloads", async args => { | ||
const [out, err, code] = await run("cli-merge.ts", { args: args.split(" "), cwd: dir }); | ||
expect(err).toEqual(""); | ||
expect(out).toEqual(""); | ||
expect(code).toBe(0); | ||
}); | ||
}); // </given a `bunfig.toml` with a list of preloads> | ||
|
||
describe("Given a `bunfig.toml` with a plugin preload", () => { | ||
const dir = fixturePath("plugin"); | ||
|
||
it("When `bun run` is run, preloads are run", async () => { | ||
const [out, err, code] = await run("index.ts", { cwd: dir }); | ||
expect(err).toEqual(""); | ||
expect(out).toEqual(""); | ||
expect(code).toBe(0); | ||
}); | ||
}); // </given a `bunfig.toml` with a plugin preload> | ||
|
||
describe("Given a `bunfig.toml` file with a relative path to a preload in a parent directory", () => { | ||
const dir = fixturePath("parent", "foo"); | ||
|
||
it("When `bun run` is run, preloads are run", async () => { | ||
const [out, err, code] = await run("index.ts", { cwd: dir }); | ||
expect(err).toEqual(""); | ||
expect(out).toEqual(""); | ||
expect(code).toBe(0); | ||
}); | ||
}); // </given a `bunfit.toml` file with a relative path to a preload in a parent directory> | ||
|
||
describe("Given a `bunfig.toml` file with a relative path without a leading './'", () => { | ||
const dir = fixturePath("relative"); | ||
|
||
// FIXME: currently treaded as an import to an external package | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bug. |
||
it.skip("preload = 'preload.ts' is treated like a relative path and loaded", async () => { | ||
const [out, err, code] = await run("index.ts", { cwd: dir }); | ||
expect(err).toEqual(""); | ||
expect(out).toEqual(""); | ||
expect(code).toBe(0); | ||
}); | ||
}); // </given a `bunfig.toml` file with a relative path without a leading './'> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bug.