123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package main
- import (
- "os"
- "os/user"
- "testing"
- )
- func TestConfig(t *testing.T) {
- var filename string = "/invalid"
- testValues := map[string]string{
- "turns": "5",
- "planet": "Earth",
- "size": "Medium",
- "PI": "Raspberry",
- }
- u, err := user.Current()
- if err != nil {
- t.Fatalf("user.Current: %v", err)
- }
- if u.Name == "root" {
- t.Skip("Don't run tests as root.")
- }
- err = SaveConfig(filename, testValues)
- if err == nil {
- t.Errorf("SaveConfig: Expected error writing to %s\n", filename)
- }
- fail, err := LoadConfig(filename)
- if fail == nil {
- t.Errorf("LoadConfig: Expected error reading %s\n", filename)
- }
- filename = ".test.config"
- os.Remove(filename)
- if FileExists(filename) {
- t.Errorf("FileExists says %s exists.\n", filename)
- }
- err = SaveConfig(filename, testValues)
- if err != nil {
- t.Errorf("SaveConfig: %s\n", err)
- }
- file, err := os.OpenFile(filename, os.O_APPEND|os.O_RDWR, 0666)
- if err != nil {
- t.Errorf("OpenFile %s : %s\n", filename, err)
- }
- file.WriteString("\n")
- file.WriteString("# Comment goes here\n")
- file.Close()
- if !FileExists(filename) {
- t.Errorf("FileExists say %s missing.\n", filename)
- }
- got, err := LoadConfig(filename)
- if err != nil {
- t.Errorf("LoadConfig: Got error reading %s : %s\n", filename, err)
- }
- for k, v := range got {
- testValue, ok := testValues[k]
- if !ok {
- t.Errorf("LoadConfig key %s wasn't in original\n", k)
- } else {
- if testValue != v {
- t.Errorf("LoadConfig key %s expected %s, got %s\n", k, v, testValue)
- }
- }
- }
- os.Remove(filename)
- var b bool
- b = SetConfigDefault(&got, "turns", "10")
- if !b {
- t.Errorf("Setting existing key default failed.\n")
- }
- b = SetConfigDefault(&got, "missing", "yes")
- if b {
- t.Errorf("Setting missing default returned true.\n")
- }
- }
|