diff --git a/fail.go b/fail.go index 61e8955..cf122ea 100644 --- a/fail.go +++ b/fail.go @@ -2,50 +2,44 @@ package ci import "testing" +func failIf[T Testing](f func() bool, t T) { + if f() { + t.FailNow() + } +} + // FailTestIfCI is a testing helper function that fails the execution of a test // if it is running within a CI/CD pipeline. func FailTestIfCI(t *testing.T) { - if IsCI() { - t.FailNow() - } + failIf(IsCI, t) } // FailTestIfNotCI is a testing helper function that fails the execution of a // test if it is not running within a CI/CD pipeline. func FailTestIfNotCI(t *testing.T) { - if IsNotCI() { - t.FailNow() - } + failIf(IsNotCI, t) } // FailBenchmarkIfCI is a testing helper function that fails the execution of a // benchmark if it is running within a CI/CD pipeline. func FailBenchmarkIfCI(b *testing.B) { - if IsCI() { - b.FailNow() - } + failIf(IsCI, b) } // FailBenchmarkIfNotCI is a testing helper function that fails the execution of // a benchmark if it is not running within a CI/CD pipeline. func FailBenchmarkIfNotCI(b *testing.B) { - if IsNotCI() { - b.FailNow() - } + failIf(IsNotCI, b) } // FailFuzzIfCI is a testing helper function that fails the execution of a fuzz // test if it is running within a CI/CD pipeline. func FailFuzzIfCI(f *testing.F) { - if IsCI() { - f.FailNow() - } + failIf(IsCI, f) } // FailFuzzIfNotCI is a testing helper function that fails the execution of a // fuzz test if it is not running within a CI/CD pipeline. func FailFuzzIfNotCI(f *testing.F) { - if IsNotCI() { - f.FailNow() - } + failIf(IsNotCI, f) } diff --git a/skip.go b/skip.go index 45c4e4e..37a6eb1 100644 --- a/skip.go +++ b/skip.go @@ -2,6 +2,12 @@ package ci import "testing" +func skipIf[T Testing](f func() bool, t T) { + if f() { + t.SkipNow() + } +} + // SkipTestIfCI is a testing helper function that skips the execution of a test // if it is running within a CI/CD pipeline. // @@ -19,47 +25,35 @@ import "testing" // // do your test... // } func SkipTestIfCI(t *testing.T) { - if IsCI() { - t.SkipNow() - } + skipIf(IsCI, t) } // SkipTestIfNotCI is a testing helper function that skips the execution of a // test if it is not running within a CI/CD pipeline. func SkipTestIfNotCI(t *testing.T) { - if IsNotCI() { - t.SkipNow() - } + skipIf(IsNotCI, t) } // SkipBenchmarkIfCI is a testing helper function that skips the execution of a // benchmark if it is running within a CI/CD pipeline. func SkipBenchmarkIfCI(b *testing.B) { - if IsCI() { - b.SkipNow() - } + skipIf(IsCI, b) } // SkipBenchmarkIfNotCI is a testing helper function that skips the execution of // a benchmark if it is not running within a CI/CD pipeline. func SkipBenchmarkIfNotCI(b *testing.B) { - if IsNotCI() { - b.SkipNow() - } + skipIf(IsNotCI, b) } // SkipFuzzIfCI is a testing helper function that skips the execution of a fuzz // test if it is running within a CI/CD pipeline. func SkipFuzzIfCI(f *testing.F) { - if IsCI() { - f.SkipNow() - } + skipIf(IsCI, f) } // SkipFuzzIfNotCI is a testing helper function that skips the execution of a // fuzz test if it is not running within a CI/CD pipeline. func SkipFuzzIfNotCI(f *testing.F) { - if IsNotCI() { - f.SkipNow() - } + skipIf(IsNotCI, f) }