Skip to content
This repository was archived by the owner on Mar 24, 2024. It is now read-only.

Commit f4494bb

Browse files
authored
chore: add config tests (#299)
1 parent 49f6a53 commit f4494bb

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

internal/fleek/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os/exec"
99
"path/filepath"
1010
"runtime"
11+
"sort"
1112
"strings"
1213

1314
"github.com/hashicorp/go-version"
@@ -328,6 +329,7 @@ func (c *Config) UniqueSystems() []string {
328329
systems = append(systems, syskey)
329330
m[syskey] = true
330331
}
332+
sort.Strings(systems)
331333
return systems
332334

333335
}

internal/fleek/config_test.go

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package fleek
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/ublue-os/fleek/internal/xdg"
9+
)
10+
11+
func TestHostname(t *testing.T) {
12+
h, err := Hostname()
13+
if err != nil {
14+
t.Error(err)
15+
}
16+
overrideHost := "fleekhost"
17+
os.Setenv("FLEEK_HOST_OVERRIDE", overrideHost)
18+
hOverride, err := Hostname()
19+
if err != nil {
20+
t.Error(err)
21+
}
22+
if h == hOverride {
23+
t.Fatalf("host override: expected %s got %s", overrideHost, hOverride)
24+
}
25+
}
26+
27+
func TestUniqueSystems(t *testing.T) {
28+
c := &Config{
29+
Systems: []*System{
30+
{
31+
Arch: "x86-64",
32+
OS: "darwin",
33+
},
34+
},
35+
}
36+
want := "x86-64-darwin"
37+
got := c.UniqueSystems()
38+
if got[0] != want {
39+
t.Fatalf("unique systems: expected %s got %s", want, got)
40+
}
41+
c = &Config{
42+
Systems: []*System{
43+
{
44+
Arch: "x86-64",
45+
OS: "darwin",
46+
},
47+
{
48+
Arch: "aarch64",
49+
OS: "linux",
50+
},
51+
},
52+
}
53+
want = "aarch64-linux"
54+
got = c.UniqueSystems()
55+
if got[0] != want {
56+
t.Fatalf("unique systems: expected %s got %s", want, got)
57+
}
58+
c = &Config{
59+
Systems: []*System{
60+
{
61+
Hostname: "a",
62+
Arch: "x86-64",
63+
OS: "darwin",
64+
},
65+
{
66+
Hostname: "b",
67+
Arch: "aarch64",
68+
OS: "linux",
69+
},
70+
{
71+
Hostname: "c",
72+
Arch: "aarch64",
73+
OS: "darwin",
74+
},
75+
{
76+
Hostname: "d",
77+
Arch: "aarch64",
78+
OS: "darwin",
79+
},
80+
},
81+
}
82+
wantCount := 3
83+
got = c.UniqueSystems()
84+
if len(got) != wantCount {
85+
t.Fatalf("unique systems count: expected %d got %d", wantCount, len(got))
86+
}
87+
}
88+
func TestUserFlakeDir(t *testing.T) {
89+
c := &Config{}
90+
home, _ := os.UserHomeDir()
91+
92+
blankFlakeDir := c.UserFlakeDir()
93+
want := filepath.Join(home, xdg.DataSubpathRel("fleek"))
94+
95+
if blankFlakeDir != want {
96+
t.Fatalf("user flake dir: expected %s, got %s", want, blankFlakeDir)
97+
}
98+
want = filepath.Join(home, ".config", "fleek", "flake")
99+
100+
c = &Config{
101+
FlakeDir: ".config/fleek/flake",
102+
}
103+
manualFlakeDir := c.UserFlakeDir()
104+
if manualFlakeDir != want {
105+
t.Fatalf("manual user flake dir: expected %s, got %s", want, manualFlakeDir)
106+
}
107+
108+
}

0 commit comments

Comments
 (0)