viye/internal/plumbing/config/config_test.go (view raw)
| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/olexsmir/viye/internal/viye" |
| 9 | |
| 10 | "olexsmir.xyz/x/is" |
| 11 | ) |
| 12 | |
| 13 | func TestMatch(t *testing.T) { |
| 14 | got := (Tool{}).Match(&viye.Context{Cmd: "config"}) |
| 15 | is.Equal(t, true, got) |
| 16 | } |
| 17 | |
| 18 | func TestExecute(t *testing.T) { |
| 19 | t.Run("no body empty config", func(t *testing.T) { |
| 20 | dir := t.TempDir() |
| 21 | t.Setenv("XDG_CONFIG_HOME", dir) |
| 22 | |
| 23 | got, err := (Tool{}).Execute(&viye.Context{Path: []string{"config"}}) |
| 24 | is.Err(t, err, nil) |
| 25 | is.Equal(t, "\n", got) |
| 26 | }) |
| 27 | |
| 28 | t.Run("no body with existing key", func(t *testing.T) { |
| 29 | dir := t.TempDir() |
| 30 | t.Setenv("XDG_CONFIG_HOME", dir) |
| 31 | |
| 32 | cfgPath := filepath.Join(dir, "viye.conf") |
| 33 | os.WriteFile(cfgPath, []byte("k = v\n"), 0o644) |
| 34 | |
| 35 | got, err := (Tool{}).Execute(&viye.Context{Path: []string{"config"}}) |
| 36 | is.Err(t, err, nil) |
| 37 | is.Equal(t, ": k = v\n\n", got) |
| 38 | }) |
| 39 | |
| 40 | t.Run("set key", func(t *testing.T) { |
| 41 | dir := t.TempDir() |
| 42 | t.Setenv("XDG_CONFIG_HOME", dir) |
| 43 | |
| 44 | got, err := (Tool{}).Execute(&viye.Context{ |
| 45 | Path: []string{"config"}, |
| 46 | Body: []string{"name = olex"}, |
| 47 | }) |
| 48 | is.Err(t, err, nil) |
| 49 | is.Equal(t, ": name = olex\n\n", got) |
| 50 | }) |
| 51 | |
| 52 | t.Run("update and delete", func(t *testing.T) { |
| 53 | dir := t.TempDir() |
| 54 | t.Setenv("XDG_CONFIG_HOME", dir) |
| 55 | |
| 56 | cfgPath := filepath.Join(dir, "viye.conf") |
| 57 | os.WriteFile(cfgPath, []byte("a = 1\nb = 2\n"), 0o644) |
| 58 | |
| 59 | got, err := (Tool{}).Execute(&viye.Context{ |
| 60 | Path: []string{"config"}, |
| 61 | Body: []string{"a = updated"}, |
| 62 | }) |
| 63 | is.Err(t, err, nil) |
| 64 | is.Equal(t, ": a = updated\n\n", got) |
| 65 | }) |
| 66 | |
| 67 | t.Run("key with spaces", func(t *testing.T) { |
| 68 | dir := t.TempDir() |
| 69 | t.Setenv("XDG_CONFIG_HOME", dir) |
| 70 | |
| 71 | _, err := (Tool{}).Execute(&viye.Context{ |
| 72 | Path: []string{"config"}, |
| 73 | Body: []string{"my key = val"}, |
| 74 | }) |
| 75 | is.Err(t, err, `contains spaces`) |
| 76 | }) |
| 77 | } |