Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove testutil package #2

Merged
merged 6 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ jobs:
- name: Test
run: go test -v -covermode atomic -coverpkg ./... -coverprofile cover.out ./...

- name: Convert GOCOVERDIR coverage data
run: go tool covdata textfmt -i=cmd/mkuimage/cover -o cmdcover.out

- uses: codecov/codecov-action@v4-beta
env:
CODECOV_TOKEN: '804457b0-03f8-4cf6-bc99-eaf43399177b'
with:
flags: ${{ matrix.platform }}-unit
fail_ci_if_error: true
verbose: true
files: cover.out,cmdcover.out

race:
name: Race test
Expand Down
57 changes: 39 additions & 18 deletions cmd/mkuimage/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"

"github.com/u-root/gobusybox/src/pkg/golang"
"github.com/u-root/mkuimage/cpio"
"github.com/u-root/mkuimage/testutil"
itest "github.com/u-root/mkuimage/uroot/initramfs/test"
)

Expand All @@ -27,12 +28,31 @@ var twocmds = []string{
}

func TestUrootCmdline(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}

execPath := filepath.Join(t.TempDir(), "binary")
// Build the stuff.
// NoTrimPath ensures that the right Go version is used when running the tests.
goEnv := golang.Default()
if err := goEnv.BuildDir(wd, execPath, &golang.BuildOpts{NoStrip: true, NoTrimPath: true, ExtraArgs: []string{"-cover"}}); err != nil {
t.Fatal(err)
}

gocoverdir := filepath.Join(wd, "cover")
if err := os.Mkdir(gocoverdir, 0o777); err != nil && !os.IsNotExist(err) {
t.Fatal(err)
}

samplef, err := os.CreateTemp("", "u-root-test-")
if err != nil {
t.Fatal(err)
}
samplef.Close()
defer os.RemoveAll(samplef.Name())

sampledir := t.TempDir()
if err = os.WriteFile(filepath.Join(sampledir, "foo"), nil, 0o644); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -232,7 +252,7 @@ func TestUrootCmdline(t *testing.T) {
wg.Add(2)
go func() {
defer wg.Done()
f1, sum1, err = buildIt(t, tt.args, tt.env, tt.err)
f1, sum1, err = buildIt(t, execPath, tt.args, tt.env, tt.err, gocoverdir)
if err != nil {
errs[0] = err
return
Expand All @@ -257,7 +277,7 @@ func TestUrootCmdline(t *testing.T) {
go func() {
defer wg.Done()
var err error
f2, sum2, err = buildIt(t, tt.args, tt.env, tt.err)
f2, sum2, err = buildIt(t, execPath, tt.args, tt.env, tt.err, gocoverdir)
if err != nil {
errs[1] = err
return
Expand Down Expand Up @@ -293,32 +313,33 @@ func TestUrootCmdline(t *testing.T) {
}
}

func buildIt(t *testing.T, args, env []string, want error) (*os.File, []byte, error) {
func buildIt(t *testing.T, execPath string, args, env []string, want error, gocoverdir string) (*os.File, []byte, error) {
t.Helper()
f, err := os.CreateTemp("", "u-root-")
initramfs, err := os.CreateTemp(t.TempDir(), "u-root-")
if err != nil {
return nil, nil, err
}

// Use the u-root command outside of the $GOPATH tree to make sure it
// still works.
arg := append([]string{"-o", f.Name()}, args...)
c := testutil.Command(t, arg...)
t.Logf("Commandline: %v u-root %v", strings.Join(env, " "), strings.Join(arg, " "))
c.Env = append(c.Env, env...)
args = append([]string{"-o", initramfs.Name()}, args...)
t.Logf("Commandline: %v mkuimage %v", strings.Join(env, " "), strings.Join(args, " "))

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)
if out, err := c.CombinedOutput(); err != want {
return nil, nil, fmt.Errorf("Error: %v\nOutput:\n%s", err, out)
} else if err != nil {
h1 := sha256.New()
if _, err := io.Copy(h1, f); err != nil {
return nil, nil, err
}
return f, h1.Sum(nil), nil
return initramfs, nil, err
}
return f, nil, nil
}

func TestMain(m *testing.M) {
testutil.Run(m, main)
h1 := sha256.New()
if _, err := io.Copy(h1, initramfs); err != nil {
return nil, nil, err
}
return initramfs, h1.Sum(nil), nil
}

func TestCheckArgs(t *testing.T) {
Expand Down
108 changes: 0 additions & 108 deletions testutil/testutil.go

This file was deleted.

Loading