File: stream_test.go

package info (click to toggle)
golang-github-juju-utils 0.0~git20200923.4646bfe-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 1,324 kB
  • sloc: makefile: 37
file content (47 lines) | stat: -rw-r--r-- 1,211 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package ssh_test

import (
	"io/ioutil"
	"strings"
	"testing/iotest"

	"github.com/juju/testing"
	jc "github.com/juju/testing/checkers"
	gc "gopkg.in/check.v1"

	"github.com/juju/utils/v2/ssh"
)

type SSHStreamSuite struct {
	testing.IsolationSuite
}

var _ = gc.Suite(&SSHStreamSuite{})

func (s *SSHStreamSuite) TestNewStripCRNil(c *gc.C) {
	reader := ssh.StripCRReader(nil)
	c.Assert(reader, gc.IsNil)
}

func (s *SSHStreamSuite) TestStripCR(c *gc.C) {
	reader := ssh.StripCRReader(strings.NewReader("One\r\nTwo"))
	output, err := ioutil.ReadAll(reader)
	c.Assert(err, jc.ErrorIsNil)
	c.Check(string(output), gc.Equals, "One\nTwo")
}

func (s *SSHStreamSuite) TestStripCROneByte(c *gc.C) {
	reader := ssh.StripCRReader(strings.NewReader("One\r\r\rTwo"))
	output, err := ioutil.ReadAll(iotest.OneByteReader(reader))
	c.Assert(err, jc.ErrorIsNil)
	c.Check(string(output), gc.Equals, "OneTwo")
}

func (s *SSHStreamSuite) TestStripCRError(c *gc.C) {
	reader := ssh.StripCRReader(strings.NewReader("One\r\r\rTwo"))
	_, err := ioutil.ReadAll(iotest.TimeoutReader(reader))
	c.Assert(err.Error(), gc.Equals, "timeout")
}