requirement.go 760 B

123456789101112131415161718192021222324252627282930313233
  1. package requirement
  2. import (
  3. "fmt"
  4. "path"
  5. "reflect"
  6. "runtime"
  7. "strings"
  8. )
  9. type skipT interface {
  10. Skip(reason string)
  11. }
  12. // Test represent a function that can be used as a requirement validation.
  13. type Test func() bool
  14. // Is checks if the environment satisfies the requirements
  15. // for the test to run or skips the tests.
  16. func Is(s skipT, requirements ...Test) {
  17. for _, r := range requirements {
  18. isValid := r()
  19. if !isValid {
  20. requirementFunc := runtime.FuncForPC(reflect.ValueOf(r).Pointer()).Name()
  21. s.Skip(fmt.Sprintf("unmatched requirement %s", extractRequirement(requirementFunc)))
  22. }
  23. }
  24. }
  25. func extractRequirement(requirementFunc string) string {
  26. requirement := path.Base(requirementFunc)
  27. return strings.SplitN(requirement, ".", 2)[1]
  28. }