Skip to content

Commit

Permalink
add fixenv.RunTests
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby committed Nov 26, 2023
1 parent def74bd commit dac6ede
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
5 changes: 4 additions & 1 deletion env.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,10 @@ func (e *EnvT) fixtureCallWrapper(key cacheKey, f FixtureCallbackFunc, opt *Fixt
e.m.Unlock()

if si == nil {
e.t.Fatalf("Unexpected scope. Create env for test %q", scopeName)
if opt.Scope == ScopePackage {
e.t.Fatalf("Initialize package scope before use ", scopeName)
}
e.t.Fatalf("Unexpected scope: %q", scopeName)
// not reachable
return nil, nil
}
Expand Down
38 changes: 38 additions & 0 deletions examples/simple_main_test/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package simple_main_test

import (
"github.com/rekby/fixenv"
"math/rand"
"testing"
)

var global int = -1

func FSingleRandom(e fixenv.Env) int {
var f fixenv.GenericFixtureFunction[int] = func() (*fixenv.GenericResult[int], error) {

Check failure on line 12 in examples/simple_main_test/example_test.go

View workflow job for this annotation

GitHub Actions / test (1.16)

expected ';', found '['
return fixenv.NewGenericResult(rand.Int()), nil
}
return fixenv.CacheResult(e, f, fixenv.CacheOptions{Scope: fixenv.ScopePackage})
}

func TestFirst(t *testing.T) {
e := fixenv.New(t)
if global == -1 {
global = FSingleRandom(e)
}

if singleRnd := FSingleRandom(e); singleRnd != global {
t.Fatalf("%v != %v", singleRnd, global)
}
}

func TestSecond(t *testing.T) {
e := fixenv.New(t)
if global == -1 {
global = FSingleRandom(e)
}

if singleRnd := FSingleRandom(e); singleRnd != global {
t.Fatalf("%v != %v", singleRnd, global)
}
}
11 changes: 11 additions & 0 deletions examples/simple_main_test/testmain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package simple_main

import (
"github.com/rekby/fixenv"
"os"
"testing"
)

func TestMain(m *testing.M) {
os.Exit(fixenv.RunTests(m))
}
25 changes: 25 additions & 0 deletions maintest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"sync"
"testing"
)

// FatalfFunction function signature of Fatalf
Expand Down Expand Up @@ -46,6 +47,30 @@ func CreateMainTestEnv(opts *CreateMainTestEnvOpts) (env *EnvT, tearDown func())
return env, packageLevelVirtualTest.cleanup
}

// RunTests runs the tests. It returns an exit code to pass to os.Exit.
//
// Usage:
// declare in _test file TestMain function:
//
// func TestMain(m *testing.M) {
// os.Exit(fixenv.RunTests(m))
// }
func RunTests(m *testing.M, opts ...*CreateMainTestEnvOpts) int {
var options *CreateMainTestEnvOpts
switch len(opts) {
case 0:
// pass
case 1:
options = opts[0]
default:
panic("allow not more then one optional arg")
}

_, cancel := CreateMainTestEnv(options)
defer cancel()
return m.Run()
}

// virtualTest implement T interface for global env scope
type virtualTest struct {
m sync.Mutex
Expand Down

0 comments on commit dac6ede

Please sign in to comment.