github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/boot/grub/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 grub
     6  
     7  import (
     8  	"context"
     9  	"io/ioutil"
    10  	"path/filepath"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/u-root/u-root/pkg/boot/boottest"
    15  )
    16  
    17  // Enable this to generate new configs.
    18  func DISABLEDTestGenerateConfigs(t *testing.T) {
    19  	tests, err := filepath.Glob("testdata_new/*.json")
    20  	if err != nil {
    21  		t.Error("Failed to find test config files:", err)
    22  	}
    23  
    24  	for _, test := range tests {
    25  		configPath := strings.TrimRight(test, ".json")
    26  		t.Run(configPath, func(t *testing.T) {
    27  			imgs, err := ParseLocalConfig(context.Background(), configPath)
    28  			if err != nil {
    29  				t.Fatalf("Failed to parse %s: %v", test, err)
    30  			}
    31  
    32  			if err := boottest.ToJSONFile(imgs, test); err != nil {
    33  				t.Errorf("failed to generate file: %v", err)
    34  			}
    35  		})
    36  	}
    37  }
    38  
    39  func TestConfigs(t *testing.T) {
    40  	// find all saved configs
    41  	tests, err := filepath.Glob("testdata_new/*.json")
    42  	if err != nil {
    43  		t.Error("Failed to find test config files:", err)
    44  	}
    45  
    46  	for _, test := range tests {
    47  		configPath := strings.TrimRight(test, ".json")
    48  		t.Run(configPath, func(t *testing.T) {
    49  			want, err := ioutil.ReadFile(test)
    50  			if err != nil {
    51  				t.Errorf("Failed to read test json '%v':%v", test, err)
    52  			}
    53  
    54  			imgs, err := ParseLocalConfig(context.Background(), configPath)
    55  			if err != nil {
    56  				t.Fatalf("Failed to parse %s: %v", test, err)
    57  			}
    58  
    59  			if err := boottest.CompareImagesToJSON(imgs, want); err != nil {
    60  				t.Errorf("ParseLocalConfig(): %v", err)
    61  			}
    62  		})
    63  	}
    64  }