From 7117a5e35308c0f1c694a736ca9482f79caab8ee Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Thu, 12 Dec 2024 23:27:52 +0100 Subject: [PATCH 1/2] tests: Playing with Carbon Default is dangerous. These variable are global. It has consequences. People launching tests another timezone where facing random issues. It was random because the tests are launched in different order on each run. This was hidden by the CI forcing the timezone to PRC, and impossible to see by someone working in PRC timezone. --- database_unit_test.go | 2 ++ default_bench_test.go | 2 ++ default_unit_test.go | 2 ++ test.go | 20 ++++++++++++++++++++ 4 files changed, 26 insertions(+) diff --git a/database_unit_test.go b/database_unit_test.go index 8a9e85e8..6a4c93df 100755 --- a/database_unit_test.go +++ b/database_unit_test.go @@ -913,6 +913,8 @@ func TestCarbon_Issue240(t *testing.T) { // https://github.com/dromara/carbon/issues/243 func TestCarbon_Issue243(t *testing.T) { + prepareTest(t) + SetDefault(Default{ Layout: DateTimeLayout, Timezone: PRC, diff --git a/default_bench_test.go b/default_bench_test.go index d3f4b089..5a0de996 100755 --- a/default_bench_test.go +++ b/default_bench_test.go @@ -3,6 +3,8 @@ package carbon import "testing" func BenchmarkCarbon_SetDefault(b *testing.B) { + prepareTest(b) + d := Default{ Layout: DateTimeLayout, Timezone: Local, diff --git a/default_unit_test.go b/default_unit_test.go index 97b00c20..84963e48 100644 --- a/default_unit_test.go +++ b/default_unit_test.go @@ -7,6 +7,8 @@ import ( ) func TestCarbon_SetDefault(t *testing.T) { + prepareTest(t) + SetDefault(Default{ Layout: DateTimeLayout, Timezone: PRC, diff --git a/test.go b/test.go index c4481d02..eca6c584 100644 --- a/test.go +++ b/test.go @@ -1,5 +1,9 @@ package carbon +import ( + "testing" +) + // SetTestNow sets a test Carbon instance (real or mock) to be returned when a "now" instance is created. // 设置当前测试时间 func (c *Carbon) SetTestNow(carbon Carbon) { @@ -17,3 +21,19 @@ func (c *Carbon) UnSetTestNow() { func (c Carbon) IsSetTestNow() bool { return c.testNow > 0 } + +func prepareTest(tb testing.TB) { + tb.Helper() + + // Store the current default + savedDefault := Default{ + Layout: defaultLayout, + Timezone: defaultTimezone, + Locale: defaultLocale, + WeekStartsAt: defaultWeekStartsAt, + } + tb.Cleanup(func() { + // restore the default when test is done + SetDefault(savedDefault) + }) +} From 83a65e5e4f7f6428c60bf07997270ffdf56de8fa Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Thu, 12 Dec 2024 23:24:32 +0100 Subject: [PATCH 2/2] ci: tests were written by someone in PRC timezone Nothing was working if you were not in this timezone. The CI was forced in this timezone, but it was uneasy to catch. Now all tests are launched in the PRC timezone. We are using TestMain to sort this out. https://pkg.go.dev/testing#hdr-Main --- .github/workflows/test.yml | 2 -- test.go | 13 +++++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ea1563af..990f7aac 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,8 +2,6 @@ name: test on: push -env: - TZ: "PRC" jobs: test: diff --git a/test.go b/test.go index eca6c584..c1947519 100644 --- a/test.go +++ b/test.go @@ -1,6 +1,7 @@ package carbon import ( + "os" "testing" ) @@ -22,6 +23,18 @@ func (c Carbon) IsSetTestNow() bool { return c.testNow > 0 } +// TestMain sets up the testing environment for all tests +// https://pkg.go.dev/testing#hdr-Main +func TestMain(m *testing.M) { + // The whole tests were written for PRC timezone (China). + // The codebase of test is too large to be changed. + // Without this hack the tests will fail if you use a different timezone than PRC + // This will affect the way Go compute the timezone when using time.Local + _ = os.Setenv("TZ", "PRC") + + m.Run() +} + func prepareTest(tb testing.TB) { tb.Helper()