Skip to content

Test Coverage PR-1: Tests added in ./licensecheck and ./repodata subdir#231

Merged
mohanmanikanta2299 merged 5 commits into
mainfrom
mohan/test_coverage
Jun 16, 2026
Merged

Test Coverage PR-1: Tests added in ./licensecheck and ./repodata subdir#231
mohanmanikanta2299 merged 5 commits into
mainfrom
mohan/test_coverage

Conversation

@mohanmanikanta2299

@mohanmanikanta2299 mohanmanikanta2299 commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator

🛠️ 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

  • Implemented comprehensive table-driven tests for the ./licensecheck and ./repodata packages.
  • Added validation for CLI command flags, ensuring default values and flag bindings are properly configured.
  • Added strict assertions (assert.Equal, assert.Contains) and explicit error checking to validate function outputs and edge cases.
  • Used require for critical initializations (like loggers and flag lookups) to prevent nil-pointer panics during test execution.

Testing

  • Ran go test -v ./... (All tests passing)
  • Ran go test -race ./... (No race conditions detected)
  • Verified coverage metrics locally.

🔗 External Links

https://hashicorp.atlassian.net/browse/CCEN-512

👍 Definition of Done

  • Tests added : ✅

🤔 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.

@mohanmanikanta2299 mohanmanikanta2299 requested a review from a team as a code owner June 3, 2026 09:40
Comment thread licensecheck/licensecheck_test.go Outdated
// 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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mohanmanikanta2299 , please enable co-pilot review as well, it will help saving some time in review from other Devs.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed the concern.
Thanks

@CreatorHead CreatorHead Jun 12, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still not fixed — assert.Contains with "C" as the header still passes since "C" exists in the original content.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

Comment thread licensecheck/update_test.go Outdated
Comment thread licensecheck/update_test.go Outdated
Comment thread licensecheck/update_test.go Outdated
Comment thread licensecheck/licensecheck_test.go Outdated
Comment thread repodata/repodata_test.go Outdated
@mohanmanikanta2299 mohanmanikanta2299 marked this pull request as ready for review June 4, 2026 06:17
@mohanmanikanta2299 mohanmanikanta2299 marked this pull request as draft June 4, 2026 06:21
@mohanmanikanta2299 mohanmanikanta2299 changed the title Test Coverage Improvement - 1 Test Coverage PR-1: Tests added in ./licensecheck and ./repodata subdir Jun 5, 2026
@mohanmanikanta2299 mohanmanikanta2299 marked this pull request as ready for review June 5, 2026 15:49
@mohanmanikanta2299 mohanmanikanta2299 requested review from a team and CreatorHead June 5, 2026 15:49
Comment thread licensecheck/copyright_test.go Outdated
Comment thread licensecheck/copyright_test.go Outdated
Comment thread licensecheck/licensecheck_test.go Outdated
Comment thread licensecheck/licensecheck_test.go Outdated
Comment thread licensecheck/licensecheck_test.go Outdated
Comment thread licensecheck/update_test.go Outdated
Comment thread licensecheck/update_test.go Outdated
Comment on lines +1062 to +1063
once = sync.Once{}
err := InitializeGitCache(evalRoot)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These subtests directly reset shared package variables like once, firstCommitYearCached, and lastCommitYearsCache. What happens if these tests ever run in parallel?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These subtests share package-level variables oncelastCommitYearsCachefirstCommitYearCached.
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.

Comment thread licensecheck/copyright_test.go Outdated
Comment thread licensecheck/update_test.go Outdated
Comment thread licensecheck/licensecheck_test.go Outdated
// 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)

@CreatorHead CreatorHead Jun 12, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still not fixed — assert.Contains with "C" as the header still passes since "C" exists in the original content.

@mohanmanikanta2299 mohanmanikanta2299 merged commit d581371 into main Jun 16, 2026
5 checks passed
@mohanmanikanta2299 mohanmanikanta2299 deleted the mohan/test_coverage branch June 16, 2026 13:41
mohanmanikanta2299 added a commit that referenced this pull request Jun 17, 2026
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants