requirement.go 787 B

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