-
Notifications
You must be signed in to change notification settings - Fork 6k
feat: add tests for shouldRunVsCodeCli and bindAddrFromArgs #4211
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
Conversation
Codecov Report
@@ Coverage Diff @@
## main #4211 +/- ##
==========================================
+ Coverage 64.94% 65.09% +0.14%
==========================================
Files 36 36
Lines 1880 1882 +2
Branches 381 380 -1
==========================================
+ Hits 1221 1225 +4
+ Misses 560 559 -1
+ Partials 99 98 -1
Continue to review full report at Codecov.
|
✨ Coder.com for PR #4211 deployed! It will be updated on every commit.
|
I'm a bit dumbfounded. The tests pass in CI: https://github.com/cdr/code-server/pull/4211/checks?check_run_id=3657430012 But 1 test is not passing locally 🤔 It's the it("should catch errors thrown when unlinking a socket", async () => {
const tmpDir2 = await tmpdir("unlink-socket-error")
const tmpFile = path.join(tmpDir2, "unlink-socket-file")
// await promises.writeFile(tmpFile, "")
const socketPath = tmpFile
const defaultArgs = await setDefaults({
_: [],
socket: socketPath,
})
const app = await createApp(defaultArgs)
const server = app[2]
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith(`ENOENT: no such file or directory, unlink '${socketPath}'`)
server.close()
// Ensure directory was removed
rmdirSync(tmpDir2, { recursive: true })
}) Basically, what's happening is:
BUT it does all this but then gets to See below: try {
await fs.unlink(args.socket)
} catch (error: any) {
handleArgsSocketCatchError(error)
}
server.listen(args.socket, resolve |
help @code-asher - any ideas? |
* This function creates the bind address | ||
* using the CLI args. | ||
*/ | ||
export function bindAddrFromArgs(addr: Addr, args: Args): Addr { |
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.
🛠️ Exported so that we can unit test. Added short description for good measure.
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.
Cool!
src/node/cli.ts
Outdated
return !!args["list-extensions"] || !!args["install-extension"] || !!args["uninstall-extension"] | ||
// Create new interface with only these keys | ||
// Pick<Args, "list-extensions" | "install-extension" | "uninstall-extension"> | ||
// Get the keys of new interface | ||
// keyof ... | ||
// Turn that into an array | ||
// Array<...> | ||
type ExtensionArgs = Array<keyof Pick<Args, "list-extensions" | "install-extension" | "uninstall-extension">> | ||
const extensionRelatedArgs: ExtensionArgs = ["list-extensions", "install-extension", "uninstall-extension"] | ||
|
||
const argKeys = Object.keys(args) | ||
|
||
// If any of the extensionRelatedArgs are included in args | ||
// then we don't want to run the vscode cli | ||
return extensionRelatedArgs.some((arg) => argKeys.includes(arg)) |
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.
🔁 Refactored this function.
"But isn't this code more verbose?"
"Yes, but here's why I think it's an improvement:
!!
is a clever technique in JS to cast a value to a Boolean, but it's hard to read.- previously these were hard-coded values, which means they weren't tied to the
Args
type they're based off
Now we have:
- clear comments explaining what's happening
- type-safety because the args we check for are now based on the
Args
type so if those change, TS will complain and we'll catch that here"
Happy to revert the change if we think this function should be left as it was!
src/node/cli.ts
Outdated
// keyof ... | ||
// Turn that into an array | ||
// Array<...> | ||
type ExtensionArgs = Array<keyof Pick<Args, "list-extensions" | "install-extension" | "uninstall-extension">> |
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.
I like the overall strategy to make this type-safe and get the benefit from the compiler! The only thing I find a bit awkward is that we have to reference the "list-extensions" | "install-extensions" | "uninstall-extensions"
twice here.
I wonder if we could get most of the benefits, without having to define these multiple times, by doing:
type ArgumentKeys = Array<keyof Args>;
const extensionRelatedArgs: ArgumentKeys = ["list-extensions", "install-extension", "uninstall-extension"];
That way - the compiler will still flag an error if these get out-of-sync, but you only have to define them once here in shouldRunVsCodeCli
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.
Oh this is a huge improvement. Waaaaay better. Didn't think about that. Thanks for providing this suggestion! I'll incorporate this suggestion before merging! 🙌
}) | ||
|
||
it("should use the args.port over process.env.PORT if both set", () => { | ||
const [setValue, resetValue] = useEnv("PORT") |
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.
Nice set of tests 👍
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.
Thanks!
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.
Looks great to me - nice work @jsjoeio ! I like your approach to leveraging the TS type system as much as possible.
Just had minor feedback about streamlining having to define the "list-extensions" | "install-extension" | "uninstall-extension"
. Up to you if you want to incorporate that or not 👍
541b3cc
to
f847575
Compare
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.
✔️ Nice work!
This PR adds more tests for
src/node/cli.ts
. Specifically, these functions:shouldRunVsCodeCli
andbindAddrFromArgs
Fixes #4210
TODOS
shouldRunVsCodeCli
bindAddrFromArgs