Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions internal/iostreams/logfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"github.com/slackapi/slack-cli/internal/goutils"
"github.com/slackapi/slack-cli/internal/slackcontext"
"github.com/slackapi/slack-cli/internal/slackdeps"
"github.com/slackapi/slack-cli/internal/slackerror"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -191,3 +192,61 @@
})
}
}

func Test_IOStreams_InitLogFile(t *testing.T) {
tests := map[string]struct {
missingTraceID bool
expectedErr bool
}{
"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.

expectedErr: true,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())
if tc.missingTraceID {
ctx = slackcontext.SetOpenTracingTraceID(ctx, "")

Check failure on line 211 in internal/iostreams/logfile_test.go

View workflow job for this annotation

GitHub Actions / Lints and Tests

undefined: slackcontext.SetOpenTracingTraceID
}
logsDir := t.TempDir()
oldLogFilePath := filepath.Join(logsDir, "slack-debug-20000101.log")
require.NoError(t, os.WriteFile(oldLogFilePath, []byte("old"), 0600))
oldTime := time.Now().AddDate(0, 0, -8)
require.NoError(t, os.Chtimes(oldLogFilePath, oldTime, oldTime))
recentLogFilePath := filepath.Join(logsDir, "slack-debug-20000102.log")
require.NoError(t, os.WriteFile(recentLogFilePath, []byte("recent"), 0600))
recentTime := time.Now().AddDate(0, 0, -6)
require.NoError(t, os.Chtimes(recentLogFilePath, recentTime, recentTime))
otherFilePath := filepath.Join(logsDir, "other-20000101.log")
require.NoError(t, os.WriteFile(otherFilePath, []byte("other"), 0600))
require.NoError(t, os.Chtimes(otherFilePath, oldTime, oldTime))
fsMock := slackdeps.NewFsMock()
osMock := slackdeps.NewOsMock()
osMock.AddDefaultMocks()
var systemConfigMock config.SystemConfigMock
systemConfigMock.On("LogsDir", mock.Anything).Return(logsDir, nil)
config := config.NewConfig(fsMock, osMock)
config.SystemConfig = &systemConfigMock
io := NewIOStreams(config, fsMock, osMock)

err := io.InitLogFile(ctx)
if tc.expectedErr {
assert.Equal(t, slackerror.ErrContextValueNotFound, slackerror.ToSlackError(err).Code)
return
}
require.NoError(t, err)
logFile, err := os.ReadFile(filepath.Join(logsDir, filename))
require.NoError(t, err)
sessionID, _ := slackcontext.SessionID(ctx)
traceID, _ := slackcontext.OpenTracingTraceID(ctx)

Check failure on line 243 in internal/iostreams/logfile_test.go

View workflow job for this annotation

GitHub Actions / Lints and Tests

undefined: slackcontext.OpenTracingTraceID (typecheck)
assert.Contains(t, string(logFile), "------------------------------------")
assert.Contains(t, string(logFile), "SessionID: ["+sessionID+"]")
assert.Contains(t, string(logFile), "Slack-CLI-TraceID: ["+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.

assert.FileExists(t, recentLogFilePath)
assert.FileExists(t, otherFilePath)
})
}
}
Loading