github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/util/config/config_test.go (about) 1 package config 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/tickoalcantara12/micro/v3/util/user" 9 "github.com/nightlyone/lockfile" 10 ) 11 12 func Test(t *testing.T) { 13 tt := []struct { 14 name string 15 values map[string]string 16 }{ 17 { 18 name: "No values", 19 }, 20 { 21 name: "Single value", 22 values: map[string]string{ 23 "foo": "bar", 24 }, 25 }, 26 { 27 name: "Multiple values", 28 values: map[string]string{ 29 "foo": "bar", 30 "apple": "tree", 31 }, 32 }, 33 } 34 35 saveLock := lock 36 saveFile := File 37 38 File = filepath.Join(user.Dir, "config-test.json") 39 lock, _ = lockfile.New(File) 40 41 defer func() { 42 File = saveFile 43 lock = saveLock 44 }() 45 46 fp := File 47 48 for _, tc := range tt { 49 t.Run(tc.name, func(t *testing.T) { 50 defer os.Remove(fp) 51 52 if _, err := os.Stat(fp); err != os.ErrNotExist { 53 os.Remove(fp) 54 } 55 56 for k, v := range tc.values { 57 if err := Set(k, v); err != nil { 58 t.Error(err) 59 } 60 } 61 62 for k, v := range tc.values { 63 val, err := Get(k) 64 if err != nil { 65 t.Error(err) 66 continue 67 } 68 69 if v != val { 70 t.Errorf("Got '%v' but expected '%v'", val, v) 71 } 72 } 73 }) 74 } 75 }