github.com/jd-ly/tools@v0.5.7/internal/lsp/cmd/test/format.go (about) 1 // Copyright 2019 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package cmdtest 6 7 import ( 8 "bytes" 9 "io/ioutil" 10 "os" 11 "os/exec" 12 "regexp" 13 "strings" 14 "testing" 15 16 "github.com/jd-ly/tools/internal/span" 17 "github.com/jd-ly/tools/internal/testenv" 18 ) 19 20 func (r *runner) Format(t *testing.T, spn span.Span) { 21 tag := "gofmt" 22 uri := spn.URI() 23 filename := uri.Filename() 24 expect := string(r.data.Golden(tag, filename, func() ([]byte, error) { 25 cmd := exec.Command("gofmt", filename) 26 contents, _ := cmd.Output() // ignore error, sometimes we have intentionally ungofmt-able files 27 contents = []byte(r.Normalize(fixFileHeader(string(contents)))) 28 return contents, nil 29 })) 30 if expect == "" { 31 //TODO: our error handling differs, for now just skip unformattable files 32 t.Skip("Unformattable file") 33 } 34 got, _ := r.NormalizeGoplsCmd(t, "format", filename) 35 if expect != got { 36 t.Errorf("format failed for %s expected:\n%s\ngot:\n%s", filename, expect, got) 37 } 38 // now check we can build a valid unified diff 39 unified, _ := r.NormalizeGoplsCmd(t, "format", "-d", filename) 40 checkUnified(t, filename, expect, unified) 41 } 42 43 var unifiedHeader = regexp.MustCompile(`^diff -u.*\n(---\s+\S+\.go\.orig)\s+[\d-:. ]+(\n\+\+\+\s+\S+\.go)\s+[\d-:. ]+(\n@@)`) 44 45 func fixFileHeader(s string) string { 46 match := unifiedHeader.FindStringSubmatch(s) 47 if match == nil { 48 return s 49 } 50 return strings.Join(append(match[1:], s[len(match[0]):]), "") 51 } 52 53 func checkUnified(t *testing.T, filename string, expect string, patch string) { 54 testenv.NeedsTool(t, "patch") 55 if strings.Count(patch, "\n+++ ") > 1 { 56 // TODO(golang/go/#34580) 57 t.Skip("multi-file patch tests not supported yet") 58 } 59 applied := "" 60 if patch == "" { 61 applied = expect 62 } else { 63 temp, err := ioutil.TempFile("", "applied") 64 if err != nil { 65 t.Fatal(err) 66 } 67 temp.Close() 68 defer os.Remove(temp.Name()) 69 cmd := exec.Command("patch", "-u", "-p0", "-o", temp.Name(), filename) 70 cmd.Stdin = bytes.NewBuffer([]byte(patch)) 71 msg, err := cmd.CombinedOutput() 72 if err != nil { 73 t.Errorf("failed applying patch to %s: %v\ngot:\n%s\npatch:\n%s", filename, err, msg, patch) 74 return 75 } 76 out, err := ioutil.ReadFile(temp.Name()) 77 if err != nil { 78 t.Errorf("failed reading patched output for %s: %v\n", filename, err) 79 return 80 } 81 applied = string(out) 82 } 83 if expect != applied { 84 t.Errorf("apply unified gave wrong result for %s expected:\n%s\ngot:\n%s\npatch:\n%s", filename, expect, applied, patch) 85 } 86 }