package emulator import ( "testing" "github.com/stretchr/testify/assert" ) //nolint:goconst func TestProcessSessionInput(t *testing.T) { t.Parallel() tests := map[string]struct { input string command string opts CommandOpts errChecker assert.ErrorAssertionFunc }{ "No Options": { input: "echo hello", command: "echo hello", opts: CommandOpts{}, errChecker: assert.NoError, }, "With options": { input: "echo hello " + sendOptionsSplitter + ` > file.txt`, command: "echo hello ", // Notice trailing space opts: CommandOpts{FileRedirection: "file.txt"}, errChecker: assert.NoError, }, "Empty Options": { input: `echo hello ` + sendOptionsSplitter + ` `, command: "echo hello ", opts: CommandOpts{}, // TODO confirm error type/value errChecker: assert.Error, }, "Unknown option type": { input: `echo hello ` + sendOptionsSplitter + ` | file.txt`, command: "echo hello ", opts: CommandOpts{}, errChecker: assert.Error, }, "Too many options": { input: `echo hello ` + sendOptionsSplitter + ` > file.txt help`, command: "echo hello ", opts: CommandOpts{}, errChecker: assert.Error, }, "Too few options": { input: `echo hello ` + sendOptionsSplitter + ` > `, command: "echo hello ", opts: CommandOpts{}, errChecker: assert.Error, }, "Invalid command": { // Command includes an incomplete quote // Currently the split on the separator is done first, and then options // validation is carried out, without validating command, so this passes // Command validation is expected later on, e.g. in cliservices input: `echo "hello ` + sendOptionsSplitter + ` > file.txt`, command: `echo "hello `, opts: CommandOpts{FileRedirection: "file.txt"}, errChecker: assert.NoError, }, "Invalid options": { // Options include an incomplete quote // Options are validated so we would expect an error returned here. input: `echo hello ` + sendOptionsSplitter + ` > "file.txt`, command: `echo hello `, opts: CommandOpts{}, errChecker: assert.Error, }, "homonym": { // It looks the same but isn't input: `echo hello ` + `||||` + ` > file.txt`, command: `echo hello |||| > file.txt`, opts: CommandOpts{}, errChecker: assert.NoError, }, } for name, tc := range tests { tc := tc t.Run(name, func(t *testing.T) { command, opts, err := processSessionInput(tc.input) tc.errChecker(t, err) assert.Equal(t, tc.command, command) assert.Equal(t, tc.opts, opts) }) } }