From 7c437e90f3f99a3d0ee3acc0de0899f7afe37ea9 Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Thu, 8 Jun 2017 14:24:20 +0200 Subject: [PATCH] gopy: add test for gofmt'd code --- main_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/main_test.go b/main_test.go index 755beac..47d91c1 100644 --- a/main_test.go +++ b/main_test.go @@ -14,6 +14,35 @@ import ( "testing" ) +func TestGofmt(t *testing.T) { + exe, err := exec.LookPath("goimports") + if err != nil { + switch e := err.(type) { + case *exec.Error: + if e.Err == exec.ErrNotFound { + exe, err = exec.LookPath("gofmt") + } + } + } + if err != nil { + t.Fatal(err) + } + + cmd := exec.Command(exe, "-d", ".") + buf := new(bytes.Buffer) + cmd.Stdout = buf + cmd.Stderr = buf + + err = cmd.Run() + if err != nil { + t.Fatalf("error running %s:\n%s\n%v", exe, string(buf.Bytes()), err) + } + + if len(buf.Bytes()) != 0 { + t.Errorf("some files were not gofmt'ed:\n%s\n", string(buf.Bytes())) + } +} + type pkg struct { path string want []byte