@@ -3,46 +3,49 @@ package config
33import (
44 "os"
55 "path/filepath"
6- "runtime"
76 "testing"
87)
98
10- func TestWriteEmbeddedConfig (t * testing.T ) {
11- testHomeDir := t .TempDir ()
9+ func TestInstallDefaultConfig (t * testing.T ) {
10+ path := filepath . Join ( t .TempDir (), "trakx" , "trakx.yaml" )
1211
13- homeEnv := "HOME"
14- switch runtime .GOOS {
15- case "windows" :
16- homeEnv = "USERPROFILE"
17- case "plan9" :
18- homeEnv = "home"
12+ if err := installDefaultConfig (path ); err != nil {
13+ t .Fatalf ("installDefaultConfig failed: %v" , err )
1914 }
2015
21- t .Setenv (homeEnv , testHomeDir )
22-
23- // macOS: ~/Library/Application Support and ~/Library/Caches
24- // Linux: $XDG_CONFIG_HOME or ~/.config, $XDG_CACHE_HOME or ~/.cache
25- var testConfigPath , testCachePath string
26- switch runtime .GOOS {
27- case "darwin" :
28- testConfigPath = filepath .Join (testHomeDir , "Library" , "Application Support" , "trakx" , "trakx.yaml" )
29- testCachePath = filepath .Join (testHomeDir , "Library" , "Caches" , "trakx" )
30- default :
31- xdgConfigHome := filepath .Join (testHomeDir , ".config" )
32- xdgCacheHome := filepath .Join (testHomeDir , ".cache" )
33- testConfigPath = filepath .Join (xdgConfigHome , "trakx" , "trakx.yaml" )
34- testCachePath = filepath .Join (xdgCacheHome , "trakx" )
16+ content , err := os .ReadFile (path )
17+ if err != nil {
18+ t .Fatalf ("failed to read config file: %v" , err )
3519 }
3620
37- _ , err := Load ( LoadOptions {} )
21+ embedded , err := embeddedFS . ReadFile ( "embedded/trakx.yaml" )
3822 if err != nil {
39- t .Fatal ("failed to load config" )
23+ t .Fatalf ("failed to read embedded config: %v" , err )
24+ }
25+
26+ if string (content ) != string (embedded ) {
27+ t .Error ("installed config doesn't match embedded config" )
28+ }
29+ }
30+
31+ func TestInstallDefaultConfigSkipsExisting (t * testing.T ) {
32+ path := filepath .Join (t .TempDir (), "trakx.yaml" )
33+ existing := []byte ("existing: config" )
34+
35+ if err := os .WriteFile (path , existing , 0644 ); err != nil {
36+ t .Fatalf ("failed to write existing config: %v" , err )
37+ }
38+
39+ if err := installDefaultConfig (path ); err != nil {
40+ t .Fatalf ("installDefaultConfig failed: %v" , err )
4041 }
4142
42- if _ , err := os .Stat (testConfigPath ); os .IsNotExist (err ) {
43- t .Error ("load failed to write configuration to default path" )
43+ content , err := os .ReadFile (path )
44+ if err != nil {
45+ t .Fatalf ("failed to read config file: %v" , err )
4446 }
45- if _ , err := os .Stat (testCachePath ); os .IsNotExist (err ) {
46- t .Error ("load failed to create cache directory" )
47+
48+ if string (content ) != string (existing ) {
49+ t .Error ("installDefaultConfig overwrote existing config" )
4750 }
4851}
0 commit comments