Test Coverage PR-1: Tests added in ./licensecheck and ./repodata subdir#231
Test Coverage PR-1: Tests added in ./licensecheck and ./repodata subdir#231mohanmanikanta2299 wants to merge 3 commits into
Conversation
| // Header should come before original content | ||
| headerIdx := lo.IndexOf([]byte(string(content)), []byte(header)[0]) | ||
| contentIdx := lo.IndexOf([]byte(string(content)), []byte(originalContent)[0]) | ||
| assert.Less(t, headerIdx, contentIdx) |
There was a problem hiding this comment.
Hey @mohanmanikanta2299, I changed header to just "C" in the "add header to file with existing content" sub-test and the test still passed. Please take a look and review the PR properly before sending it for review.
aatish@aatish-CLPJCQ9LWW copywrite % go test ./licensecheck/ -run "TestAddHeader/add_header_to_file_with_existing_content" -v -count=1
=== RUN TestAddHeader
=== RUN TestAddHeader/add_header_to_file_with_existing_content
--- PASS: TestAddHeader (0.00s)
--- PASS: TestAddHeader/add_header_to_file_with_existing_content (0.00s)
PASS
ok github.com/hashicorp/copywrite/licensecheck 0.971s
There was a problem hiding this comment.
@mohanmanikanta2299 , please enable co-pilot review as well, it will help saving some time in review from other Devs.
There was a problem hiding this comment.
Addressed the concern.
Thanks
There was a problem hiding this comment.
This is still not fixed — assert.Contains with "C" as the header still passes since "C" exists in the original content.
| } | ||
| content := string(header) + "\nCopyright notice here" | ||
|
|
||
| f, _ := afero.TempFile(AppFs, tempDir, "") |
There was a problem hiding this comment.
Several subtests silently discard errors with _, inconsistent with require.NoError used elsewhere:
If afero.TempFile fails, f is nil, and f.Name() panics. These should use require.NoError.
| content := string(header) + "\nCopyright notice here" | ||
|
|
||
| f, _ := afero.TempFile(AppFs, tempDir, "") | ||
| _ = afero.WriteFile(AppFs, f.Name(), []byte(content), 0644) |
There was a problem hiding this comment.
For consistency we should use require.NoError.
| assert.Nil(t, err) | ||
|
|
||
| // Read file and verify header was added | ||
| content, _ := afero.ReadFile(AppFs, filePath) |
There was a problem hiding this comment.
For consistency we should use require.NoError?
| assert.Nil(t, err) | ||
|
|
||
| // Read file and verify header was prepended | ||
| content, _ := afero.ReadFile(AppFs, filePath) |
There was a problem hiding this comment.
For consistency we should use require.NoError
| err := AddHeader(filePath, header) | ||
| assert.Nil(t, err) | ||
|
|
||
| content, _ := afero.ReadFile(AppFs, filePath) |
There was a problem hiding this comment.
same as above comments
| fileName := "test.go" | ||
| if tt.name == "Skip .copywrite.hcl file" { | ||
| fileName = ".copywrite.hcl" | ||
| } |
There was a problem hiding this comment.
Please check, I think this is an anti-pattern design? (CMIIW) :
The file name shouldn't be derived from tt.name, if the test name is ever reworded or typo'd, this silently breaks the test case it's meant to cover without any compile-time or runtime signal?
| once = sync.Once{} | ||
| err := InitializeGitCache(evalRoot) |
There was a problem hiding this comment.
These subtests directly reset shared package variables like once, firstCommitYearCached, and lastCommitYearsCache. What happens if these tests ever run in parallel?
|
|
||
| t.Run("file larger than 300 bytes with copyright after header", func(t *testing.T) { | ||
| // Create content > 300 bytes with copyright appearing after byte 300 | ||
| header := make([]byte, 350) |
There was a problem hiding this comment.
Magic number embedded in test assertion?
| assert.Equal(t, 2023, lastCommitYearsCache["test2.txt"]) | ||
| }) | ||
|
|
||
| t.Run("InitializeGitCache - Failure", func(t *testing.T) { |
There was a problem hiding this comment.
The test name says "Failure" but asserts NoError. it seems to be testing that the function swallows errors gracefully. If that's intentional, rename the subtest to reflect the expected behavior, and add a comment explaining why errors are suppressed here.
| // Header should come before original content | ||
| headerIdx := lo.IndexOf([]byte(string(content)), []byte(header)[0]) | ||
| contentIdx := lo.IndexOf([]byte(string(content)), []byte(originalContent)[0]) | ||
| assert.Less(t, headerIdx, contentIdx) |
There was a problem hiding this comment.
This is still not fixed — assert.Contains with "C" as the header still passes since "C" exists in the original content.

🛠️ Description
Summary
This PR is the 1st 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
./licensecheckand./repodatapackages.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
👍 Definition of Done
🤔 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.