def test_single_command_with_args(self):
     lexems = [Lexem(LexemType.STRING, 'echo', 0, 3),
               Lexem(LexemType.QUOTED_STRING, '"hello"', 5, 10),
               Lexem(LexemType.STRING, 'world', 11, 15),
               Lexem(LexemType.QUOTED_STRING, "'hello'", 16, 20)]
     runnable = Parser.build_command(lexems)
     self.assertEqual(type(runnable), CommandEcho)
 def test_pipe_two_cmd(self):
     lexems = [Lexem(LexemType.STRING, 'echo', 0, 4),
               Lexem(LexemType.ASSIGNMENT, 'yyy=123', 5, 10),
               Lexem(LexemType.PIPE, '|', 12, 12),
               Lexem(LexemType.STRING, 'wc', 14, 15)]
     runnable = Parser.build_command(lexems)
     self.assertEqual(type(runnable), CommandChainPipe)
 def test_triple_pipe(self):
     lexems = [Lexem(LexemType.STRING, 'pwd', 0, 4),
               Lexem(LexemType.PIPE, '|', 5, 5),
               Lexem(LexemType.STRING, 'wc', 6, 7),
               Lexem(LexemType.PIPE, '|', 8, 8),
               Lexem(LexemType.STRING, 'wc', 9, 10)]
     runnable = Parser.build_command(lexems)
     self.assertEqual(type(runnable), CommandChainPipe)
Example #4
0
    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)
Example #5
0
    def test_wc_command_file(self):
        cmd = self.build_cmd([
            Lexem(LexemType.STRING, 'wc', 0, 3),
            Lexem(LexemType.QUOTED_STRING, wc_file_path, 4, 15)
        ])
        cmd_result = cmd.run(self.init_input, self.init_env)

        self.assertEqual(cmd_result.get_output(), '2 6 24')
        self.assertEqual(cmd_result.get_return_code(), 0)
Example #6
0
    def test_cat_command(self):
        cmd = self.build_cmd([
            Lexem(LexemType.STRING, 'cat', 0, 2),
            Lexem(LexemType.QUOTED_STRING, wc_file_path, 4, 10)
        ])
        cmd_result = cmd.run(self.init_input, self.init_env)

        self.assertEqual(cmd_result.get_output(), 'this is a word.\nA line.\n')
        self.assertEqual(cmd_result.get_return_code(), 0)
Example #7
0
    def test_external_command(self):
        cmd = self.build_cmd([
            Lexem(LexemType.STRING, sys.executable, 0, 7),
            Lexem(LexemType.STRING, '-c', 8, 10),
            Lexem(LexemType.QUOTED_STRING, "'print(\"hello\", end='')'", 11,
                  20)
        ])
        cmd_result = cmd.run(self.init_input, self.init_env)

        self.assertEqual(cmd_result.get_output(), 'hello')
        self.assertEqual(cmd_result.get_return_code(), 0)
Example #8
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)
Example #9
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)
Example #10
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)
Example #11
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)
Example #12
0
    def test_cd_command(self):
        cmd = self.build_cmd([
            Lexem(LexemType.STRING, 'cd', 0, 1),
            Lexem(LexemType.STRING, os.pardir, 3, 5)
        ])
        cmd_result = cmd.run(self.init_input, self.init_env)

        real_parent_dir = os.path.join(os.getcwd(), os.pardir)

        cmd = self.build_cmd([Lexem(LexemType.STRING, 'pwd', 0, 3)])
        cmd_result = cmd.run(self.init_input, self.init_env)

        self.assertEqual(cmd_result.get_output(), real_parent_dir)
        self.assertEqual(cmd_result.get_return_code(), 0)
Example #13
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)
Example #14
0
    def test_external_command_nonexistant(self):
        cmd = self.build_cmd(
            [Lexem(LexemType.STRING, 'some_ololo_command', 0, 7)])
        cmd_result = cmd.run(self.init_input, self.init_env)

        self.assertEqual(cmd_result.get_return_code(),
                         CommandExternal.COMMAND_NOT_FOUND)
Example #15
0
    def test_assignment(self):
        cmd = CommandAssignment(
            [Lexem(LexemType.ASSIGNMENT, 'qwerty=123', 0, 10).get_value()])
        cmd_result = cmd.run(self.init_input, self.init_env)

        self.assertEqual(cmd_result.get_result_environment().get_var('qwerty'),
                         '123')
        self.assertEqual(cmd_result.get_output(), '')
        self.assertEqual(cmd_result.get_return_code(), 0)
Example #16
0
    def test_wc_cat_nonexistant_file(self):
        cmd = self.build_cmd([
            Lexem(LexemType.STRING, 'cat', 0, 3),
            Lexem(LexemType.STRING, 'some_dummy_file_qqqq', 4, 15)
        ])
        cmd_result = cmd.run(self.init_input, self.init_env)

        self.assertEqual(cmd_result.get_return_code(),
                         CommandCat.FILE_NOT_FOUND)

        cmd = self.build_cmd([
            Lexem(LexemType.STRING, 'wc', 0, 3),
            Lexem(LexemType.STRING, 'some_dummy_file_qqqq', 4, 15)
        ])
        cmd_result = cmd.run(self.init_input, self.init_env)

        self.assertEqual(cmd_result.get_return_code(),
                         CommandWc.FILE_NOT_FOUND)
Example #17
0
    def test_echo_command(self):
        cmd = self.build_cmd([
            Lexem(LexemType.STRING, 'echo', 0, 3),
            Lexem(LexemType.QUOTED_STRING, '"bla bla    bla"', 4, 15)
        ])
        cmd_result = cmd.run(self.init_input, self.init_env)

        self.assertEqual(cmd_result.get_output(),
                         'bla bla    bla{}'.format(os.linesep))
        self.assertEqual(cmd_result.get_return_code(), 0)

        cmd = self.build_cmd([
            Lexem(LexemType.STRING, 'echo', 0, 3),
            Lexem(LexemType.QUOTED_STRING, '"1"', 4, 6),
            Lexem(LexemType.STRING, '234', 7, 10)
        ])
        cmd_result = cmd.run(self.init_input, self.init_env)

        self.assertEqual(cmd_result.get_output(), '1 234{}'.format(os.linesep))
        self.assertEqual(cmd_result.get_return_code(), 0)
 def test_empty_between_pipes(self):
     lexems = [Lexem(LexemType.ASSIGNMENT, 'exit', 0, 3),
               Lexem(LexemType.PIPE, '|', 4, 4),
               Lexem(LexemType.PIPE, '|', 5, 5),
               Lexem(LexemType.STRING, 'wc', 6, 7)]
     self.assertRaises(ParseException, Parser.build_command, lexems)
 def test_smth_after_assignment(self):
     lexems = [Lexem(LexemType.ASSIGNMENT, 'x=1', 0, 3),
               Lexem(LexemType.STRING, 'blabla', 4, 7)]
     self.assertRaises(ParseException, Parser.build_command, lexems)
    def test_external_command_no_args(self):
        lexems = [Lexem(LexemType.STRING, 'some_command', 0, 10)]

        runnable = Parser.build_command(lexems)
        self.assertEqual(type(runnable), CommandExternal)
    def test_single_command_no_args(self):
        lexems = [Lexem(LexemType.STRING, 'pwd', 0, 5)]

        runnable = Parser.build_command(lexems)
        self.assertEqual(type(runnable), CommandPwd)
 def test_external_command_args(self):
     lexems = [Lexem(LexemType.STRING, 'python3', 0, 7),
               Lexem(LexemType.STRING, '-c', 8, 10),
               Lexem(LexemType.QUOTED_STRING, "'print(\"hello\")'", 11, 20)]
     runnable = Parser.build_command(lexems)
     self.assertEqual(type(runnable), CommandExternal)
Example #23
0
    def test_exit_command(self):
        cmd = self.build_cmd([Lexem(LexemType.STRING, 'exit', 0, 3)])

        self.assertRaises(ExitException, cmd.run, self.init_input,
                          self.init_env)
    def test_command_exit(self):
        lexems = [Lexem(LexemType.STRING, 'exit', 0, 2)]

        runnable = Parser.build_command(lexems)
        self.assertEqual(type(runnable), CommandExit)
    def test_assignment(self):
        lexems = [Lexem(LexemType.ASSIGNMENT, 'x=1', 0, 3)]

        runnable = Parser.build_command(lexems)
        self.assertEqual(type(runnable), CommandAssignment)
    def test_command_pwd(self):
        lexems = [Lexem(LexemType.STRING, 'pwd', 0, 2)]

        runnable = Parser.build_command(lexems)
        self.assertEqual(type(runnable), CommandPwd)
 def test_command_echo(self):
     lexems = [Lexem(LexemType.STRING, 'echo', 0, 2),
               Lexem(LexemType.STRING, '1', 4, 5)]
     runnable = Parser.build_command(lexems)
     self.assertEqual(type(runnable), CommandEcho)
 def test_command_cd(self):
     lexems = [Lexem(LexemType.STRING, 'cd', 0, 2),
               Lexem(LexemType.STRING, '..', 4, 5)]
     runnable = Parser.build_command(lexems)
     self.assertEqual(type(runnable), CommandCd)
 def test_assignment_pipe(self):
     lexems = [Lexem(LexemType.ASSIGNMENT, 'x=1', 0, 3),
               Lexem(LexemType.PIPE, '|', 4, 4),
               Lexem(LexemType.STRING, 'pwd', 6, 8)]
     runnable = Parser.build_command(lexems)
     self.assertEqual(type(runnable), CommandChainPipe)
 def test_empty_after_pipe(self):
     lexems = [Lexem(LexemType.ASSIGNMENT, 'pwd', 0, 3),
               Lexem(LexemType.PIPE, '|', 4, 4)]
     self.assertRaises(ParseException, Parser.build_command, lexems)