diff --git a/internal/command/command_virtual.go b/internal/command/command_virtual.go index 8e2da1b71..ebebec48a 100644 --- a/internal/command/command_virtual.go +++ b/internal/command/command_virtual.go @@ -240,7 +240,9 @@ func (c *VirtualCommand) Wait() error { waitErr := c.cmd.Wait() c.logger.Info("the virtual command finished", zap.Error(waitErr)) - _ = c.closeIO() + if err := c.closeIO(); err != nil { + return err + } c.wg.Wait() @@ -266,11 +268,22 @@ func (c *VirtualCommand) setErr(err error) { c.mx.Unlock() } -func (c *VirtualCommand) closeIO() (err error) { +func (c *VirtualCommand) closeIO() error { + if err := c.tty.Close(); err != nil { + return errors.WithMessage(err, "failed to close tty") + } + + if err := c.pty.Close(); err != nil { + return errors.WithMessage(err, "failed to close pty") + } + if !isNil(c.stdin) { - err = c.stdin.Close() + if err := c.stdin.Close(); err != nil { + return errors.WithMessage(err, "failed to close stdin") + } } - return + + return nil } func (c *VirtualCommand) cleanup() { diff --git a/internal/command/command_virtual_test.go b/internal/command/command_virtual_test.go index 1dc5d4760..21c72eff5 100644 --- a/internal/command/command_virtual_test.go +++ b/internal/command/command_virtual_test.go @@ -16,6 +16,23 @@ import ( runnerv2alpha1 "github.com/stateful/runme/internal/gen/proto/go/runme/runner/v2alpha1" ) +func TestVirtualCommand1(t *testing.T) { + logger, err := zap.NewDevelopment() + require.NoError(t, err) + defer logger.Sync() + + stdout := bytes.NewBuffer(nil) + opts := &VirtualCommandOptions{ + Stdout: stdout, + Logger: logger, + } + cmd, err := NewVirtual(testConfigBasicProgram, opts) + require.NoError(t, err) + require.NoError(t, cmd.Start(context.Background())) + require.NoError(t, cmd.Wait()) + assert.Equal(t, "test", stdout.String()) +} + func TestVirtualCommand(t *testing.T) { t.Run("OptionsIsNil", func(t *testing.T) { cmd, err := NewVirtual(testConfigBasicProgram, nil)