summaryrefslogtreecommitdiff
path: root/internal/config/config_test.go
blob: db0e0682f757a3e97f2a0e09a9eefa485ca10432 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package config

import (
	"bytes"
	"os"
	"path/filepath"
	"testing"
)

func TestConfig_parseReader(t *testing.T) {
	tests := []struct {
		name    string
		input   string
		wantBar int
		wantExt bool
	}{
		{"empty", "", 1200, false},
		{"barwidth", "barwidth=42\n", 42, false},
		{"extended_1", "extended=1\n", 1200, true},
		{"extended_true", "extended=true\n", 1200, true},
		{"comments", "# foo\nbarwidth=10\n# bar\n", 10, false},
		{"unknown_key", "barwidth=5\nunknown=ignored\n", 5, false},
		{"multiple", "barwidth=30\nextended=1\nshowcores=1\n", 30, true},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			c := Default()
			f, _ := os.Open(os.DevNull)
			defer f.Close()
			// Use a temp file with the content since parseReader takes *os.File
			dir := t.TempDir()
			path := filepath.Join(dir, "rc")
			if err := os.WriteFile(path, []byte(tt.input), 0600); err != nil {
				t.Fatal(err)
			}
			f2, err := os.Open(path)
			if err != nil {
				t.Fatal(err)
			}
			defer f2.Close()
			if err := c.parseReader(f2); err != nil {
				t.Fatal(err)
			}
			if c.BarWidth != tt.wantBar {
				t.Errorf("BarWidth = %d, want %d", c.BarWidth, tt.wantBar)
			}
			if c.Extended != tt.wantExt {
				t.Errorf("Extended = %v, want %v", c.Extended, tt.wantExt)
			}
		})
	}
}

func TestConfig_writeTo(t *testing.T) {
	c := Default()
	c.BarWidth = 25
	c.ShowCores = true
	dir := t.TempDir()
	path := filepath.Join(dir, "out")
	f, err := os.Create(path)
	if err != nil {
		t.Fatal(err)
	}
	err = c.writeTo(f)
	f.Close()
	if err != nil {
		t.Fatal(err)
	}
	data, _ := os.ReadFile(path)
	if len(data) == 0 {
		t.Error("writeTo wrote nothing")
	}
	if !bytes.Contains(data, []byte("barwidth=25")) {
		t.Errorf("expected barwidth=25 in %s", data)
	}
	if !bytes.Contains(data, []byte("showcores=1")) {
		t.Errorf("expected showcores=1 in %s", data)
	}
}

func TestConfig_showAvgLineRoundTrip(t *testing.T) {
	// Write a config with showavgline=1, read it back, verify round-trip
	c := Default()
	c.ShowAvgLine = true

	dir := t.TempDir()
	path := filepath.Join(dir, "rc")
	f, err := os.Create(path)
	if err != nil {
		t.Fatal(err)
	}
	if err := c.writeTo(f); err != nil {
		f.Close()
		t.Fatal(err)
	}
	f.Close()

	data, _ := os.ReadFile(path)
	if !bytes.Contains(data, []byte("showavgline=1")) {
		t.Errorf("expected showavgline=1 in output, got:\n%s", data)
	}

	// Read it back
	c2 := Default()
	f2, err := os.Open(path)
	if err != nil {
		t.Fatal(err)
	}
	defer f2.Close()
	if err := c2.parseReader(f2); err != nil {
		t.Fatal(err)
	}
	if !c2.ShowAvgLine {
		t.Error("expected ShowAvgLine=true after round-trip")
	}
}

func TestGetClusterHostsFromFile(t *testing.T) {
	dir := t.TempDir()
	path := filepath.Join(dir, "clusters")

	tests := []struct {
		name      string
		content   string
		cluster   string
		wantHosts []string
		wantErr   bool
	}{
		{"single_host", "foo host1\n", "foo", []string{"host1"}, false},
		{"two_hosts", "bar host1 host2\n", "bar", []string{"host1", "host2"}, false},
		{"missing_returns_cluster", "x y\n", "missing", []string{"missing"}, false},
		{"recursive", "a b\nb c\nc d\n", "a", []string{"d"}, false},
		{"cycle", "a b\nb a\n", "a", nil, true},
		{"comment_ignored", "# comment\na h1\n", "a", []string{"h1"}, false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if err := os.WriteFile(path, []byte(tt.content), 0600); err != nil {
				t.Fatal(err)
			}
			got, err := GetClusterHostsFromFile(tt.cluster, path)
			if (err != nil) != tt.wantErr {
				t.Errorf("GetClusterHostsFromFile() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if tt.wantErr {
				return
			}
			if len(got) != len(tt.wantHosts) {
				t.Errorf("got %v, want %v", got, tt.wantHosts)
				return
			}
			for i := range got {
				if got[i] != tt.wantHosts[i] {
					t.Errorf("got[%d] = %s, want %s", i, got[i], tt.wantHosts[i])
				}
			}
		})
	}
}