Test Coverage PR-1: Tests added in ./licensecheck and ./repodata subdir#231
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.
There was a problem hiding this comment.
This unit test is written for the method AddHeader in licensecheck.go file. The purpose of this method is to read the existing the file content, prepend header+"\n\n" to the file and write it back into the file.
Since it is a unit test written and we are mocking the parameters filepath and header, any valid String value in header will be appended to the given valid filepath mocked in the unit test.
Even if we write "C" or "Copyright (c) 2023 Test Corp", the header should be prepended to the file since both of them are valid strings.
We are not validating the Copyright Header in the AddHeader method which we are testing.
However, I have tightened assertions around the test case to make sure the order of statements written are correct.
| 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?
There was a problem hiding this comment.
These subtests share package-level variables once, lastCommitYearsCache, firstCommitYearCached.
Each subtest needs to reset that cache to test a different scenario and that reset is not safe to do concurrently. sync.Once cannot be reassigned while another goroutine is inside once.Do, and the map has no lock protecting it.
In production this is fine because InitializeGitCache is called only one time before the worker pool starts, so by the time goroutines read the map it's already fully written. But in tests we're deliberately forcing re-initialisation.
Adding t.Parallel() here would cause data races that go test -race would catch. Sequential execution is the right call for this test. The resetGitCacheForTest helper and the comment on TestGitOperations are added to make that constraint visible so no one accidentally changes it later.
| // 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.
…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 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.