|
| 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