Repository navigation
test: add unit tests for InitLogFile in internal/iostreams #860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
|
@@ -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, | ||
| 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)) | ||
| 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) | ||
| 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| }) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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
InitLogFileleaves the log file open when it returns early, becausedefer errorLogFile.Close()only runs at the end. Moving the defer right afteros.OpenFilefixes it. Otherwiset.TempDircleanup can fail on Windows. Fine here or as a follow-up.There was a problem hiding this comment.
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
deferso this one stays test-only.