config_test.go (1253B)
1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "testing" 8 ) 9 10 // func TestNewConfig(test *testing.T) {} // don't waste time 11 12 func TestNewConfigFromFile(test *testing.T) { 13 test.Parallel() 14 15 tdir := filepath.Join(os.TempDir(), "pagr_test", "TestNewConfigFromFile") 16 if err := os.MkdirAll(tdir, 0775); err != nil { 17 test.Errorf("failed to create temporary test dir: %s", tdir) 18 } 19 cfgp := fmt.Sprintf("%s/%s.toml", tdir, Name) 20 21 if f, err := os.Create(cfgp); err != nil { 22 test.Skipf("failed to create config file: '%s'", cfgp) 23 } else { 24 f.WriteString(` 25 Contents = "./p" 26 Templates = "./t" 27 Assets = ["./a"] 28 Output = "./o"`) 29 f.Close() 30 } 31 32 if cfg, err := NewConfigFromFile(cfgp); err == nil { 33 if cfg.Contents != filepath.Join(tdir, "p") { 34 test.Fatalf("invalid Contents path: '%s'", cfg.Contents) 35 } 36 if cfg.Templates != filepath.Join(tdir, "t") { 37 test.Fatalf("invalid Templates path: '%s'", cfg.Templates) 38 } 39 if cfg.Assets[0] != filepath.Join(tdir, "a") { 40 test.Fatalf("invalid Assets path: '%s'", cfg.Assets) 41 } 42 if cfg.Output != filepath.Join(tdir, "o") { 43 test.Fatalf("invalid Output path: '%s'", cfg.Output) 44 } 45 } else { 46 test.Fatal(err) 47 } 48 49 if err := os.RemoveAll(tdir); err != nil { 50 test.Error(err) 51 } 52 }