1
0
mirror of https://github.com/golang/go synced 2024-10-01 15:58:33 -06:00
go/internal/lsp/regtest/formatting_test.go
Rob Findley c20a87c16a internal/lsp/fake: split up and rename the Workspace type
The Workspace type has accumulated too much additional functionality of
late: managing the Env, GOPATH, and GOPROXY in addition to the working
directory. Additionally, the name 'Workspace' can easily be confused
with 'workspaceFolder' in the LSP spec, and they're not quite
equivalent.

Split off a Proxy type to be responsible for the fake module proxy, and
a Workdir type to be responsible for working with the temporary
directory. Rename what remains of 'Workspace' to a more appropriate name
for such a collection of resources: Sandbox.

This is mostly just moving things around, with one significant change in
functionality: previously our three temporary directories (workdir,
gopath, and goproxy) were in separate toplevel directories below
$TMPDIR. Now they are all below a new sandbox temp directory, so that
they are correlated in the filesystem and can be cleaned up with one
call to os.RemoveAll.

Change-Id: I1e160a31ae22f0132355117df941fe65822900eb
Reviewed-on: https://go-review.googlesource.com/c/tools/+/230758
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-05-06 18:17:57 +00:00

143 lines
2.7 KiB
Go

package regtest
import (
"testing"
)
const unformattedProgram = `
-- main.go --
package main
import "fmt"
func main( ) {
fmt.Println("Hello World.")
}
-- main.go.golden --
package main
import "fmt"
func main() {
fmt.Println("Hello World.")
}
`
func TestFormatting(t *testing.T) {
runner.Run(t, unformattedProgram, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
env.FormatBuffer("main.go")
got := env.Editor.BufferText("main.go")
want := env.ReadWorkspaceFile("main.go.golden")
if got != want {
t.Errorf("\n## got formatted file:\n%s\n## want:\n%s", got, want)
}
})
}
// this is the fixed case from #36824
const onelineProgram = `
-- a.go --
package main; func f() {}
-- a.go.formatted --
package main
func f() {}
`
func TestFormattingOneLine36824(t *testing.T) {
runner.Run(t, onelineProgram, func(t *testing.T, env *Env) {
env.OpenFile("a.go")
env.FormatBuffer("a.go")
got := env.Editor.BufferText("a.go")
want := env.ReadWorkspaceFile("a.go.formatted")
if got != want {
t.Errorf("got\n%q wanted\n%q", got, want)
}
})
}
const onelineProgramA = `
-- a.go --
package x; func f() {fmt.Println()}
-- a.go.imported --
package x
import "fmt"
func f() { fmt.Println() }
`
// this is the example from #36824 done properly
// but gopls does not reformat before fixing the imports
func TestFormattingOneLineImports36824(t *testing.T) {
runner.Run(t, onelineProgramA, func(t *testing.T, env *Env) {
env.OpenFile("a.go")
env.FormatBuffer("a.go")
env.OrganizeImports("a.go")
got := env.Editor.BufferText("a.go")
want := env.ReadWorkspaceFile("a.go.imported")
if got != want {
t.Errorf("OneLineImports go\n%q wanted\n%q", got, want)
}
})
}
const disorganizedProgram = `
-- main.go --
package main
import (
"fmt"
"errors"
)
func main( ) {
fmt.Println(errors.New("bad"))
}
-- main.go.organized --
package main
import (
"errors"
"fmt"
)
func main( ) {
fmt.Println(errors.New("bad"))
}
-- main.go.formatted --
package main
import (
"errors"
"fmt"
)
func main() {
fmt.Println(errors.New("bad"))
}
`
func TestOrganizeImports(t *testing.T) {
runner.Run(t, disorganizedProgram, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
env.OrganizeImports("main.go")
got := env.Editor.BufferText("main.go")
want := env.ReadWorkspaceFile("main.go.organized")
if got != want {
t.Errorf("\n## got formatted file:\n%s\n## want:\n%s", got, want)
}
})
}
func TestFormattingOnSave(t *testing.T) {
runner.Run(t, disorganizedProgram, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
env.SaveBuffer("main.go")
got := env.Editor.BufferText("main.go")
want := env.ReadWorkspaceFile("main.go.formatted")
if got != want {
t.Errorf("\n## got formatted file:\n%s\n## want:\n%s", got, want)
}
})
}