From b7c8a85330f1b5164ab2e20931b666e96860011d Mon Sep 17 00:00:00 2001 From: konoui Date: Wed, 22 May 2024 18:02:45 +0900 Subject: [PATCH] update output --- cmd/execute.go | 11 ++------ pkg/lipo/detailed_info.go | 14 +++++----- pkg/lipo/detailed_info_test.go | 28 ++++++++----------- pkg/lipo/info.go | 12 ++++++--- pkg/lipo/info_test.go | 49 ++++++++++++++++------------------ pkg/testlipo/testlipo.go | 2 +- 6 files changed, 52 insertions(+), 64 deletions(-) diff --git a/cmd/execute.go b/cmd/execute.go index aa67225..3b1aee1 100644 --- a/cmd/execute.go +++ b/cmd/execute.go @@ -181,17 +181,10 @@ func Execute(stdout, stderr io.Writer, args []string) (exitCode int) { fmt.Fprintln(stdout, strings.Join(arches, " ")) return case "info": - v, err := l.Info() - if err != nil { - return fatal(stderr, err.Error()) - } - fmt.Fprintln(stdout, strings.Join(v, "\n")) + l.Info(stdout, stderr) return case "detailed_info": - err := l.DetailedInfo(stdout) - if err != nil { - return fatal(stderr, err.Error()) - } + l.DetailedInfo(stdout, stderr) return case "verify_arch": ok, err := l.VerifyArch(verifyArch.Get()...) diff --git a/pkg/lipo/detailed_info.go b/pkg/lipo/detailed_info.go index 3bb94d1..0c2ac98 100644 --- a/pkg/lipo/detailed_info.go +++ b/pkg/lipo/detailed_info.go @@ -1,7 +1,6 @@ package lipo import ( - "bytes" "fmt" "io" "strings" @@ -27,18 +26,20 @@ architecture {{ .Arch }} var tpl = template.Must(template.New("detailed_info").Parse(detailedInfoTpl)) -func (l *Lipo) DetailedInfo(w io.Writer) error { +func (l *Lipo) DetailedInfo(stdout, stderr io.Writer) { if len(l.in) == 0 { - return errNoInput + fmt.Fprintln(stderr, "fatal error: "+errNoInput.Error()) + return } - var out bytes.Buffer + var out strings.Builder thin := []string{} for _, bin := range l.in { v, isFat, err := detailedInfo(bin) if err != nil { - return err + fmt.Fprintln(stderr, "fatal error: "+err.Error()) + return } if isFat { out.WriteString(v) @@ -52,8 +53,7 @@ func (l *Lipo) DetailedInfo(w io.Writer) error { out.WriteString(strings.Join(thin, "\n") + "\n") } - _, err := w.Write(out.Bytes()) - return err + fmt.Fprint(stdout, out.String()) } type tplFatArch struct { diff --git a/pkg/lipo/detailed_info_test.go b/pkg/lipo/detailed_info_test.go index a8d7ed0..bcd1f8c 100644 --- a/pkg/lipo/detailed_info_test.go +++ b/pkg/lipo/detailed_info_test.go @@ -48,10 +48,7 @@ func TestLipo_DetailedInfo(t *testing.T) { l := lipo.New(lipo.WithInputs(args...)) got := &bytes.Buffer{} - err := l.DetailedInfo(got) - if err != nil { - t.Fatal(err) - } + l.DetailedInfo(got, got) want := p.DetailedInfo(t, args...) if want != got.String() { @@ -63,14 +60,13 @@ func TestLipo_DetailedInfo(t *testing.T) { func TestLipo_DetailedInfoWithError(t *testing.T) { t.Run("not found", func(t *testing.T) { - err := lipo.New(lipo.WithInputs("not-found")).DetailedInfo(io.Discard) - if err == nil { - t.Error("should occur error") - return - } + stderr := &bytes.Buffer{} + lipo.New(lipo.WithInputs("not-found")).DetailedInfo(io.Discard, stderr) + + got := stderr.String() + want := "open not-found: no such file or directory" - got := err.Error() - if got != want { + if !strings.Contains(got, want) { t.Errorf("want: %s, got: %s", want, got) } }) @@ -82,16 +78,14 @@ func TestLipo_DetailedInfoWithError(t *testing.T) { defer f.Close() input := f.Name() - err = lipo.New(lipo.WithInputs(input)).DetailedInfo(io.Discard) - if err == nil { - t.Error("should occur error") - return - } + + stderr := &bytes.Buffer{} + lipo.New(lipo.WithInputs(input)).DetailedInfo(io.Discard, stderr) tl := testlipo.NewLipoBin(t, testlipo.WithIgnoreErr(true)) want := "can't figure out the architecture type of: not-binary" got1 := tl.DetailedInfo(t, input) - got2 := err.Error() + got2 := stderr.String() if !strings.Contains(got1, want) { t.Errorf("want: %s, got1: %s", want, got1) } diff --git a/pkg/lipo/info.go b/pkg/lipo/info.go index 8fbb8c6..75ebb9b 100644 --- a/pkg/lipo/info.go +++ b/pkg/lipo/info.go @@ -2,12 +2,14 @@ package lipo import ( "fmt" + "io" "strings" ) -func (l *Lipo) Info() ([]string, error) { +func (l *Lipo) Info(stdout, stderr io.Writer) { if len(l.in) == 0 { - return nil, errNoInput + fmt.Fprintln(stderr, "fatal error: "+errNoInput.Error()) + return } fat := make([]string, 0, len(l.in)) @@ -15,7 +17,8 @@ func (l *Lipo) Info() ([]string, error) { for _, bin := range l.in { v, typ, err := info(bin) if err != nil { - return nil, err + fmt.Fprintln(stderr, err.Error()) + return } if typ == inspectFat { fat = append(fat, v) @@ -24,7 +27,8 @@ func (l *Lipo) Info() ([]string, error) { } } - return append(fat, thin...), nil + out := strings.Join(append(fat, thin...), "\n") + fmt.Fprintln(stdout, out) } func info(bin string) (string, inspectType, error) { diff --git a/pkg/lipo/info_test.go b/pkg/lipo/info_test.go index a07b0cb..f1f629a 100644 --- a/pkg/lipo/info_test.go +++ b/pkg/lipo/info_test.go @@ -1,6 +1,8 @@ package lipo_test import ( + "bytes" + "io" "strings" "testing" @@ -13,13 +15,12 @@ func TestLipo_Info(t *testing.T) { p := testlipo.Setup(t, bm, []string{"arm64", "arm64e", "x86_64"}) ins := []string{p.Bin(t, "arm64"), p.FatBin, p.Bin(t, "arm64e")} l := lipo.New(lipo.WithInputs(ins...)) - info, err := l.Info() - if err != nil { - t.Fatal(err) - } - want := strings.Join(info, "\n") - got := p.Info(t, ins...) + stdout := &bytes.Buffer{} + l.Info(stdout, stdout) + got := stdout.String() + + want := p.Info(t, ins...) if want != got { t.Errorf("\nwant:\n%s\ngot:\n%s", want, got) } @@ -29,13 +30,12 @@ func TestLipo_Info(t *testing.T) { p := testlipo.Setup(t, bm, []string{"arm64"}) ins := []string{p.FatBin} l := lipo.New(lipo.WithInputs(ins...)) - info, err := l.Info() - if err != nil { - t.Fatal(err) - } - want := strings.Join(info, "\n") - got := p.Info(t, ins...) + stdout := &bytes.Buffer{} + l.Info(stdout, stdout) + got := stdout.String() + + want := p.Info(t, ins...) if want != got { t.Errorf("\nwant:\n%s\ngot:\n%s", want, got) } @@ -44,14 +44,13 @@ func TestLipo_Info(t *testing.T) { t.Run("archive-object", func(t *testing.T) { in := "../ar/testdata/arm64-func12.a" l := lipo.New(lipo.WithInputs(in)) - info, err := l.Info() - if err != nil { - t.Fatal(err) - } - want := strings.Join(info, "\n") - tl := testlipo.NewLipoBin(t) - got := tl.Info(t, in) + + stdout := &bytes.Buffer{} + l.Info(stdout, stdout) + got := stdout.String() + + want := tl.Info(t, in) if want != got { t.Errorf("want %s, got %s", want, got) @@ -61,17 +60,15 @@ func TestLipo_Info(t *testing.T) { t.Run("invalid-archive-object", func(t *testing.T) { in := "../ar/testdata/arm64-amd64-func12.a" l := lipo.New(lipo.WithInputs(in)) - _, err := l.Info() - if err == nil { - t.Fatal("error is nil") - } + stderr := &bytes.Buffer{} + l.Info(io.Discard, stderr) - want := err.Error() + got := stderr.String() tl := testlipo.NewLipoBin(t, testlipo.WithIgnoreErr(true)) - got := tl.Info(t, in) + want := tl.Info(t, in) - if !strings.HasSuffix(got, want) { + if !strings.Contains(want, got) { t.Errorf("\nwant: %s\ngot: %s", want, got) } }) diff --git a/pkg/testlipo/testlipo.go b/pkg/testlipo/testlipo.go index cfa813c..1f995f0 100644 --- a/pkg/testlipo/testlipo.go +++ b/pkg/testlipo/testlipo.go @@ -159,7 +159,7 @@ func (l *LipoBin) Info(t *testing.T, bins ...string) string { vvs := util.Map(vs, func(s string) string { return strings.TrimSuffix(strings.TrimSuffix(s, "\n"), " ") }) - return strings.Join(vvs, "\n") + return strings.Join(vvs, "\n") + "\n" } func (l *LipoBin) Create(t *testing.T, out string, inputs ...string) {