Commit 4defa8fc authored by Quentin Smith's avatar Quentin Smith

all: use Windows-supporting diff function

With this CL, the perf repo should now pass all tests on Windows.

Change-Id: I91fead1e065f22b54236ff96c6ace529dc3964a8
Reviewed-on: https://go-review.googlesource.com/36627
Run-TryBot: Quentin Smith <quentin@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRuss Cox <rsc@golang.org>
parent 07bde3b0
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package diff
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"runtime"
)
// Diff returns a human-readable description of the differences between s1 and s2.
// If the "diff" command is available, it returns the output of unified diff on s1 and s2.
// If the result is non-empty, the strings differ or the diff command failed.
func Diff(s1, s2 string) string {
if s1 == s2 {
return ""
}
if _, err := exec.LookPath("diff"); err != nil {
return fmt.Sprintf("diff command unavailable\nold: %q\nnew: %q", s1, s2)
}
f1, err := ioutil.TempFile("", "benchfmt_test")
if err != nil {
return err.Error()
}
defer os.Remove(f1.Name())
defer f1.Close()
f2, err := ioutil.TempFile("", "benchfmt_test")
if err != nil {
return err.Error()
}
defer os.Remove(f2.Name())
defer f2.Close()
f1.Write([]byte(s1))
f2.Write([]byte(s2))
cmd := "diff"
if runtime.GOOS == "plan9" {
cmd = "/bin/ape/diff"
}
data, err := exec.Command(cmd, "-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
}
if err != nil {
data = append(data, []byte(err.Error())...)
}
return string(data)
}
......@@ -7,12 +7,11 @@ package benchfmt
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"reflect"
"strings"
"testing"
"golang.org/x/perf/internal/diff"
)
func readAllResults(t *testing.T, r *Reader) []*Result {
......@@ -176,42 +175,9 @@ BenchmarkOne 1 ns/sec
t.Errorf("Print returned %v", err)
}
}
if diff := diff(have.String(), test.want); diff != "" {
if diff := diff.Diff(have.String(), test.want); diff != "" {
t.Errorf("wrong output: (- got/+ want)\n%s", diff)
}
})
}
}
// diff returns the output of unified diff on s1 and s2. If the result
// is non-empty, the strings differ or the diff command failed.
func diff(s1, s2 string) string {
f1, err := ioutil.TempFile("", "benchfmt_test")
if err != nil {
return err.Error()
}
defer os.Remove(f1.Name())
defer f1.Close()
f2, err := ioutil.TempFile("", "benchfmt_test")
if err != nil {
return err.Error()
}
defer os.Remove(f2.Name())
defer f2.Close()
f1.Write([]byte(s1))
f2.Write([]byte(s2))
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
}
if err != nil {
data = append(data, []byte(err.Error())...)
}
return string(data)
}
......@@ -7,14 +7,12 @@ package storage
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"reflect"
"testing"
"golang.org/x/perf/internal/diff"
"golang.org/x/perf/storage/benchfmt"
)
......@@ -63,7 +61,7 @@ func TestQuery(t *testing.T) {
t.Fatalf("Err: %v", err)
}
want := "key: value\nBenchmarkOne 5 ns/op\nkey: value2\nBenchmarkTwo 10 ns/op\n"
if diff := diff(buf.String(), want); diff != "" {
if diff := diff.Diff(buf.String(), want); diff != "" {
t.Errorf("wrong results: (- have/+ want)\n%s", diff)
}
}
......@@ -92,36 +90,3 @@ func TestListUploads(t *testing.T) {
t.Fatalf("Err: %v", err)
}
}
// diff returns the output of unified diff on s1 and s2. If the result
// is non-empty, the strings differ or the diff command failed.
func diff(s1, s2 string) string {
f1, err := ioutil.TempFile("", "benchfmt_test")
if err != nil {
return err.Error()
}
defer os.Remove(f1.Name())
defer f1.Close()
f2, err := ioutil.TempFile("", "benchfmt_test")
if err != nil {
return err.Error()
}
defer os.Remove(f2.Name())
defer f2.Close()
f1.Write([]byte(s1))
f2.Write([]byte(s2))
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
}
if err != nil {
data = append(data, []byte(err.Error())...)
}
return string(data)
}
......@@ -8,9 +8,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"reflect"
"sort"
"strconv"
......@@ -18,6 +15,7 @@ import (
"testing"
"time"
"golang.org/x/perf/internal/diff"
"golang.org/x/perf/storage/benchfmt"
. "golang.org/x/perf/storage/db"
"golang.org/x/perf/storage/db/dbtest"
......@@ -84,7 +82,7 @@ func checkQueryResults(t *testing.T, db *DB, query, results string) {
if err := q.Err(); err != nil {
t.Fatalf("Err: %v", err)
}
if diff := diff(buf.String(), results); diff != "" {
if diff := diff.Diff(buf.String(), results); diff != "" {
t.Errorf("wrong results: (- have/+ want)\n%s", diff)
}
}
......@@ -329,39 +327,6 @@ func TestQuery(t *testing.T) {
}
}
// diff returns the output of unified diff on s1 and s2. If the result
// is non-empty, the strings differ or the diff command failed.
func diff(s1, s2 string) string {
f1, err := ioutil.TempFile("", "benchfmt_test")
if err != nil {
return err.Error()
}
defer os.Remove(f1.Name())
defer f1.Close()
f2, err := ioutil.TempFile("", "benchfmt_test")
if err != nil {
return err.Error()
}
defer os.Remove(f2.Name())
defer f2.Close()
f1.Write([]byte(s1))
f2.Write([]byte(s2))
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
}
if err != nil {
data = append(data, []byte(err.Error())...)
}
return string(data)
}
// TestListUploads verifies that ListUploads returns the correct values.
func TestListUploads(t *testing.T) {
SetNow(time.Unix(0, 0))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment