|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + gogitconfig "github.com/go-git/go-git/v6/config" |
| 9 | +) |
| 10 | + |
| 11 | +func TestPickIndexVersion(t *testing.T) { |
| 12 | + t.Parallel() |
| 13 | + |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + envValue string |
| 17 | + envSet bool |
| 18 | + configVersion string |
| 19 | + manyFiles string |
| 20 | + hadExistingIndex bool |
| 21 | + wantVersion uint32 |
| 22 | + wantWarnPrefix string |
| 23 | + }{ |
| 24 | + {name: "default", wantVersion: 2}, |
| 25 | + {name: "env=3 silently demotes to 2", envSet: true, envValue: "3", wantVersion: 2}, |
| 26 | + {name: "env=4", envSet: true, envValue: "4", wantVersion: 4}, |
| 27 | + { |
| 28 | + name: "env=2bogus", envSet: true, envValue: "2bogus", wantVersion: 2, |
| 29 | + wantWarnPrefix: "warning: GIT_INDEX_VERSION set, but the value is invalid.\nUsing version 2\n", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "env=1 out of bounds", envSet: true, envValue: "1", wantVersion: 2, |
| 33 | + wantWarnPrefix: "warning: GIT_INDEX_VERSION set, but the value is invalid.\nUsing version 2\n", |
| 34 | + }, |
| 35 | + {name: "env bogus but existing index", envSet: true, envValue: "1", hadExistingIndex: true, wantVersion: 2}, |
| 36 | + {name: "config=3 silently demotes to 2", configVersion: "3", wantVersion: 2}, |
| 37 | + { |
| 38 | + name: "config=5 invalid", configVersion: "5", wantVersion: 2, |
| 39 | + wantWarnPrefix: "warning: index.version set, but the value is invalid.\nUsing version 2\n", |
| 40 | + }, |
| 41 | + {name: "config invalid but existing index", configVersion: "5", hadExistingIndex: true, wantVersion: 2}, |
| 42 | + {name: "manyFiles default 4", manyFiles: "true", wantVersion: 4}, |
| 43 | + {name: "manyFiles overridden by config=2", configVersion: "2", manyFiles: "true", wantVersion: 2}, |
| 44 | + {name: "env wins over config", envSet: true, envValue: "4", configVersion: "2", wantVersion: 4}, |
| 45 | + {name: "env wins over manyFiles", envSet: true, envValue: "2", manyFiles: "true", wantVersion: 2}, |
| 46 | + } |
| 47 | + |
| 48 | + for _, tc := range tests { |
| 49 | + t.Run(tc.name, func(t *testing.T) { |
| 50 | + t.Parallel() |
| 51 | + |
| 52 | + cfg := gogitconfig.NewConfig() |
| 53 | + if tc.configVersion != "" { |
| 54 | + cfg.Raw.Section("index").SetOption("version", tc.configVersion) |
| 55 | + } |
| 56 | + |
| 57 | + if tc.manyFiles != "" { |
| 58 | + cfg.Raw.Section("feature").SetOption("manyFiles", tc.manyFiles) |
| 59 | + } |
| 60 | + |
| 61 | + env := func(string) (string, bool) { return "", false } |
| 62 | + if tc.envSet { |
| 63 | + env = func(name string) (string, bool) { |
| 64 | + if name == "GIT_INDEX_VERSION" { |
| 65 | + return tc.envValue, true |
| 66 | + } |
| 67 | + |
| 68 | + return "", false |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + var stderr bytes.Buffer |
| 73 | + |
| 74 | + got := pickIndexVersion(cfg, env, tc.hadExistingIndex, &stderr) |
| 75 | + if got != tc.wantVersion { |
| 76 | + t.Fatalf("version = %d; want %d", got, tc.wantVersion) |
| 77 | + } |
| 78 | + |
| 79 | + if tc.wantWarnPrefix == "" { |
| 80 | + if stderr.Len() != 0 { |
| 81 | + t.Fatalf("unexpected stderr: %q", stderr.String()) |
| 82 | + } |
| 83 | + |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + if !strings.HasPrefix(stderr.String(), tc.wantWarnPrefix) { |
| 88 | + t.Fatalf("stderr = %q; want prefix %q", stderr.String(), tc.wantWarnPrefix) |
| 89 | + } |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
0 commit comments