96 lines
2.6 KiB
Go
96 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestInstallFromFreshCheckout(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
source := filepath.Join(tmp, "source")
|
|
bin := filepath.Join(tmp, "bin")
|
|
for _, dir := range []string{source, bin} {
|
|
if err := os.Mkdir(dir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
entries, err := os.ReadDir(".")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, entry := range entries {
|
|
if !entry.Type().IsRegular() || entry.Name() == "state.toml" {
|
|
continue
|
|
}
|
|
data, err := os.ReadFile(entry.Name())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(source, entry.Name()), data, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
writeExecutable(t, filepath.Join(bin, "tinty"), `#!/bin/sh
|
|
case "$1" in
|
|
sync|info) exit 0 ;;
|
|
apply) printf '%s\n' "$2" >> "$TINTY_LOG" ;;
|
|
*) exit 2 ;;
|
|
esac
|
|
`)
|
|
for _, name := range []string{"dark-notify", "launchctl"} {
|
|
writeExecutable(t, filepath.Join(bin, name), "#!/bin/sh\nexit 0\n")
|
|
}
|
|
home := filepath.Join(tmp, "home")
|
|
config := filepath.Join(home, ".config")
|
|
log := filepath.Join(tmp, "applied")
|
|
cache, err := exec.Command("go", "env", "GOMODCACHE").Output()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("GOMODCACHE", strings.TrimSpace(string(cache)))
|
|
t.Setenv("PATH", bin+":"+os.Getenv("PATH"))
|
|
t.Setenv("TINTY_LOG", log)
|
|
install := func() {
|
|
t.Helper()
|
|
cmd := exec.Command("make", "install", "HOME="+home, "XDG_CONFIG_HOME="+config)
|
|
cmd.Dir = source
|
|
if output, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("install failed: %v\n%s", err, output)
|
|
}
|
|
}
|
|
install()
|
|
statePath := filepath.Join(config, "tinted-theming", "tinty", "state.toml")
|
|
value, err := loadState(statePath)
|
|
if err != nil {
|
|
t.Fatalf("fresh install did not provide usable state: %v", err)
|
|
}
|
|
cmd := exec.Command(filepath.Join(home, ".local", "bin", "tint"), "swamp")
|
|
cmd.Env = append(os.Environ(), "TINT_STATE_FILE="+statePath, "TINTY_PAIR_APPEARANCE=dark")
|
|
if output, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("selecting an installed pair failed: %v\n%s", err, output)
|
|
}
|
|
if got := strings.TrimSpace(readFile(t, log)); got != "base16-swamp-dark" {
|
|
t.Fatalf("applied theme = %q, want base16-swamp-dark", got)
|
|
}
|
|
value.Current = currentPair{Light: "personal-light", Dark: "personal-dark"}
|
|
if err := saveState(statePath, value); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before, err := os.ReadFile(statePath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
install()
|
|
after, err := os.ReadFile(statePath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !bytes.Equal(before, after) {
|
|
t.Fatal("reinstall overwrote personal state")
|
|
}
|
|
}
|