Example #1
0
    def test_execute_pipe_when_verbose_is_less_than_3_then_stdout_is_not_printed_to_terminal(self):
        """Test that stdout is not printed to terminal when verbose level < 3."""

        # Arrange
        command = ["test_command", "test_arguments"]

        # Act
        process = Subprocess(command, verbose=2)
        process.execute_pipe("test_directory", "filename")

        # Assert
        self.print_mock.assert_not_called()
Example #2
0
    def test_execute_pipe_when_command_is_executed_successful_then_stdout_is_returned():
        """Test that execute pipe returns stdout when successful."""

        # Arrange
        command = ["test_command", "test_arguments"]

        # Act
        process = Subprocess(command)
        output = process.execute_pipe("test_directory", "filename")

        # Assert
        assert output.stdout == b"standard_output"
Example #3
0
    def test_execute_pipe_when_verbose_is_greater_than_3_then_stdout_is_printed_to_terminal(self):
        """Test that stdout is printed to terminal when verbose level > 3."""

        # Arrange
        command = ["test_command", "test_arguments"]

        # Act
        process = Subprocess(command, verbose=8)
        process.execute_pipe("test_directory", "filename")

        # Assert
        self.print_mock.assert_called_once_with("standard_output")
Example #4
0
    def test_execute_raises_exception_with_subprocess_called_process_error_exception(self):
        """Test that execute raises an process error exception."""

        # Arrange
        command = ["test_command", "test_arguments"]
        process = Subprocess(command)

        self.check_call_mock.side_effect = CalledProcessError("Error", "process_name")

        # Act
        # Assert
        with raises(ProcessError):
            process.execute()
Example #5
0
    def test_execute_raises_exception_with_subprocess_timeout_expired_exception(self):
        """Test that execute raises and process timeout expired exception."""

        # Arrange
        command = ["test_command", "test_arguments"]
        process = Subprocess(command)

        self.check_call_mock.side_effect = TimeoutExpired("Error", "timout_value")

        # Act
        # Assert
        with raises(ProcessError):
            process.execute()
Example #6
0
    def test_execute_pipe_when_command_is_executed_then_stdout_is_saved_to_file(self):
        """Test that execute pipe saves sdtout to file."""

        # Arrange
        command = ["test_command", "test_arguments"]

        # Act
        process = Subprocess(command)
        process.execute_pipe("test_directory", "filename")

        # Assert
        self.open_mock.assert_called_once_with(join("test_directory", "filename_yyyymmdd-hhmmss.log"), "wb")
        self.open_mock.has_calls([call().__enter__().write("standard_output")])
Example #7
0
    def test_execute_pipe_when_non_zero_return_code_then_stdout_is_saved_to_file(self):
        """Test that stdout is saved to file when non zero is returned."""

        # Arrange
        command = ["test_command", "test_arguments"]
        self.run_mock.return_value = SimpleNamespace(stdout=b"standard_out", returncode=1)

        # Act
        process = Subprocess(command)
        with raises(ProcessError):
            process.execute_pipe("test_directory", "filename")

        # Assert
        self.open_mock.assert_called_once_with(join("test_directory", "filename_yyyymmdd-hhmmss.log"), "wb")
        self.open_mock.has_calls([call().__enter__().write("standard_output")])
Example #8
0
def measure_lines_of_code(input_dir, report_file, measure_filter):
    """Measure the lines of code using a filter."""

    measure_language_size_command = [
        "cloc",
        "--csv",
        "--csv-delimiter=,",
        "--hide-rate",
        f"--report-file={report_file}",
        measure_filter,
        input_dir,
    ]

    process = Subprocess(measure_language_size_command, verbose=1)
    process.execute()
Example #9
0
def measure_function_metrics(input_dir, output_dir):
    """Measure the function metrics."""

    function_metrics_file = os.path.join(output_dir, "function_metrics.csv")

    measure_function_size_command = [
        "lizard",
        "--csv",
        f"-o{function_metrics_file}",
        input_dir,
    ]

    process = Subprocess(measure_function_size_command, verbose=3)
    process.execute()

    return function_metrics_file
Example #10
0
    def test_execute_pipe_when_unable_to_open_logfile_then_subprocess_runtime_error_is_raised(self):
        """Test that execute pipe raises runtime error when unable to open logfile."""

        # Arrange
        command = ["test_command", "test_arguments"]
        self.open_mock.side_effect = FileNotFoundError(None, None, "filename")

        # Act
        process = Subprocess(command)
        with raises(SubprocessRuntimeError) as exception:
            process.execute_pipe("test_directory", "filename")

        # Assert
        assert (
            str(exception.value) == "Unable to open ('filename') and write results.\n"
            "Please use preconditions to enforce: ['OutputDirectoryExists', 'OutputDirectoryIsEmpty']."
        )
Example #11
0
    def test_standard_streams_are_modified_when_overridden(self):
        """Test that standard streams are modified when overridden."""

        # Arrange
        command = ["test_command", "test_arguments"]
        process = Subprocess(command, stdout="stdout_stream", stderr="stderr_stream")

        # Act
        process.execute()

        # Assert
        self.check_call_mock.assert_called_once_with(
            ["/test_path/test_command", "test_arguments"],
            stdout="stdout_stream",
            stderr="stderr_stream",
            shell=False,
            timeout=None,
        )
Example #12
0
    def test_execute_is_successful_with_correct_command_parameters(self):
        """Test that execute is successful with correct command parameters."""

        # Arrange
        command = ["test_command", "test_arguments"]
        process = Subprocess(command)

        # Act
        process.execute()

        # Assert
        self.check_call_mock.assert_called_once_with(
            ["/test_path/test_command", "test_arguments"],
            stdout=DEVNULL,
            stderr=DEVNULL,
            shell=False,
            timeout=None,
        )
Example #13
0
    def test_execute_runs_subprocess_with_stderr_output_with_verbose_is_2(self):
        """Test that execute run subprocess with stderr output with verbose level 2."""

        # Arrange
        command = ["test_command", "test_arguments"]
        process = Subprocess(command, stdout="stdout_stream", stderr="stderr_stream", verbose=2)

        # Act
        process.execute()

        # Assert
        self.check_call_mock.assert_called_once_with(
            ["/test_path/test_command", "test_arguments"],
            stdout="stdout_stream",
            stderr=None,
            shell=False,
            timeout=None,
        )
Example #14
0
    def test_execute_runs_subprocess_with_stdout_redirect_with_defined_stdout_stream_and_verbose_greater_than_3(self):
        """Test that execute run subprocess with stdout redirected to stream output with verbose level > 3."""

        # Arrange
        command = ["test_command", "test_arguments"]
        process = Subprocess(command, stdout="stdout_stream", stderr="stderr_stream", verbose=8)

        # Act
        process.execute()

        # Assert
        self.check_call_mock.assert_called_once_with(
            ["/test_path/test_command", "test_arguments"],
            stdout=None,
            stderr=None,
            shell=False,
            timeout=None,
        )
Example #15
0
    def test_execute_runs_subprocess_with_correct_timeout_value_with_timeout_set(self):
        """Test that execute runs subprocess times out at specified moment."""

        # Arrange
        command = ["test_command", "test_arguments"]
        process = Subprocess(command, timeout=180)

        # Act
        process.execute()

        # Assert
        self.check_call_mock.assert_called_once_with(
            ["/test_path/test_command", "test_arguments"],
            stdout=DEVNULL,
            stderr=DEVNULL,
            shell=False,
            timeout=180,
        )
Example #16
0
def measure_lines_of_code(settings):
    """Measure the lines of code using cloc."""

    report_dir = create_report_directory(settings["report_directory"])
    report_file = os.path.join(report_dir, "code_duplication")

    command = [
        "cloc",
        "--csv",
        "--hide-rate",
        "--quiet",
        "--exclude-dir=test,tst",
        settings["analysis_directory"],
    ]

    process = Subprocess(command, verbose=1)
    output = process.execute_pipe(report_dir, report_file, check_return_code=False)

    return output.stdout.decode("utf-8")
Example #17
0
    def test_subprocess_throws_exception_when_providing_command_as_plain_string():
        """Test that subprocess throws an exception when a command is provided as plain string."""

        # Arrange
        command = "test_command test_arguments"

        # Act
        # Assert
        with raises(SubprocessRuntimeError):
            Subprocess(command)
Example #18
0
    def test_which_raises_exception_with_shutil_which_returning_none(self):
        """Test that which raises an exception with shutil."""

        # Arrange
        command = ["unexisting_test_command", "test_arguments"]
        self.which_mock.return_value = None

        # Act
        with raises(SubprocessRuntimeError):
            Subprocess(command)

        # Assert
        self.which_mock.assert_called_once_with("unexisting_test_command")
Example #19
0
def measure_code_duplication(settings):
    """Measure the amount of code duplication."""

    report_dir = create_report_directory(settings["report_directory"])
    report_file = os.path.join(report_dir, "code_duplication")

    measure_function_size_command = [
        "cpd",
        "--language",
        settings["language"],
        "--minimum-tokens",
        settings["tokens"],
        "--format",
        "csv",
        "--files",
        settings["analysis_directory"],
    ]

    process = Subprocess(measure_function_size_command, verbose=1)
    output = process.execute_pipe(report_dir, report_file, check_return_code=False)

    return output.stdout.decode("utf-8")
Example #20
0
    def test_which_is_successful_with_correct_parameters(self):
        """Test that which is successful with correct parameters."""

        # Arrange
        command = ["test_command", "test_arguments"]
        self.which_mock.return_value = "/long/test/path/test_command"

        # Act
        process = Subprocess(command)

        # Assert
        self.which_mock.assert_called_with("test_command")
        assert process.base_command == "test_command"
        assert process.command[0] == "/long/test/path/test_command"