Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions private/buf/bufcurl/invoker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,38 @@ import (
"os"
"testing"

"github.com/bufbuild/buf/private/buf/buftesting"
"github.com/bufbuild/buf/private/pkg/protoencoding"
"github.com/bufbuild/protocompile"
"github.com/bufbuild/protocompile/experimental/incremental"
"github.com/bufbuild/protocompile/experimental/incremental/queries"
"github.com/bufbuild/protocompile/experimental/ir"
"github.com/bufbuild/protocompile/experimental/source"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
)

func TestCountUnrecognized(t *testing.T) {
t.Parallel()
descriptors, err := (&protocompile.Compiler{
Resolver: &protocompile.SourceResolver{
ImportPaths: []string{"./testdata"},
},
}).Compile(t.Context(), "test.proto")
results, _, err := incremental.Run(t.Context(), incremental.New(), queries.FDS{
Opener: &source.Openers{&source.FS{FS: os.DirFS("./testdata")}, buftesting.WKTOpener()},
Session: new(ir.Session),
Workspace: source.NewWorkspace("test.proto"),
})
require.NoError(t, err)
require.Len(t, results, 1)
require.NoError(t, results[0].Fatal)
// fdp stores option values (e.g. MessageOptions.map_entry) as unknown bytes;
// a wire round-trip materializes them as typed fields so the resolver
// recognizes map fields as maps. Mirrors build_image.go's resolverForFDS.
fdsBytes, err := protoencoding.NewWireMarshaler().Marshal(results[0].Value)
require.NoError(t, err)
fds := new(descriptorpb.FileDescriptorSet)
require.NoError(t, protoencoding.NewWireUnmarshaler(nil).Unmarshal(fdsBytes, fds))
resolver, err := protoencoding.NewResolver(fds.File...)
require.NoError(t, err)
msgType, err := descriptors.AsResolver().FindMessageByName("foo.bar.Message")
msgType, err := resolver.FindMessageByName("foo.bar.Message")
require.NoError(t, err)
msg := msgType.New()
msgData, err := os.ReadFile("./testdata/testdata.txt")
Expand Down
33 changes: 33 additions & 0 deletions private/buf/buftesting/buftesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package buftesting

import (
"context"
"errors"
"io"
"log/slog"
"net/http"
Expand All @@ -25,10 +26,12 @@ import (

"github.com/bufbuild/buf/private/buf/bufprotoc"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/gen/data/datawkt"
"github.com/bufbuild/buf/private/pkg/github/githubtesting"
"github.com/bufbuild/buf/private/pkg/normalpath"
"github.com/bufbuild/buf/private/pkg/prototesting"
"github.com/bufbuild/buf/private/pkg/storage/storageos"
"github.com/bufbuild/protocompile/experimental/source"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/descriptorpb"
)
Expand Down Expand Up @@ -156,3 +159,33 @@ func GetProtocFilePathsErr(ctx context.Context, dirPath string, limit int) ([]st
}
return realFilePaths, nil
}

// WKTOpener returns a [source.Opener] backed by [datawkt.ReadBucket]. Tests that
// compile proto files referencing the well-known types — including the implicit
// descriptor.proto dependency requested by the experimental compiler — can chain
// this opener via [source.Openers] to provide WKT fallback resolution from the
// same in-process bucket production code uses.
func WKTOpener() source.Opener {
return datawktOpener{}
}

type datawktOpener struct{}

// Open implements [source.Opener].
//
// Returns errors wrapping [fs.ErrNotExist] for unknown paths, which
// [source.Openers] uses to fall through to the next opener.
func (datawktOpener) Open(path string) (_ *source.File, retErr error) {
readObjectCloser, err := datawkt.ReadBucket.Get(context.Background(), path)
if err != nil {
return nil, err
}
defer func() {
retErr = errors.Join(retErr, readObjectCloser.Close())
}()
data, err := io.ReadAll(readObjectCloser)
if err != nil {
return nil, err
}
return source.NewFile(path, string(data)), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ import (
"math"
"testing"

"github.com/bufbuild/protocompile"
"github.com/bufbuild/buf/private/buf/buftesting"
"github.com/bufbuild/buf/private/pkg/protoencoding"
"github.com/bufbuild/protocompile/experimental/incremental"
"github.com/bufbuild/protocompile/experimental/incremental/queries"
"github.com/bufbuild/protocompile/experimental/ir"
"github.com/bufbuild/protocompile/experimental/source"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/descriptorpb"
)

func TestDefaultsEqual(t *testing.T) {
Expand Down Expand Up @@ -111,16 +117,27 @@ func TestGetDefault(t *testing.T) {
V1 = 1;
V123 = 123;
}`
compiler := &protocompile.Compiler{
Resolver: &protocompile.SourceResolver{
Accessor: protocompile.SourceAccessorFromMap(map[string]string{
"test.proto": testFile,
}),
},
}
results, err := compiler.Compile(t.Context(), "test.proto")
opener := source.NewMap(nil)
opener.Add("test.proto", testFile)
results, _, err := incremental.Run(t.Context(), incremental.New(), queries.FDS{
Opener: &source.Openers{opener, buftesting.WKTOpener()},
Session: new(ir.Session),
Workspace: source.NewWorkspace("test.proto"),
})
require.NoError(t, err)
require.Len(t, results, 1)
require.NoError(t, results[0].Fatal)
// fdp stores option values as unknown bytes; a wire round-trip
// materializes them as typed fields. Mirrors build_image.go.
fdsBytes, err := protoencoding.NewWireMarshaler().Marshal(results[0].Value)
require.NoError(t, err)
fds := new(descriptorpb.FileDescriptorSet)
require.NoError(t, protoencoding.NewWireUnmarshaler(nil).Unmarshal(fdsBytes, fds))
resolver, err := protoencoding.NewResolver(fds.File...)
require.NoError(t, err)
fd, err := resolver.FindFileByPath("test.proto")
require.NoError(t, err)
msg := results[0].Messages().ByName("A")
msg := fd.Messages().ByName("A")

assert.Equal(t,
fieldDefault{
Expand Down
38 changes: 30 additions & 8 deletions private/pkg/protosourcepath/protosourcepath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@
package protosourcepath

import (
"os"
"testing"

"github.com/bufbuild/protocompile"
"github.com/bufbuild/buf/private/buf/buftesting"
"github.com/bufbuild/buf/private/pkg/protoencoding"
"github.com/bufbuild/protocompile/experimental/fdp"
"github.com/bufbuild/protocompile/experimental/incremental"
"github.com/bufbuild/protocompile/experimental/incremental/queries"
"github.com/bufbuild/protocompile/experimental/ir"
"github.com/bufbuild/protocompile/experimental/source"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
)

func TestGetAssociatedSourcePathsProto2(t *testing.T) {
Expand Down Expand Up @@ -376,14 +384,28 @@ func testGetAssociatedSourcePaths(
sourcePathToExpectedAssociatedPaths map[string][]protoreflect.SourcePath,
excludeChildAssociatedPaths bool,
) {
compiler := protocompile.Compiler{
Resolver: &protocompile.SourceResolver{},
SourceInfoMode: protocompile.SourceInfoStandard,
}
files, err := compiler.Compile(t.Context(), testFilePath)
var fdpOptions fdp.Options
fdpOptions.Apply(fdp.IncludeSourceCodeInfo(true))
results, _, err := incremental.Run(t.Context(), incremental.New(), queries.FDS{
Opener: &source.Openers{&source.FS{FS: os.DirFS(".")}, buftesting.WKTOpener()},
Session: new(ir.Session),
Workspace: source.NewWorkspace(testFilePath),
Options: fdpOptions,
})
require.NoError(t, err)
require.Len(t, results, 1)
require.NoError(t, results[0].Fatal)
// fdp stores option values as unknown bytes; a wire round-trip
// materializes them as typed fields. Mirrors build_image.go.
fdsBytes, err := protoencoding.NewWireMarshaler().Marshal(results[0].Value)
require.NoError(t, err)
fds := new(descriptorpb.FileDescriptorSet)
require.NoError(t, protoencoding.NewWireUnmarshaler(nil).Unmarshal(fdsBytes, fds))
resolver, err := protoencoding.NewResolver(fds.File...)
require.NoError(t, err)
fd, err := resolver.FindFileByPath(testFilePath)
require.NoError(t, err)
require.Len(t, files, 1, "expect only one test file")
sourceLocations := files[0].SourceLocations()
sourceLocations := fd.SourceLocations()
// SourceLocations are indexed starting from 1
for i := 1; i < sourceLocations.Len(); i++ {
sourceLocation := sourceLocations.Get(i)
Expand Down
Loading
Loading