gopkg.in/hugelgupf/u-root.v4@v4.0.0-20180831060141-1d761fb73d50/pkg/uroot/builder/bb_test.go (about)

     1  // Copyright 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 builder
     6  
     7  import (
     8  	"go/ast"
     9  	"io/ioutil"
    10  	"os"
    11  	"os/exec"
    12  	"path/filepath"
    13  	"testing"
    14  
    15  	"github.com/u-root/u-root/pkg/golang"
    16  	"github.com/u-root/u-root/pkg/uroot/initramfs"
    17  )
    18  
    19  func TestBBBuild(t *testing.T) {
    20  	dir, err := ioutil.TempDir("", "u-root")
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	defer os.RemoveAll(dir)
    25  
    26  	opts := Opts{
    27  		Env: golang.Default(),
    28  		Packages: []string{
    29  			"github.com/u-root/u-root/pkg/uroot/test/foo",
    30  			"github.com/u-root/u-root/cmds/rush",
    31  		},
    32  		TempDir:   dir,
    33  		BinaryDir: "bbin",
    34  	}
    35  	af := initramfs.NewFiles()
    36  	var bbb BBBuilder
    37  	if err := bbb.Build(af, opts); err != nil {
    38  		t.Error(err)
    39  	}
    40  
    41  	var mustContain = []string{
    42  		"bbin/rush",
    43  		"bbin/foo",
    44  	}
    45  	for _, name := range mustContain {
    46  		if !af.Contains(name) {
    47  			t.Errorf("expected files to include %q; archive: %v", name, af)
    48  		}
    49  	}
    50  
    51  }
    52  
    53  func findFile(filemap map[string]*ast.File, basename string) *ast.File {
    54  	for name, f := range filemap {
    55  		if filepath.Base(name) == basename {
    56  			return f
    57  		}
    58  	}
    59  	return nil
    60  }
    61  
    62  func TestPackageRewriteFile(t *testing.T) {
    63  	dir, err := ioutil.TempDir("", "u-root")
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	defer os.RemoveAll(dir)
    68  
    69  	bin := filepath.Join(dir, "foo")
    70  	if err := BuildBusybox(golang.Default(), []string{"github.com/u-root/u-root/pkg/uroot/test/foo"}, bin); err != nil {
    71  		t.Fatal(err)
    72  	}
    73  
    74  	cmd := exec.Command(bin)
    75  	o, err := cmd.CombinedOutput()
    76  	if err != nil {
    77  		t.Fatalf("foo failed: %v %v", string(o), err)
    78  	}
    79  }