Skip to content

Commit 8e9d4f2

Browse files
test: refactor Topics test to improve output handling and error checking
1 parent 8d17f52 commit 8e9d4f2

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

internal/agrupamento_de_dados/topic_test.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,25 @@ import (
55
"testing"
66
)
77

8-
type MockContentsProvider struct {
9-
CalledWithRootDir string
10-
CalledWithTopics []string
8+
func init() {
9+
rootDir = "./"
1110
}
1211

13-
func (m *MockContentsProvider) TopicsContents(rootDir string, topics []string) {
14-
m.CalledWithRootDir = rootDir
15-
m.CalledWithTopics = topics
16-
}
17-
18-
func TestTopicsAgrupamentoDeDados(t *testing.T) {
19-
os.Setenv("GOENV", "test")
12+
func TestTopics(t *testing.T) {
13+
oldStdout := os.Stdout
14+
defer func() { os.Stdout = oldStdout }()
2015

21-
rootDir := os.Getenv("GOENV")
22-
23-
if rootDir != "test" {
24-
t.Errorf("Expected rootDir to be 'test', got '%s'", rootDir)
16+
nullFile, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
17+
if err != nil {
18+
t.Fatalf("failed to open null device: %v", err)
2519
}
20+
defer nullFile.Close()
2621

27-
mockProvider := &MockContentsProvider{}
28-
mockProvider.TopicsContents(rootDir, []string{"topic1", "topic2"})
22+
os.Stdout = nullFile
2923

30-
if mockProvider.CalledWithRootDir != rootDir {
31-
t.Errorf("Expected rootDir to be 'test', got '%s'", mockProvider.CalledWithRootDir)
32-
}
24+
Topics()
3325

34-
if len(mockProvider.CalledWithTopics) != 2 {
35-
t.Errorf("Expected 2 topics, got %d", len(mockProvider.CalledWithTopics))
26+
if err != nil {
27+
t.Errorf("Topics() failed: %v", err)
3628
}
3729
}

0 commit comments

Comments
 (0)