github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/cli/connhelper/commandconn/commandconn_unix_test.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package commandconn
     5  
     6  import (
     7  	"context"
     8  	"io"
     9  	"testing"
    10  
    11  	"gotest.tools/v3/assert"
    12  	is "gotest.tools/v3/assert/cmp"
    13  )
    14  
    15  // For https://github.com/docker/cli/pull/1014#issuecomment-409308139
    16  func TestEOFWithError(t *testing.T) {
    17  	ctx := context.TODO()
    18  	cmd := "sh"
    19  	args := []string{"-c", "echo hello; echo some error >&2; exit 42"}
    20  	c, err := New(ctx, cmd, args...)
    21  	assert.NilError(t, err)
    22  	b := make([]byte, 32)
    23  	n, err := c.Read(b)
    24  	assert.Check(t, is.Equal(len("hello\n"), n))
    25  	assert.NilError(t, err)
    26  	n, err = c.Read(b)
    27  	assert.Check(t, is.Equal(0, n))
    28  	assert.ErrorContains(t, err, "some error")
    29  	assert.ErrorContains(t, err, "42")
    30  }
    31  
    32  func TestEOFWithoutError(t *testing.T) {
    33  	ctx := context.TODO()
    34  	cmd := "sh"
    35  	args := []string{"-c", "echo hello; echo some debug log >&2; exit 0"}
    36  	c, err := New(ctx, cmd, args...)
    37  	assert.NilError(t, err)
    38  	b := make([]byte, 32)
    39  	n, err := c.Read(b)
    40  	assert.Check(t, is.Equal(len("hello\n"), n))
    41  	assert.NilError(t, err)
    42  	n, err = c.Read(b)
    43  	assert.Check(t, is.Equal(0, n))
    44  	assert.Check(t, is.Equal(io.EOF, err))
    45  }