Load embedded tzdata and force TZ on some time related tests.

time/tzdata embeds a local copy of tzdata if the system has none.

use testing.T.Setenv to force a specific timezone on some tests.
This commit is contained in:
Olivier Meunier
2025-06-12 22:13:47 +02:00
parent a0734193ae
commit e3f79284e7
3 changed files with 13 additions and 6 deletions

View File

@@ -35,6 +35,8 @@ type adapterTest struct {
}
func TestFileAdapters(t *testing.T) {
t.Setenv("TZ", "Europe/Paris")
tests := []adapterTest{
{
importer.LoadAdapter("text"),
@@ -412,6 +414,8 @@ func TestFileAdapters(t *testing.T) {
}
func TestWallabagImporter(t *testing.T) {
t.Setenv("TZ", "Europe/Paris")
adapter := importer.LoadAdapter("wallabag")
f := importer.NewImportForm(context.Background(), adapter)
_ = f.Get("url").UnmarshalValues([]string{"https://wallabag/"})
@@ -520,6 +524,8 @@ func TestWallabagImporter(t *testing.T) {
}
func TestOmnivoreImporter(t *testing.T) {
t.Setenv("TZ", "Europe/Paris")
httpmock.Activate()
defer httpmock.DeactivateAndReset()

View File

@@ -8,6 +8,7 @@ package main
import (
"fmt"
"os"
_ "time/tzdata" // load embedded tzdata
"codeberg.org/readeck/readeck/internal/app"
)

View File

@@ -15,6 +15,8 @@ import (
)
func TestStrftime(t *testing.T) {
t.Setenv("TZ", "Europe/Amsterdam")
tests := []struct {
date string
format string
@@ -62,9 +64,7 @@ func TestStrftime(t *testing.T) {
for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
location, err := time.LoadLocation("CET")
require.NoError(t, err)
date, err := time.ParseInLocation(time.RFC3339, test.date, location)
date, err := time.Parse(time.RFC3339, test.date)
require.NoError(t, err)
t.Log(date, test.format, strftime.Strftime(test.format, date))
@@ -74,6 +74,8 @@ func TestStrftime(t *testing.T) {
}
func TestTranslator(t *testing.T) {
t.Setenv("TZ", "Europe/Amsterdam")
tests := []struct {
date string
format string
@@ -96,9 +98,7 @@ func TestTranslator(t *testing.T) {
f := strftime.New(translation)
for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
location, err := time.LoadLocation("CET")
require.NoError(t, err)
date, err := time.ParseInLocation(time.RFC3339, test.date, location)
date, err := time.Parse(time.RFC3339, test.date)
require.NoError(t, err)
t.Log(date, test.format, f.Strftime(test.format, date))