|
| 1 | +// Copyright 2023 Google LLC |
| 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 cmd |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "hpc-toolkit/pkg/config" |
| 20 | + "testing" |
| 21 | +) |
| 22 | + |
| 23 | +func TestIsGroupSelected(t *testing.T) { |
| 24 | + type test struct { |
| 25 | + only []string |
| 26 | + skip []string |
| 27 | + group config.GroupName |
| 28 | + want bool |
| 29 | + } |
| 30 | + tests := []test{ |
| 31 | + {nil, nil, "green", true}, |
| 32 | + {[]string{"green"}, nil, "green", true}, |
| 33 | + {[]string{"green"}, nil, "blue", false}, |
| 34 | + {nil, []string{"green"}, "green", false}, |
| 35 | + {nil, []string{"green"}, "blue", true}, |
| 36 | + } |
| 37 | + |
| 38 | + for _, tc := range tests { |
| 39 | + t.Run(fmt.Sprintf("%v;%v;%q", tc.only, tc.skip, tc.group), func(t *testing.T) { |
| 40 | + flagOnlyGroups, flagSkipGroups = tc.only, tc.skip |
| 41 | + got := isGroupSelected(tc.group) |
| 42 | + if got != tc.want { |
| 43 | + t.Errorf("isGroupSelected(%v) = %v; want %v", tc.group, got, tc.want) |
| 44 | + } |
| 45 | + }) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestValidateGroupSelectionFlags(t *testing.T) { |
| 50 | + type test struct { |
| 51 | + only []string |
| 52 | + skip []string |
| 53 | + groups []string |
| 54 | + err bool |
| 55 | + } |
| 56 | + tests := []test{ |
| 57 | + {nil, nil, []string{"green"}, false}, |
| 58 | + {[]string{"green"}, []string{"blue"}, []string{"green", "blue"}, true}, |
| 59 | + {[]string{"green"}, nil, []string{"green"}, false}, |
| 60 | + {[]string{"green"}, nil, []string{"blue"}, true}, |
| 61 | + {nil, []string{"green"}, []string{"green"}, false}, |
| 62 | + {nil, []string{"green"}, []string{"blue"}, true}, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tc := range tests { |
| 66 | + t.Run(fmt.Sprintf("%v;%v;%v", tc.only, tc.skip, tc.groups), func(t *testing.T) { |
| 67 | + flagOnlyGroups, flagSkipGroups = tc.only, tc.skip |
| 68 | + bp := config.Blueprint{} |
| 69 | + for _, g := range tc.groups { |
| 70 | + bp.Groups = append(bp.Groups, config.Group{Name: config.GroupName(g)}) |
| 71 | + } |
| 72 | + |
| 73 | + err := validateGroupSelectionFlags(bp) |
| 74 | + if tc.err && err == nil { |
| 75 | + t.Errorf("validateGroupSelectionFlags(%v) = nil; want error", tc.groups) |
| 76 | + } |
| 77 | + if !tc.err && err != nil { |
| 78 | + t.Errorf("validateGroupSelectionFlags(%v) = %v; want nil", tc.groups, err) |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments