Skip to content

Commit

Permalink
Add a failing test for golang/go#5128.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitshur committed Jan 31, 2015
1 parent 0ff5fe1 commit 821ff3c
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions 101/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"go/format"
"io/ioutil"
"os"
"os/exec"
"testing"
)

func Test(t *testing.T) {
in := []byte(`package main
import "fmt"
// An example of gofmt not being idempotent.
// Repeatedly invoking gofmt will add a level of indent to the b string line each time.
type Foo struct {
/*a string
b string*/
c string
}
func main() {
fmt.Println("Hello, playground")
}
`)

out, err := format.Source(in)
if err != nil {
panic(err)
}

if string(in) != string(out) {
diff, err := diff(in, out)
if err != nil {
panic(err)
}
t.Errorf("diff:\n%s\n", diff)
}
}

func diff(b1, b2 []byte) (data []byte, err error) {
f1, err := ioutil.TempFile("", "")
if err != nil {
return
}
defer os.Remove(f1.Name())
defer f1.Close()

f2, err := ioutil.TempFile("", "")
if err != nil {
return
}
defer os.Remove(f2.Name())
defer f2.Close()

f1.Write(b1)
f2.Write(b2)

data, err = exec.Command("diff", "-u", f1.Name(), f2.Name()).CombinedOutput()
if len(data) > 0 {
// diff exits with a non-zero status when the files don't match.
// Ignore that failure as long as we get output.
err = nil
}
return
}

0 comments on commit 821ff3c

Please sign in to comment.