Test Coverage PR-3: Tests added in ./cmd subdir#234
Test Coverage PR-3: Tests added in ./cmd subdir#234mohanmanikanta2299 wants to merge 12 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds/expands table-driven tests in the cmd/ package to improve overall repository test coverage, focusing primarily on Cobra command wiring (flags/help/registration) and a few utility helpers.
Changes:
- Added new unit tests for core CLI commands (
root,init,report,dispatch,debug) to validate flag defaults, help text, and command registration. - Expanded
cmd/utils_test.goto cover additional utility behaviors (GetVersion, table writer setup/rendering,stringArrayToRow). - Added subprocess-based tests for
initPreRun validation paths that callcobra.CheckErr(which exits).
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| cmd/utils_test.go | Expands tests for CLI utility helpers (version formatting, table rendering, row conversion, colorize). |
| cmd/root_test.go | Adds tests for root flags/help/version plus config/logger initialization paths. |
| cmd/report_test.go | Adds tests ensuring report command registration, subcommands, help text, and flag defaults. |
| cmd/init_test.go | Adds tests for init flags/help, HCL rendering, and PreRun validation (incl. subprocess exit cases). |
| cmd/dispatch_test.go | Adds tests for dispatch flag defaults, shorthands, help text, and registration. |
| cmd/debug_test.go | Adds tests for debug flags/help/registration and a runtime execution check (see comments re: network dependence). |
) Bumps [github.com/jedib0t/go-pretty/v6](https://github.com/jedib0t/go-pretty) from 6.7.9 to 6.8.0. - [Release notes](https://github.com/jedib0t/go-pretty/releases) - [Commits](jedib0t/go-pretty@v6.7.9...v6.8.0) --- updated-dependencies: - dependency-name: github.com/jedib0t/go-pretty/v6 dependency-version: 6.8.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps [actions/checkout](https://github.com/actions/checkout) from 6.0.2 to 6.0.3. - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](actions/checkout@de0fac2...df4cb1c) --- updated-dependencies: - dependency-name: actions/checkout dependency-version: 6.0.3 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
…e.go files (#235) * Tests added for cmd/headers.go and cmd/license.go * fix: refactor tests to address copilot review comments * fix: address review comments and nits. * fix: error check in test case * Refactor tests to call methods with boilerplate.
| assert.Equal(t, ".copywrite.hcl", flag.DefValue) | ||
| } | ||
|
|
||
| func Test_initConfig_FileNotExist(t *testing.T) { |
There was a problem hiding this comment.
This test verifies that initConfig() is a no operation when the config file doesn't exist, it should not panic or modify the global conf state. The assertions confirm that conf still holds its default values (empty License, SchemaVersion = 1) after the call, proving no accidental mutation occurred.
| oldCfgPath := cfgPath | ||
| defer func() { cfgPath = oldCfgPath }() | ||
|
|
||
| cfgPath = "/tmp/nonexistent_copywrite_test_config_12345.hcl" |
There was a problem hiding this comment.
The tmp path is not present for windows systems. Try using the line filepath.Join(t.TempDir(), "nonexistent.hcl") to keep it agnostic of the underlying running system
There was a problem hiding this comment.
Added filepath.Join instead of /tmp/nonexistent_copywrite_test_config_12345.hcl in the test case for supporting Windows system.
| t.Chdir(tmpDir) | ||
|
|
||
| buf := new(bytes.Buffer) | ||
| restoreRootCmd(t) |
There was a problem hiding this comment.
It is usually more idiomatic to have the cleanup commands run after a test is done, so that other tests don't have to rely on doing a cleanup beforehand.
There was a problem hiding this comment.
Used helper functions that perform cleanup at the end of the test case and restore original cmd State
| buf := new(bytes.Buffer) | ||
| initCmd.SetOut(buf) | ||
| initCmd.SetErr(buf) | ||
| initCmd.SetArgs([]string{"--help"}) |
There was a problem hiding this comment.
Please check that cleanup is done for all the commands to prevent non-deterministic between runs.
There was a problem hiding this comment.
Created restoreInitCmd in helpers and used it for cleanup
| t.Run(tt.name, func(t *testing.T) { | ||
| oldVersion := version | ||
| oldCommit := commit | ||
| defer func() { |
There was a problem hiding this comment.
t.Cleanup can be used here for testcases
There was a problem hiding this comment.
Added cleanup instead of defer func()
…ir (#231) * Add test cases to increase coverage for licensecheck and repodata directories * Lint error fix. * Address co-pilot comments. * fix: addressed review comments * fix: tighten assertion in TestAddHeader test-case
🛠️ Description
Summary
This PR is the 3rd part of the test coverage PRs intended to increase the test coverage of the entire repo from 55% to 80+%, fulfilling our coverage requirements.
Changes Made
cmdpackage.debug.go,dispatch.go,init.go,report.go,root.go,utils.go-- list of files incmdpackage for which the test have been created/updated.assert.Equal,assert.Contains) and explicit error checking to validate function outputs and edge cases.requirefor critical initializations (like loggers and flag lookups) to prevent nil-pointer panics during test execution.Testing
go test -v ./...(All tests passing)go test -race ./...(No race conditions detected)🔗 External Links
https://hashicorp.atlassian.net/browse/CCEN-512
🤔 Can be merged upon approval?
✅
PCI review checklist
I have documented a clear reason for, and description of, the change I am making.
If applicable, I've documented a plan to revert these changes if they require more than reverting the pull request.
If applicable, I've documented the impact of any changes to security controls.
Examples of changes to security controls include using new access control methods, adding or removing logging pipelines, etc.