all repos

mugit @ 8703591

🐮 git server that your cow will love

mugit/internal/config/config_test.go(view raw)

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

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

	"olexsmir.xyz/x/is"
)

func TestPathOrDefaultWithCandidates(t *testing.T) {
	first := candidateFile(t, "first.yaml")
	second := candidateFile(t, "second.yaml")
	third := candidateFile(t, "third.yaml")

	t.Run("returns user path when exists", func(t *testing.T) {
		userPath := candidateFile(t, "user.yaml")
		candidates := []string{first, second, third}
		got := pathOrDefaultWithCandidates(userPath, candidates)
		if got != userPath {
			t.Errorf("got %q, want %q", got, userPath)
		}
	})

	t.Run("returns first existing candidate", func(t *testing.T) {
		candidates := []string{first, second, third}
		got := pathOrDefaultWithCandidates("", candidates)
		is.Equal(t, got, first)
	})

	t.Run("returns empty when nothing exists", func(t *testing.T) {
		candidates := []string{}
		got := pathOrDefaultWithCandidates("", candidates)
		if got != "" {
			t.Errorf("got %q, want empty", got)
		}
	})
}

func candidateFile(t *testing.T, name string) string {
	t.Helper()
	out := filepath.Join(t.TempDir(), name)
	os.WriteFile(out, []byte("test"), 0o644)
	return out
}