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