2019-04-07 21:08:47 -06:00
|
|
|
// Copyright 2019 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.
|
|
|
|
|
2019-09-18 21:51:24 -06:00
|
|
|
package cmdtest
|
2019-04-07 21:08:47 -06:00
|
|
|
|
|
|
|
import (
|
2019-09-23 20:50:50 -06:00
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-04-07 21:08:47 -06:00
|
|
|
"os/exec"
|
2019-04-26 10:45:14 -06:00
|
|
|
"regexp"
|
2019-04-07 21:08:47 -06:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2019-09-26 11:56:23 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-10-07 20:20:05 -06:00
|
|
|
"golang.org/x/tools/internal/testenv"
|
2019-04-07 21:08:47 -06:00
|
|
|
)
|
|
|
|
|
2019-09-26 11:56:23 -06:00
|
|
|
func (r *runner) Format(t *testing.T, spn span.Span) {
|
2019-09-23 20:50:50 -06:00
|
|
|
tag := "gofmt"
|
|
|
|
uri := spn.URI()
|
|
|
|
filename := uri.Filename()
|
|
|
|
expect := string(r.data.Golden(tag, filename, func() ([]byte, error) {
|
|
|
|
cmd := exec.Command("gofmt", filename)
|
|
|
|
contents, _ := cmd.Output() // ignore error, sometimes we have intentionally ungofmt-able files
|
2019-11-21 17:58:38 -07:00
|
|
|
contents = []byte(r.Normalize(fixFileHeader(string(contents))))
|
2019-09-23 20:50:50 -06:00
|
|
|
return contents, nil
|
|
|
|
}))
|
|
|
|
if expect == "" {
|
|
|
|
//TODO: our error handling differs, for now just skip unformattable files
|
|
|
|
t.Skip("Unformattable file")
|
2019-04-07 21:08:47 -06:00
|
|
|
}
|
2019-11-21 17:58:38 -07:00
|
|
|
got, _ := r.NormalizeGoplsCmd(t, "format", filename)
|
2019-09-23 20:50:50 -06:00
|
|
|
if expect != got {
|
|
|
|
t.Errorf("format failed for %s expected:\n%s\ngot:\n%s", filename, expect, got)
|
|
|
|
}
|
|
|
|
// now check we can build a valid unified diff
|
2019-11-21 17:58:38 -07:00
|
|
|
unified, _ := r.NormalizeGoplsCmd(t, "format", "-d", filename)
|
2019-09-23 20:50:50 -06:00
|
|
|
checkUnified(t, filename, expect, unified)
|
2019-04-07 21:08:47 -06:00
|
|
|
}
|
|
|
|
|
2019-04-26 10:45:14 -06:00
|
|
|
var unifiedHeader = regexp.MustCompile(`^diff -u.*\n(---\s+\S+\.go\.orig)\s+[\d-:. ]+(\n\+\+\+\s+\S+\.go)\s+[\d-:. ]+(\n@@)`)
|
|
|
|
|
|
|
|
func fixFileHeader(s string) string {
|
|
|
|
match := unifiedHeader.FindStringSubmatch(s)
|
|
|
|
if match == nil {
|
|
|
|
return s
|
2019-04-07 21:08:47 -06:00
|
|
|
}
|
2019-04-26 10:45:14 -06:00
|
|
|
return strings.Join(append(match[1:], s[len(match[0]):]), "")
|
2019-04-07 21:08:47 -06:00
|
|
|
}
|
2019-09-23 20:50:50 -06:00
|
|
|
|
|
|
|
func checkUnified(t *testing.T, filename string, expect string, patch string) {
|
2019-10-07 20:20:05 -06:00
|
|
|
testenv.NeedsTool(t, "patch")
|
2019-09-23 20:50:50 -06:00
|
|
|
if strings.Count(patch, "\n+++ ") > 1 {
|
|
|
|
// TODO(golang/go/#34580)
|
|
|
|
t.Skip("multi-file patch tests not supported yet")
|
|
|
|
}
|
|
|
|
applied := ""
|
|
|
|
if patch == "" {
|
|
|
|
applied = expect
|
|
|
|
} else {
|
|
|
|
temp, err := ioutil.TempFile("", "applied")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
temp.Close()
|
|
|
|
defer os.Remove(temp.Name())
|
|
|
|
cmd := exec.Command("patch", "-u", "-p0", "-o", temp.Name(), filename)
|
|
|
|
cmd.Stdin = bytes.NewBuffer([]byte(patch))
|
|
|
|
msg, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed applying patch to %s: %v\ngot:\n%s\npatch:\n%s", filename, err, msg, patch)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
out, err := ioutil.ReadFile(temp.Name())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed reading patched output for %s: %v\n", filename, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
applied = string(out)
|
|
|
|
}
|
|
|
|
if expect != applied {
|
|
|
|
t.Errorf("apply unified gave wrong result for %s expected:\n%s\ngot:\n%s\npatch:\n%s", filename, expect, applied, patch)
|
|
|
|
}
|
|
|
|
}
|