github.com/jonsyu1/godel@v0.0.0-20171017211503-64567a0cf169/apps/gunit/config/config.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 config 16 17 import ( 18 "encoding/json" 19 "io/ioutil" 20 21 "github.com/palantir/pkg/matcher" 22 "github.com/pkg/errors" 23 "gopkg.in/yaml.v2" 24 25 "github.com/palantir/godel/apps/gunit/params" 26 ) 27 28 type GUnit struct { 29 // Tags group tests into different sets. The key is the name of the tag and the value is a 30 // matcher.NamesPathsWithExcludeCfg that specifies the rules for matching the tests that are part of the tag. 31 // Any test that matches the provided matcher is considered part of the tag. 32 Tags map[string]matcher.NamesPathsWithExcludeCfg `yaml:"tags" json:"tags"` 33 34 // Exclude specifies the files that should be excluded from tests. 35 Exclude matcher.NamesPathsCfg `yaml:"exclude" json:"exclude"` 36 } 37 38 func (r *GUnit) ToParams() params.GUnit { 39 m := make(map[string]matcher.Matcher, len(r.Tags)) 40 for k, v := range r.Tags { 41 m[k] = v.Matcher() 42 } 43 return params.GUnit{ 44 Tags: m, 45 Exclude: r.Exclude.Matcher(), 46 } 47 } 48 49 func Load(cfgPath, jsonContent string) (params.GUnit, error) { 50 var yml []byte 51 if cfgPath != "" { 52 var err error 53 yml, err = ioutil.ReadFile(cfgPath) 54 if err != nil { 55 return params.GUnit{}, errors.Wrapf(err, "failed to read file %s", cfgPath) 56 } 57 } 58 cfg, err := LoadRawConfig(string(yml), jsonContent) 59 if err != nil { 60 return params.GUnit{}, err 61 } 62 return cfg.ToParams(), nil 63 } 64 65 func LoadRawConfig(ymlContent, jsonContent string) (GUnit, error) { 66 cfg := GUnit{} 67 if ymlContent != "" { 68 if err := yaml.Unmarshal([]byte(ymlContent), &cfg); err != nil { 69 return GUnit{}, errors.Wrapf(err, "failed to unmarshal YML %s", ymlContent) 70 } 71 } 72 if jsonContent != "" { 73 jsonCfg := GUnit{} 74 if err := json.Unmarshal([]byte(jsonContent), &jsonCfg); err != nil { 75 return GUnit{}, err 76 } 77 cfg.Exclude.Add(jsonCfg.Exclude) 78 } 79 return cfg, nil 80 }