| 1 |
package gitconfig |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"math/rand" |
| 7 |
"os" |
| 8 |
"path" |
| 9 |
"path/filepath" |
| 10 |
"runtime" |
| 11 |
"strconv" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/gopasspw/gopass/pkg/set" |
| 16 |
"github.com/stretchr/testify/assert" |
| 17 |
"github.com/stretchr/testify/require" |
| 18 |
) |
| 19 |
|
| 20 |
func TestInsertOnce(t *testing.T) { |
| 21 |
t.Parallel() |
| 22 |
|
| 23 |
c := &Config{ |
| 24 |
noWrites: true, |
| 25 |
} |
| 26 |
|
| 27 |
require.NoError(t, c.insertValue("foo.bar", "baz")) |
| 28 |
assert.Equal(t, `[foo] |
| 29 |
bar = baz |
| 30 |
`, c.raw.String()) |
| 31 |
} |
| 32 |
|
| 33 |
func TestConditionalIncludeOnBranch(t *testing.T) { |
| 34 |
t.Parallel() |
| 35 |
|
| 36 |
if runtime.GOOS == "windows" { |
| 37 |
t.Skip("Skipping test on windows") |
| 38 |
} |
| 39 |
|
| 40 |
td := t.TempDir() |
| 41 |
|
| 42 |
// base config |
| 43 |
fn := filepath.Join(td, "config") |
| 44 |
require.NoError(t, os.WriteFile(fn, []byte(`[core] |
| 45 |
int = 7 |
| 46 |
[includeIf "onbranch:main"] |
| 47 |
path = main.config |
| 48 |
[includeIf "onbranch:feat/*"] |
| 49 |
path = feat.config |
| 50 |
[includeIf "onbranch:feat/**/test"] |
| 51 |
path = feat-test.config`), 0o600)) |
| 52 |
|
| 53 |
// main.config, should be included on main branch |
| 54 |
fnMain := filepath.Join(td, "main.config") |
| 55 |
require.NoError(t, os.WriteFile(fnMain, []byte(`[core] |
| 56 |
int = 8`), 0o600)) |
| 57 |
|
| 58 |
// feat.config, should be included on feature branches |
| 59 |
fnFeat := filepath.Join(td, "feat.config") |
| 60 |
require.NoError(t, os.WriteFile(fnFeat, []byte(`[core] |
| 61 |
int = 9`), 0o600)) |
| 62 |
|
| 63 |
// feat-test.config, should be included on feature test branches |
| 64 |
fnFeatTest := filepath.Join(td, "feat-test.config") |
| 65 |
require.NoError(t, os.WriteFile(fnFeatTest, []byte(`[core] |
| 66 |
int = 10`), 0o600)) |
| 67 |
|
| 68 |
// test with no branch set |
| 69 |
cfg, err := LoadConfig(fn) |
| 70 |
require.NoError(t, err) |
| 71 |
vs, ok := cfg.GetAll("core.int") |
| 72 |
assert.True(t, ok) |
| 73 |
assert.Equal(t, []string{"7"}, vs) |
| 74 |
|
| 75 |
// test with main branch |
| 76 |
require.NoError(t, os.MkdirAll(filepath.Join(td, ".git"), 0o755)) |
| 77 |
require.NoError(t, os.WriteFile(filepath.Join(td, ".git", "HEAD"), []byte("ref: refs/heads/main"), 0o644)) |
| 78 |
cfg, err = LoadConfigWithWorkdir(fn, td) |
| 79 |
require.NoError(t, err) |
| 80 |
vs, ok = cfg.GetAll("core.int") |
| 81 |
assert.True(t, ok) |
| 82 |
assert.Equal(t, []string{"7", "8"}, vs) |
| 83 |
|
| 84 |
// test with feature branch |
| 85 |
require.NoError(t, os.WriteFile(filepath.Join(td, ".git", "HEAD"), []byte("ref: refs/heads/feat/test"), 0o644)) |
| 86 |
cfg, err = LoadConfigWithWorkdir(fn, td) |
| 87 |
require.NoError(t, err) |
| 88 |
vs, ok = cfg.GetAll("core.int") |
| 89 |
assert.True(t, ok) |
| 90 |
assert.ElementsMatch(t, []string{"7", "9", "10"}, vs) |
| 91 |
|
| 92 |
// test with feature test branch |
| 93 |
require.NoError(t, os.WriteFile(filepath.Join(td, ".git", "HEAD"), []byte("ref: refs/heads/feat/foo/bar/test"), 0o644)) |
| 94 |
cfg, err = LoadConfigWithWorkdir(fn, td) |
| 95 |
require.NoError(t, err) |
| 96 |
vs, ok = cfg.GetAll("core.int") |
| 97 |
assert.True(t, ok) |
| 98 |
assert.Equal(t, []string{"7", "10"}, vs) |
| 99 |
} |
| 100 |
|
| 101 |
func TestConditionalIncludeGitDirI(t *testing.T) { |
| 102 |
t.Parallel() |
| 103 |
|
| 104 |
if runtime.GOOS == "windows" { |
| 105 |
t.Skip("Skipping test on windows") |
| 106 |
} |
| 107 |
|
| 108 |
td := t.TempDir() |
| 109 |
tdSub := filepath.Join(td, "SUB") |
| 110 |
require.NoError(t, os.Mkdir(tdSub, 0o755)) |
| 111 |
|
| 112 |
// base config |
| 113 |
fn := filepath.Join(td, "config") |
| 114 |
require.NoError(t, os.WriteFile(fn, []byte(fmt.Sprintf(`[core] |
| 115 |
int = 7 |
| 116 |
[includeIf "gitdir/i:%s/"] |
| 117 |
path = sub.config`, strings.ToUpper(tdSub))), 0o600)) |
| 118 |
|
| 119 |
// sub.config, should be included |
| 120 |
fnSub := filepath.Join(td, "sub.config") |
| 121 |
require.NoError(t, os.WriteFile(fnSub, []byte(`[core] |
| 122 |
int = 9`), 0o600)) |
| 123 |
|
| 124 |
cfg, err := LoadConfigWithWorkdir(fn, tdSub) |
| 125 |
require.NoError(t, err) |
| 126 |
vs, ok := cfg.GetAll("core.int") |
| 127 |
assert.True(t, ok) |
| 128 |
assert.Equal(t, []string{"7", "9"}, vs) |
| 129 |
} |
| 130 |
|
| 131 |
func TestInsertMultipleSameKey(t *testing.T) { |
| 132 |
t.Parallel() |
| 133 |
|
| 134 |
c := &Config{ |
| 135 |
noWrites: true, |
| 136 |
} |
| 137 |
|
| 138 |
require.NoError(t, c.Set("foo.bar", "baz")) |
| 139 |
assert.Equal(t, `[foo] |
| 140 |
bar = baz |
| 141 |
`, c.raw.String()) |
| 142 |
require.NoError(t, c.Set("foo.bar", "zab")) |
| 143 |
assert.Equal(t, `[foo] |
| 144 |
bar = zab |
| 145 |
`, c.raw.String()) |
| 146 |
} |
| 147 |
|
| 148 |
func TestGetAll(t *testing.T) { |
| 149 |
t.Parallel() |
| 150 |
|
| 151 |
r := bytes.NewReader([]byte(`[core] |
| 152 |
foo = bar |
| 153 |
foo = zab |
| 154 |
foo = 123 |
| 155 |
`)) |
| 156 |
|
| 157 |
c := ParseConfig(r) |
| 158 |
require.NotNil(t, c) |
| 159 |
vs, found := c.GetAll("core.foo") |
| 160 |
assert.True(t, found) |
| 161 |
assert.Equal(t, []string{"bar", "zab", "123"}, vs) |
| 162 |
|
| 163 |
require.NoError(t, c.Set("core.foo", "456")) |
| 164 |
vs, found = c.GetAll("core.foo") |
| 165 |
assert.True(t, found) |
| 166 |
assert.Equal(t, []string{"456", "zab", "123"}, vs) |
| 167 |
|
| 168 |
assert.Equal(t, `[core] |
| 169 |
foo = 456 |
| 170 |
foo = zab |
| 171 |
foo = 123 |
| 172 |
`, c.raw.String()) |
| 173 |
} |
| 174 |
|
| 175 |
func TestSubsection(t *testing.T) { |
| 176 |
t.Parallel() |
| 177 |
|
| 178 |
in := `[core] |
| 179 |
showsafecontent = true |
| 180 |
readonly = true |
| 181 |
[aliases "subsection with spaces"] |
| 182 |
foo = bar |
| 183 |
` |
| 184 |
c := ParseConfig(strings.NewReader(in)) |
| 185 |
c.noWrites = true |
| 186 |
|
| 187 |
assert.Equal(t, []string{"bar"}, c.vars["aliases.subsection with spaces.foo"]) |
| 188 |
} |
| 189 |
|
| 190 |
func TestParseSection(t *testing.T) { |
| 191 |
t.Parallel() |
| 192 |
|
| 193 |
for in, out := range map[string]struct { |
| 194 |
section string |
| 195 |
subs string |
| 196 |
skip bool |
| 197 |
}{ |
| 198 |
`[aliases]`: { |
| 199 |
section: "aliases", |
| 200 |
}, |
| 201 |
`[aliases "subsection"]`: { |
| 202 |
section: "aliases", |
| 203 |
subs: "subsection", |
| 204 |
}, |
| 205 |
`[aliases "subsection with spaces"]`: { |
| 206 |
section: "aliases", |
| 207 |
subs: "subsection with spaces", |
| 208 |
}, |
| 209 |
`[aliases "subsection with spaces and \" \t \0 escapes"]`: { |
| 210 |
section: "aliases", |
| 211 |
subs: `subsection with spaces and " t 0 escapes`, |
| 212 |
}, |
| 213 |
} { |
| 214 |
section, subsection, skip := parseSectionHeader(in) |
| 215 |
assert.Equal(t, out.section, section, in) |
| 216 |
assert.Equal(t, out.subs, subsection, in) |
| 217 |
assert.Equal(t, out.skip, skip, in) |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
func TestInsertMultiple(t *testing.T) { |
| 222 |
t.Parallel() |
| 223 |
|
| 224 |
c := &Config{ |
| 225 |
noWrites: true, |
| 226 |
} |
| 227 |
|
| 228 |
updates := map[string]string{ |
| 229 |
"foo.bar": "baz", |
| 230 |
"core.show": "true", |
| 231 |
"core.noshow": "true", |
| 232 |
} |
| 233 |
|
| 234 |
for _, k := range set.SortedKeys(updates) { |
| 235 |
v := updates[k] |
| 236 |
require.NoError(t, c.insertValue(k, v)) |
| 237 |
} |
| 238 |
|
| 239 |
assert.Equal(t, `[core] |
| 240 |
show = true |
| 241 |
noshow = true |
| 242 |
[foo] |
| 243 |
bar = baz |
| 244 |
`, c.raw.String()) |
| 245 |
} |
| 246 |
|
| 247 |
func TestRewriteRaw(t *testing.T) { |
| 248 |
t.Parallel() |
| 249 |
|
| 250 |
in := `[core] |
| 251 |
autoimport = true |
| 252 |
readonly = true |
| 253 |
[mounts] |
| 254 |
path = /tmp/foo |
| 255 |
` |
| 256 |
c := ParseConfig(strings.NewReader(in)) |
| 257 |
c.noWrites = true |
| 258 |
|
| 259 |
updates := map[string]string{ |
| 260 |
"foo.bar": "baz", |
| 261 |
"mounts.readonly": "true", |
| 262 |
"show.safecontent": "false", |
| 263 |
"core.autoimport": "false", |
| 264 |
} |
| 265 |
for _, k := range set.SortedKeys(updates) { |
| 266 |
v := updates[k] |
| 267 |
require.NoError(t, c.Set(k, v)) |
| 268 |
} |
| 269 |
|
| 270 |
assert.Equal(t, `[core] |
| 271 |
autoimport = false |
| 272 |
readonly = true |
| 273 |
[mounts] |
| 274 |
readonly = true |
| 275 |
path = /tmp/foo |
| 276 |
[foo] |
| 277 |
bar = baz |
| 278 |
[show] |
| 279 |
safecontent = false |
| 280 |
`, c.raw.String()) |
| 281 |
} |
| 282 |
|
| 283 |
func TestUnsetSection(t *testing.T) { |
| 284 |
t.Parallel() |
| 285 |
|
| 286 |
in := `[core] |
| 287 |
showsafecontent = true |
| 288 |
readonly = true |
| 289 |
[mounts] |
| 290 |
path = /tmp/foo |
| 291 |
[foo] |
| 292 |
bar = baz |
| 293 |
` |
| 294 |
c := ParseConfig(strings.NewReader(in)) |
| 295 |
c.noWrites = true |
| 296 |
|
| 297 |
require.NoError(t, c.Unset("core.readonly")) |
| 298 |
assert.Equal(t, `[core] |
| 299 |
showsafecontent = true |
| 300 |
[mounts] |
| 301 |
path = /tmp/foo |
| 302 |
[foo] |
| 303 |
bar = baz |
| 304 |
`, c.raw.String()) |
| 305 |
|
| 306 |
// should not exist |
| 307 |
require.NoError(t, c.Unset("foo.bla")) |
| 308 |
|
| 309 |
// TODO: support remvoing sections |
| 310 |
t.Skip("removing sections is not supported, yet") |
| 311 |
|
| 312 |
require.NoError(t, c.Unset("foo.bar")) |
| 313 |
assert.Equal(t, `[core] |
| 314 |
showsafecontent = false |
| 315 |
readonly = true |
| 316 |
[mounts] |
| 317 |
readonly = true |
| 318 |
path = /tmp/foo |
| 319 |
`, c.raw.String()) |
| 320 |
} |
| 321 |
|
| 322 |
func TestNewFromMap(t *testing.T) { |
| 323 |
t.Parallel() |
| 324 |
|
| 325 |
tc := map[string]string{ |
| 326 |
"core.foo": "bar", |
| 327 |
"core.pager": "false", |
| 328 |
"core.timeout": "10", |
| 329 |
} |
| 330 |
|
| 331 |
cfg := NewFromMap(tc) |
| 332 |
for k, v := range tc { |
| 333 |
assert.Equal(t, []string{v}, cfg.vars[k]) |
| 334 |
} |
| 335 |
|
| 336 |
assert.True(t, cfg.IsSet("core.foo")) |
| 337 |
assert.False(t, cfg.IsSet("core.bar")) |
| 338 |
require.NoError(t, cfg.Unset("core.foo")) |
| 339 |
assert.True(t, cfg.IsSet("core.foo")) |
| 340 |
} |
| 341 |
|
| 342 |
func TestLoadConfig(t *testing.T) { |
| 343 |
t.Parallel() |
| 344 |
|
| 345 |
td := t.TempDir() |
| 346 |
fn := filepath.Join(td, "config") |
| 347 |
require.NoError(t, os.WriteFile(fn, []byte(`[core] |
| 348 |
int = 7 |
| 349 |
string = foo |
| 350 |
bar = false`), 0o600)) |
| 351 |
|
| 352 |
cfg, err := LoadConfig(fn) |
| 353 |
require.NoError(t, err) |
| 354 |
|
| 355 |
v, ok := cfg.Get("core.int") |
| 356 |
assert.True(t, ok) |
| 357 |
assert.Equal(t, "7", v) |
| 358 |
|
| 359 |
v, ok = cfg.Get("core.string") |
| 360 |
assert.True(t, ok) |
| 361 |
assert.Equal(t, "foo", v) |
| 362 |
|
| 363 |
v, ok = cfg.Get("core.bar") |
| 364 |
assert.True(t, ok) |
| 365 |
assert.Equal(t, "false", v) |
| 366 |
} |
| 367 |
|
| 368 |
func TestLoadConfigWithInclude(t *testing.T) { |
| 369 |
t.Parallel() |
| 370 |
|
| 371 |
if runtime.GOOS == "windows" { |
| 372 |
// this test is currently failing on windows. |
| 373 |
// skip it for now, but we should try to fix it. |
| 374 |
t.Skip("Skipping test on windows") |
| 375 |
} |
| 376 |
|
| 377 |
td := t.TempDir() |
| 378 |
fn := filepath.Join(td, "config") |
| 379 |
|
| 380 |
tdBar := t.TempDir() |
| 381 |
fnBar := path.Join(tdBar, "bar.config") |
| 382 |
|
| 383 |
require.NoError(t, os.WriteFile(fn, []byte(`[core] |
| 384 |
int = 7 |
| 385 |
string = foo |
| 386 |
bar = false |
| 387 |
[include] |
| 388 |
path = foo.config |
| 389 |
path = foo.config`), 0o600)) |
| 390 |
fnFoo := filepath.Join(td, "foo.config") |
| 391 |
|
| 392 |
require.NoError(t, os.WriteFile(fnFoo, []byte(fmt.Sprintf(`[core] |
| 393 |
int = 8 |
| 394 |
[include] |
| 395 |
path = config |
| 396 |
path = %s`, fnBar)), 0o600)) |
| 397 |
require.NoError(t, os.WriteFile(fnBar, []byte(`[core] |
| 398 |
int = 9`), 0o600)) |
| 399 |
|
| 400 |
cfg, err := LoadConfig(fn) |
| 401 |
require.NoError(t, err) |
| 402 |
|
| 403 |
v, ok := cfg.Get("core.int") |
| 404 |
assert.True(t, ok) |
| 405 |
assert.Equal(t, "7", v) |
| 406 |
|
| 407 |
vs, ok := cfg.GetAll("core.int") |
| 408 |
assert.True(t, ok) |
| 409 |
assert.Equal(t, []string{"7", "8", "9"}, vs) |
| 410 |
|
| 411 |
v, ok = cfg.Get("core.string") |
| 412 |
assert.True(t, ok) |
| 413 |
assert.Equal(t, "foo", v) |
| 414 |
|
| 415 |
v, ok = cfg.Get("core.bar") |
| 416 |
assert.True(t, ok) |
| 417 |
assert.Equal(t, "false", v) |
| 418 |
} |
| 419 |
|
| 420 |
func TestLoadFromEnv(t *testing.T) { |
| 421 |
tc := map[string]string{ |
| 422 |
"core.foo": "bar", |
| 423 |
"core.pager": "false", |
| 424 |
"core.timeout": "10", |
| 425 |
} |
| 426 |
|
| 427 |
prefix := fmt.Sprintf("GPTEST%d", rand.Int31n(8192)) |
| 428 |
|
| 429 |
i := 0 |
| 430 |
for k, v := range tc { |
| 431 |
t.Setenv(fmt.Sprintf("%s_KEY_%d", prefix, i), k) |
| 432 |
t.Setenv(fmt.Sprintf("%s_VALUE_%d", prefix, i), v) |
| 433 |
i++ |
| 434 |
} |
| 435 |
t.Setenv(fmt.Sprintf("%s_COUNT", prefix), strconv.Itoa(i)) |
| 436 |
|
| 437 |
cfg := LoadConfigFromEnv(prefix) |
| 438 |
for k, v := range tc { |
| 439 |
got, ok := cfg.Get(k) |
| 440 |
assert.True(t, ok) |
| 441 |
assert.Equal(t, v, got) |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
func TestGetPathsForNestedConfig(t *testing.T) { |
| 446 |
t.Setenv("HOME", "/home/user") |
| 447 |
tc := map[string][3]string{ |
| 448 |
"relative": {"/home/user/config", "foo.config", "/home/user/foo.config"}, |
| 449 |
"~": {"/home/user/config", "~/foo.config", "/home/user/foo.config"}, |
| 450 |
"absolute": {"/home/user/config", "/home/user/foo.config", "/home/user/foo.config"}, |
| 451 |
} |
| 452 |
|
| 453 |
for _, v := range tc { |
| 454 |
got := getPathsForNestedConfig([]string{v[1]}, v[0]) |
| 455 |
assert.Equal(t, []string{v[2]}, got) |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
func TestMergeConfigs(t *testing.T) { |
| 460 |
t.Parallel() |
| 461 |
|
| 462 |
baseConfig := Config{path: "/home/user/config", noWrites: true, readonly: true, raw: strings.Builder{}, vars: map[string][]string{"core.bar": {"1"}}} |
| 463 |
baseConfig.raw.WriteString("base") |
| 464 |
extensionConfig := Config{path: "/home/user/config.foo", noWrites: false, readonly: false, raw: strings.Builder{}, vars: map[string][]string{"core.bar": {"2"}}} |
| 465 |
extensionConfig.raw.WriteString("foo") |
| 466 |
|
| 467 |
mergedConfig := mergeConfigs(&baseConfig, &extensionConfig) |
| 468 |
assert.NotSame(t, &baseConfig, mergedConfig) |
| 469 |
assert.NotSame(t, &baseConfig.raw, &mergedConfig.raw) |
| 470 |
assert.NotSame(t, &extensionConfig, mergedConfig) |
| 471 |
assert.NotSame(t, &extensionConfig.raw, &mergedConfig.raw) |
| 472 |
assert.Equal(t, baseConfig.noWrites, mergedConfig.noWrites) |
| 473 |
assert.Equal(t, baseConfig.readonly, mergedConfig.readonly) |
| 474 |
assert.Equal(t, baseConfig.path, mergedConfig.path) |
| 475 |
assert.Equal(t, map[string][]string{"core.bar": {"1", "2"}}, mergedConfig.vars) |
| 476 |
} |
| 477 |
|
| 478 |
func TestMultiInclude(t *testing.T) { |
| 479 |
t.Parallel() |
| 480 |
|
| 481 |
if runtime.GOOS == "windows" { |
| 482 |
// this test is currently failing on windows. |
| 483 |
// skip it for now, but we should try to fix it. |
| 484 |
t.Skip("Skipping test on windows") |
| 485 |
} |
| 486 |
|
| 487 |
td := t.TempDir() |
| 488 |
fn := filepath.Join(td, "config") |
| 489 |
require.NoError(t, os.WriteFile(fn, []byte(`[core] |
| 490 |
int = 7 |
| 491 |
string = foo |
| 492 |
bar = false |
| 493 |
[include] |
| 494 |
path = foo.config`), 0o600)) |
| 495 |
fnFoo := filepath.Join(td, "foo.config") |
| 496 |
require.NoError(t, os.WriteFile(fnFoo, []byte(`[core] |
| 497 |
int = 8 |
| 498 |
[include] |
| 499 |
path = bar.config`), 0o600)) |
| 500 |
fnBar := filepath.Join(td, "bar.config") |
| 501 |
require.NoError(t, os.WriteFile(fnBar, []byte(`[core] |
| 502 |
int = 9 |
| 503 |
[include] |
| 504 |
path = baz.config`), 0o600)) |
| 505 |
fnBaz := filepath.Join(td, "baz.config") |
| 506 |
require.NoError(t, os.WriteFile(fnBaz, []byte(`[core] |
| 507 |
int = 10`), 0o600)) |
| 508 |
|
| 509 |
cfg, err := LoadConfig(fn) |
| 510 |
require.NoError(t, err) |
| 511 |
v, ok := cfg.Get("core.int") |
| 512 |
assert.True(t, ok) |
| 513 |
assert.Equal(t, "7", v) |
| 514 |
vs, ok := cfg.GetAll("core.int") |
| 515 |
assert.True(t, ok) |
| 516 |
assert.Equal(t, []string{"7", "8", "9", "10"}, vs) |
| 517 |
v, ok = cfg.Get("core.string") |
| 518 |
assert.True(t, ok) |
| 519 |
assert.Equal(t, "foo", v) |
| 520 |
v, ok = cfg.Get("core.bar") |
| 521 |
assert.True(t, ok) |
| 522 |
assert.Equal(t, "false", v) |
| 523 |
} |
| 524 |
|
| 525 |
func TestIncludeWrite(t *testing.T) { |
| 526 |
t.Parallel() |
| 527 |
|
| 528 |
if runtime.GOOS == "windows" { |
| 529 |
// this test is currently failing on windows. |
| 530 |
// skip it for now, but we should try to fix it. |
| 531 |
t.Skip("Skipping test on non-linux OS") |
| 532 |
} |
| 533 |
|
| 534 |
td := t.TempDir() |
| 535 |
fn := filepath.Join(td, "config") |
| 536 |
require.NoError(t, os.WriteFile(fn, []byte(`[core] |
| 537 |
int = 7 |
| 538 |
string = foo |
| 539 |
bar = false |
| 540 |
[include] |
| 541 |
path = foo.config`), 0o600)) |
| 542 |
fnFoo := filepath.Join(td, "foo.config") |
| 543 |
require.NoError(t, os.WriteFile(fnFoo, []byte(`[core] |
| 544 |
int = 8`), 0o600)) |
| 545 |
|
| 546 |
cfg, err := LoadConfig(fn) |
| 547 |
require.NoError(t, err) |
| 548 |
|
| 549 |
require.NoError(t, cfg.Set("core.int", "9")) |
| 550 |
require.NoError(t, cfg.Set("core.string", "bar")) |
| 551 |
require.NoError(t, cfg.Set("core.bar", "true")) |
| 552 |
|
| 553 |
cfg, err = LoadConfig(fn) |
| 554 |
require.NoError(t, err) |
| 555 |
v, ok := cfg.Get("core.int") |
| 556 |
assert.True(t, ok) |
| 557 |
assert.Equal(t, "9", v) |
| 558 |
vs, ok := cfg.GetAll("core.int") |
| 559 |
assert.True(t, ok) |
| 560 |
assert.Equal(t, []string{"9", "8"}, vs) |
| 561 |
v, ok = cfg.Get("core.string") |
| 562 |
assert.True(t, ok) |
| 563 |
assert.Equal(t, "bar", v) |
| 564 |
v, ok = cfg.Get("core.bar") |
| 565 |
assert.True(t, ok) |
| 566 |
assert.Equal(t, "true", v) |
| 567 |
|
| 568 |
// Check if the config was written correctly |
| 569 |
expected := `[core] |
| 570 |
int = 9 |
| 571 |
string = bar |
| 572 |
bar = true |
| 573 |
[include] |
| 574 |
path = foo.config |
| 575 |
` |
| 576 |
|
| 577 |
actual, err := os.ReadFile(fn) |
| 578 |
require.NoError(t, err) |
| 579 |
assert.Equal(t, expected, string(actual)) |
| 580 |
} |
| 581 |
|
| 582 |
func TestConditionalInclude(t *testing.T) { |
| 583 |
t.Parallel() |
| 584 |
|
| 585 |
if runtime.GOOS == "windows" { |
| 586 |
// this test is currently failing on windows. |
| 587 |
// skip it for now, but we should try to fix it. |
| 588 |
t.Skip("Skipping test on windows") |
| 589 |
} |
| 590 |
|
| 591 |
td := t.TempDir() |
| 592 |
|
| 593 |
// base config |
| 594 |
fn := filepath.Join(td, "config") |
| 595 |
require.NoError(t, os.WriteFile(fn, fmt.Appendf(nil, `[core] |
| 596 |
int = 7 |
| 597 |
string = foo |
| 598 |
bar = false |
| 599 |
[includeIf "gitdir:/foo/bar/repo"] |
| 600 |
path = foo.config |
| 601 |
[includeIf "gitdir:%s/"] |
| 602 |
path = bar.config`, td), 0o600)) |
| 603 |
|
| 604 |
// foo.config, should NOT be included |
| 605 |
fnFoo := filepath.Join(td, "foo.config") |
| 606 |
require.NoError(t, os.WriteFile(fnFoo, []byte(`[core] |
| 607 |
int = 8`), 0o600)) |
| 608 |
|
| 609 |
// bar.config, should be included |
| 610 |
fnBar := filepath.Join(td, "bar.config") |
| 611 |
require.NoError(t, os.WriteFile(fnBar, fmt.Appendf(nil, `[core] |
| 612 |
int = 9 |
| 613 |
[includeIf "gitdir:/foo/bar/repo"] |
| 614 |
path = baz.config |
| 615 |
[includeIf "gitdir:%s/"] |
| 616 |
path = zab.config`, td), 0o600)) |
| 617 |
|
| 618 |
// baz.config, nested, should NOT be included |
| 619 |
fnBaz := filepath.Join(td, "baz.config") |
| 620 |
require.NoError(t, os.WriteFile(fnBaz, []byte(`[core] |
| 621 |
int = 10`), 0o600)) |
| 622 |
|
| 623 |
// zab.config, nested, should be included |
| 624 |
fnZab := filepath.Join(td, "zab.config") |
| 625 |
require.NoError(t, os.WriteFile(fnZab, []byte(`[core] |
| 626 |
int = 11 |
| 627 |
deep = rock`), 0o600)) |
| 628 |
|
| 629 |
cfg, err := LoadConfigWithWorkdir(fn, td) |
| 630 |
require.NoError(t, err) |
| 631 |
v, ok := cfg.Get("core.int") |
| 632 |
assert.True(t, ok) |
| 633 |
assert.Equal(t, "7", v) |
| 634 |
vs, ok := cfg.GetAll("core.int") |
| 635 |
assert.True(t, ok) |
| 636 |
assert.Equal(t, []string{"7", "9", "11"}, vs) |
| 637 |
v, ok = cfg.Get("core.string") |
| 638 |
assert.True(t, ok) |
| 639 |
assert.Equal(t, "foo", v) |
| 640 |
v, ok = cfg.Get("core.deep") |
| 641 |
assert.True(t, ok) |
| 642 |
assert.Equal(t, "rock", v) |
| 643 |
} |
| 644 |
|
| 645 |
func TestUnescapeValue(t *testing.T) { |
| 646 |
t.Parallel() |
| 647 |
|
| 648 |
tests := map[string]struct { |
| 649 |
input string |
| 650 |
expected string |
| 651 |
}{ |
| 652 |
"no escape sequences": { |
| 653 |
input: "plain text", |
| 654 |
expected: "plain text", |
| 655 |
}, |
| 656 |
"newline escape": { |
| 657 |
input: "line1\\nline2", |
| 658 |
expected: "line1\nline2", |
| 659 |
}, |
| 660 |
"tab escape": { |
| 661 |
input: "key\\tvalue", |
| 662 |
expected: "key\tvalue", |
| 663 |
}, |
| 664 |
"backspace escape": { |
| 665 |
input: "abc\\bdef", |
| 666 |
expected: "abc\bdef", |
| 667 |
}, |
| 668 |
"double quote escape": { |
| 669 |
input: "value with \\\"quotes\\\"", |
| 670 |
expected: `value with "quotes"`, |
| 671 |
}, |
| 672 |
"backslash escape": { |
| 673 |
input: "path\\\\of\\\\file", |
| 674 |
expected: "path\\of\\file", |
| 675 |
}, |
| 676 |
"multiple escapes": { |
| 677 |
input: "line1\\nline2\\tkey\\bvalue\\\"quoted\\\"\\\\path", |
| 678 |
expected: "line1\nline2\tkey\bvalue\"quoted\"\\path", |
| 679 |
}, |
| 680 |
"invalid escape sequences": { |
| 681 |
input: "invalid\\xescape", |
| 682 |
expected: "invalid\\xescape", |
| 683 |
}, |
| 684 |
} |
| 685 |
|
| 686 |
for name, tc := range tests { |
| 687 |
t.Run(name, func(t *testing.T) { |
| 688 |
result := unescapeValue(tc.input) |
| 689 |
assert.Equal(t, tc.expected, result) |
| 690 |
}) |
| 691 |
} |
| 692 |
} |