Skip to content

Commit

Permalink
Merge pull request #29 from rekby/dev
Browse files Browse the repository at this point in the history
add fixenv.New
  • Loading branch information
rekby authored May 10, 2023
2 parents 08bddb5 + 276047e commit 9e868cd
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 30 deletions.
2 changes: 1 addition & 1 deletion cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
35 changes: 23 additions & 12 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
})
})
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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))
})
Expand All @@ -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()

Expand All @@ -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))
})
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
}

Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_env/custom_env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/http_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions examples/simple/package_scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
8 changes: 4 additions & 4 deletions examples/simple/simple_old_style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions examples/simple/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/use_cache_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion maintest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 9e868cd

Please sign in to comment.