From 501371f4d0561bf60a985ba34df17ef8890d9088 Mon Sep 17 00:00:00 2001 From: Chris Koch Date: Fri, 23 Feb 2024 03:30:01 +0000 Subject: [PATCH 1/5] Rename uflags to mkuimage Signed-off-by: Chris Koch --- cmd/mkuimage/main.go | 22 +++++++++++----------- uimage/{uflags => mkuimage}/uflags.go | 4 ++-- uimage/{uflags => mkuimage}/uflags_test.go | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) rename uimage/{uflags => mkuimage}/uflags.go (98%) rename uimage/{uflags => mkuimage}/uflags_test.go (97%) diff --git a/cmd/mkuimage/main.go b/cmd/mkuimage/main.go index cdc7f07..c965e87 100644 --- a/cmd/mkuimage/main.go +++ b/cmd/mkuimage/main.go @@ -19,7 +19,7 @@ import ( "github.com/u-root/gobusybox/src/pkg/golang" "github.com/u-root/mkuimage/uimage" "github.com/u-root/mkuimage/uimage/builder" - "github.com/u-root/mkuimage/uimage/uflags" + "github.com/u-root/mkuimage/uimage/mkuimage" "github.com/u-root/uio/llog" ) @@ -68,8 +68,8 @@ func main() { } env := golang.Default(golang.DisableCGO()) - f := &uflags.Flags{ - Commands: uflags.CommandFlags{ + f := &mkuimage.Flags{ + Commands: mkuimage.CommandFlags{ Builder: "bb", BuildOpts: &golang.BuildOpts{}, }, @@ -89,8 +89,13 @@ func main() { l.Warnf("GOOS is not linux. Did you mean to set GOOS=linux?") } + m := []uimage.Modifier{ + uimage.WithReplaceEnv(env), + uimage.WithBaseArchive(uimage.DefaultRamfs()), + uimage.WithCPIOOutput(defaultFile(env)), + } // Main is in a separate functions so defers run on return. - if err := Main(l, env, f); err != nil { + if err := Main(l, m, env, f); err != nil { l.Errorf("Build error: %v", err) return } @@ -124,7 +129,7 @@ func defaultFile(env *golang.Environ) string { // Main is a separate function so defers are run on return, which they wouldn't // on exit. -func Main(l *llog.Logger, env *golang.Environ, f *uflags.Flags) error { +func Main(l *llog.Logger, base []uimage.Modifier, env *golang.Environ, f *mkuimage.Flags) error { v, err := env.Version() if err != nil { l.Infof("Could not get environment's Go version, using runtime's version: %v", err) @@ -159,17 +164,12 @@ func Main(l *llog.Logger, env *golang.Environ, f *uflags.Flags) error { } // Set defaults. - m := []uimage.Modifier{ - uimage.WithReplaceEnv(env), - uimage.WithBaseArchive(uimage.DefaultRamfs()), - uimage.WithCPIOOutput(defaultFile(env)), - } more, err := f.Modifiers(flag.Args()...) if err != nil { return err } - err = uimage.Create(l, append(m, more...)...) + err = uimage.Create(l, append(base, more...)...) if errors.Is(err, builder.ErrBusyboxFailed) { l.Errorf("Preserving temp dir due to busybox build error") keepTempDir = true diff --git a/uimage/uflags/uflags.go b/uimage/mkuimage/uflags.go similarity index 98% rename from uimage/uflags/uflags.go rename to uimage/mkuimage/uflags.go index 8121dab..317c96d 100644 --- a/uimage/uflags/uflags.go +++ b/uimage/mkuimage/uflags.go @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// Package uflags defines mkuimage flags. -package uflags +// Package mkuimage defines mkuimage flags and creation function. +package mkuimage import ( "flag" diff --git a/uimage/uflags/uflags_test.go b/uimage/mkuimage/uflags_test.go similarity index 97% rename from uimage/uflags/uflags_test.go rename to uimage/mkuimage/uflags_test.go index 153355d..2abc344 100644 --- a/uimage/uflags/uflags_test.go +++ b/uimage/mkuimage/uflags_test.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package uflags +package mkuimage import ( "errors" From 4319a6daee81ae48afbbe73083d77d5bd8d6ae90 Mon Sep 17 00:00:00 2001 From: Chris Koch Date: Fri, 23 Feb 2024 03:35:48 +0000 Subject: [PATCH 2/5] mkuimage: move Main to package to be reusable Signed-off-by: Chris Koch --- cmd/mkuimage/main.go | 78 +---------------------------------- uimage/mkuimage/cmd.go | 92 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 76 deletions(-) create mode 100644 uimage/mkuimage/cmd.go diff --git a/cmd/mkuimage/main.go b/cmd/mkuimage/main.go index c965e87..33d649e 100644 --- a/cmd/mkuimage/main.go +++ b/cmd/mkuimage/main.go @@ -12,13 +12,10 @@ import ( "log" "log/slog" "os" - "runtime" - "strings" "github.com/dustin/go-humanize" "github.com/u-root/gobusybox/src/pkg/golang" "github.com/u-root/mkuimage/uimage" - "github.com/u-root/mkuimage/uimage/builder" "github.com/u-root/mkuimage/uimage/mkuimage" "github.com/u-root/uio/llog" ) @@ -84,19 +81,13 @@ func main() { l.RegisterVerboseFlag(flag.CommandLine, "v", slog.LevelDebug) flag.Parse() - l.Infof("Build environment: %s", env) - if env.GOOS != "linux" { - l.Warnf("GOOS is not linux. Did you mean to set GOOS=linux?") - } - m := []uimage.Modifier{ uimage.WithReplaceEnv(env), uimage.WithBaseArchive(uimage.DefaultRamfs()), uimage.WithCPIOOutput(defaultFile(env)), } - // Main is in a separate functions so defers run on return. - if err := Main(l, m, env, f); err != nil { - l.Errorf("Build error: %v", err) + if err := mkuimage.CreateUimage(l, m, f, flag.Args()); err != nil { + l.Errorf("mkuimage error: %v", err) return } @@ -105,74 +96,9 @@ func main() { } } -var recommendedVersions = []string{ - "go1.20", - "go1.21", - "go1.22", -} - -func isRecommendedVersion(v string) bool { - for _, r := range recommendedVersions { - if strings.HasPrefix(v, r) { - return true - } - } - return false -} - func defaultFile(env *golang.Environ) string { if len(env.GOOS) == 0 || len(env.GOARCH) == 0 { return "/tmp/initramfs.cpio" } return fmt.Sprintf("/tmp/initramfs.%s_%s.cpio", env.GOOS, env.GOARCH) } - -// Main is a separate function so defers are run on return, which they wouldn't -// on exit. -func Main(l *llog.Logger, base []uimage.Modifier, env *golang.Environ, f *mkuimage.Flags) error { - v, err := env.Version() - if err != nil { - l.Infof("Could not get environment's Go version, using runtime's version: %v", err) - v = runtime.Version() - } - if !isRecommendedVersion(v) { - l.Warnf(`You are not using one of the recommended Go versions (have = %s, recommended = %v). - Some packages may not compile. - Go to https://golang.org/doc/install to find out how to install a newer version of Go, - or use https://godoc.org/golang.org/dl/%s to install an additional version of Go.`, - v, recommendedVersions, recommendedVersions[0]) - } - - keepTempDir := f.KeepTempDir - if f.TempDir == "" { - var err error - f.TempDir, err = os.MkdirTemp("", "u-root") - if err != nil { - return err - } - defer func() { - if keepTempDir { - l.Infof("Keeping temp dir %s", f.TempDir) - } else { - os.RemoveAll(f.TempDir) - } - }() - } else if _, err := os.Stat(f.TempDir); os.IsNotExist(err) { - if err := os.MkdirAll(f.TempDir, 0o755); err != nil { - return fmt.Errorf("temporary directory %q did not exist; tried to mkdir but failed: %v", f.TempDir, err) - } - } - - // Set defaults. - more, err := f.Modifiers(flag.Args()...) - if err != nil { - return err - } - - err = uimage.Create(l, append(base, more...)...) - if errors.Is(err, builder.ErrBusyboxFailed) { - l.Errorf("Preserving temp dir due to busybox build error") - keepTempDir = true - } - return err -} diff --git a/uimage/mkuimage/cmd.go b/uimage/mkuimage/cmd.go new file mode 100644 index 0000000..d91efe3 --- /dev/null +++ b/uimage/mkuimage/cmd.go @@ -0,0 +1,92 @@ +// Copyright 2024 the u-root Authors. All rights reserved +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package mkuimage + +import ( + "errors" + "fmt" + "os" + "runtime" + "strings" + + "github.com/u-root/mkuimage/uimage" + "github.com/u-root/mkuimage/uimage/builder" + "github.com/u-root/uio/llog" +) + +var recommendedVersions = []string{ + "go1.20", + "go1.21", + "go1.22", +} + +func isRecommendedVersion(v string) bool { + for _, r := range recommendedVersions { + if strings.HasPrefix(v, r) { + return true + } + } + return false +} + +// CreateUimage creates a uimage from the given base modifiers and flags. +func CreateUimage(l *llog.Logger, base []uimage.Modifier, f *Flags, args []string) error { + keepTempDir := f.KeepTempDir + if f.TempDir == "" { + var err error + f.TempDir, err = os.MkdirTemp("", "u-root") + if err != nil { + return err + } + defer func() { + if keepTempDir { + l.Infof("Keeping temp dir %s", f.TempDir) + } else { + os.RemoveAll(f.TempDir) + } + }() + } else if _, err := os.Stat(f.TempDir); os.IsNotExist(err) { + if err := os.MkdirAll(f.TempDir, 0o755); err != nil { + return fmt.Errorf("temporary directory %q did not exist; tried to mkdir but failed: %v", f.TempDir, err) + } + } + + // Set defaults. + more, err := f.Modifiers(args...) + if err != nil { + return err + } + + opts, err := uimage.OptionsFor(append(base, more...)...) + if err != nil { + return err + } + + env := opts.Env + + l.Infof("Build environment: %s", env) + if env.GOOS != "linux" { + l.Warnf("GOOS is not linux. Did you mean to set GOOS=linux?") + } + v, err := env.Version() + if err != nil { + l.Infof("Could not get environment's Go version, using runtime's version: %v", err) + v = runtime.Version() + } + if !isRecommendedVersion(v) { + l.Warnf(`You are not using one of the recommended Go versions (have = %s, recommended = %v). + Some packages may not compile. + Go to https://golang.org/doc/install to find out how to install a newer version of Go, + or use https://godoc.org/golang.org/dl/%s to install an additional version of Go.`, + v, recommendedVersions, recommendedVersions[0]) + } + + err = opts.Create(l) + if errors.Is(err, builder.ErrBusyboxFailed) { + l.Errorf("Preserving temp dir due to busybox build error") + keepTempDir = true + } + return err +} From 00b735d30742d797230535cddc08a6e952fdadfd Mon Sep 17 00:00:00 2001 From: Chris Koch Date: Fri, 23 Feb 2024 03:45:15 +0000 Subject: [PATCH 3/5] Exit with exit code 1 on failure Signed-off-by: Chris Koch --- cmd/mkuimage/main.go | 2 +- cmd/mkuimage/main_test.go | 60 +++++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/cmd/mkuimage/main.go b/cmd/mkuimage/main.go index 33d649e..7bfed17 100644 --- a/cmd/mkuimage/main.go +++ b/cmd/mkuimage/main.go @@ -88,7 +88,7 @@ func main() { } if err := mkuimage.CreateUimage(l, m, f, flag.Args()); err != nil { l.Errorf("mkuimage error: %v", err) - return + os.Exit(1) } if stat, err := os.Stat(f.OutputFile); err == nil && f.ArchiveFormat == "cpio" { diff --git a/cmd/mkuimage/main_test.go b/cmd/mkuimage/main_test.go index efd5b65..15bd2e8 100644 --- a/cmd/mkuimage/main_test.go +++ b/cmd/mkuimage/main_test.go @@ -14,6 +14,7 @@ import ( "os/exec" "path/filepath" "strings" + "syscall" "testing" "github.com/u-root/gobusybox/src/pkg/golang" @@ -22,11 +23,6 @@ import ( "golang.org/x/sync/errgroup" ) -var twocmds = []string{ - "github.com/u-root/u-root/cmds/core/ls", - "github.com/u-root/u-root/cmds/core/init", -} - func TestUrootCmdline(t *testing.T) { wd, err := os.Getwd() if err != nil { @@ -65,7 +61,7 @@ func TestUrootCmdline(t *testing.T) { name string env []string args []string - err error + exitCode int validators []itest.ArchiveValidator } @@ -74,7 +70,6 @@ func TestUrootCmdline(t *testing.T) { name: "include one extra file", args: []string{"-nocmd", "-files=/bin/bash"}, env: []string{"GO111MODULE=off"}, - err: nil, validators: []itest.ArchiveValidator{ itest.HasFile{Path: "bin/bash"}, }, @@ -83,7 +78,6 @@ func TestUrootCmdline(t *testing.T) { name: "fix usage of an absolute path", args: []string{"-nocmd", fmt.Sprintf("-files=%s:/bin", sampledir)}, env: []string{"GO111MODULE=off"}, - err: nil, validators: []itest.ArchiveValidator{ itest.HasFile{Path: "/bin/foo"}, itest.HasFile{Path: "/bin/bar"}, @@ -122,7 +116,6 @@ func TestUrootCmdline(t *testing.T) { { name: "uinitcmd", args: []string{"-uinitcmd=echo foobar fuzz", "-defaultsh=", "github.com/u-root/u-root/cmds/core/init", "github.com/u-root/u-root/cmds/core/echo"}, - err: nil, validators: []itest.ArchiveValidator{ itest.HasRecord{R: cpio.Symlink("bin/uinit", "../bbin/echo")}, itest.HasContent{ @@ -133,8 +126,12 @@ func TestUrootCmdline(t *testing.T) { }, { name: "binary build", - args: []string{"-build=binary", "-defaultsh=", "github.com/u-root/u-root/cmds/core/init", "github.com/u-root/u-root/cmds/core/echo"}, - err: nil, + args: []string{ + "-build=binary", + "-defaultsh=", + "github.com/u-root/u-root/cmds/core/init", + "github.com/u-root/u-root/cmds/core/echo", + }, validators: []itest.ArchiveValidator{ itest.HasFile{Path: "bin/init"}, itest.HasFile{Path: "bin/echo"}, @@ -143,14 +140,20 @@ func TestUrootCmdline(t *testing.T) { }, { name: "hosted mode", - args: append([]string{"-base=/dev/null", "-defaultsh=", "-initcmd="}, twocmds...), + args: []string{ + "-base=/dev/null", + "-defaultsh=", + "-initcmd=", + "github.com/u-root/u-root/cmds/core/ls", + "github.com/u-root/u-root/cmds/core/init", + }, }, { name: "AMD64 build", env: []string{"GOARCH=amd64"}, args: []string{ + "-defaultsh=echo", "github.com/u-root/u-root/cmds/core/init", - "github.com/u-root/u-root/cmds/core/elvish", "github.com/u-root/u-root/cmds/core/echo", }, }, @@ -158,8 +161,8 @@ func TestUrootCmdline(t *testing.T) { name: "ARM7 build", env: []string{"GOARCH=arm", "GOARM=7"}, args: []string{ + "-defaultsh=", "github.com/u-root/u-root/cmds/core/init", - "github.com/u-root/u-root/cmds/core/elvish", "github.com/u-root/u-root/cmds/core/echo", }, }, @@ -167,8 +170,8 @@ func TestUrootCmdline(t *testing.T) { name: "ARM64 build", env: []string{"GOARCH=arm64"}, args: []string{ + "-defaultsh=", "github.com/u-root/u-root/cmds/core/init", - "github.com/u-root/u-root/cmds/core/elvish", "github.com/u-root/u-root/cmds/core/echo", }, }, @@ -176,8 +179,8 @@ func TestUrootCmdline(t *testing.T) { name: "RISCV 64bit build", env: []string{"GOARCH=riscv64"}, args: []string{ + "-defaultsh=", "github.com/u-root/u-root/cmds/core/init", - "github.com/u-root/u-root/cmds/core/elvish", "github.com/u-root/u-root/cmds/core/echo", }, }, @@ -192,18 +195,25 @@ func TestUrootCmdline(t *testing.T) { g.Go(func() error { var err error - f1, sum1, err = buildIt(t, execPath, tt.args, tt.env, tt.err, gocoverdir) + f1, sum1, err = buildIt(t, execPath, tt.args, tt.env, gocoverdir) return err }) g.Go(func() error { var err error - f2, sum2, err = buildIt(t, execPath, tt.args, tt.env, tt.err, gocoverdir) + f2, sum2, err = buildIt(t, execPath, tt.args, tt.env, gocoverdir) return err }) - if err := g.Wait(); err != nil { - t.Fatal(err) + var exitErr *exec.ExitError + if err := g.Wait(); errors.As(err, &exitErr) { + if ec := exitErr.Sys().(syscall.WaitStatus).ExitStatus(); ec != tt.exitCode { + t.Errorf("mkuimage exit code = %d, want %d", ec, tt.exitCode) + } + return + } else if err != nil { + t.Errorf("mkuimage failed: %v", err) + return } a, err := itest.ReadArchive(f1.Name()) @@ -225,7 +235,7 @@ func TestUrootCmdline(t *testing.T) { } } -func buildIt(t *testing.T, execPath string, args, env []string, want error, gocoverdir string) (*os.File, []byte, error) { +func buildIt(t *testing.T, execPath string, args, env []string, gocoverdir string) (*os.File, []byte, error) { t.Helper() initramfs, err := os.CreateTemp(t.TempDir(), "u-root-") if err != nil { @@ -241,10 +251,10 @@ func buildIt(t *testing.T, execPath string, args, env []string, want error, goco c.Env = append(os.Environ(), env...) c.Env = append(c.Env, golang.Default().Env()...) c.Env = append(c.Env, "GOCOVERDIR="+gocoverdir) - if out, err := c.CombinedOutput(); err != want { - return nil, nil, fmt.Errorf("Error: %v\nOutput:\n%s", err, out) - } else if err != nil { - return initramfs, nil, err + out, err := c.CombinedOutput() + t.Logf("Output:\n%s", out) + if err != nil { + return nil, nil, err } h1 := sha256.New() From 6ad4c77a2a4c171ab22eda428e6cf5b660ec02be Mon Sep 17 00:00:00 2001 From: Chris Koch Date: Fri, 23 Feb 2024 04:44:37 +0000 Subject: [PATCH 4/5] Test temp dir Signed-off-by: Chris Koch --- cmd/mkuimage/main_test.go | 50 +++++++++++++++++++++++++++++------ dependencies.go | 1 - go.mod | 3 --- go.sum | 10 ------- uimage/builder/binary_test.go | 2 -- 5 files changed, 42 insertions(+), 24 deletions(-) diff --git a/cmd/mkuimage/main_test.go b/cmd/mkuimage/main_test.go index 15bd2e8..7f1eacd 100644 --- a/cmd/mkuimage/main_test.go +++ b/cmd/mkuimage/main_test.go @@ -13,6 +13,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "strings" "syscall" "testing" @@ -23,6 +24,20 @@ import ( "golang.org/x/sync/errgroup" ) +func hasTempDir(t *testing.T, output string) { + t.Helper() + tempDir := regexp.MustCompile(`Keeping temp dir (.+)`).FindStringSubmatch(output) + if tempDir == nil { + t.Errorf("Keeping temp dir not found in output") + return + } + if fi, err := os.Stat(tempDir[1]); err != nil { + t.Error(err) + } else if !fi.IsDir() { + t.Errorf("Stat(%s) = %v, want directory", tempDir[1], fi) + } +} + func TestUrootCmdline(t *testing.T) { wd, err := os.Getwd() if err != nil { @@ -63,6 +78,7 @@ func TestUrootCmdline(t *testing.T) { args []string exitCode int validators []itest.ArchiveValidator + wantOutput func(*testing.T, string) } noCmdTests := []testCase{ @@ -153,9 +169,21 @@ func TestUrootCmdline(t *testing.T) { env: []string{"GOARCH=amd64"}, args: []string{ "-defaultsh=echo", + "github.com/u-root/u-root/cmds/core/echo", "github.com/u-root/u-root/cmds/core/init", + }, + }, + { + name: "AMD64 build with temp dir", + env: []string{"GOARCH=amd64"}, + args: []string{ + "--keep-tmp-dir", + "--defaultsh=echo", "github.com/u-root/u-root/cmds/core/echo", + "github.com/u-root/u-root/cmds/core/init", }, + exitCode: 1, + wantOutput: hasTempDir, }, { name: "ARM7 build", @@ -191,22 +219,28 @@ func TestUrootCmdline(t *testing.T) { t.Run(tt.name, func(t *testing.T) { var g errgroup.Group var f1, f2 *os.File + var out string var sum1, sum2 []byte g.Go(func() error { var err error - f1, sum1, err = buildIt(t, execPath, tt.args, tt.env, gocoverdir) + f1, out, sum1, err = buildIt(t, execPath, tt.args, tt.env, gocoverdir) return err }) g.Go(func() error { var err error - f2, sum2, err = buildIt(t, execPath, tt.args, tt.env, gocoverdir) + f2, _, sum2, err = buildIt(t, execPath, tt.args, tt.env, gocoverdir) return err }) + err := g.Wait() + if tt.wantOutput != nil { + tt.wantOutput(t, out) + } + var exitErr *exec.ExitError - if err := g.Wait(); errors.As(err, &exitErr) { + if errors.As(err, &exitErr) { if ec := exitErr.Sys().(syscall.WaitStatus).ExitStatus(); ec != tt.exitCode { t.Errorf("mkuimage exit code = %d, want %d", ec, tt.exitCode) } @@ -235,11 +269,11 @@ func TestUrootCmdline(t *testing.T) { } } -func buildIt(t *testing.T, execPath string, args, env []string, gocoverdir string) (*os.File, []byte, error) { +func buildIt(t *testing.T, execPath string, args, env []string, gocoverdir string) (*os.File, string, []byte, error) { t.Helper() initramfs, err := os.CreateTemp(t.TempDir(), "u-root-") if err != nil { - return nil, nil, err + return nil, "", nil, err } // Use the u-root command outside of the $GOPATH tree to make sure it @@ -254,14 +288,14 @@ func buildIt(t *testing.T, execPath string, args, env []string, gocoverdir strin out, err := c.CombinedOutput() t.Logf("Output:\n%s", out) if err != nil { - return nil, nil, err + return nil, string(out), nil, err } h1 := sha256.New() if _, err := io.Copy(h1, initramfs); err != nil { - return nil, nil, err + return nil, string(out), nil, err } - return initramfs, h1.Sum(nil), nil + return initramfs, string(out), h1.Sum(nil), nil } func TestCheckArgs(t *testing.T) { diff --git a/dependencies.go b/dependencies.go index 5afec72..cadf10b 100644 --- a/dependencies.go +++ b/dependencies.go @@ -9,7 +9,6 @@ package mkuimage import ( _ "github.com/u-root/u-root/cmds/core/dhclient" _ "github.com/u-root/u-root/cmds/core/echo" - _ "github.com/u-root/u-root/cmds/core/elvish" _ "github.com/u-root/u-root/cmds/core/init" _ "github.com/u-root/u-root/cmds/core/ip" _ "github.com/u-root/u-root/cmds/core/ls" diff --git a/go.mod b/go.mod index 2f12558..bb1c1b2 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,6 @@ require ( github.com/josharian/native v1.1.0 // indirect github.com/klauspost/compress v1.17.4 // indirect github.com/klauspost/pgzip v1.2.6 // indirect - github.com/mattn/go-isatty v0.0.20 // indirect github.com/mdlayher/packet v1.1.2 // indirect github.com/mdlayher/socket v0.5.0 // indirect github.com/pierrec/lz4/v4 v4.1.14 // indirect @@ -27,8 +26,6 @@ require ( github.com/ulikunitz/xz v0.5.11 // indirect github.com/vishvananda/netlink v1.2.1-beta.2 // indirect github.com/vishvananda/netns v0.0.4 // indirect - golang.org/dl v0.0.0-20240206215513-68cc34b6ca6f // indirect golang.org/x/mod v0.14.0 // indirect golang.org/x/net v0.20.0 // indirect - src.elv.sh v0.16.0-rc1.0.20220116211855-fda62502ad7f // indirect ) diff --git a/go.sum b/go.sum index 2ef9d30..bfcee98 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,5 @@ github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s= github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w= -github.com/creack/pty v1.1.15/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= github.com/creack/pty v1.1.21 h1:1/QdRyBaHHJP61QkWMXlOIBfsgdDeeKfK8SYVUWJKf0= github.com/creack/pty v1.1.21/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -25,7 +24,6 @@ github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= github.com/klauspost/pgzip v1.2.6 h1:8RXeL5crjEUFnR2/Sn6GJNWtSQ3Dk8pq4CL3jvdDyjU= github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs= -github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mdlayher/packet v1.1.2 h1:3Up1NG6LZrsgDVn6X4L9Ge/iyRyxFEFD9o6Pr3Q1nQY= @@ -55,10 +53,6 @@ github.com/vishvananda/netlink v1.2.1-beta.2/go.mod h1:twkDnbuQxJYemMlGd4JFIcuhg github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0= github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8= github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM= -go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU= -go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4= -golang.org/dl v0.0.0-20240206215513-68cc34b6ca6f h1:IWM5GianvyTx750h5TRfWms43pREUt1gQ0ZdVQdoiOM= -golang.org/dl v0.0.0-20240206215513-68cc34b6ca6f/go.mod h1:fwQ+hlTD8I6TIzOGkQqxQNfE2xqR+y7SzGaDkksVFkw= golang.org/x/exp v0.0.0-20231219180239-dc181d75b848 h1:+iq7lrkxmFNBM7xx+Rae2W6uyPfhPeDWD+n+JgppptE= golang.org/x/exp v0.0.0-20231219180239-dc181d75b848/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI= golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= @@ -69,10 +63,6 @@ golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20200217220822-9197077df867/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc= diff --git a/uimage/builder/binary_test.go b/uimage/builder/binary_test.go index 8b881c7..2697684 100644 --- a/uimage/builder/binary_test.go +++ b/uimage/builder/binary_test.go @@ -18,7 +18,6 @@ func TestBinaryBuild(t *testing.T) { Env: golang.Default(golang.DisableCGO()), Packages: []string{ "../../cmd/mkuimage", - "github.com/u-root/u-root/cmds/core/elvish", "github.com/u-root/u-root/cmds/core/init", "cmd/test2json", }, @@ -31,7 +30,6 @@ func TestBinaryBuild(t *testing.T) { } mustContain := []string{ - "bin/elvish", "bin/mkuimage", "bin/test2json", "bin/init", From e84d29fb8a070f680dd34116a9792c2fedfb2200 Mon Sep 17 00:00:00 2001 From: Chris Koch Date: Fri, 23 Feb 2024 05:05:52 +0000 Subject: [PATCH 5/5] More temp dir tests Signed-off-by: Chris Koch --- cmd/mkuimage/main_test.go | 52 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/cmd/mkuimage/main_test.go b/cmd/mkuimage/main_test.go index 7f1eacd..f53a47d 100644 --- a/cmd/mkuimage/main_test.go +++ b/cmd/mkuimage/main_test.go @@ -38,6 +38,17 @@ func hasTempDir(t *testing.T, output string) { } } +func dirExists(name string) func(t *testing.T, output string) { + return func(t *testing.T, output string) { + t.Helper() + if fi, err := os.Stat(name); err != nil { + t.Error(err) + } else if !fi.IsDir() { + t.Errorf("Stat(%s) = %v, want directory", name, fi) + } + } +} + func TestUrootCmdline(t *testing.T) { wd, err := os.Getwd() if err != nil { @@ -71,6 +82,7 @@ func TestUrootCmdline(t *testing.T) { if err = os.WriteFile(filepath.Join(sampledir, "bar"), nil, 0o644); err != nil { t.Fatal(err) } + tempDir := filepath.Join(t.TempDir(), "tempdir") type testCase struct { name string @@ -212,6 +224,45 @@ func TestUrootCmdline(t *testing.T) { "github.com/u-root/u-root/cmds/core/echo", }, }, + { + name: "build invalid", + args: []string{ + "-build=source", + "github.com/u-root/u-root/cmds/core/init", + "github.com/u-root/u-root/cmds/core/echo", + }, + exitCode: 1, + }, + { + name: "build invalid", + args: []string{ + "-build=source", + "github.com/u-root/u-root/cmds/core/init", + "github.com/u-root/u-root/cmds/core/echo", + }, + exitCode: 1, + }, + { + name: "arch invalid preserves temp dir", + env: []string{"GOARCH=doesnotexist"}, + args: []string{ + "--defaultsh=echo", + "github.com/u-root/u-root/cmds/core/echo", + "github.com/u-root/u-root/cmds/core/init", + }, + exitCode: 1, + wantOutput: hasTempDir, + }, + { + name: "specify temp dir", + args: []string{ + "--tmp-dir=" + tempDir, + "github.com/u-root/u-root/cmds/core/echo", + "github.com/u-root/u-root/cmds/core/init", + }, + exitCode: 1, + wantOutput: dirExists(tempDir), + }, } for _, tt := range append(noCmdTests, bareTests...) { @@ -283,7 +334,6 @@ func buildIt(t *testing.T, execPath string, args, env []string, gocoverdir strin c := exec.Command(execPath, args...) c.Env = append(os.Environ(), env...) - c.Env = append(c.Env, golang.Default().Env()...) c.Env = append(c.Env, "GOCOVERDIR="+gocoverdir) out, err := c.CombinedOutput() t.Logf("Output:\n%s", out)