github.com/jonsyu1/godel@v0.0.0-20171017211503-64567a0cf169/apps/gunit/params/params.go (about)

     1  // Copyright 2016 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package params
    16  
    17  import (
    18  	"regexp"
    19  	"sort"
    20  	"strings"
    21  
    22  	"github.com/palantir/pkg/matcher"
    23  	"github.com/pkg/errors"
    24  )
    25  
    26  const (
    27  	AllTagName  = "all"
    28  	NoneTagName = "none"
    29  )
    30  
    31  type GUnit struct {
    32  	// Tags group tests into different sets. The key is the name of the tag and the value is a matcher.NamesPathsCfg
    33  	// that specifies the rules for matching the tests that are part of the tag. Any test that matches the provided
    34  	// matcher is considered part of the tag.
    35  	Tags map[string]matcher.Matcher
    36  
    37  	// Exclude specifies the files that should be excluded from tests.
    38  	Exclude matcher.Matcher
    39  }
    40  
    41  func (p *GUnit) Validate() error {
    42  	var invalidTagNames []string
    43  	seenTagNames := make(map[string]struct{})
    44  	duplicateTagNames := make(map[string]struct{})
    45  
    46  	for k := range p.Tags {
    47  		if !validTagName(k) {
    48  			invalidTagNames = append(invalidTagNames, k)
    49  		}
    50  		normalized := strings.ToLower(k)
    51  		if _, ok := seenTagNames[normalized]; ok {
    52  			duplicateTagNames[normalized] = struct{}{}
    53  		}
    54  		seenTagNames[normalized] = struct{}{}
    55  
    56  		switch normalized {
    57  		case AllTagName, NoneTagName:
    58  			return errors.Errorf("%q is a reserved name that cannot be used as a tag name", normalized)
    59  		}
    60  	}
    61  
    62  	if len(invalidTagNames) > 0 {
    63  		sort.Strings(invalidTagNames)
    64  		return errors.Errorf("invalid tag names: %v", invalidTagNames)
    65  	}
    66  
    67  	if len(duplicateTagNames) > 0 {
    68  		var sorted []string
    69  		for k := range duplicateTagNames {
    70  			sorted = append(sorted, k)
    71  		}
    72  		sort.Strings(sorted)
    73  		return errors.Errorf("tag names were defined multiple times (names must be unique in case-insensitive manner): %v", sorted)
    74  	}
    75  
    76  	// normalize tags to all lowercase
    77  	for k, v := range p.Tags {
    78  		delete(p.Tags, k)
    79  		p.Tags[strings.ToLower(k)] = v
    80  	}
    81  
    82  	return nil
    83  }
    84  
    85  var tagRegExp = regexp.MustCompile(`[A-Za-z0-9_-]+`)
    86  
    87  func validTagName(tag string) bool {
    88  	return len(tagRegExp.ReplaceAllString(tag, "")) == 0
    89  }