Skip to content

Commit

Permalink
feat: implement Testing with generics
Browse files Browse the repository at this point in the history
Signed-off-by: Dwi Siswanto <me@dw1.io>
  • Loading branch information
dwisiswant0 committed Feb 28, 2024
1 parent dac8a61 commit ae76a15
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
30 changes: 12 additions & 18 deletions fail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
30 changes: 12 additions & 18 deletions skip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand All @@ -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)
}

0 comments on commit ae76a15

Please sign in to comment.