internal/config_test.go

97 lines · 3093 bytes

 1package internal
 2
 3import (
 4	"os"
 5	"path/filepath"
 6	"strings"
 7	"testing"
 8)
 9
10func TestLoadConfigForPlatformCreatesAndroidSchema(t *testing.T) {
11	t.Setenv("HOME", t.TempDir())
12
13	configPath := filepath.Join(os.Getenv("HOME"), ".config", "curd", "android.conf")
14	config, err := LoadConfigForPlatform(configPath, PlatformAndroid)
15	if err != nil {
16		t.Fatalf("LoadConfigForPlatform returned error: %v", err)
17	}
18
19	if !config.IsAndroid() {
20		t.Fatalf("expected android platform config, got %+v", config)
21	}
22	if config.AndroidPlayerPackage == "" {
23		t.Fatalf("expected AndroidPlayerPackage to be populated")
24	}
25	if config.Player != "" {
26		t.Fatalf("expected desktop Player to be excluded from android schema, got %q", config.Player)
27	}
28
29	data, err := os.ReadFile(configPath)
30	if err != nil {
31		t.Fatalf("failed to read config file: %v", err)
32	}
33
34	content := string(data)
35	if !strings.Contains(content, "AndroidPlayerPackage=") {
36		t.Fatalf("expected AndroidPlayerPackage in android config: %s", content)
37	}
38	if strings.Contains(content, "Player=") {
39		t.Fatalf("did not expect desktop Player key in android config: %s", content)
40	}
41}
42
43func TestLoadConfigForPlatformCreatesDesktopSchema(t *testing.T) {
44	t.Setenv("HOME", t.TempDir())
45
46	configPath := filepath.Join(os.Getenv("HOME"), ".config", "curd", "curd.conf")
47	config, err := LoadConfigForPlatform(configPath, PlatformDesktop)
48	if err != nil {
49		t.Fatalf("LoadConfigForPlatform returned error: %v", err)
50	}
51
52	if config.IsAndroid() {
53		t.Fatalf("expected desktop platform config, got %+v", config)
54	}
55	if config.Player == "" {
56		t.Fatalf("expected desktop Player to be populated")
57	}
58	if config.AndroidPlayerPackage != "" {
59		t.Fatalf("expected AndroidPlayerPackage to be excluded from desktop schema, got %q", config.AndroidPlayerPackage)
60	}
61
62	data, err := os.ReadFile(configPath)
63	if err != nil {
64		t.Fatalf("failed to read config file: %v", err)
65	}
66
67	content := string(data)
68	if !strings.Contains(content, "Player=") {
69		t.Fatalf("expected Player in desktop config: %s", content)
70	}
71	if strings.Contains(content, "AndroidPlayerPackage=") {
72		t.Fatalf("did not expect AndroidPlayerPackage in desktop config: %s", content)
73	}
74}
75
76func TestDefaultConfigPathForPlatform(t *testing.T) {
77	t.Setenv("HOME", "/tmp/curd-home")
78
79	androidPath := DefaultConfigPathForPlatform(PlatformAndroid)
80	desktopPath := DefaultConfigPathForPlatform(PlatformDesktop)
81
82	if !strings.HasSuffix(androidPath, filepath.Join(".config", "curd", "android.conf")) {
83		t.Fatalf("unexpected android config path: %s", androidPath)
84	}
85	if !strings.HasSuffix(desktopPath, filepath.Join(".config", "curd", "curd.conf")) {
86		t.Fatalf("unexpected desktop config path: %s", desktopPath)
87	}
88}
89
90func TestInferPlatformForConfigPath(t *testing.T) {
91	if got := InferPlatformForConfigPath("/tmp/android.conf", PlatformDesktop); got != PlatformAndroid {
92		t.Fatalf("expected android platform for android.conf, got %q", got)
93	}
94	if got := InferPlatformForConfigPath("/tmp/curd.conf", PlatformAndroid); got != PlatformDesktop {
95		t.Fatalf("expected desktop platform for curd.conf, got %q", got)
96	}
97}