Skip to content

Commit ec00671

Browse files
Add LSP rename/formatting tests (#4264)
1 parent ff6edae commit ec00671

File tree

8 files changed

+622
-0
lines changed

8 files changed

+622
-0
lines changed

private/buf/buflsp/format_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2020-2025 Buf Technologies, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package buflsp_test
16+
17+
import (
18+
"context"
19+
"path/filepath"
20+
"testing"
21+
22+
"github.com/bufbuild/buf/private/buf/bufformat"
23+
"github.com/bufbuild/buf/private/pkg/storage"
24+
"github.com/bufbuild/buf/private/pkg/storage/storageos"
25+
"github.com/stretchr/testify/assert"
26+
"github.com/stretchr/testify/require"
27+
"go.lsp.dev/protocol"
28+
)
29+
30+
func TestFormatting(t *testing.T) {
31+
t.Parallel()
32+
33+
ctx := t.Context()
34+
35+
tests := []struct {
36+
name string
37+
protoFile string
38+
expectEdits bool
39+
expectNumEdits int
40+
}{
41+
{
42+
name: "format_unformatted_file",
43+
protoFile: "unformatted.proto",
44+
expectEdits: true,
45+
expectNumEdits: 1,
46+
},
47+
{
48+
name: "format_already_formatted_file",
49+
protoFile: "formatted.proto",
50+
expectEdits: false,
51+
expectNumEdits: 0,
52+
},
53+
}
54+
55+
for _, tt := range tests {
56+
t.Run(tt.name, func(t *testing.T) {
57+
t.Parallel()
58+
testProtoPath, err := filepath.Abs(filepath.Join("testdata/format", tt.protoFile))
59+
require.NoError(t, err)
60+
clientJSONConn, testURI := setupLSPServer(t, testProtoPath)
61+
var textEdits []protocol.TextEdit
62+
_, formatErr := clientJSONConn.Call(ctx, protocol.MethodTextDocumentFormatting, protocol.DocumentFormattingParams{
63+
TextDocument: protocol.TextDocumentIdentifier{
64+
URI: testURI,
65+
},
66+
}, &textEdits)
67+
require.NoError(t, formatErr)
68+
assert.Len(t, textEdits, tt.expectNumEdits)
69+
if tt.expectEdits {
70+
expectedFormatted := getExpectedFormattedContent(t, ctx, testProtoPath)
71+
assert.Equal(t, expectedFormatted, textEdits[0].NewText)
72+
assert.Equal(t, uint32(0), textEdits[0].Range.Start.Line)
73+
assert.Equal(t, uint32(0), textEdits[0].Range.Start.Character)
74+
}
75+
})
76+
}
77+
}
78+
79+
func getExpectedFormattedContent(t *testing.T, ctx context.Context, protoPath string) string {
80+
t.Helper()
81+
dir := filepath.Dir(protoPath)
82+
bucket, err := storageos.NewProvider().NewReadWriteBucket(dir)
83+
require.NoError(t, err)
84+
formattedBucket, err := bufformat.FormatBucket(ctx, bucket)
85+
require.NoError(t, err)
86+
formatted, err := storage.ReadPath(ctx, formattedBucket, filepath.Base(protoPath))
87+
require.NoError(t, err)
88+
return string(formatted)
89+
}

0 commit comments

Comments
 (0)