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

feat: run command #103

Merged
merged 2 commits into from
Jan 8, 2022
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
1 change: 1 addition & 0 deletions cmd/aspect/root/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_library(
"//cmd/aspect/docs",
"//cmd/aspect/info",
"//cmd/aspect/root/flags",
"//cmd/aspect/run",
"//cmd/aspect/test",
"//cmd/aspect/version",
"//docs/help/topics",
Expand Down
4 changes: 3 additions & 1 deletion cmd/aspect/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"aspect.build/cli/cmd/aspect/docs"
"aspect.build/cli/cmd/aspect/info"
"aspect.build/cli/cmd/aspect/root/flags"
"aspect.build/cli/cmd/aspect/run"
"aspect.build/cli/cmd/aspect/test"
"aspect.build/cli/cmd/aspect/version"
"aspect.build/cli/docs/help/topics"
Expand Down Expand Up @@ -80,10 +81,11 @@ func NewRootCmd(
// IMPORTANT: when adding a new command, also update the _DOCS list in /docs/BUILD.bazel
cmd.AddCommand(build.NewDefaultBuildCmd(pluginSystem))
cmd.AddCommand(clean.NewDefaultCleanCmd())
cmd.AddCommand(version.NewDefaultVersionCmd())
cmd.AddCommand(docs.NewDefaultDocsCmd())
cmd.AddCommand(info.NewDefaultInfoCmd())
cmd.AddCommand(run.NewDefaultRunCmd(pluginSystem))
cmd.AddCommand(test.NewDefaultTestCmd(pluginSystem))
cmd.AddCommand(version.NewDefaultVersionCmd())

// ### "Additional help topic commands" which are not runnable
// https://pkg.go.dev/github.com/spf13/cobra#Command.IsAdditionalHelpTopicCommand
Expand Down
17 changes: 17 additions & 0 deletions cmd/aspect/run/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "run",
srcs = ["run.go"],
importpath = "aspect.build/cli/cmd/aspect/run",
visibility = ["//visibility:public"],
deps = [
"//pkg/aspect/run",
"//pkg/bazel",
"//pkg/interceptors",
"//pkg/ioutils",
"//pkg/plugin/system",
"//pkg/plugin/system/bep",
"@com_github_spf13_cobra//:cobra",
],
)
64 changes: 64 additions & 0 deletions cmd/aspect/run/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright © 2021 Aspect Build Systems

Not licensed for re-use
*/

package run

import (
"context"

"github.com/spf13/cobra"

"aspect.build/cli/pkg/aspect/run"
"aspect.build/cli/pkg/bazel"
"aspect.build/cli/pkg/interceptors"
"aspect.build/cli/pkg/ioutils"
"aspect.build/cli/pkg/plugin/system"
"aspect.build/cli/pkg/plugin/system/bep"
)

// NewDefaultRunCmd creates a new run cobra command with the default
// dependencies.
func NewDefaultRunCmd(pluginSystem system.PluginSystem) *cobra.Command {
return NewRunCmd(
ioutils.DefaultStreams,
pluginSystem,
bazel.New(),
)
}

func NewRunCmd(
streams ioutils.Streams,
pluginSystem system.PluginSystem,
bzl bazel.Bazel,
) *cobra.Command {
return &cobra.Command{
Use: "run",
Short: "Builds the specified target and runs it with the given arguments.",
// TODO(f0rmiga): the following comment from 'bazel --help run' may not
// be what we want to provide to our users.
Long: `'run' accepts any 'build' options, and will inherit any defaults
provided by .bazelrc.

If your script needs stdin or execution not constrained by the bazel lock,
use 'bazel run --script_path' to write a script and then execute it.
`,
RunE: interceptors.Run(
[]interceptors.Interceptor{
interceptors.WorkspaceRootInterceptor(),
pluginSystem.BESBackendInterceptor(),
},
func(ctx context.Context, cmd *cobra.Command, args []string) (exitErr error) {
// TODO(f0rmiga): post-run hook.

workspaceRoot := ctx.Value(interceptors.WorkspaceRootKey).(string)
bzl.SetWorkspaceRoot(workspaceRoot)
r := run.New(streams, bzl)
besBackend := ctx.Value(system.BESBackendInterceptorKey).(bep.BESBackend)
return r.Run(args, besBackend)
},
),
}
}
1 change: 1 addition & 0 deletions docs/aspect.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Aspect CLI is a better frontend for running bazel
* [aspect clean](aspect_clean.md) - Removes the output tree.
* [aspect docs](aspect_docs.md) - Open documentation in the browser.
* [aspect info](aspect_info.md) - Displays runtime info about the bazel server.
* [aspect run](aspect_run.md) - Builds the specified target and runs it with the given arguments.
* [aspect test](aspect_test.md) - Builds the specified targets and runs all test targets among them.
* [aspect version](aspect_version.md) - Print the version of aspect CLI as well as tools it invokes.

28 changes: 28 additions & 0 deletions pkg/aspect/run/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "run",
srcs = ["run.go"],
importpath = "aspect.build/cli/pkg/aspect/run",
visibility = ["//cmd/aspect/run:__pkg__"],
deps = [
"//pkg/aspecterrors",
"//pkg/bazel",
"//pkg/ioutils",
"//pkg/plugin/system/bep",
],
)

go_test(
name = "run_test",
srcs = ["run_test.go"],
deps = [
":run",
"//pkg/aspecterrors",
"//pkg/bazel/mock",
"//pkg/ioutils",
"//pkg/plugin/system/bep/mock",
"@com_github_golang_mock//gomock",
"@com_github_onsi_gomega//:gomega",
],
)
59 changes: 59 additions & 0 deletions pkg/aspect/run/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright © 2021 Aspect Build Systems Inc

Not licensed for re-use.
*/

package run

import (
"fmt"

"aspect.build/cli/pkg/aspecterrors"
"aspect.build/cli/pkg/bazel"
"aspect.build/cli/pkg/ioutils"
"aspect.build/cli/pkg/plugin/system/bep"
)

// Run represents the aspect run command.
type Run struct {
ioutils.Streams
bzl bazel.Bazel
}

// New creates a Run command.
func New(
streams ioutils.Streams,
bzl bazel.Bazel,
) *Run {
return &Run{
Streams: streams,
bzl: bzl,
}
}

// Run runs the aspect run command, calling `bazel run` with a local Build
// Event Protocol backend used by Aspect plugins to subscribe to build events.
func (cmd *Run) Run(args []string, besBackend bep.BESBackend) (exitErr error) {
besBackendFlag := fmt.Sprintf("--bes_backend=grpc://%s", besBackend.Addr())
exitCode, bazelErr := cmd.bzl.Spawn(append([]string{"run", besBackendFlag}, args...))

// Process the subscribers errors before the Bazel one.
subscriberErrors := besBackend.Errors()
if len(subscriberErrors) > 0 {
for _, err := range subscriberErrors {
fmt.Fprintf(cmd.Streams.Stderr, "Error: failed to run 'aspect run' command: %v\n", err)
}
exitCode = 1
}

if exitCode != 0 {
err := &aspecterrors.ExitError{ExitCode: exitCode}
if bazelErr != nil {
err.Err = bazelErr
}
return err
}

return nil
}
118 changes: 118 additions & 0 deletions pkg/aspect/run/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
Copyright © 2021 Aspect Build Systems Inc

Not licensed for re-use.
*/

package run_test

import (
"fmt"
"strings"
"testing"

"github.com/golang/mock/gomock"
. "github.com/onsi/gomega"

"aspect.build/cli/pkg/aspect/run"
"aspect.build/cli/pkg/aspecterrors"
bazel_mock "aspect.build/cli/pkg/bazel/mock"
"aspect.build/cli/pkg/ioutils"
bep_mock "aspect.build/cli/pkg/plugin/system/bep/mock"
)

func TestRun(t *testing.T) {
t.Run("when the bazel runner fails, the aspect run fails", func(t *testing.T) {
g := NewGomegaWithT(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

streams := ioutils.Streams{}
bzl := bazel_mock.NewMockBazel(ctrl)
expectErr := &aspecterrors.ExitError{
Err: fmt.Errorf("failed to run bazel run"),
ExitCode: 5,
}
bzl.
EXPECT().
Spawn([]string{"run", "--bes_backend=grpc://127.0.0.1:12345", "//..."}).
Return(expectErr.ExitCode, expectErr.Err)
besBackend := bep_mock.NewMockBESBackend(ctrl)
besBackend.
EXPECT().
Addr().
Return("127.0.0.1:12345").
Times(1)
besBackend.
EXPECT().
Errors().
Times(1)

b := run.New(streams, bzl)
err := b.Run([]string{"//..."}, besBackend)

g.Expect(err).To(MatchError(expectErr))
})

t.Run("when the BES backend contains errors, the aspect run fails", func(t *testing.T) {
g := NewGomegaWithT(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

var stderr strings.Builder
streams := ioutils.Streams{Stderr: &stderr}
bzl := bazel_mock.NewMockBazel(ctrl)
bzl.
EXPECT().
Spawn([]string{"run", "--bes_backend=grpc://127.0.0.1:12345", "//..."}).
Return(0, nil)
besBackend := bep_mock.NewMockBESBackend(ctrl)
besBackend.
EXPECT().
Addr().
Return("127.0.0.1:12345").
Times(1)
besBackend.
EXPECT().
Errors().
Return([]error{
fmt.Errorf("error 1"),
fmt.Errorf("error 2"),
}).
Times(1)

b := run.New(streams, bzl)
err := b.Run([]string{"//..."}, besBackend)

g.Expect(err).To(MatchError(&aspecterrors.ExitError{ExitCode: 1}))
g.Expect(stderr.String()).To(Equal("Error: failed to run 'aspect run' command: error 1\nError: failed to run 'aspect run' command: error 2\n"))
})

t.Run("when the bazel runner succeeds, the aspect run succeeds", func(t *testing.T) {
g := NewGomegaWithT(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

streams := ioutils.Streams{}
bzl := bazel_mock.NewMockBazel(ctrl)
bzl.
EXPECT().
Spawn([]string{"run", "--bes_backend=grpc://127.0.0.1:12345", "//..."}).
Return(0, nil)
besBackend := bep_mock.NewMockBESBackend(ctrl)
besBackend.
EXPECT().
Addr().
Return("127.0.0.1:12345").
Times(1)
besBackend.
EXPECT().
Errors().
Times(1)

b := run.New(streams, bzl)
err := b.Run([]string{"//..."}, besBackend)

g.Expect(err).To(BeNil())
})
}
1 change: 0 additions & 1 deletion pkg/aspect/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ go_library(
"//pkg/bazel",
"//pkg/ioutils",
"//pkg/plugin/system/bep",
"@com_github_spf13_cobra//:cobra",
],
)

Expand Down