// Copyright 2024 Matthew Rich . All rights reserved. package command import ( _ "fmt" "github.com/stretchr/testify/assert" _ "os" _ "strings" "testing" "bytes" ) func TestNewCommand(t *testing.T) { c := NewCommand() assert.NotNil(t, c) } func TestCommandLoad(t *testing.T) { c := NewCommand() assert.NotNil(t, c) decl := ` path: find args: - "{{ .Path }}" ` assert.Nil(t, c.LoadDecl(decl)) assert.Equal(t, "find", c.Path) } func TestCommandTemplate(t *testing.T) { c := NewCommand() assert.NotNil(t, c) decl := ` path: find args: - "{{ .Path }}" ` assert.Nil(t, c.LoadDecl(decl)) assert.Equal(t, "find", c.Path) assert.Equal(t, 1, len(c.Args)) f := struct { Path string } { Path: "./", } args, templateErr := c.Template(f) assert.Nil(t, templateErr) assert.Equal(t, 1, len(args)) assert.Equal(t, "./", string(args[0])) out, err := c.Execute(f) assert.Nil(t, err) assert.Greater(t, len(out), 0) } func TestCommandStdin(t *testing.T) { var expected string = "stdin test data" var stdinBuffer bytes.Buffer stdinBuffer.WriteString(expected) c := NewCommand() assert.NotNil(t, c) decl := ` path: cat stdinavailable: true ` assert.Nil(t, c.LoadDecl(decl)) assert.Equal(t, "cat", c.Path) c.SetStdinReader(&stdinBuffer) out, err := c.Execute(nil) assert.Nil(t, err) assert.Equal(t, expected, string(out)) }