| 425 | } |
| 426 | |
| 427 | func TestConfigPath(t *testing.T) { |
| 428 | oldDir := Dir() |
| 429 | |
| 430 | for _, tc := range []struct { |
| 431 | name string |
| 432 | dir string |
| 433 | path []string |
| 434 | expected string |
| 435 | expectedErr string |
| 436 | }{ |
| 437 | { |
| 438 | name: "valid_path", |
| 439 | dir: "dummy", |
| 440 | path: []string{"a", "b"}, |
| 441 | expected: filepath.Join("dummy", "a", "b"), |
| 442 | }, |
| 443 | { |
| 444 | name: "valid_path_absolute_dir", |
| 445 | dir: "/dummy", |
| 446 | path: []string{"a", "b"}, |
| 447 | expected: filepath.Join("/dummy", "a", "b"), |
| 448 | }, |
| 449 | { |
| 450 | name: "invalid_relative_path", |
| 451 | dir: "dummy", |
| 452 | path: []string{"e", "..", "..", "f"}, |
| 453 | expectedErr: fmt.Sprintf("is outside of root config directory %q", "dummy"), |
| 454 | }, |
| 455 | { |
| 456 | name: "invalid_absolute_path", |
| 457 | dir: "dummy", |
| 458 | path: []string{"/a", "..", ".."}, |
| 459 | expectedErr: fmt.Sprintf("is outside of root config directory %q", "dummy"), |
| 460 | }, |
| 461 | } { |
| 462 | t.Run(tc.name, func(t *testing.T) { |
| 463 | SetDir(tc.dir) |
| 464 | f, err := Path(tc.path...) |
| 465 | assert.Equal(t, f, tc.expected) |
| 466 | if tc.expectedErr == "" { |
| 467 | assert.NilError(t, err) |
| 468 | } else { |
| 469 | assert.ErrorContains(t, err, tc.expectedErr) |
| 470 | } |
| 471 | }) |
| 472 | } |
| 473 | |
| 474 | SetDir(oldDir) |
| 475 | } |
| 476 | |
| 477 | // TestSetDir verifies that Dir() does not overwrite the value set through |
| 478 | // SetDir() if it has not been run before. |