-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache node building in graph and add testing (#2245)
- Loading branch information
Showing
19 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright 2020-2023 Buf Technologies, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package bufgraph | ||
|
||
import ( | ||
"context" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/bufbuild/buf/private/buf/bufwork" | ||
"github.com/bufbuild/buf/private/bufpkg/bufconfig" | ||
"github.com/bufbuild/buf/private/bufpkg/bufmodule" | ||
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmodulebuild" | ||
"github.com/bufbuild/buf/private/pkg/storage" | ||
"github.com/bufbuild/buf/private/pkg/storage/storageos" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestBasic(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx := context.Background() | ||
workspace, err := testBuildWorkspace(ctx, filepath.Join("testdata", "basic")) | ||
require.NoError(t, err) | ||
builder := NewBuilder( | ||
zap.NewNop(), | ||
bufmodule.NewNopModuleResolver(), | ||
bufmodule.NewNopModuleReader(), | ||
) | ||
graph, fileAnnotations, err := builder.Build( | ||
ctx, | ||
workspace.GetModules(), | ||
BuildWithWorkspace(workspace), | ||
) | ||
require.NoError(t, err) | ||
require.Empty(t, fileAnnotations) | ||
dotString, err := graph.DOTString(func(key Node) string { return key.String() }) | ||
require.NoError(t, err) | ||
require.Equal( | ||
t, | ||
`digraph { | ||
1 [label="bsr.internal/foo/test-a"] | ||
2 [label="bsr.internal/foo/test-b"] | ||
3 [label="bsr.internal/foo/test-c"] | ||
4 [label="bsr.internal/foo/test-d"] | ||
5 [label="bsr.internal/foo/test-e"] | ||
6 [label="bsr.internal/foo/test-f"] | ||
7 [label="bsr.internal/foo/test-g"] | ||
1 -> 2 | ||
2 -> 3 | ||
3 -> 4 | ||
1 -> 4 | ||
1 -> 5 | ||
5 -> 6 | ||
7 | ||
}`, | ||
dotString, | ||
) | ||
} | ||
|
||
// TODO: This entire function is all you should need to do to build workspaces, and even | ||
// this is overly complicated because of the wonkiness of bufmodulebuild and NewWorkspace. | ||
// We should have this in a common place for at least testing. | ||
func testBuildWorkspace(ctx context.Context, workspacePath string) (bufmodule.Workspace, error) { | ||
workspaceBucket, err := storageos.NewProvider().NewReadWriteBucket(workspacePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
workspaceConfig, err := bufwork.GetConfigForBucket(ctx, workspaceBucket, ".") | ||
if err != nil { | ||
return nil, err | ||
} | ||
moduleBucketBuilder := bufmodulebuild.NewModuleBucketBuilder() | ||
namedModules := make(map[string]bufmodule.Module, len(workspaceConfig.Directories)) | ||
allModules := make([]bufmodule.Module, 0, len(workspaceConfig.Directories)) | ||
for _, directory := range workspaceConfig.Directories { | ||
moduleBucket := storage.MapReadBucket( | ||
workspaceBucket, | ||
storage.MapOnPrefix(directory), | ||
) | ||
moduleConfig, err := bufconfig.GetConfigForBucket(ctx, moduleBucket) | ||
if err != nil { | ||
return nil, err | ||
} | ||
module, err := moduleBucketBuilder.BuildForBucket( | ||
ctx, | ||
moduleBucket, | ||
moduleConfig.Build, | ||
bufmodulebuild.WithModuleIdentity( | ||
moduleConfig.ModuleIdentity, | ||
), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if moduleConfig.ModuleIdentity != nil { | ||
namedModules[moduleConfig.ModuleIdentity.IdentityString()] = module | ||
} | ||
allModules = append(allModules, module) | ||
} | ||
return bufmodule.NewWorkspace( | ||
ctx, | ||
namedModules, | ||
allModules, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
version: v1 | ||
directories: | ||
- test-a | ||
- test-b | ||
- test-c | ||
- test-d | ||
- test-e | ||
- test-f | ||
- test-g |
13 changes: 13 additions & 0 deletions
13
private/bufpkg/bufgraph/testdata/basic/test-a/a/v1/a.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
syntax = "proto3"; | ||
|
||
package a.v1; | ||
|
||
import "b/v1/b.proto"; | ||
import "d/v1/d.proto"; | ||
import "e/v1/e.proto"; | ||
|
||
message A { | ||
b.v1.B b = 1; | ||
d.v1.D d = 2; | ||
e.v1.E e = 3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
syntax = "proto3"; | ||
|
||
package a2.v1; | ||
|
||
import "e/v1/e.proto"; | ||
|
||
message A2 { | ||
e.v1.E e = 3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
version: v1 | ||
name: bsr.internal/foo/test-a | ||
deps: | ||
- bsr.internal/foo/test-b | ||
- bsr.internal/foo/test-d | ||
- bsr.internal/foo/test-e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
syntax = "proto3"; | ||
|
||
package b.v1; | ||
|
||
import "c/v1/c.proto"; | ||
|
||
message B { | ||
c.v1.C c = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
version: v1 | ||
name: bsr.internal/foo/test-b | ||
deps: | ||
- bsr.internal/foo/test-c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
version: v1 | ||
name: bsr.internal/foo/test-c | ||
deps: | ||
- bsr.internal/foo/test-d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
syntax = "proto3"; | ||
|
||
package c.v1; | ||
|
||
import "d/v1/d.proto"; | ||
|
||
message C { | ||
d.v1.D d = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
version: v1 | ||
name: bsr.internal/foo/test-d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
syntax = "proto3"; | ||
|
||
package d.v1; | ||
|
||
message D {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
version: v1 | ||
name: bsr.internal/foo/test-e | ||
deps: | ||
- bsr.internal/foo/test-f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
syntax = "proto3"; | ||
|
||
package e.v1; | ||
|
||
import "f/v1/f.proto"; | ||
|
||
message E { | ||
f.v1.F f = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
version: v1 | ||
name: bsr.internal/foo/test-f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
syntax = "proto3"; | ||
|
||
package f.v1; | ||
|
||
message F {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
version: v1 | ||
name: bsr.internal/foo/test-g |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
syntax = "proto3"; | ||
|
||
package g.v1; | ||
|
||
message G {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters