| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | package limayaml |
| 7 | |
| 8 | import ( |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "testing" |
| 12 | |
| 13 | "gotest.tools/v3/assert" |
| 14 | ) |
| 15 | |
| 16 | func TestExtractTimezoneFromPath(t *testing.T) { |
| 17 | tmpDir := t.TempDir() |
| 18 | |
| 19 | |
| 20 | assert.NilError(t, os.MkdirAll(filepath.Join(tmpDir, "Etc"), 0o755)) |
| 21 | assert.NilError(t, os.WriteFile(filepath.Join(tmpDir, "Etc", "UTC"), []byte{}, 0o644)) |
| 22 | assert.NilError(t, os.WriteFile(filepath.Join(tmpDir, "UTC"), []byte{}, 0o644)) |
| 23 | assert.NilError(t, os.MkdirAll(filepath.Join(tmpDir, "Antarctica"), 0o755)) |
| 24 | assert.NilError(t, os.WriteFile(filepath.Join(tmpDir, "Antarctica", "Troll"), []byte{}, 0o644)) |
| 25 | |
| 26 | tests := []struct { |
| 27 | name string |
| 28 | path string |
| 29 | want string |
| 30 | wantErr bool |
| 31 | }{ |
| 32 | { |
| 33 | "valid_timezone", |
| 34 | filepath.Join(tmpDir, "Antarctica", "Troll"), |
| 35 | "Antarctica/Troll", |
| 36 | false, |
| 37 | }, |
| 38 | { |
| 39 | "root_level_zone", |
| 40 | filepath.Join(tmpDir, "UTC"), |
| 41 | "UTC", |
| 42 | false, |
| 43 | }, |
| 44 | { |
| 45 | "outside_zoneinfo", |
| 46 | "/tmp/somefile", |
| 47 | "", |
| 48 | true, |
| 49 | }, |
| 50 | { |
| 51 | "empty_path", |
| 52 | "", |
| 53 | "", |
| 54 | true, |
| 55 | }, |
| 56 | { |
| 57 | "nonexistent_file", |
| 58 | filepath.Join(tmpDir, "Invalid", "Zone"), |
| 59 | "", |
| 60 | true, |
| 61 | }, |
| 62 | } |
| 63 | |
| 64 | for _, tt := range tests { |
| 65 | t.Run(tt.name, func(t *testing.T) { |
| 66 | got, err := extractTZFromPath(tt.path) |
| 67 | if tt.wantErr { |
| 68 | assert.Assert(t, err != nil, "expected error but got none") |
| 69 | } else { |
| 70 | assert.NilError(t, err) |
| 71 | } |
| 72 | assert.Equal(t, tt.want, got) |
| 73 | }) |
| 74 | } |
| 75 | } |