github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/pkg/boot/diskboot/config_test.go (about) 1 // Copyright 2017-2018 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package diskboot 6 7 import ( 8 "encoding/json" 9 "io/ioutil" 10 "path/filepath" 11 "strings" 12 "testing" 13 14 "github.com/go-test/deep" 15 ) 16 17 func TestParseEmpty(t *testing.T) { 18 config := ParseConfig("/", "/grub.cfg", nil) 19 if config == nil { 20 t.Error("Expected non-nil config") 21 } else if len(config.Entries) > 0 { 22 t.Error("Expected no entries: got", config.Entries) 23 } 24 } 25 26 func TestConfigs(t *testing.T) { 27 // find all saved configs 28 tests, err := filepath.Glob("testdata/*.json") 29 if err != nil { 30 t.Error("Failed to find test config files:", err) 31 } 32 33 for _, test := range tests { 34 testJSON, err := ioutil.ReadFile(test) 35 if err != nil { 36 t.Errorf("Failed to read test json '%v':%v", test, err) 37 } 38 testConfigs := make([]*Config, 0) 39 err = json.Unmarshal(testJSON, &testConfigs) 40 if err != nil { 41 t.Errorf("Failed to unmarshall test json '%v':%v", test, err) 42 } 43 44 configPath := strings.TrimRight(test, ".json") 45 configs := FindConfigs(configPath) 46 configJSON, _ := json.Marshal(configs) 47 t.Logf("Configs for %v\n%v", test, string(configJSON)) 48 49 if diff := deep.Equal(testConfigs, configs); diff != nil { 50 t.Error(diff) 51 } 52 } 53 }