Skip to content

test: add unit tests for InitLogFile in internal/iostreams - #860

Open
mwbrooks wants to merge 3 commits into
mainfrom
mwbrooks-test-init-log-file
Open

mwbrooks wants to merge 3 commits into
mainfrom
mwbrooks-test-init-log-file

Conversation

@mwbrooks

Copy link
Copy Markdown
Member

Changelog

N/A - adds tests only, with no user-facing changes.

Summary

This pull request adds unit tests for InitLogFile in internal/iostreams/logfile.go, which had no test coverage. It is one in a series of small coverage PRs.

  • Adds Test_IOStreams_InitLogFile to internal/iostreams/logfile_test.go with cases for the session details written to the log file, the cleanup of week-old logs, and a missing trace ID
  • Raises internal/iostreams coverage from 57.2% to 63.5%

Testing

  1. Run go test ./internal/iostreams/... -run Test_IOStreams_InitLogFile -v and confirm the cases pass

Requirements

@mwbrooks
mwbrooks requested a review from a team as a code owner November 24, 2026 17:25
@mwbrooks mwbrooks added this to the Next Release milestone Nov 24, 2026
@mwbrooks mwbrooks added code health M-T: Test improvements and anything that improves code health semver:patch Use on pull requests to describe the release version increment labels Nov 24, 2026
@mwbrooks mwbrooks self-assigned this Nov 24, 2026
@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.86%. Comparing base (83e125d) to head (9319d07).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #860      +/-   ##
==========================================
+ Coverage   80.65%   80.86%   +0.20%     
==========================================
  Files         246      246              
  Lines       18837    18832       -5     
==========================================
+ Hits        15193    15228      +35     
+ Misses       3644     3604      -40     

☔ View full report in Codecov by Harness.
📢 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.

@WilliamBergamin WilliamBergamin left a comment

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.

🧪 Thanks @mwbrooks! Backdating a real file with Chtimes checks that cleanup actually runs, not just that the code paths execute.
Left a few comments, nothing blocking 🙏

Comment thread internal/iostreams/logfile_test.go Outdated

err := io.InitLogFile(ctx)
if tc.expectedErr {
assert.Error(t, err)

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.

🧪 suggestion (non-blocking): Could the error case check the code, like update_test.go does? Right now any error passes it, including the log file failing to open. It needs the slackerror import.

Suggested change
assert.Error(t, err)
assert.Equal(t, slackerror.ErrContextValueNotFound, slackerror.ToSlackError(err).Code)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in commit 9319d07, thanks! 🙇

Comment thread internal/iostreams/logfile_test.go Outdated
Comment on lines +237 to +238
assert.Contains(t, string(logFile), sessionID)
assert.Contains(t, string(logFile), traceID)

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.

🧪 suggestion (non-blocking): Could we match the labeled lines? A bare UUID would still match if the two labels were swapped.

Suggested change
assert.Contains(t, string(logFile), sessionID)
assert.Contains(t, string(logFile), traceID)
assert.Contains(t, string(logFile), "SessionID: ["+sessionID+"]")
assert.Contains(t, string(logFile), "Slack-CLI-TraceID: ["+traceID+"]")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in commit 9319d07, thanks! 🙇

assert.Contains(t, string(logFile), "------------------------------------")
assert.Contains(t, string(logFile), sessionID)
assert.Contains(t, string(logFile), traceID)
assert.NoFileExists(t, oldLogFilePath)

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.

🧪 suggestion (non-blocking): Could we add two files that must survive cleanup: a 6-day-old slack-debug- log and an 8-day-old file without that prefix? Without them, the test still passes if the prefix or age check in deleteLogFilesOlderThanOneWeek is dropped.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added in commit 9319d07, great catch! Both files now have to survive cleanup.

}{
"writes the session details to the log file and removes week old logs": {},
"returns an error when the trace ID is missing from the context": {
missingTraceID: true,

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.

🔭 suggestion (non-blocking): This case shows that InitLogFile leaves the log file open when it returns early, because defer errorLogFile.Close() only runs at the end. Moving the defer right after os.OpenFile fixes it. Otherwise t.TempDir cleanup can fail on Windows. Fine here or as a follow-up.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch! I'll open a follow-up PR to move the defer so this one stays test-only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code health M-T: Test improvements and anything that improves code health semver:patch Use on pull requests to describe the release version increment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants