github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/integration-cli/requirement/requirement.go (about)

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