|
1 | 1 | package fixenv |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "fmt" |
5 | 6 | "log" |
6 | 7 | "sync" |
7 | 8 | ) |
8 | 9 |
|
| 10 | +var errTooManyOptionalArgs = errors.New("allow not more then one optional arg") |
| 11 | + |
9 | 12 | // FatalfFunction function signature of Fatalf |
10 | 13 | type FatalfFunction func(format string, args ...interface{}) |
11 | 14 |
|
@@ -34,18 +37,52 @@ type CreateMainTestEnvOpts struct { |
34 | 37 | SkipNow SkipNowFunction |
35 | 38 | } |
36 | 39 |
|
| 40 | +// packageLevelVirtualTest now used for tests only |
| 41 | +var lastPackageLevelVirtualTest *virtualTest |
| 42 | + |
37 | 43 | // CreateMainTestEnv called from TestMain for create global environment. |
38 | 44 | // It need only for use ScopePackage cache scope. |
39 | 45 | // If ScopePackage not used - no need to create main env. |
40 | 46 | func CreateMainTestEnv(opts *CreateMainTestEnvOpts) (env *EnvT, tearDown func()) { |
| 47 | + // TODO: handle second time initialize |
41 | 48 | globalMutex.Lock() |
42 | 49 | packageLevelVirtualTest := newVirtualTest(opts) |
| 50 | + lastPackageLevelVirtualTest = packageLevelVirtualTest |
43 | 51 | globalMutex.Unlock() |
44 | 52 |
|
45 | 53 | env = New(packageLevelVirtualTest) // register global test for env |
46 | 54 | return env, packageLevelVirtualTest.cleanup |
47 | 55 | } |
48 | 56 |
|
| 57 | +// RunTests runs the tests. It returns an exit code to pass to os.Exit. |
| 58 | +// |
| 59 | +// Usage: |
| 60 | +// declare in _test file TestMain function: |
| 61 | +// |
| 62 | +// func TestMain(m *testing.M) { |
| 63 | +// os.Exit(fixenv.RunTests(m)) |
| 64 | +// } |
| 65 | +func RunTests(m RunTestsI, opts ...CreateMainTestEnvOpts) int { |
| 66 | + var options *CreateMainTestEnvOpts |
| 67 | + switch len(opts) { |
| 68 | + case 0: |
| 69 | + // pass |
| 70 | + case 1: |
| 71 | + options = &opts[0] |
| 72 | + default: |
| 73 | + panic(errTooManyOptionalArgs) |
| 74 | + } |
| 75 | + |
| 76 | + _, cancel := CreateMainTestEnv(options) |
| 77 | + defer cancel() |
| 78 | + return m.Run() |
| 79 | +} |
| 80 | + |
| 81 | +type RunTestsI interface { |
| 82 | + // Run runs the tests. It returns an exit code to pass to os.Exit. |
| 83 | + Run() (code int) |
| 84 | +} |
| 85 | + |
49 | 86 | // virtualTest implement T interface for global env scope |
50 | 87 | type virtualTest struct { |
51 | 88 | m sync.Mutex |
|
0 commit comments