github.com/jlowellwofford/u-root@v1.0.0/pkg/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 } 22 if len(config.Entries) > 0 { 23 t.Error("Expected no entries: got", config.Entries) 24 } 25 } 26 27 func TestConfigs(t *testing.T) { 28 // find all saved configs 29 tests, err := filepath.Glob("testdata/*.json") 30 if err != nil { 31 t.Error("Failed to find test config files:", err) 32 } 33 34 for _, test := range tests { 35 testJSON, err := ioutil.ReadFile(test) 36 if err != nil { 37 t.Errorf("Failed to read test json '%v':%v", test, err) 38 } 39 testConfigs := make([]*Config, 0) 40 err = json.Unmarshal(testJSON, &testConfigs) 41 if err != nil { 42 t.Errorf("Failed to unmarshall test json '%v':%v", test, err) 43 } 44 45 configPath := strings.TrimRight(test, ".json") 46 configs := FindConfigs(configPath) 47 configJSON, _ := json.Marshal(configs) 48 t.Logf("Configs for %v\n%v", test, string(configJSON)) 49 50 if diff := deep.Equal(testConfigs, configs); diff != nil { 51 t.Error(diff) 52 } 53 } 54 }