From e1daea74302b6f58e8d58cbb75a10b475c38a4e4 Mon Sep 17 00:00:00 2001 From: Michael Brooks Date: Tue, 24 Nov 2026 10:22:09 -0700 Subject: [PATCH 1/2] test: add unit tests for InitLogFile in internal/iostreams --- internal/iostreams/logfile_test.go | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/internal/iostreams/logfile_test.go b/internal/iostreams/logfile_test.go index fa5fc348..6f6b7f57 100644 --- a/internal/iostreams/logfile_test.go +++ b/internal/iostreams/logfile_test.go @@ -191,3 +191,52 @@ func Test_FlushToLogFile(t *testing.T) { }) } } + +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, + 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, "") + } + 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)) + 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.Error(t, err) + 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) + assert.Contains(t, string(logFile), "------------------------------------") + assert.Contains(t, string(logFile), sessionID) + assert.Contains(t, string(logFile), traceID) + assert.NoFileExists(t, oldLogFilePath) + }) + } +} From 9319d07dcbf0e298f06766983962a2b48ff9a580 Mon Sep 17 00:00:00 2001 From: Michael Brooks Date: Tue, 24 Nov 2026 10:33:46 -0700 Subject: [PATCH 2/2] test: check error code, labeled IDs, and kept logs in InitLogFile tests --- internal/iostreams/logfile_test.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/internal/iostreams/logfile_test.go b/internal/iostreams/logfile_test.go index 6f6b7f57..855934a0 100644 --- a/internal/iostreams/logfile_test.go +++ b/internal/iostreams/logfile_test.go @@ -27,6 +27,7 @@ import ( "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" @@ -214,6 +215,13 @@ func Test_IOStreams_InitLogFile(t *testing.T) { 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() @@ -225,7 +233,7 @@ func Test_IOStreams_InitLogFile(t *testing.T) { err := io.InitLogFile(ctx) if tc.expectedErr { - assert.Error(t, err) + assert.Equal(t, slackerror.ErrContextValueNotFound, slackerror.ToSlackError(err).Code) return } require.NoError(t, err) @@ -234,9 +242,11 @@ func Test_IOStreams_InitLogFile(t *testing.T) { sessionID, _ := slackcontext.SessionID(ctx) traceID, _ := slackcontext.OpenTracingTraceID(ctx) assert.Contains(t, string(logFile), "------------------------------------") - 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+"]") assert.NoFileExists(t, oldLogFilePath) + assert.FileExists(t, recentLogFilePath) + assert.FileExists(t, otherFilePath) }) } }