Skip to content

Commit

Permalink
Fix VirtualCommand on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
adambabik committed Feb 2, 2024
1 parent e4f813c commit 1382399
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
21 changes: 17 additions & 4 deletions internal/command/command_virtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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() {
Expand Down
17 changes: 17 additions & 0 deletions internal/command/command_virtual_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1382399

Please sign in to comment.