test: add coverage for the config subcommand (show, set, unset) - #1092
Conversation
Covers runConfigCommand in src/cli/config-command.ts: show with and without a configured ca-cert, set with a valid and an invalid path, and unset. console.log is captured and os.homedir() is stubbed to a temp dir so the real user config is never touched. Closes OWASP#1090
sonukapoor
left a comment
There was a problem hiding this comment.
This is solid. I mutation-tested it rather than just reading it: 17 of 20 mutations to config-command.ts were caught, including every wording change, both writeConfig calls, the keys.length === 0 guard, and the validation call.
Two things I want to call out. It tests the real module and verifies persistence through the actual readConfig(), rather than reimplementing the logic in the test body. We had exactly that problem in another PR last week, so it is good to see it done properly here. And the os.homedir spy genuinely isolates the temp home; I checksummed my real config before and after the run and it was untouched.
Three mutations survived, all the same gap. config-command.ts prints Config file: <path> on four paths and the tests assert it on one, so removing that line from the populated show, set and unset paths leaves the suite green. One line per test closes it:
expect(logged.join("\n")).toContain(getConfigPath());Not holding the merge for it. If you want a quick follow-up it is a good one, and it is a nice illustration of what mutation testing catches that coverage percentages do not.
Two corrections on the description, neither affecting the code. os.homedir() does honour $HOME, I checked: setting it and calling os.homedir() returns the override. Your spy approach is still the better choice because it also works on Windows where USERPROFILE is what matters. And the scan-fix-verify-exitcode failure you mentioned does not reproduce here, on your branch or rebased on main, so main is not broken. Likely local.
|
Merged, thank you @chiliec. |
What changed and why
src/cli/config-command.ts(runConfigCommand) had no test coverage. This addstests/cli/config-command.test.tscovering all of its branches:showwith no config printsNo configuration set.and the config file pathshowwith aca-certconfigured prints the key and its valuesetwith a valid PEM path stores it (asserted viareadConfig())setwith an invalid path surfaces thecannot read filevalidation error and writes nothingunsetremoves a previously stored valueunsetwith nothing set reports it is not setThe command writes to
console.log, so the tests capture it withjest.spyOn(console, "log").os.homedir()is stubbed to amkdtempSynctemp dir per test so the real~/.cve-lite-cli/config.jsonis never touched (in-process,os.homedir()ignores$HOME, unlike the child-process e2e tests). Style mirrors the existingtests/cli/config.test.ts.No source changed — test-only.
Validation
RED→GREEN: gutting
runConfigCommand's body turns all 6 tests red; restoring it turns them green, so the tests exercise real behavior rather than passing vacuously.tsc -p tsconfig.test.json --noEmitreports no errors in this file (pre-existing errors in other unrelated test files are untouched). One unrelated test,tests/cli/scan-fix-verify-exitcode.test.ts, fails on a clean checkout here too (independent of this PR, confirmed by removing this file and rerunning) — happy to look if that's unexpected on CI.Closes #1090