Skip to content

Commit 2658dc0

Browse files
committed
internal/lsp: add a regtest for formatting one-line files
Issue golang/go#36824 complained about legal go code (e.g., 'package a; func f() {}') that was mishandled (by being rewritten just as 'package a'). This bug seems to have been partially fixed, as certified by the new regtests. The comment on OneLineImports36824 says that the bug would be fixed if gopls formatted the file before fixing the imports, but it doesn't. Change-Id: If27fa738e54d9434d5b2f17ed4e52d555cb7c499 Reviewed-on: https://go-review.googlesource.com/c/tools/+/229303 Run-TryBot: Peter Weinberger <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent ab2804f commit 2658dc0

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

internal/lsp/regtest/formatting_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,56 @@ func TestFormatting(t *testing.T) {
3333
})
3434
}
3535

36+
// this is the fixed case from #36824
37+
const onelineProgram = `
38+
-- a.go --
39+
package main; func f() {}
40+
41+
-- a.go.formatted --
42+
package main
43+
44+
func f() {}
45+
`
46+
47+
func TestFormattingOneLine36824(t *testing.T) {
48+
runner.Run(t, onelineProgram, func(t *testing.T, env *Env) {
49+
env.OpenFile("a.go")
50+
env.FormatBuffer("a.go")
51+
got := env.E.BufferText("a.go")
52+
want := env.ReadWorkspaceFile("a.go.formatted")
53+
if got != want {
54+
t.Errorf("got\n%q wanted\n%q", got, want)
55+
}
56+
})
57+
}
58+
59+
const onelineProgramA = `
60+
-- a.go --
61+
package x; func f() {fmt.Println()}
62+
63+
-- a.go.imported --
64+
package x
65+
66+
import "fmt"
67+
68+
func f() { fmt.Println() }
69+
`
70+
71+
// this is the example from #36824 done properly
72+
// but gopls does not reformat before fixing the imports
73+
func TestFormattingOneLineImports36824(t *testing.T) {
74+
runner.Run(t, onelineProgramA, func(t *testing.T, env *Env) {
75+
env.OpenFile("a.go")
76+
env.FormatBuffer("a.go")
77+
env.OrganizeImports("a.go")
78+
got := env.E.BufferText("a.go")
79+
want := env.ReadWorkspaceFile("a.go.imported")
80+
if got != want {
81+
t.Errorf("OneLineImports go\n%q wanted\n%q", got, want)
82+
}
83+
})
84+
}
85+
3686
const disorganizedProgram = `
3787
-- main.go --
3888
package main

0 commit comments

Comments
 (0)