1 // Copyright (C) 2021 Simon Ruderich
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
25 "github.com/google/go-cmp/cmp"
28 func TestLoadHosts(t *testing.T) {
29 cwd, err := os.Getwd()
35 sliceToHosts := func(hosts []*Host) *Hosts {
38 Map: make(map[string]*Host),
40 for _, x := range hosts {
53 "../testdata/project",
56 Name: "host1.example.org",
62 Name: "host3.example.net",
69 "../testdata/host-invalid-all",
71 fmt.Errorf("hosts.yaml: host \"all\": conflict with pre-defined group \"all\""),
74 "../testdata/host-invalid-detected",
76 fmt.Errorf("hosts.yaml: host \"detected\": name must not start with \"detected\" (reserved for detected groups)"),
79 "../testdata/host-invalid-special",
81 fmt.Errorf("hosts.yaml: host \"special:host\": name must not contain \":\""),
85 for _, tc := range tests {
86 t.Run(tc.path, func(t *testing.T) {
87 err := os.Chdir(filepath.Join(cwd, tc.path))
92 res, err := LoadHosts()
94 if !reflect.DeepEqual(tc.exp, res) {
96 cmp.Diff(tc.exp, res))
98 // Ugly but the simplest way to compare errors (including nil)
99 if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
100 t.Errorf("err = %#v, want %#v",