From 22acba87ddc4f94756beb75927c043a0a796635b Mon Sep 17 00:00:00 2001 From: Chris Koch Date: Fri, 23 Feb 2024 05:05:52 +0000 Subject: [PATCH] More temp dir tests Signed-off-by: Chris Koch --- cmd/mkuimage/main_test.go | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/cmd/mkuimage/main_test.go b/cmd/mkuimage/main_test.go index 7f1eacd..45033e8 100644 --- a/cmd/mkuimage/main_test.go +++ b/cmd/mkuimage/main_test.go @@ -38,6 +38,16 @@ func hasTempDir(t *testing.T, output string) { } } +func dirExists(name string) func(t *testing.T, output string) { + return func(t *testing.T, output string) { + 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 +81,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 +223,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{ + "--temp-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...) {