Skip to content

Conversation

@ncode
Copy link
Owner

@ncode ncode commented Sep 6, 2025

This pull request refactors Consul client handling to use a new interface-based abstraction, improving testability and decoupling command logic from direct dependency on the HashiCorp Consul API. It introduces a factory pattern for Consul client creation, allowing easy mocking in tests, and updates both the run and cleanup commands and their tests to use this new approach. The changes also add comprehensive tests for various edge cases using the new mock infrastructure.

Consul client abstraction and factory pattern:

  • Introduced ConsulClient and ConsulAgent interfaces, along with a ClientFactory abstraction in pkg/consul/factory.go, enabling dependency injection and easier mocking for tests. Added DefaultFactory for real clients and MockFactory for test clients. ([pkg/consul/factory.goR1-R102](https://github.com/ncode/tagit/pull/21/files#diff-1142f61e0e32cf8339152cc77944982a9586034efa8d560f7b1bf950d664cdcfR1-R102))
  • Replaced direct usage of github.com/hashicorp/consul/api in cmd/cleanup.go and cmd/run.go with the new factory-based approach, using consul.CreateClient for client instantiation. ([[1]](https://github.com/ncode/tagit/pull/21/files#diff-1af21b9d6fc06756cac01f80ce66e7e10df29ff796851fb52e0870501b111868L23-R23), [[2]](https://github.com/ncode/tagit/pull/21/files#diff-8146f8148ccbf6711d65f532f6ab9a7c8dfbdc3960c7ffc974f17d6d224dd349L27-R27))
  • Updated command logic in cmd/cleanup.go and cmd/run.go to use the new interfaces and removed direct references to API-specific types, passing the abstracted client to tagit.New. ([[1]](https://github.com/ncode/tagit/pull/21/files#diff-1af21b9d6fc06756cac01f80ce66e7e10df29ff796851fb52e0870501b111868L37-R50), [[2]](https://github.com/ncode/tagit/pull/21/files#diff-8146f8148ccbf6711d65f532f6ab9a7c8dfbdc3960c7ffc974f17d6d224dd349L62-R76), [[3]](https://github.com/ncode/tagit/pull/21/files#diff-8146f8148ccbf6711d65f532f6ab9a7c8dfbdc3960c7ffc974f17d6d224dd349L97-R96))

Testing improvements:

  • Added extensive mocking infrastructure and test cases in cmd/cleanup_test.go and cmd/run_test.go to cover success, error, and edge scenarios for both commands. Tests now use consul.SetFactory to inject mock clients, verifying correct behavior without requiring a real Consul server. ([[1]](https://github.com/ncode/tagit/pull/21/files#diff-88bf4cf247b2469ebabdf721d68b5a01087e99ac1688ef053dc13b7f23dc7b7eR250-R279), [[2]](https://github.com/ncode/tagit/pull/21/files#diff-88bf4cf247b2469ebabdf721d68b5a01087e99ac1688ef053dc13b7f23dc7b7eR291-R501), [[3]](https://github.com/ncode/tagit/pull/21/files#diff-e5c6b0d9de21f1d8dee56a4b65988b10e5e74030b7dd52ef18195901e6f879adR489-R681))
  • Updated imports in test files to support the new abstraction and mock types. ([[1]](https://github.com/ncode/tagit/pull/21/files#diff-88bf4cf247b2469ebabdf721d68b5a01087e99ac1688ef053dc13b7f23dc7b7eR5-R9), [[2]](https://github.com/ncode/tagit/pull/21/files#diff-e5c6b0d9de21f1d8dee56a4b65988b10e5e74030b7dd52ef18195901e6f879adR11-R12))

@codecov
Copy link

codecov bot commented Sep 10, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.99%. Comparing base (7a33e19) to head (451c087).
⚠️ Report is 8 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main      #21       +/-   ##
===========================================
+ Coverage   72.91%   88.99%   +16.07%     
===========================================
  Files           7        8        +1     
  Lines         432      436        +4     
===========================================
+ Hits          315      388       +73     
+ Misses        104       36       -68     
+ Partials       13       12        -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ncode ncode merged commit 09d3fe8 into main Sep 10, 2025
5 checks passed
@ncode ncode deleted the ncode/coverage branch September 10, 2025 20:00
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.

1 participant