Skip to content

Commit

Permalink
Reduce parallelism in command pkg tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adambabik committed Feb 2, 2024
1 parent efb5d85 commit e4f813c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 69 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@ wasm:
.PHONY: test/execute
test/execute: PKGS ?= "./..."
test/execute: build test/prep-git-project
@TZ=UTC go test -ldflags="$(LDTESTFLAGS)" -timeout=30s -covermode=atomic -coverprofile=cover.out -coverpkg=./... $(PKGS)
TZ=UTC go test -ldflags="$(LDTESTFLAGS)" -timeout=60s -covermode=atomic -coverprofile=cover.out -coverpkg=./... $(PKGS)

.PHONY: test/prep-git-project
test/prep-git-project:
@cp -r -f internal/project/testdata/git-project/.git.bkp internal/project/testdata/git-project/.git
@cp -r -f internal/project/testdata/git-project/.gitignore.bkp internal/project/testdata/git-project/.gitignore
@cp -r -f internal/project/testdata/git-project/nested/.gitignore.bkp internal/project/testdata/git-project/nested/.gitignore

.PHONY: test
test: test/execute
.PHONY: test/clean-git-project
test/clean-git-project:
@rm -r -f internal/project/testdata/git-project/.git
@rm -r -f internal/project/testdata/git-project/.gitignore
@rm -r -f internal/project/testdata/git-project/nested/.gitignore

.PHONY: test
test: test/prep-git-project test/execute test/clean-git-project

.PHONY: test/update-snapshots
test/update-snapshots:
@TZ=UTC UPDATE_SNAPSHOTS=true go test ./...
Expand Down
7 changes: 1 addition & 6 deletions internal/command/command_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,10 @@ func (c *NativeCommand) Start(ctx context.Context) (err error) {
logger: c.logger,
}

source := []envSource{c.opts.GetEnv}
if c.opts.Session != nil {
source = append(source, c.opts.Session.GetEnv)
}

cfg, err := normalizeConfig(
c.cfg,
argsNormalizer,
&envNormalizer{sources: source},
&envNormalizer{sources: []envSource{c.opts.Session.GetEnv}},
)
if err != nil {
return
Expand Down
10 changes: 5 additions & 5 deletions internal/command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ func init() {
}

func TestExecutionCommandFromCodeBlocks(t *testing.T) {
t.Parallel()

logger, err := zap.NewDevelopment()
require.NoError(t, err)
defer logger.Sync()
Expand Down Expand Up @@ -112,6 +110,8 @@ func TestExecutionCommandFromCodeBlocks(t *testing.T) {
t.Run("NativeCommand", func(t *testing.T) {
t.Parallel()

tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

Expand All @@ -130,6 +130,8 @@ func TestExecutionCommandFromCodeBlocks(t *testing.T) {
t.Run("VirtualCommand", func(t *testing.T) {
t.Parallel()

tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -189,7 +191,7 @@ func testExecuteNativeCommand(
command, err := NewNative(cfg, options)
require.NoError(t, err)

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

require.NoError(t, command.Start(ctx))
Expand Down Expand Up @@ -240,8 +242,6 @@ func testExecuteVirtualCommand(
}

func TestCommandWithSession(t *testing.T) {
t.Parallel()

setterCfg := &Config{
ProgramName: "bash",
Source: &runnerv2alpha1.ProgramConfig_Commands{
Expand Down
14 changes: 7 additions & 7 deletions internal/command/command_virtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ type readCloser struct {
done chan struct{}
}

func (r *readCloser) Read(p []byte) (n int, err error) {
func (r *readCloser) Read(p []byte) (int, error) {
var (
n int
err error
)

readc := make(chan struct{})

go func() {
Expand Down Expand Up @@ -119,15 +124,10 @@ func (c *VirtualCommand) Start(ctx context.Context) (err error) {
logger: c.logger,
}

source := []envSource{c.opts.GetEnv}
if c.opts.Session != nil {
source = append(source, c.opts.Session.GetEnv)
}

cfg, err := normalizeConfig(
c.cfg,
argsNormalizer,
&envNormalizer{sources: source},
&envNormalizer{sources: []envSource{c.opts.Session.GetEnv}},
)
if err != nil {
return
Expand Down
16 changes: 0 additions & 16 deletions internal/command/command_virtual_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@ import (

func TestVirtualCommand(t *testing.T) {
t.Run("OptionsIsNil", func(t *testing.T) {
t.Parallel()

cmd, err := NewVirtual(testConfigBasicProgram, nil)
require.NoError(t, err)
require.NoError(t, cmd.Start(context.Background()))
require.NoError(t, cmd.Wait())
})

t.Run("Output", func(t *testing.T) {
t.Parallel()

stdout := bytes.NewBuffer(nil)
opts := &VirtualCommandOptions{
Stdout: stdout,
Expand All @@ -41,8 +37,6 @@ func TestVirtualCommand(t *testing.T) {
})

t.Run("Getters", func(t *testing.T) {
t.Parallel()

cmd, err := NewVirtual(&Config{
ProgramName: "sleep",
Arguments: []string{"1"},
Expand All @@ -56,8 +50,6 @@ func TestVirtualCommand(t *testing.T) {
})

t.Run("SetWinsize", func(t *testing.T) {
t.Parallel()

stdout := bytes.NewBuffer(nil)

cmd, err := NewVirtual(
Expand Down Expand Up @@ -88,10 +80,7 @@ func TestVirtualCommandFromCodeBlocksWithInputUsingPipe(t *testing.T) {
defer logger.Sync()

t.Run("Cat", func(t *testing.T) {
t.Parallel()

source := "```sh\ncat - | tr a-z A-Z\n```"

doc := document.New([]byte(source), idResolver)
node, err := doc.Root()
require.NoError(t, err)
Expand Down Expand Up @@ -128,10 +117,7 @@ func TestVirtualCommandFromCodeBlocksWithInputUsingPipe(t *testing.T) {
})

t.Run("Read", func(t *testing.T) {
t.Parallel()

source := "```sh\nread name\necho \"My name is $name\"\n```"

doc := document.New([]byte(source), idResolver)
node, err := doc.Root()
require.NoError(t, err)
Expand Down Expand Up @@ -166,8 +152,6 @@ func TestVirtualCommandFromCodeBlocksWithInputUsingPipe(t *testing.T) {
})

t.Run("SimulateCtrlC", func(t *testing.T) {
t.Parallel()

// Using sh start bash. We need to go deeper...
source := "```sh\nbash\n```"

Expand Down
48 changes: 24 additions & 24 deletions internal/command/config_code_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@ type configBuilder struct {
block *document.CodeBlock
}

func (b *configBuilder) Build() (*Config, error) {
cfg := &Config{
ProgramName: b.programPath(),
Directory: b.dir(),
Interactive: b.block.Interactive(),
}

if isShellLanguage(filepath.Base(cfg.ProgramName)) {
cfg.Mode = runnerv2alpha1.CommandMode_COMMAND_MODE_INLINE
cfg.Source = &runnerv2alpha1.ProgramConfig_Commands{
Commands: &runnerv2alpha1.ProgramConfig_CommandList{
Items: b.block.Lines(),
},
}
} else {
cfg.Mode = runnerv2alpha1.CommandMode_COMMAND_MODE_FILE
cfg.Source = &runnerv2alpha1.ProgramConfig_Script{
Script: prepareScriptFromLines(cfg.ProgramName, b.block.Lines()),
}
}

return cfg, nil
}

func (b *configBuilder) dir() string {
var dirs []string

Expand Down Expand Up @@ -55,30 +79,6 @@ func (b *configBuilder) programPath() string {
return interpreter
}

func (b *configBuilder) Build() (*Config, error) {
cfg := &Config{
ProgramName: b.programPath(),
Directory: b.dir(),
Interactive: b.block.Interactive(),
}

if isShellLanguage(filepath.Base(cfg.ProgramName)) {
cfg.Mode = runnerv2alpha1.CommandMode_COMMAND_MODE_INLINE
cfg.Source = &runnerv2alpha1.ProgramConfig_Commands{
Commands: &runnerv2alpha1.ProgramConfig_CommandList{
Items: b.block.Lines(),
},
}
} else {
cfg.Mode = runnerv2alpha1.CommandMode_COMMAND_MODE_FILE
cfg.Source = &runnerv2alpha1.ProgramConfig_Script{
Script: prepareScriptFromLines(cfg.ProgramName, b.block.Lines()),
}
}

return cfg, nil
}

func resolveDir(parentDir string, candidates []string) string {
for _, dir := range candidates {
dir := filepath.FromSlash(dir)
Expand Down
12 changes: 4 additions & 8 deletions internal/runnerv2service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
)

func TestRunnerServiceServerExecute(t *testing.T) {
t.Parallel()

lis, stop := testStartRunnerServiceServer(t)
t.Cleanup(stop)
_, client := testCreateRunnerServiceClient(t, lis)
Expand Down Expand Up @@ -134,8 +132,6 @@ func TestRunnerServiceServerExecute(t *testing.T) {
}

func TestRunnerServiceServerExecute_Input(t *testing.T) {
t.Parallel()

lis, stop := testStartRunnerServiceServer(t)
t.Cleanup(stop)
_, client := testCreateRunnerServiceClient(t, lis)
Expand Down Expand Up @@ -258,8 +254,6 @@ func TestRunnerServiceServerExecute_Input(t *testing.T) {
}

func TestRunnerServiceServerExecute_WithStop(t *testing.T) {
t.Parallel()

lis, stop := testStartRunnerServiceServer(t)
t.Cleanup(stop)
_, client := testCreateRunnerServiceClient(t, lis)
Expand Down Expand Up @@ -306,13 +300,13 @@ func TestRunnerServiceServerExecute_WithStop(t *testing.T) {
}

func TestRunnerServiceServerExecute_Winsize(t *testing.T) {
t.Parallel()

lis, stop := testStartRunnerServiceServer(t)
t.Cleanup(stop)
_, client := testCreateRunnerServiceClient(t, lis)

t.Run("DefaultWinsize", func(t *testing.T) {
t.Parallel()

stream, err := client.Execute(context.Background())
require.NoError(t, err)

Expand Down Expand Up @@ -343,6 +337,8 @@ func TestRunnerServiceServerExecute_Winsize(t *testing.T) {
})

t.Run("SetWinsizeInInitialRequest", func(t *testing.T) {
t.Parallel()

stream, err := client.Execute(context.Background())
require.NoError(t, err)

Expand Down

0 comments on commit e4f813c

Please sign in to comment.