From 52fbabd26dff101889726bc8e6ef04d580cc4e11 Mon Sep 17 00:00:00 2001 From: Timofey Koolin Date: Tue, 9 May 2023 21:32:50 +0300 Subject: [PATCH 1/3] add fixenv.New --- env.go | 10 ++++++-- env_test.go | 24 +++++++++---------- examples/custom_env/custom_env_test.go | 2 +- .../custom_env_with_shared_content_test.go | 2 +- examples/simple/http_server_test.go | 2 +- examples/simple/package_scope_test.go | 4 ++-- examples/simple/simple_old_style_test.go | 8 +++---- examples/simple/simple_test.go | 8 +++---- examples/simple/use_cache_params_test.go | 2 +- maintest.go | 2 +- 10 files changed, 35 insertions(+), 29 deletions(-) diff --git a/env.go b/env.go index 8dae951..f3cef1c 100644 --- a/env.go +++ b/env.go @@ -45,13 +45,19 @@ type EnvT struct { scopes map[string]*scopeInfo } -// NewEnv create EnvT from test -func NewEnv(t T) *EnvT { +// New create EnvT from test +func New(t T) *EnvT { env := newEnv(t, globalCache, &globalMutex, globalScopeInfo) env.onCreate() return env } +// NewEnv create EnvT from test +// Deprecated: use New instead +func NewEnv(t T) *EnvT { + return New(t) +} + func newEnv(t T, c *cache, m sync.Locker, scopes map[string]*scopeInfo) *EnvT { return &EnvT{ t: t, diff --git a/env_test.go b/env_test.go index 0746ba4..8dd704a 100644 --- a/env_test.go +++ b/env_test.go @@ -101,7 +101,7 @@ func Test_Env__NewEnv(t *testing.T) { tMock := &testMock{TestName: "mock"} defer tMock.callCleanup() - e := NewEnv(tMock) + e := New(tMock) at.Equal(tMock, e.t) at.Equal(globalCache, e.c) at.Equal(&globalMutex, e.m) @@ -123,21 +123,21 @@ func Test_Env__NewEnv(t *testing.T) { tMock := &testMock{TestName: "mock"} defer tMock.callCleanup() - _ = NewEnv(tMock) + _ = New(tMock) at.Len(tMock.fatals, 0) runUntilFatal(func() { - _ = NewEnv(tMock) + _ = New(tMock) }) at.Len(tMock.fatals, 1) }) t.Run("double_env_similar_scope_different_time", func(t *testing.T) { t.Run("test", func(t *testing.T) { - _ = NewEnv(t) + _ = New(t) }) t.Run("test", func(t *testing.T) { - _ = NewEnv(t) + _ = New(t) }) }) } @@ -151,7 +151,7 @@ func testFailedFixture(env Env) { func Test_Env_Cache(t *testing.T) { t.Run("simple", func(t *testing.T) { at := assert.New(t) - e := NewEnv(t) + e := New(t) val := 0 cntF := func() int { @@ -171,7 +171,7 @@ func Test_Env_Cache(t *testing.T) { t.Run("subtest_and_test_scope", func(t *testing.T) { at := assert.New(t) - e := NewEnv(t) + e := New(t) val := 0 cntF := func(env Env) int { @@ -187,7 +187,7 @@ func Test_Env_Cache(t *testing.T) { t.Run("subtest", func(t *testing.T) { at := assert.New(t) - subEnv := NewEnv(t) + subEnv := New(t) at.Equal(2, cntF(subEnv)) at.Equal(2, cntF(subEnv)) }) @@ -198,7 +198,7 @@ func Test_Env_Cache(t *testing.T) { t.Run("subtest_and_package_scope", func(t *testing.T) { at := assert.New(t) - e := NewEnv(t) + e := New(t) _, mainClose := CreateMainTestEnv(nil) defer mainClose() @@ -216,7 +216,7 @@ func Test_Env_Cache(t *testing.T) { t.Run("subtest", func(t *testing.T) { at := assert.New(t) - subEnv := NewEnv(t) + subEnv := New(t) at.Equal(1, cntF(subEnv)) at.Equal(1, cntF(subEnv)) }) @@ -308,7 +308,7 @@ func Test_Env_Cache(t *testing.T) { at := assert.New(t) tMock := &testMock{TestName: "mock", SkipGoexit: true} - e := NewEnv(tMock) + e := New(tMock) at.Panics(func() { e.Cache(nil, nil, func() (res interface{}, err error) { return nil, ErrSkipTest @@ -499,7 +499,7 @@ func Test_Env_Skip(t *testing.T) { func Test_Env_T(t *testing.T) { at := assert.New(t) - e := NewEnv(t) + e := New(t) at.Equal(t, e.T()) } diff --git a/examples/custom_env/custom_env_test.go b/examples/custom_env/custom_env_test.go index ca83b4a..bd81a27 100644 --- a/examples/custom_env/custom_env_test.go +++ b/examples/custom_env/custom_env_test.go @@ -23,7 +23,7 @@ type Env struct { func NewEnv(t *testing.T) (context.Context, *Env) { at := assert.New(t) - fEnv := fixenv.NewEnv(t) + fEnv := fixenv.New(t) ctx, ctxCancel := context.WithCancel(context.Background()) t.Cleanup(func() { ctxCancel() diff --git a/examples/custom_env_with_shared_content/custom_env_with_shared_content_test.go b/examples/custom_env_with_shared_content/custom_env_with_shared_content_test.go index af9a2b0..afe92de 100644 --- a/examples/custom_env_with_shared_content/custom_env_with_shared_content_test.go +++ b/examples/custom_env_with_shared_content/custom_env_with_shared_content_test.go @@ -21,7 +21,7 @@ type Env struct { func NewEnv(t *testing.T) *Env { return &Env{ - Env: fixenv.NewEnv(t), + Env: fixenv.New(t), Resp: "OK", } } diff --git a/examples/simple/http_server_test.go b/examples/simple/http_server_test.go index f5e8614..4c78333 100644 --- a/examples/simple/http_server_test.go +++ b/examples/simple/http_server_test.go @@ -32,7 +32,7 @@ func testServer(e fixenv.Env, response string) *httptest.Server { func TestHttpServer(t *testing.T) { at := assert.New(t) - e := fixenv.NewEnv(t) + e := fixenv.New(t) s1 := testServer(e, "OK") resp, err := http.Get(s1.URL) diff --git a/examples/simple/package_scope_test.go b/examples/simple/package_scope_test.go index c0c9389..e5b8440 100644 --- a/examples/simple/package_scope_test.go +++ b/examples/simple/package_scope_test.go @@ -36,14 +36,14 @@ func packageCounter(e fixenv.Env) int { } func TestPackageFirst(t *testing.T) { - e := fixenv.NewEnv(t) + e := fixenv.New(t) if packageCounter(e) != 1 { t.Error() } } func TestPackageSecond(t *testing.T) { - e := fixenv.NewEnv(t) + e := fixenv.New(t) if packageCounter(e) != 1 { t.Error() } diff --git a/examples/simple/simple_old_style_test.go b/examples/simple/simple_old_style_test.go index 3ab1f17..80d4ae3 100644 --- a/examples/simple/simple_old_style_test.go +++ b/examples/simple/simple_old_style_test.go @@ -21,7 +21,7 @@ func counterOldStyle(e fixenv.Env) int { } func TestCounterOldStyle(t *testing.T) { - e := fixenv.NewEnv(t) + e := fixenv.New(t) r1 := counterOldStyle(e) r2 := counterOldStyle(e) @@ -30,7 +30,7 @@ func TestCounterOldStyle(t *testing.T) { } t.Run("subtest", func(t *testing.T) { - e := fixenv.NewEnv(t) + e := fixenv.New(t) r3 := counterOldStyle(e) if r3 == r1 { t.Error() @@ -50,7 +50,7 @@ func counterTestAndSubtestOldStyle(e fixenv.Env) int { } func TestTestAndSubtestCounterOldStyle(t *testing.T) { - e := fixenv.NewEnv(t) + e := fixenv.New(t) r1 := counterTestAndSubtestOldStyle(e) r2 := counterTestAndSubtestOldStyle(e) @@ -59,7 +59,7 @@ func TestTestAndSubtestCounterOldStyle(t *testing.T) { } t.Run("subtest", func(t *testing.T) { - e := fixenv.NewEnv(t) + e := fixenv.New(t) r3 := counterTestAndSubtestOldStyle(e) if r3 != r1 { t.Error() diff --git a/examples/simple/simple_test.go b/examples/simple/simple_test.go index 40296a3..04d825a 100644 --- a/examples/simple/simple_test.go +++ b/examples/simple/simple_test.go @@ -19,7 +19,7 @@ func counter(e fixenv.Env) int { } func TestCounter(t *testing.T) { - e := fixenv.NewEnv(t) + e := fixenv.New(t) r1 := counter(e) r2 := counter(e) @@ -28,7 +28,7 @@ func TestCounter(t *testing.T) { } t.Run("subtest", func(t *testing.T) { - e := fixenv.NewEnv(t) + e := fixenv.New(t) r3 := counter(e) if r3 == r1 { t.Error() @@ -48,7 +48,7 @@ func counterTestAndSubtest(e fixenv.Env) int { } func TestTestAndSubtestCounter(t *testing.T) { - e := fixenv.NewEnv(t) + e := fixenv.New(t) r1 := counterTestAndSubtest(e) r2 := counterTestAndSubtest(e) @@ -57,7 +57,7 @@ func TestTestAndSubtestCounter(t *testing.T) { } t.Run("subtest", func(t *testing.T) { - e := fixenv.NewEnv(t) + e := fixenv.New(t) r3 := counterTestAndSubtest(e) if r3 != r1 { t.Error() diff --git a/examples/simple/use_cache_params_test.go b/examples/simple/use_cache_params_test.go index 0f9146f..e390969 100644 --- a/examples/simple/use_cache_params_test.go +++ b/examples/simple/use_cache_params_test.go @@ -20,7 +20,7 @@ func namedRandom(e fixenv.Env, name string) int { } func TestNamedRandom(t *testing.T) { - e := fixenv.NewEnv(t) + e := fixenv.New(t) first := namedRandom(e, "first") second := namedRandom(e, "second") require.NotEqual(t, first, second) diff --git a/maintest.go b/maintest.go index 34cab32..d405ad4 100644 --- a/maintest.go +++ b/maintest.go @@ -41,7 +41,7 @@ func CreateMainTestEnv(opts *CreateMainTestEnvOpts) (env *EnvT, tearDown func()) packageLevelVirtualTest := newVirtualTest(opts) globalMutex.Unlock() - env = NewEnv(packageLevelVirtualTest) // register global test for env + env = New(packageLevelVirtualTest) // register global test for env return env, packageLevelVirtualTest.cleanup } From 7b92d17763cff38ecb0818e0828e1583c639d5c5 Mon Sep 17 00:00:00 2001 From: Timofey Koolin Date: Tue, 9 May 2023 21:35:36 +0300 Subject: [PATCH 2/3] fix linter --- cache_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cache_test.go b/cache_test.go index fbafac9..4de6a18 100644 --- a/cache_test.go +++ b/cache_test.go @@ -300,7 +300,7 @@ func TestCache_SetOnce(t *testing.T) { }) } -func TestCache_GetOrSetRaceCondition(t *testing.T) { +func TestCache_GetOrSetRaceCondition(_ *testing.T) { parallels := 100 iterations := 1000 From 276047e4349ca2c50abbc494e74d07785cd21a57 Mon Sep 17 00:00:00 2001 From: Timofey Koolin Date: Wed, 10 May 2023 19:27:55 +0300 Subject: [PATCH 3/3] test deprecated constructor --- env_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/env_test.go b/env_test.go index 8dd704a..c36d70c 100644 --- a/env_test.go +++ b/env_test.go @@ -715,6 +715,17 @@ func Test_ScopeName(t *testing.T) { }) } +func TestNewEnv(t *testing.T) { + tm := &testMock{} + tm.SkipGoexit = true + New(tm) + + NewEnv(tm) + if len(tm.fatals) == 0 { + t.Fatal("bad double login between new and NewEnv") + } +} + func runUntilFatal(f func()) { stopped := make(chan bool) go func() {