mirror of
https://github.com/golang/go
synced 2024-11-18 19:04:40 -07:00
5adc211bf7
In it previous implementation, tool.Main was meant to be called from an applications main(). If it encountered an error it would print the error to standard error and exit with a non-zero status (2). It is also called recursively and in various test functions. Exiting on an error makes testing difficult, unnecessarily. This change breaks the functionality into to parts: an outer tool.MustMain() that is intended to be called by main() functions and an inner Main that is used by MustMain() and by test functions. None of the existing test functions use Main()'s error value, but the failure case tests for the command line invocation of rename (#194878) require it. Fixes #34291 Change-Id: Id0d80fc4670d56c87398b86b1ad9fdf7a676c95b GitHub-Last-Rev: cd64995c91c94b997754c8d8b1004afc488bf8b7 GitHub-Pull-Request: golang/tools#159 Reviewed-on: https://go-review.googlesource.com/c/tools/+/195338 Reviewed-by: Ian Cottrell <iancottrell@google.com> Run-TryBot: Ian Cottrell <iancottrell@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
62 lines
1.8 KiB
Go
62 lines
1.8 KiB
Go
// 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.
|
|
|
|
package cmd_test
|
|
|
|
import (
|
|
"os/exec"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
|
|
"golang.org/x/tools/internal/lsp/cmd"
|
|
"golang.org/x/tools/internal/lsp/tests"
|
|
"golang.org/x/tools/internal/tool"
|
|
)
|
|
|
|
var formatModes = [][]string{
|
|
[]string{},
|
|
[]string{"-d"},
|
|
}
|
|
|
|
func (r *runner) Format(t *testing.T, data tests.Formats) {
|
|
for _, spn := range data {
|
|
for _, mode := range formatModes {
|
|
tag := "gofmt" + strings.Join(mode, "")
|
|
uri := spn.URI()
|
|
filename := uri.Filename()
|
|
args := append(mode, filename)
|
|
expect := string(r.data.Golden(tag, filename, func() ([]byte, error) {
|
|
cmd := exec.Command("gofmt", args...)
|
|
contents, _ := cmd.Output() // ignore error, sometimes we have intentionally ungofmt-able files
|
|
contents = []byte(normalizePaths(r.data, fixFileHeader(string(contents))))
|
|
return contents, nil
|
|
}))
|
|
if expect == "" {
|
|
//TODO: our error handling differs, for now just skip unformattable files
|
|
continue
|
|
}
|
|
app := cmd.New("gopls-test", r.data.Config.Dir, r.data.Config.Env)
|
|
got := captureStdOut(t, func() {
|
|
_ = tool.Run(r.ctx, app, append([]string{"-remote=internal", "format"}, args...))
|
|
})
|
|
got = normalizePaths(r.data, got)
|
|
// check the first two lines are the expected file header
|
|
if expect != got {
|
|
t.Errorf("format failed with %#v expected:\n%s\ngot:\n%s", args, expect, got)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
return strings.Join(append(match[1:], s[len(match[0]):]), "")
|
|
}
|