Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable exclude-path on breaking, build, export, and lint #713

Merged
merged 7 commits into from
Nov 10, 2021
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## [Unreleased]

- Add new endpoints to the recommendation service to make it configurable
- Add `--exclude-path` flag to `buf generate` command. This allows users to exclude specific paths when running `buf generate`.
- Add `--exclude-path` flag to `buf breaking`, `buf build`, `buf export`, `buf generate`, and `buf lint` commands. This allows users to exclude specific paths when running commands.

## [v1.0.0-rc7] - 2021-11-08

Expand Down
15 changes: 15 additions & 0 deletions private/buf/bufcli/bufcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,21 @@ func BindInputHashtag(flagSet *pflag.FlagSet, addr *string) {
_ = flagSet.MarkHidden(inputHashtagFlagName)
}

// BindExcludePaths binds the exclude-path flag.
func BindExcludePaths(
flagSet *pflag.FlagSet,
excludePathsAddr *[]string,
excludePathsFlagName string,
) {
flagSet.StringSliceVar(
excludePathsAddr,
excludePathsFlagName,
nil,
`Exclude specific files or directories, for example "proto/a/a.proto" or "proto/a".
If specified multiple times, the union will be taken.`,
)
}

// GetInputLong gets the long command description for an input-based command.
func GetInputLong(inputArgDescription string) string {
return fmt.Sprintf(
Expand Down
107 changes: 107 additions & 0 deletions private/buf/cmd/buf/buf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,43 @@ func TestExportPaths(t *testing.T) {
)
}

func TestExportPathsAndExcludes(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
testRunStdout(
t,
nil,
0,
``,
"export",
filepath.Join("testdata", "paths"),
"--path",
filepath.Join("testdata", "paths", "a", "v3"),
"--exclude-path",
filepath.Join("testdata", "paths", "a", "v3", "foo"),
"-o",
tempDir,
)
readWriteBucket, err := storageos.NewProvider().NewReadWriteBucket(tempDir)
require.NoError(t, err)
storagetesting.AssertPaths(
t,
readWriteBucket,
"",
"a/v3/a.proto",
)
storagetesting.AssertNotExist(
t,
readWriteBucket,
"a/v3/foo/foo.proto",
)
storagetesting.AssertNotExist(
t,
readWriteBucket,
"a/v3/foo/bar.proto",
)
}

func TestExportProtoFileRef(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
Expand Down Expand Up @@ -1411,6 +1448,76 @@ func TestExportProtoFileRefWithPathFlag(t *testing.T) {
)
}

func TestBuildWithPaths(t *testing.T) {
t.Parallel()
testRunStdout(t, nil, 0, ``, "build", filepath.Join("testdata", "paths"), "--path", filepath.Join("testdata", "paths", "a", "v3"), "--exclude-path", filepath.Join("testdata", "paths", "a", "v3", "foo"))
testRunStdout(t, nil, 0, ``, "build", filepath.Join("testdata", "paths"), "--path", filepath.Join("testdata", "paths", "a", "v3", "foo"), "--exclude-path", filepath.Join("testdata", "paths", "a", "v3"))
}

func TestLintWithPaths(t *testing.T) {
t.Parallel()
testRunStdoutStderr(
t,
nil,
bufcli.ExitCodeFileAnnotation,
filepath.FromSlash(`testdata/paths/a/v3/a.proto:7:10:Field name "Value" should be lower_snake_case, such as "value".`),
"",
"lint",
filepath.Join("testdata", "paths"),
"--path",
filepath.Join("testdata", "paths", "a", "v3"),
"--exclude-path",
filepath.Join("testdata", "paths", "a", "v3", "foo"),
)
testRunStdoutStderr(
t,
nil,
bufcli.ExitCodeFileAnnotation,
filepath.FromSlash(
`testdata/paths/a/v3/foo/bar.proto:3:1:Package name "a.v3.foo" should be suffixed with a correctly formed version, such as "a.v3.foo.v1".
testdata/paths/a/v3/foo/foo.proto:3:1:Package name "a.v3.foo" should be suffixed with a correctly formed version, such as "a.v3.foo.v1".`),
"",
"lint",
filepath.Join("testdata", "paths"),
"--path",
filepath.Join("testdata", "paths", "a", "v3", "foo"),
"--exclude-path",
filepath.Join("testdata", "paths", "a", "v3"),
)
}

func TestBreakingWithPaths(t *testing.T) {
tempDir := t.TempDir()
testRunStdout(t, nil, 0, ``, "build", filepath.Join("command", "generate", "testdata", "paths"), "-o", filepath.Join(tempDir, "previous.bin"))
testRunStdout(t, nil, 0, ``, "build", filepath.Join("testdata", "paths"), "-o", filepath.Join(tempDir, "current.bin"))
readWriteBucket, err := storageos.NewProvider().NewReadWriteBucket(tempDir)
require.NoError(t, err)
storagetesting.AssertPaths(
t,
readWriteBucket,
"",
"previous.bin",
"current.bin",
)
testRunStdoutStderr(
t,
nil,
bufcli.ExitCodeFileAnnotation,
`a/v3/a.proto:6:3:Field "1" on message "Foo" changed type from "string" to "int32".
a/v3/a.proto:7:3:Field "2" with name "Value" on message "Foo" changed option "json_name" from "value" to "Value".
a/v3/a.proto:7:10:Field "2" on message "Foo" changed name from "value" to "Value".`,
"",
"breaking",
filepath.Join(tempDir, "current.bin"),
"--against",
filepath.Join(tempDir, "previous.bin"),
"--path",
filepath.Join("a", "v3"),
"--exclude-path",
filepath.Join("a", "v3", "foo"),
)
}

func TestVersion(t *testing.T) {
t.Parallel()
testRunStdout(t, nil, 0, bufcli.Version, "--version")
Expand Down
19 changes: 11 additions & 8 deletions private/buf/cmd/buf/command/breaking/breaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const (
configFlagName = "config"
againstFlagName = "against"
againstConfigFlagName = "against-config"
excludePathsFlagName = "exclude-path"

// deprecated
inputFlagName = "input"
Expand Down Expand Up @@ -84,6 +85,7 @@ type flags struct {
Config string
Against string
AgainstConfig string
ExcludePaths []string

// deprecated
Input string
Expand All @@ -106,6 +108,7 @@ func newFlags() *flags {
func (f *flags) Bind(flagSet *pflag.FlagSet) {
bufcli.BindPathsAndDeprecatedFiles(flagSet, &f.Paths, pathsFlagName, &f.Files, filesFlagName)
bufcli.BindInputHashtag(flagSet, &f.InputHashtag)
bufcli.BindExcludePaths(flagSet, &f.ExcludePaths, excludePathsFlagName)
flagSet.StringVar(
&f.ErrorFormat,
errorFormatFlagName,
Expand Down Expand Up @@ -269,10 +272,10 @@ func run(
container,
ref,
inputConfig,
paths, // we filter checks for files
nil, // exclude file paths are not supported in this command
false, // files specified must exist on the main input
false, // we must include source info for this side of the check
paths, // we filter checks for files
flags.ExcludePaths, // we exclude these paths
false, // files specified must exist on the main input
false, // we must include source info for this side of the check
)
if err != nil {
return err
Expand Down Expand Up @@ -305,10 +308,10 @@ func run(
container,
againstRef,
againstInputConfig,
externalPaths, // we filter checks for files
nil, // exclude file paths are not supported in this command
true, // files are allowed to not exist on the against input
true, // no need to include source info for against
externalPaths, // we filter checks for files
flags.ExcludePaths, // we exclude these paths
true, // files are allowed to not exist on the against input
true, // no need to include source info for against
)
if err != nil {
return err
Expand Down
5 changes: 4 additions & 1 deletion private/buf/cmd/buf/command/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
outputFlagName = "output"
outputFlagShortName = "o"
configFlagName = "config"
excludePathsFlagName = "exclude-path"

// deprecated
sourceFlagName = "source"
Expand Down Expand Up @@ -79,6 +80,7 @@ type flags struct {
Paths []string
Output string
Config string
ExcludePaths []string

// deprecated
Source string
Expand All @@ -100,6 +102,7 @@ func (f *flags) Bind(flagSet *pflag.FlagSet) {
bufcli.BindExcludeImports(flagSet, &f.ExcludeImports, excludeImportsFlagName)
bufcli.BindExcludeSourceInfo(flagSet, &f.ExcludeSourceInfo, excludeSourceInfoFlagName)
bufcli.BindPathsAndDeprecatedFiles(flagSet, &f.Paths, pathsFlagName, &f.Files, filesFlagName)
bufcli.BindExcludePaths(flagSet, &f.ExcludePaths, excludePathsFlagName)
flagSet.StringVar(
&f.ErrorFormat,
errorFormatFlagName,
Expand Down Expand Up @@ -205,7 +208,7 @@ func run(
ref,
inputConfig,
paths,
nil,
flags.ExcludePaths, // we exclude these paths
false,
flags.ExcludeSourceInfo,
)
Expand Down
7 changes: 5 additions & 2 deletions private/buf/cmd/buf/command/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
outputFlagName = "output"
outputFlagShortName = "o"
configFlagName = "config"
excludePathsFlagName = "exclude-path"
)

// NewCommand returns a new Command.
Expand Down Expand Up @@ -71,6 +72,7 @@ type flags struct {
Paths []string
Output string
Config string
ExcludePaths []string

// special
InputHashtag string
Expand All @@ -84,6 +86,7 @@ func (f *flags) Bind(flagSet *pflag.FlagSet) {
bufcli.BindInputHashtag(flagSet, &f.InputHashtag)
bufcli.BindExcludeImports(flagSet, &f.ExcludeImports, excludeImportsFlagName)
bufcli.BindPaths(flagSet, &f.Paths, pathsFlagName)
bufcli.BindExcludePaths(flagSet, &f.ExcludePaths, excludePathsFlagName)
flagSet.StringVarP(
&f.Output,
outputFlagName,
Expand Down Expand Up @@ -139,7 +142,7 @@ func run(
sourceOrModuleRef,
flags.Config,
flags.Paths,
nil, // excludes are not supported for export
flags.ExcludePaths,
false,
)
if err != nil {
Expand Down Expand Up @@ -228,7 +231,7 @@ func run(
sourceOrModuleRef,
flags.Config,
flags.Paths,
nil, // excludes are not supported for export
flags.ExcludePaths,
false,
true, // SourceCodeInfo is not needed here for outputting the source code
)
Expand Down
8 changes: 1 addition & 7 deletions private/buf/cmd/buf/command/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func newFlags() *flags {
func (f *flags) Bind(flagSet *pflag.FlagSet) {
bufcli.BindInputHashtag(flagSet, &f.InputHashtag)
bufcli.BindPathsAndDeprecatedFiles(flagSet, &f.Paths, pathsFlagName, &f.Files, filesFlagName)
bufcli.BindExcludePaths(flagSet, &f.ExcludePaths, excludePathsFlagName)
flagSet.BoolVar(
&f.IncludeImports,
includeImportsFlagName,
Expand Down Expand Up @@ -245,13 +246,6 @@ func (f *flags) Bind(flagSet *pflag.FlagSet) {
"",
`The config file or data to use.`,
)
flagSet.StringSliceVar(
&f.ExcludePaths,
excludePathsFlagName,
nil,
`Exclude specific files or directories, for example "proto/a/a.proto" or "proto/a".
If specified multiple times, the union will be taken.`,
)

// deprecated, but not marked as deprecated as we return error if this is used
flagSet.StringVar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ syntax = "proto3";
package a.v3.foo;

message Foo {
string id = 1;
}
23 changes: 13 additions & 10 deletions private/buf/cmd/buf/command/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ import (
)

const (
errorFormatFlagName = "error-format"
configFlagName = "config"
pathsFlagName = "path"
errorFormatFlagName = "error-format"
configFlagName = "config"
pathsFlagName = "path"
excludePathsFlagName = "exclude-path"

// deprecated
inputFlagName = "input"
Expand Down Expand Up @@ -68,9 +69,10 @@ func NewCommand(
}

type flags struct {
ErrorFormat string
Config string
Paths []string
ErrorFormat string
Config string
Paths []string
ExcludePaths []string

// deprecated
Input string
Expand All @@ -89,6 +91,7 @@ func newFlags() *flags {
func (f *flags) Bind(flagSet *pflag.FlagSet) {
bufcli.BindInputHashtag(flagSet, &f.InputHashtag)
bufcli.BindPathsAndDeprecatedFiles(flagSet, &f.Paths, pathsFlagName, &f.Files, filesFlagName)
bufcli.BindExcludePaths(flagSet, &f.ExcludePaths, excludePathsFlagName)
flagSet.StringVar(
&f.ErrorFormat,
errorFormatFlagName,
Expand Down Expand Up @@ -180,10 +183,10 @@ func run(
container,
ref,
inputConfig,
paths, // we filter checks for files
nil, // exclude is not yet supported for this commnad
false, // input files must exist
false, // we must include source info for linting
paths, // we filter checks for files
flags.ExcludePaths, // we exclude these paths
false, // input files must exist
false, // we must include source info for linting
)
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions private/buf/cmd/buf/testdata/paths/a/v1/a.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
syntax = "proto3";

package a.v1;

message Foo {}
7 changes: 7 additions & 0 deletions private/buf/cmd/buf/testdata/paths/a/v2/a.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

package a.v2;

message Foo {
string key = 1;
}
8 changes: 8 additions & 0 deletions private/buf/cmd/buf/testdata/paths/a/v3/a.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";

package a.v3;

message Foo {
int32 key = 1;
string Value = 2;
}
6 changes: 6 additions & 0 deletions private/buf/cmd/buf/testdata/paths/a/v3/foo/bar.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
syntax = "proto3";

package a.v3.foo;

message Bar {
}
7 changes: 7 additions & 0 deletions private/buf/cmd/buf/testdata/paths/a/v3/foo/foo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

package a.v3.foo;

message Foo {
int32 id = 1;
}
Loading