template: OnlyExcept skipping

This commit is contained in:
Mitchell Hashimoto 2015-05-26 09:07:16 -07:00
parent c12072ecad
commit b5f4ffa56c
2 changed files with 68 additions and 0 deletions

View File

@ -125,6 +125,31 @@ func (t *Template) Validate() error {
return err
}
// Skip says whether or not to skip the build with the given name.
func (o *OnlyExcept) Skip(n string) bool {
if len(o.Only) > 0 {
for _, v := range o.Only {
if v == n {
return false
}
}
return true
}
if len(o.Except) > 0 {
for _, v := range o.Except {
if v == n {
return true
}
}
return false
}
return false
}
// Validate validates that the OnlyExcept settings are correct for a thing.
func (o *OnlyExcept) Validate(t *Template) error {
if len(o.Only) > 0 && len(o.Except) > 0 {

View File

@ -92,3 +92,46 @@ func TestTemplateValidate(t *testing.T) {
}
}
}
func TestOnlyExceptSkip(t *testing.T) {
cases := []struct {
Only, Except []string
Input string
Result bool
}{
{
[]string{"foo"},
nil,
"foo",
false,
},
{
nil,
[]string{"foo"},
"foo",
true,
},
{
nil,
nil,
"foo",
false,
},
}
for _, tc := range cases {
oe := &OnlyExcept{
Only: tc.Only,
Except: tc.Except,
}
actual := oe.Skip(tc.Input)
if actual != tc.Result {
t.Fatalf(
"bad: %#v\n\n%#v\n\n%#v\n\n%#v",
actual, tc.Only, tc.Except, tc.Input)
}
}
}