Skip to content

Commit

Permalink
update output
Browse files Browse the repository at this point in the history
  • Loading branch information
konoui committed May 22, 2024
1 parent 43305be commit b7c8a85
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 64 deletions.
11 changes: 2 additions & 9 deletions cmd/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()...)
Expand Down
14 changes: 7 additions & 7 deletions pkg/lipo/detailed_info.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lipo

import (
"bytes"
"fmt"
"io"
"strings"
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
28 changes: 11 additions & 17 deletions pkg/lipo/detailed_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
}
})
Expand All @@ -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)
}
Expand Down
12 changes: 8 additions & 4 deletions pkg/lipo/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ 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))
thin := make([]string, 0, len(l.in))
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)
Expand All @@ -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) {
Expand Down
49 changes: 23 additions & 26 deletions pkg/lipo/info_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package lipo_test

import (
"bytes"
"io"
"strings"
"testing"

Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
}
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/testlipo/testlipo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit b7c8a85

Please sign in to comment.