def test_cat_command_from_stdin(self): cmd_1 = self.build_cmd([Lexem(LexemType.STRING, 'pwd', 0, 3)]) cmd_2 = self.build_cmd([Lexem(LexemType.STRING, 'cat', 5, 7)]) cmd = CommandChainPipe(cmd_1, cmd_2) cmd_result = cmd.run(self.init_input, self.init_env) self.assertEqual(cmd_result.get_output(), os.getcwd()) self.assertEqual(cmd_result.get_return_code(), 0)
def test_pipe_two_cmd(self): cmd_1 = self.build_cmd([ Lexem(LexemType.STRING, 'echo', 0, 4), Lexem(LexemType.ASSIGNMENT, 'yyy=123', 5, 10) ]) cmd_2 = self.build_cmd([Lexem(LexemType.STRING, 'wc', 14, 15)]) cmd = CommandChainPipe(cmd_1, cmd_2) cmd_result = cmd.run(self.init_input, self.init_env) exp_length = 7 + len(os.linesep) self.assertEqual(cmd_result.get_output(), '1 1 {}'.format(exp_length)) self.assertEqual(cmd_result.get_return_code(), 0)
def test_wc_command(self): cmd_1 = self.build_cmd([ Lexem(LexemType.STRING, 'echo', 0, 3), Lexem(LexemType.QUOTED_STRING, '"5 c 123 qwe"', 4, 15) ]) cmd_2 = self.build_cmd([Lexem(LexemType.STRING, 'wc', 16, 18)]) cmd = CommandChainPipe(cmd_1, cmd_2) cmd_result = cmd.run(self.init_input, self.init_env) exp_length = 14 + len(os.linesep) self.assertEqual(cmd_result.get_output(), '1 4 {}'.format(exp_length)) self.assertEqual(cmd_result.get_return_code(), 0)
def test_piped_assignment(self): cmd_1 = CommandAssignment( [Lexem(LexemType.ASSIGNMENT, 'tEq=1', 0, 5).get_value()]) cmd_2 = CommandAssignment( [Lexem(LexemType.ASSIGNMENT, 'x=a', 6, 15).get_value()]) cmd = CommandChainPipe(cmd_1, cmd_2) cmd_result = cmd.run(self.init_input, self.init_env) self.assertEqual(cmd_result.get_result_environment().get_var('tEq'), '1') self.assertEqual(cmd_result.get_result_environment().get_var('x'), 'a') self.assertEqual(cmd_result.get_output(), '') self.assertEqual(cmd_result.get_return_code(), 0)
def test_pipe_three_cmd(self): cmd_1 = self.build_cmd([ Lexem(LexemType.STRING, 'echo', 0, 4), Lexem(LexemType.QUOTED_STRING, "'hello$x'", 5, 15) ]) cmd_2 = self.build_cmd([Lexem(LexemType.STRING, 'wc', 17, 18)]) cmd_3 = self.build_cmd([Lexem(LexemType.STRING, 'wc', 19, 20)]) pipe_1_2 = CommandChainPipe(cmd_1, cmd_2) pipe_1_2_3 = CommandChainPipe(pipe_1_2, cmd_3) cmd_result = pipe_1_2_3.run(self.init_input, self.init_env) self.assertEqual(cmd_result.get_output(), '1 3 5') self.assertEqual(cmd_result.get_return_code(), 0)
def test_pipe_threeway_ignore_first_cmd(self): cmd_1 = self.build_cmd([Lexem(LexemType.STRING, 'pwd', 0, 4)]) cmd_2 = self.build_cmd([ Lexem(LexemType.STRING, 'echo', 5, 8), Lexem(LexemType.STRING, 'qQqQ', 9, 12) ]) cmd_3 = self.build_cmd([Lexem(LexemType.STRING, 'wc', 13, 15)]) pipe_1_2 = CommandChainPipe(cmd_1, cmd_2) pipe_1_2_3 = CommandChainPipe(pipe_1_2, cmd_3) cmd_result = pipe_1_2_3.run(self.init_input, self.init_env) exp_length = 4 + len(os.linesep) self.assertEqual(cmd_result.get_output(), '1 1 {}'.format(exp_length)) self.assertEqual(cmd_result.get_return_code(), 0)