|
1 | 1 | package open
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
4 | 7 | "testing"
|
| 8 | + |
| 9 | + "github.com/google/go-cmp/cmp" |
| 10 | + "github.com/ldez/go-git-cmd-wrapper/v2/config" |
| 11 | + "github.com/ldez/go-git-cmd-wrapper/v2/git" |
5 | 12 | )
|
6 | 13 |
|
7 | 14 | const repo = "arbourd/git-open"
|
@@ -129,3 +136,71 @@ func TestEscapePath(t *testing.T) {
|
129 | 136 | })
|
130 | 137 | }
|
131 | 138 | }
|
| 139 | + |
| 140 | +func TestLoadProviders(t *testing.T) { |
| 141 | + gitconfig := filepath.Join(t.TempDir(), ".gitconfig") |
| 142 | + _, err := os.Create(gitconfig) |
| 143 | + if err != nil { |
| 144 | + t.Fatalf("unable create .gitconfig: %s", err) |
| 145 | + } |
| 146 | + |
| 147 | + err = os.Setenv("GIT_CONFIG_GLOBAL", gitconfig) |
| 148 | + if err != nil { |
| 149 | + t.Fatalf("unable to set GIT_CONFIG_GLOBAL: %s", err) |
| 150 | + } |
| 151 | + |
| 152 | + cases := map[string]struct { |
| 153 | + config []string |
| 154 | + expectedProviders []Provider |
| 155 | + }{ |
| 156 | + "empty git config": { |
| 157 | + expectedProviders: []Provider{}, |
| 158 | + }, |
| 159 | + "single provider": { |
| 160 | + config: []string{ |
| 161 | + "open.https://my.domain.dev.commitprefix -/commit", |
| 162 | + "open.https://my.domain.dev.pathprefix -/tree", |
| 163 | + }, |
| 164 | + expectedProviders: []Provider{ |
| 165 | + {BaseURL: "https://my.domain.dev", CommitPrefix: "-/commit", PathPrefix: "-/tree"}, |
| 166 | + }, |
| 167 | + }, |
| 168 | + "multple providers": { |
| 169 | + config: []string{ |
| 170 | + "open.https://git.example1.dev.commitprefix -/commit", |
| 171 | + "open.https://git.example1.dev.pathprefix -/tree", |
| 172 | + "open.https://git.example2.dev.commitprefix commit", |
| 173 | + "open.https://git.example2.dev.pathprefix tree", |
| 174 | + }, |
| 175 | + expectedProviders: []Provider{ |
| 176 | + {BaseURL: "https://git.example1.dev", CommitPrefix: "-/commit", PathPrefix: "-/tree"}, |
| 177 | + {BaseURL: "https://git.example2.dev", CommitPrefix: "commit", PathPrefix: "tree"}, |
| 178 | + }, |
| 179 | + }, |
| 180 | + } |
| 181 | + |
| 182 | + for name, c := range cases { |
| 183 | + t.Run(name, func(t *testing.T) { |
| 184 | + // removes all `open.https://` Git config entries |
| 185 | + out, _ := git.Config(config.Global, config.GetRegexp(getRegex, "")) |
| 186 | + for _, v := range strings.Split(strings.TrimSpace(out), "\n") { |
| 187 | + key := strings.Split(strings.TrimSpace(v), " ")[0] |
| 188 | + |
| 189 | + git.Config(config.Global, config.Unset(key, "")) |
| 190 | + } |
| 191 | + |
| 192 | + for _, v := range c.config { |
| 193 | + s := strings.Split(v, " ") |
| 194 | + git.Config(config.Global, config.Entry(s[0], s[1])) |
| 195 | + } |
| 196 | + |
| 197 | + p := LoadProviders() |
| 198 | + if len(p) != len(c.expectedProviders) { |
| 199 | + t.Logf("unexpected number of providers\n\t(GOT): %#v\n\t(WNT): %#v", len(p), len(c.expectedProviders)) |
| 200 | + } |
| 201 | + if !cmp.Equal(p, c.expectedProviders) { |
| 202 | + t.Fatalf("unexpected providers:\n\t(GOT): %#v\n\t(WNT): %#v", p, c.expectedProviders) |
| 203 | + } |
| 204 | + }) |
| 205 | + } |
| 206 | +} |
0 commit comments