|
4 | 4 | package tzlocal |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
7 | 10 | "testing" |
8 | 11 | "time" |
9 | 12 | ) |
@@ -68,6 +71,103 @@ func Test_inferFromPath(t *testing.T) { |
68 | 71 | } |
69 | 72 | } |
70 | 73 |
|
| 74 | +func TestLocalTZFromPath(t *testing.T) { |
| 75 | + t.Run("absolute target", func(t *testing.T) { |
| 76 | + root := t.TempDir() |
| 77 | + target := createZoneinfoFile(t, root, "Europe/Tallinn") |
| 78 | + localtime := filepath.Join(root, "etc", "localtime") |
| 79 | + mustMkdirAll(t, filepath.Dir(localtime)) |
| 80 | + mustSymlink(t, target, localtime) |
| 81 | + |
| 82 | + assertLocalTZ(t, localtime, "Europe/Tallinn") |
| 83 | + }) |
| 84 | + |
| 85 | + t.Run("relative target through symlinked parent", func(t *testing.T) { |
| 86 | + root := t.TempDir() |
| 87 | + realRoot := filepath.Join(root, "real") |
| 88 | + createZoneinfoFile(t, realRoot, "Europe/Berlin") |
| 89 | + realEtc := filepath.Join(realRoot, "etc") |
| 90 | + mustMkdirAll(t, realEtc) |
| 91 | + mustSymlink(t, |
| 92 | + filepath.Join("..", "usr", "share", "zoneinfo", "Europe", "Berlin"), |
| 93 | + filepath.Join(realEtc, "localtime"), |
| 94 | + ) |
| 95 | + mustSymlink(t, realEtc, filepath.Join(root, "etc")) |
| 96 | + |
| 97 | + assertLocalTZ(t, filepath.Join(root, "etc", "localtime"), "Europe/Berlin") |
| 98 | + }) |
| 99 | + |
| 100 | + t.Run("regular file through symlinked parent", func(t *testing.T) { |
| 101 | + root := t.TempDir() |
| 102 | + target := createZoneinfoFile(t, root, "localtime") |
| 103 | + mustSymlink(t, filepath.Dir(target), filepath.Join(root, "etc")) |
| 104 | + |
| 105 | + _, err := localTZ(filepath.Join(root, "etc", "localtime")) |
| 106 | + if err == nil || !strings.Contains(err.Error(), "is not a symlink") { |
| 107 | + t.Fatalf("localTZ() error = %v, want a not-a-symlink error", err) |
| 108 | + } |
| 109 | + }) |
| 110 | + |
| 111 | + t.Run("missing localtime", func(t *testing.T) { |
| 112 | + localtime := filepath.Join(t.TempDir(), "etc", "localtime") |
| 113 | + |
| 114 | + _, err := localTZ(localtime) |
| 115 | + if err == nil || !strings.Contains(err.Error(), "failed to stat") { |
| 116 | + t.Fatalf("localTZ() error = %v, want a failed-to-stat error", err) |
| 117 | + } |
| 118 | + }) |
| 119 | + |
| 120 | + t.Run("dangling symlink", func(t *testing.T) { |
| 121 | + root := t.TempDir() |
| 122 | + localtime := filepath.Join(root, "etc", "localtime") |
| 123 | + mustMkdirAll(t, filepath.Dir(localtime)) |
| 124 | + mustSymlink(t, filepath.Join(root, "usr", "share", "zoneinfo", "Missing"), localtime) |
| 125 | + |
| 126 | + _, err := localTZ(localtime) |
| 127 | + if err == nil { |
| 128 | + t.Fatal("localTZ() error = nil, want an error for a dangling symlink") |
| 129 | + } |
| 130 | + if strings.Contains(err.Error(), "failed to stat") { |
| 131 | + t.Fatalf("localTZ() error = %v, final symlink exists and should not fail its stat check", err) |
| 132 | + } |
| 133 | + }) |
| 134 | +} |
| 135 | + |
| 136 | +func createZoneinfoFile(t *testing.T, root, name string) string { |
| 137 | + t.Helper() |
| 138 | + p := filepath.Join(root, "usr", "share", "zoneinfo", filepath.FromSlash(name)) |
| 139 | + mustMkdirAll(t, filepath.Dir(p)) |
| 140 | + if err := os.WriteFile(p, nil, 0o644); err != nil { |
| 141 | + t.Fatalf("os.WriteFile(%q) failed: %v", p, err) |
| 142 | + } |
| 143 | + return p |
| 144 | +} |
| 145 | + |
| 146 | +func mustMkdirAll(t *testing.T, path string) { |
| 147 | + t.Helper() |
| 148 | + if err := os.MkdirAll(path, 0o755); err != nil { |
| 149 | + t.Fatalf("os.MkdirAll(%q) failed: %v", path, err) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func mustSymlink(t *testing.T, oldname, newname string) { |
| 154 | + t.Helper() |
| 155 | + if err := os.Symlink(oldname, newname); err != nil { |
| 156 | + t.Fatalf("os.Symlink(%q, %q) failed: %v", oldname, newname, err) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +func assertLocalTZ(t *testing.T, localtime, want string) { |
| 161 | + t.Helper() |
| 162 | + got, err := localTZ(localtime) |
| 163 | + if err != nil { |
| 164 | + t.Fatalf("localTZ() failed: %v", err) |
| 165 | + } |
| 166 | + if got != want { |
| 167 | + t.Fatalf("localTZ() = %q, want %q", got, want) |
| 168 | + } |
| 169 | +} |
| 170 | + |
71 | 171 | func TestRuntimeTZ(t *testing.T) { |
72 | 172 | got, err := RuntimeTZ() |
73 | 173 | if err != nil { |
|
0 commit comments