You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.0 KiB
Go

package shell
import (
"bufio"
"fmt"
"strings"
"testing"
"git.snorba.art/hugo/nssh/commands/shell/builtins"
"github.com/stretchr/testify/suite"
)
type QuotesTest struct {
suite.Suite
}
func (t *QuotesTest) TestParse() {
expectedContents := fmt.Sprintf("'%s'", "word1 word2 word3\n word4")
parser := &BaseParser{&SingleQuoteParameters{}}
reader := bufio.NewReader(strings.NewReader(expectedContents))
iterator, err := NewCharIterator(reader)
t.NoError(err)
token, err := parser.Parse(iterator, &CharCollection{})
t.NoError(err)
expanded, _ := token.(Expandable).Expand(nil, nil, nil)
t.Equal(1, len(expanded))
t.Equal("word1 word2 word3\n word4", expanded[0])
}
func (t *QuotesTest) TestExpandVariables() {
parser := &BaseParser{&QuoteParameters{}}
reader := bufio.NewReader(
strings.NewReader("\"This is a $variable ${variable2} $variable\""),
)
state := NewShellState()
state.SetVariable("variable", "supergood")
state.SetVariable("variable2", "thing")
iterator, err := NewCharIterator(reader)
t.NoError(err)
token, err := parser.Parse(iterator, &CharCollection{})
t.NoError(err)
expanded, _ := token.(Expandable).Expand(state, nil, nil)
t.Equal("This is a supergood thing supergood", expanded[0])
}
func (t *QuotesTest) TestExpandSubshell() {
parser := &BaseParser{&QuoteParameters{}}
reader := bufio.NewReader(
strings.NewReader("\"This is $(echo a subshell)\""),
)
state := NewShellState()
echo := &builtins.Echo{}
state.SetCommand(echo.Name(), echo)
iterator, err := NewCharIterator(reader)
t.NoError(err)
token, err := parser.Parse(iterator, &CharCollection{})
t.NoError(err)
expanded, _ := token.(Expandable).Expand(state, nil, nil)
t.Equal(1, len(expanded))
t.Equal("This is a subshell", expanded[0])
}
func (t *QuotesTest) TestExpandBacktick() {
parser := &BaseParser{&QuoteParameters{}}
reader := bufio.NewReader(
strings.NewReader("\"This is `echo a backtick`\""),
)
state := NewShellState()
echo := &builtins.Echo{}
state.SetCommand(echo.Name(), echo)
iterator, err := NewCharIterator(reader)
t.NoError(err)
token, err := parser.Parse(iterator, &CharCollection{})
t.NoError(err)
expanded, _ := token.(Expandable).Expand(state, nil, nil)
t.Equal(1, len(expanded))
t.Equal("This is a backtick", expanded[0])
}
// The only way backticks can be nested at the moment is by wrapping them in
// quotes.
func (t *QuotesTest) TestExpandNestedBacktick() {
parser := &BaseParser{&QuoteParameters{}}
reader := bufio.NewReader(
strings.NewReader("\"This is `echo a \"`echo backtick`\"`\""),
)
state := NewShellState()
echo := &builtins.Echo{}
state.SetCommand(echo.Name(), echo)
iterator, err := NewCharIterator(reader)
t.NoError(err)
token, err := parser.Parse(iterator, &CharCollection{})
t.NoError(err)
expanded, _ := token.(Expandable).Expand(state, nil, nil)
t.Equal(1, len(expanded))
t.Equal("This is a backtick", expanded[0])
}
func TestQuotesTest(t *testing.T) {
suite.Run(t, new(QuotesTest))
}