go/analysis/analysistest: print unified diff for test failures
go/analysis/analysistest: print unified diff for test failures

Instead of printing the golden file and actual output using %#v, print
a unified diff. That way, instead of a possibly long, hard to decipher
error like this

        analysistest.go:134: suggested fixes failed for /home/dominikh/prj/src/honnef.co/go/tools/simple/testdata/src/CheckDeclareAssign/LintDeclareAssign.go, expected:
            "package pkg\n\nfunc fn() {\n\tvar x int = 1\n\t_ = x\n\n\tvar y interface{} = 1\n\t_ = y\n\n\tif true {\n\t\tvar x string = \"a\"\n\t\t_ = x\n\t}\n\n\tvar z []string\n\tz = append(z, \"\")\n\t_ = z\n\n\tvar f func()\n\tf = func() { f() }\n\t_ = f\n\n\tvar a int\n\ta = 1\n\ta = 2\n\t_ = a\n\n\tvar b int\n\tb = 1\n\t// do stuff\n\tb = 2\n\t_ = b\n}\n"
            got:
            "package pkg\n\nfunc fn() {\n\tvar x int = 1\n\t_ = x\n\n\tvar y interface{} = 1\n\t_ = y\n\n\tif true {\n\t\tvar x string = \"\"\n\t\t_ = x\n\t}\n\n\tvar z []string\n\tz = append(z, \"\")\n\t_ = z\n\n\tvar f func()\n\tf = func() { f() }\n\t_ = f\n\n\tvar a int\n\ta = 1\n\ta = 2\n\t_ = a\n\n\tvar b int\n\tb = 1\n\t// do stuff\n\tb = 2\n\t_ = b\n}\n"

we get a much more concise and readable diff:

        analysistest.go:133: suggested fixes failed for /home/dominikh/prj/src/honnef.co/go/tools/simple/testdata/src/CheckDeclareAssign/LintDeclareAssign.go:
            --- /home/dominikh/prj/src/honnef.co/go/tools/simple/testdata/src/CheckDeclareAssign/LintDeclareAssign.go.golden
            +++ actual
            @@ -8,7 +8,7 @@
             	_ = y

             	if true {
            -		var x string = "a"
            +		var x string = ""
             		_ = x
             	}

One downside of this approach is that unprintable characters won't be
visible in the diff. However, the vast majority of Go code does not
contain unprintable characters, and an even smaller amount of
suggested fixes affect unprintable characters. It is worth optimizing
readability for the common case.

Change-Id: I857aa6b6ee719f0fb018d5007eb162882e79cc25
Reviewed-on: https://go-review.googlesource.com/c/tools/+/228118
Run-TryBot: Dominik Honnef <aeacd46bb4fd3f59504c7762d9af6bd04f20aa1f@honnef.co>
TryBot-Result: Gobot Gobot <66cb808b70d30c07676d5e946fee83fd561249e5@golang.org>
Reviewed-by: Rebecca Stambler <d7cf11e5f299d88ea8348366a9fc4f7c335c3afa@golang.org>
Reviewed-by: Michael Matloob <4562438b12c14e81a47550acde616ff328a909f8@golang.org>

golang/tools,golang/tools,golang/tools,golang/tools,golang/tools