Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bufdev committed Nov 8, 2023
1 parent 690b82c commit a388106
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
21 changes: 18 additions & 3 deletions private/bufnew/bufmodule/bufmodule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestBasic(t *testing.T) {
ctx := context.Background()

// This represents some external dependencies from the BSR.
externalDepModuleProvider, err := bufmoduletest.NewTestProviderForPathToData(
testBSRProvider, err := bufmoduletest.NewTestProviderForPathToData(
ctx,
map[string]map[string][]byte{
"buf.build/foo/extdep1": map[string][]byte{
Expand All @@ -47,7 +47,22 @@ func TestBasic(t *testing.T) {
},
)

moduleBuilder := bufmodule.NewModuleBuilder(ctx, externalDepModuleProvider)
moduleBuilder := bufmodule.NewModuleBuilder(ctx, testBSRProvider)

// Remember, the externalDepModuleProvider is just acting like a BSR; if we actually want to
// say dependencies are part of our workspace, we need to add them! We do so now.
moduleRef, err := bufmodule.NewModuleRef("buf.build", "foo", "extdep1", "")
require.NoError(t, err)
moduleInfo, err := testBSRProvider.GetModuleInfoForModuleRef(ctx, moduleRef)
require.NoError(t, err)
err = moduleBuilder.AddModuleForModuleInfo(moduleInfo)
require.NoError(t, err)
moduleRef, err = bufmodule.NewModuleRef("buf.build", "foo", "extdep2", "")
require.NoError(t, err)
moduleInfo, err = testBSRProvider.GetModuleInfoForModuleRef(ctx, moduleRef)
require.NoError(t, err)
err = moduleBuilder.AddModuleForModuleInfo(moduleInfo)
require.NoError(t, err)

// This module has no name but is part of the workspace.
err = moduleBuilder.AddModuleForBucket(
Expand Down Expand Up @@ -82,7 +97,7 @@ func TestBasic(t *testing.T) {

modules, err := moduleBuilder.Build()
require.NoError(t, err)
//require.Equal(t, 4, len(modules))
require.Equal(t, 4, len(modules))

module2 := testFindModuleWithName(t, modules, "buf.build/bar/module2")
require.Equal(
Expand Down
13 changes: 13 additions & 0 deletions private/bufnew/bufmodule/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"errors"
"sort"
"strings"
"sync"

"github.com/bufbuild/buf/private/bufpkg/bufcas"
Expand Down Expand Up @@ -126,6 +127,18 @@ func (m *module) DepModules() ([]Module, error) {
return m.getDepModules()
}

func (m *module) String() string {
var builder strings.Builder
if m.moduleFullName != nil {
builder.WriteString(m.moduleFullName.String())
if m.commitID != "" {
builder.WriteString(":")
builder.WriteString(m.commitID)
}
}
return builder.String()
}

func (m *module) opaqueID() string {
if m.moduleFullName != nil {
return m.moduleFullName.String()
Expand Down
4 changes: 2 additions & 2 deletions private/bufnew/bufmodule/module_full_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func (*moduleFullName) isModuleFullName() {}

func getAndValidateModuleFullName(moduleInfo ModuleInfo) (ModuleFullName, error) {
moduleFullName := moduleInfo.ModuleFullName()
if moduleFullName != nil {
return nil, fmt.Errorf("ModuleInfo %v had nil ModuleFullName in a context that requires it", moduleInfo)
if moduleFullName == nil {
return nil, fmt.Errorf("ModuleInfo %T %v had nil ModuleFullName in a context that requires it", moduleInfo, moduleInfo)
}
return moduleFullName, nil
}
22 changes: 22 additions & 0 deletions private/bufnew/bufmodule/module_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package bufmodule

import (
"errors"
"fmt"
"strings"

"github.com/bufbuild/buf/private/bufpkg/bufcas"
)
Expand All @@ -29,6 +31,11 @@ import (
// depending on the context. For example, when dealing with non-colocated Modules, we will
// expect that ModuleFullName and CommitID are present, and this can be validated for.
type ModuleInfo interface {
// String returns "registry/owner/name[:commitID]" if ModuleFullName and/or commitID are present.
//
// TODO: probably remove, this is mostly for debugging right now
fmt.Stringer

// ModuleFullName returns the full name of the Module.
//
// May be nil depending on context. For example, when read from lock files, this will
Expand Down Expand Up @@ -71,6 +78,9 @@ func newModuleInfo(
if digest == nil {
return nil, errors.New("nil Digest when constructing ModuleInfo")
}
if moduleFullName == nil && commitID != "" {
return nil, fmt.Errorf("non-empty commit ID %q with nil ModuleFullName when constructing ModuleInfo", commitID)
}
return &moduleInfo{
moduleFullName: moduleFullName,
commitID: commitID,
Expand All @@ -90,4 +100,16 @@ func (m *moduleInfo) Digest() (bufcas.Digest, error) {
return m.digest, nil
}

func (m *moduleInfo) String() string {
var builder strings.Builder
if m.moduleFullName != nil {
builder.WriteString(m.moduleFullName.String())
if m.commitID != "" {
builder.WriteString(":")
builder.WriteString(m.commitID)
}
}
return builder.String()
}

func (*moduleInfo) isModuleInfo() {}

0 comments on commit a388106

Please sign in to comment.