Skip to content

test: fix test execution and improve assertions#2631

Merged
theakshaypant merged 2 commits intotektoncd:mainfrom
chmouel:refactor-logs-test-assertions
Apr 2, 2026
Merged

test: fix test execution and improve assertions#2631
theakshaypant merged 2 commits intotektoncd:mainfrom
chmouel:refactor-logs-test-assertions

Conversation

@chmouel
Copy link
Copy Markdown
Member

@chmouel chmouel commented Apr 1, 2026

Summary

Fixed test execution issues in the logs package that prevented comprehensive test coverage:

  • Replaced os.Exit() with proper error handling in showlogswithtkn()
  • Made syscall.Exec mockable via execFunc to prevent process replacement during tests
  • Upgraded assertions to gotest.tools/v3/assert for stronger validation
  • Fixed duplicate test names causing test runner confusion
  • Added gotestsum integration to Makefile for improved test output reporting

error as detected was:

PASS pkg/cmd/tknpac/logs.TestLogs/good/show_logs (0.01s)
=== RUN   TestLogs
FAIL pkg/cmd/tknpac/logs.TestLogs (-1.00s)

=== Failed
=== FAIL: pkg/cmd/tknpac/logs TestLogs (unknown)
ok  	github.com/openshift-pipelines/pipelines-as-code/pkg/cmd/tknpac/logs	0.009s

DONE 2 tests, 1 failure in 0.011s

Changes Made

Makefile

  • Detect and use gotestsum if available on the system
  • Falls back to standard go test if not installed
  • Improves test output formatting and failure reporting

pkg/cmd/tknpac/logs/logs.go

  • Return error instead of calling os.Exit() on syscall.Exec failure
  • Extract syscall.Exec into mockable execFunc variable for testing
  • Allows tests to run completely without process termination

pkg/cmd/tknpac/logs/logs_test.go

  • Remove unused "shift" field from test struct
  • Fix duplicate test case names ("good/show_logs")
  • Switch from basic assertions to gotest.tools/v3/assert
  • Mock execFunc to prevent actual process replacement
  • Simplify test setup by removing unnecessary exec.LookPath calls

Testing

All tests pass with proper assertions validated.

🧪 Testing Strategy

  • Unit tests
  • Integration tests
  • End-to-end tests
  • Manual testing

🤖 AI Assistance

  • I have used AI assistance for this PR.

Generated with Claude Code

✅ Submitter Checklist

  • 📝 Commit messages are clear and follow project guidelines
  • ✨ Commit prefix (test:) matches the type of change
  • ♽ Ran make test and make lint locally - all checks pass
  • 📖 Documentation not needed for internal test improvements
  • 🧪 Added sufficient unit tests for changes
  • 🎁 No end-to-end tests needed (test infrastructure improvement)
  • 🔎 No CI flakiness issues
  • Provider feature (N/A - not a provider feature)

@chmouel chmouel force-pushed the refactor-logs-test-assertions branch from af397b9 to cd191c9 Compare April 1, 2026 22:21
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the Makefile to use gotestsum for unit tests and refactors the logs package to allow mocking of syscall.Exec by introducing a package-level variable. It also improves error handling in showlogswithtkn by returning errors instead of calling os.Exit. Review feedback highlights that the package-level variable approach is prone to race conditions and that syscall.Exec lacks Windows support, which could lead to compilation failures on that platform.

chmouel added 2 commits April 2, 2026 00:52
Updated the unit test command to use `gotestsum` when available to
provide more readable test output, while maintaining compatibility with
the standard Go test runner.

Signed-off-by: Chmouel Boudjnah <chmouel@redhat.com>
The logs tests had a critical issue preventing all test cases from
running. The production code used os.Exit(127) when syscall.Exec failed,
which terminated the entire test process after the first subtest
completed. This made gotestsum report failures and prevented
comprehensive test coverage.

Here is the error you can see in gotestsum previously:

```console
PASS pkg/cmd/tknpac/logs.TestLogs/good/show_logs (0.01s)
=== RUN   TestLogs
FAIL pkg/cmd/tknpac/logs.TestLogs (-1.00s)

=== Failed
=== FAIL: pkg/cmd/tknpac/logs TestLogs (unknown)
ok github.com/openshift-pipelines/pipelines-as-code/pkg/cmd/tknpac/logs
0.009s

DONE 2 tests, 1 failure in 0.011s
```

Changes made:
- Replace os.Exit with proper error return in showlogswithtkn()
- Add execFunc variable to make syscall.Exec mockable in tests
- Mock execFunc in tests to prevent process replacement
- Upgrade to gotest.tools/v3/assert for stronger assertions
- Fix duplicate test name causing gotestsum confusion
- Remove unused "shift" field and invalid test case
- Simplify test setup by removing unnecessary exec.LookPath

gotestsum, and all linting checks pass.

Assisted-by: Claude Sonnet 4.5 (via Claude Code)
@chmouel chmouel force-pushed the refactor-logs-test-assertions branch from cd191c9 to 31766c3 Compare April 1, 2026 22:56
@theakshaypant theakshaypant merged commit f2b8933 into tektoncd:main Apr 2, 2026
11 checks passed
@chmouel chmouel deleted the refactor-logs-test-assertions branch April 2, 2026 07:31
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.

2 participants