-
Notifications
You must be signed in to change notification settings - Fork 756
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
feat: support outputing ND-JSON files via an environment variable #6383
Merged
+506
−43
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bd10100
refactor: remove duplicated `getWranglerSendMetricsFromEnv()` function
petebacondarwin 880ea53
feat: support outputting ND-JSON files via an environment variable
petebacondarwin 00ae502
fixup! feat: support outputting ND-JSON files via an environment vari…
petebacondarwin 3787e4f
fixup! feat: support outputting ND-JSON files via an environment vari…
petebacondarwin 3f4977a
fixup! feat: support outputting ND-JSON files via an environment vari…
petebacondarwin d281ed1
fixup! feat: support outputting ND-JSON files via an environment vari…
petebacondarwin 916ccb4
fixup! feat: support outputting ND-JSON files via an environment vari…
petebacondarwin 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
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 @@ | ||
--- | ||
"wrangler": minor | ||
--- | ||
|
||
feat: support outputting ND-JSON files via an environment variable |
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,245 @@ | ||
import { readdirSync, readFileSync } from "node:fs"; | ||
import { join } from "node:path"; | ||
import { clearOutputFilePath, writeOutput } from "../output"; | ||
import { runInTempDir } from "./helpers/run-in-tmp"; | ||
import type { OutputEntry } from "../output"; | ||
|
||
const originalProcessEnv = process.env; | ||
const { | ||
WRANGLER_OUTPUT_FILE_DIRECTORY: _, | ||
WRANGLER_OUTPUT_FILE_PATH: __, | ||
...processEnvNoVars | ||
} = originalProcessEnv; | ||
|
||
describe("writeOutput()", () => { | ||
runInTempDir({ homedir: "home" }); | ||
afterEach(clearOutputFilePath); | ||
|
||
it("should do nothing with no env vars set", () => { | ||
try { | ||
process.env = processEnvNoVars; | ||
writeOutput({ | ||
type: "wrangler-session", | ||
version: 1, | ||
wrangler_version: "0.0.0.0", | ||
command_line_args: ["--help"], | ||
log_file_path: "some/log/path.log", | ||
}); | ||
// No files written | ||
expect(readdirSync(".")).toEqual(["home"]); | ||
} finally { | ||
process.env = originalProcessEnv; | ||
} | ||
}); | ||
|
||
it("should write to the file given by WRANGLER_OUTPUT_FILE_PATH", () => { | ||
try { | ||
const WRANGLER_OUTPUT_FILE_PATH = "output.json"; | ||
process.env = { ...processEnvNoVars, WRANGLER_OUTPUT_FILE_PATH }; | ||
writeOutput({ | ||
type: "wrangler-session", | ||
version: 1, | ||
wrangler_version: "0.0.0.0", | ||
command_line_args: ["--help"], | ||
log_file_path: "some/log/path.log", | ||
}); | ||
const outputFile = readFileSync(WRANGLER_OUTPUT_FILE_PATH, "utf8"); | ||
expect(outputFile).toContainEntries([ | ||
{ | ||
type: "wrangler-session", | ||
version: 1, | ||
wrangler_version: "0.0.0.0", | ||
command_line_args: ["--help"], | ||
log_file_path: "some/log/path.log", | ||
}, | ||
]); | ||
} finally { | ||
process.env = originalProcessEnv; | ||
} | ||
}); | ||
|
||
it("should write to the file given by WRANGLER_OUTPUT_FILE_PATH, ignoring WRANGLER_OUTPUT_FILE_DIRECTORY", () => { | ||
try { | ||
const WRANGLER_OUTPUT_FILE_PATH = "output.json"; | ||
process.env = { | ||
...processEnvNoVars, | ||
WRANGLER_OUTPUT_FILE_PATH, | ||
WRANGLER_OUTPUT_FILE_DIRECTORY: ".", | ||
}; | ||
writeOutput({ | ||
type: "wrangler-session", | ||
version: 1, | ||
wrangler_version: "0.0.0.0", | ||
command_line_args: ["--help"], | ||
log_file_path: "some/log/path.log", | ||
}); | ||
const outputFile = readFileSync(WRANGLER_OUTPUT_FILE_PATH, "utf8"); | ||
expect(outputFile).toContainEntries([ | ||
{ | ||
type: "wrangler-session", | ||
version: 1, | ||
wrangler_version: "0.0.0.0", | ||
command_line_args: ["--help"], | ||
log_file_path: "some/log/path.log", | ||
}, | ||
]); | ||
} finally { | ||
process.env = originalProcessEnv; | ||
} | ||
}); | ||
|
||
it("should write multiple entries to the file given by WRANGLER_OUTPUT_FILE_PATH", () => { | ||
try { | ||
const WRANGLER_OUTPUT_FILE_PATH = "output.json"; | ||
process.env = { ...processEnvNoVars, WRANGLER_OUTPUT_FILE_PATH }; | ||
writeOutput({ | ||
type: "wrangler-session", | ||
version: 1, | ||
wrangler_version: "0.0.0.0", | ||
command_line_args: ["--help"], | ||
log_file_path: "some/log/path.log", | ||
}); | ||
writeOutput({ | ||
type: "deployment", | ||
version: 1, | ||
worker_name: "Worker", | ||
worker_tag: "ABCDE12345", | ||
deployment_id: "1234", | ||
}); | ||
|
||
const outputFile = readFileSync(WRANGLER_OUTPUT_FILE_PATH, "utf8"); | ||
expect(outputFile).toContainEntries([ | ||
{ | ||
type: "wrangler-session", | ||
version: 1, | ||
wrangler_version: "0.0.0.0", | ||
command_line_args: ["--help"], | ||
log_file_path: "some/log/path.log", | ||
}, | ||
{ | ||
type: "deployment", | ||
version: 1, | ||
worker_name: "Worker", | ||
worker_tag: "ABCDE12345", | ||
deployment_id: "1234", | ||
}, | ||
]); | ||
} finally { | ||
process.env = originalProcessEnv; | ||
} | ||
}); | ||
|
||
it("should write to a random file in WRANGLER_OUTPUT_FILE_DIRECTORY", () => { | ||
try { | ||
process.env = { | ||
...processEnvNoVars, | ||
WRANGLER_OUTPUT_FILE_DIRECTORY: "output", | ||
}; | ||
writeOutput({ | ||
type: "wrangler-session", | ||
version: 1, | ||
wrangler_version: "0.0.0.0", | ||
command_line_args: ["--help"], | ||
log_file_path: "some/log/path.log", | ||
}); | ||
|
||
const outputFilePaths = readdirSync("output"); | ||
expect(outputFilePaths.length).toEqual(1); | ||
expect(outputFilePaths[0]).toMatch(/wrangler-output-.+\.json/); | ||
const outputFile = readFileSync( | ||
join("output", outputFilePaths[0]), | ||
"utf8" | ||
); | ||
expect(outputFile).toContainEntries([ | ||
{ | ||
type: "wrangler-session", | ||
version: 1, | ||
wrangler_version: "0.0.0.0", | ||
command_line_args: ["--help"], | ||
log_file_path: "some/log/path.log", | ||
}, | ||
]); | ||
} finally { | ||
process.env = originalProcessEnv; | ||
} | ||
}); | ||
|
||
it("should write multiple entries to the same random file in WRANGLER_OUTPUT_FILE_DIRECTORY", () => { | ||
try { | ||
process.env = { | ||
...processEnvNoVars, | ||
WRANGLER_OUTPUT_FILE_DIRECTORY: "output", | ||
}; | ||
writeOutput({ | ||
type: "wrangler-session", | ||
version: 1, | ||
wrangler_version: "0.0.0.0", | ||
command_line_args: ["--help"], | ||
log_file_path: "some/log/path.log", | ||
}); | ||
writeOutput({ | ||
type: "deployment", | ||
version: 1, | ||
worker_name: "Worker", | ||
worker_tag: "ABCDE12345", | ||
deployment_id: "1234", | ||
}); | ||
|
||
const outputFilePaths = readdirSync("output"); | ||
expect(outputFilePaths.length).toEqual(1); | ||
expect(outputFilePaths[0]).toMatch(/wrangler-output-.+\.json/); | ||
const outputFile = readFileSync( | ||
join("output", outputFilePaths[0]), | ||
"utf8" | ||
); | ||
expect(outputFile).toContainEntries([ | ||
{ | ||
type: "wrangler-session", | ||
version: 1, | ||
wrangler_version: "0.0.0.0", | ||
command_line_args: ["--help"], | ||
log_file_path: "some/log/path.log", | ||
}, | ||
{ | ||
type: "deployment", | ||
version: 1, | ||
worker_name: "Worker", | ||
worker_tag: "ABCDE12345", | ||
deployment_id: "1234", | ||
}, | ||
]); | ||
} finally { | ||
process.env = originalProcessEnv; | ||
} | ||
}); | ||
}); | ||
|
||
expect.extend({ | ||
toContainEntries(received: string, expected: OutputEntry[]) { | ||
const actual = received | ||
.trim() | ||
.split("\n") | ||
.map((line) => JSON.parse(line)); | ||
|
||
const stamped = expected.map((entry) => ({ | ||
...entry, | ||
timestamp: expect.any(String), | ||
})); | ||
|
||
return { | ||
pass: this.equals(actual, stamped), | ||
message: () => `Entries are${this.isNot ? "" : " not"} as expected.`, | ||
actual, | ||
expected: stamped, | ||
}; | ||
}, | ||
}); | ||
|
||
interface CustomMatchers { | ||
toContainEntries: (expected: OutputEntry[]) => unknown; | ||
} | ||
|
||
declare module "vitest" { | ||
interface Assertion extends CustomMatchers {} | ||
interface AsymmetricMatchersContaining extends CustomMatchers {} | ||
} |
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
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.
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.
Is this custom matcher necessary? To understand what it's doing, you'd have to go looking for the definition whereas if it was a plain function call (
expect(parseEntries(outputFile)).toEqual([...
) you could just cmd+click (parseEntries
) to get to the implementationThere 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.
I think custom matchers are great for common use-cases to a whole codebase and should be introduced globally so they can be defined clearly, learnt and relied upon. Implementing them as one-offs per suite I think is obfuscating
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.
It just a helper function to avoid duplicating the same logic throughout and keeping the tests themselves nice and readable.
It is only used in this file (where it is defined) and it makes no sense outside of this file. It is nicer than using a vanilla function because Vitest treats it specially, for example it will format the errors nicely and do diffing of the actual and expected results.
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.
If you Cmd-Click on the function it takes you to the type definition below, which is exactly next to the implementation. So I don't think it is too hard to understand.
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.
Somewhat ironically, when you create global custom matchers like this it is common to put the type in a
.d.ts
file - e.g. https://github.com/cloudflare/workers-sdk/blob/f6300a71adb83e5f92cd9c9ddd56a85132e03f98/packages/create-cloudflare/vitest.d.tsIn this case it is actually much harder to find the original implementation of the matcher. For example the
toExist()
matcher implementation is over here: https://github.com/cloudflare/workers-sdk/blob/cc335ee1c1dbd58009714c4b49e23e9dcf121d1e/packages/create-cloudflare/e2e-tests/setup.ts