mugit/internal/config/validate_test.go (view raw)
| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "olexsmir.xyz/x/is" |
| 7 | ) |
| 8 | |
| 9 | func TestCheckPort(t *testing.T) { |
| 10 | is.Err(t, checkPort(1), nil) |
| 11 | is.Err(t, checkPort(80), nil) |
| 12 | is.Err(t, checkPort(65535), nil) |
| 13 | |
| 14 | is.Err(t, checkPort(0), "must be between") |
| 15 | is.Err(t, checkPort(-1), "must be between") |
| 16 | is.Err(t, checkPort(65536), "must be between") |
| 17 | } |
| 18 | |
| 19 | func TestConfig_Validate(t *testing.T) { |
| 20 | tests := []struct { |
| 21 | name string |
| 22 | expected any |
| 23 | c Config |
| 24 | }{ |
| 25 | { |
| 26 | name: "minimal", |
| 27 | c: Config{ |
| 28 | Meta: MetaConfig{Host: "example.com"}, |
| 29 | Repo: RepoConfig{Dir: t.TempDir()}, |
| 30 | }, |
| 31 | }, |
| 32 | { |
| 33 | name: "minimal with ssh", |
| 34 | c: Config{ |
| 35 | Meta: MetaConfig{Host: "example.com"}, |
| 36 | Repo: RepoConfig{Dir: t.TempDir()}, |
| 37 | SSH: SSHConfig{Enable: true}, |
| 38 | }, |
| 39 | }, |
| 40 | { |
| 41 | name: "not set meta.host", |
| 42 | expected: "meta.host is required", |
| 43 | c: Config{ |
| 44 | Repo: RepoConfig{Dir: t.TempDir()}, |
| 45 | }, |
| 46 | }, |
| 47 | { |
| 48 | name: "invalid meta.host", |
| 49 | expected: "meta.host shouldn't include protocol", |
| 50 | c: Config{ |
| 51 | Meta: MetaConfig{Host: "https://example.com"}, |
| 52 | Repo: RepoConfig{Dir: t.TempDir()}, |
| 53 | }, |
| 54 | }, |
| 55 | { |
| 56 | name: "invalid repo.dir", |
| 57 | expected: "repo.dir", |
| 58 | c: Config{ |
| 59 | Meta: MetaConfig{Host: "example.com"}, |
| 60 | Repo: RepoConfig{Dir: "nonexistent"}, |
| 61 | }, |
| 62 | }, |
| 63 | { |
| 64 | name: "invalid server port", |
| 65 | expected: "server.port", |
| 66 | c: Config{ |
| 67 | Meta: MetaConfig{Host: "example.com"}, |
| 68 | Repo: RepoConfig{Dir: t.TempDir()}, |
| 69 | Server: ServerConfig{Port: -1}, |
| 70 | }, |
| 71 | }, |
| 72 | } |
| 73 | |
| 74 | for _, tt := range tests { |
| 75 | t.Run(tt.name, func(t *testing.T) { |
| 76 | tt.c.ensureDefaults() |
| 77 | err := tt.c.validate() |
| 78 | is.Err(t, err, tt.expected) |
| 79 | }) |
| 80 | } |
| 81 | } |