def test_cpplint_tool_plugin_scan_missing_fields():
    """
    Test what happens when key fields are missing from the Package argument.

    Expected result: issues is None then empty
    """
    ctp = setup_cpplint_tool_plugin()

    # Missing tool name in package.
    package = Package(
        "valid_package", os.path.join(os.path.dirname(__file__), "valid_package")
    )
    package["make_targets"] = []
    package["make_targets"].append({})
    package["make_targets"][0]["src"] = [
        os.path.join(os.path.dirname(__file__), "valid_package", "test.c")
    ]
    issues = ctp.scan(package, "level")
    assert issues is None

    # Empty make_targets and headers in package.
    package = Package(
        "valid_package", os.path.join(os.path.dirname(__file__), "valid_package")
    )
    package["make_targets"] = []
    package["headers"] = []
    issues = ctp.scan(package, "level")
    assert not issues

    # Missing make_targets and headers in package.
    package = Package(
        "valid_package", os.path.join(os.path.dirname(__file__), "valid_package")
    )
    issues = ctp.scan(package, "level")
    assert not issues
Example #2
0
def test_discovery_plugin_find_files_multiple():
    """Test that find_files will only walk the path once."""
    dp = DiscoveryPlugin()
    package = Package("valid_package",
                      os.path.join(os.path.dirname(__file__), "valid_package"))
    package._walked = True  # pylint: disable=protected-access
    expected_dict = {}

    dp.find_files(package)

    assert package._walked  # pylint: disable=protected-access
    assert package.files == expected_dict
Example #3
0
def test_cmake_discovery_plugin_scan_valid():
    """Test the CMake discovery plugin with a valid directory."""
    cmdp = setup_cmake_discovery_plugin()
    package = Package('valid_package', os.path.join(os.path.dirname(__file__),
                                                    'valid_package'))
    cmdp.scan(package, 'level')
    assert package['cmake']
def test_catkin_lint_tool_plugin_parse_output():
    """
    Check that manual exceptions are applied.

    These tests make sure the functionality works in the absence of the tool. If the
    tool is installed the lines dealing with manual exceptions are already covered.
    """
    cltp = setup_catkin_lint_tool_plugin()
    package = Package("valid_package",
                      os.path.join(os.path.dirname(__file__), "cmake_flags"))
    package["catkin"] = "catkin"

    output = (
        "empty_pkg: CMakeLists.txt(4): warning: variable CMAKE_CXX_FLAGS is modified"
    )
    issues = cltp.parse_output(package, output)
    assert not issues

    output = (
        "empty_pkg: CMakeLists.txt(5): warning: variable CMAKE_CXX_FLAGS is modified"
    )
    issues = cltp.parse_output(package, output)
    assert not issues

    output = "empty_pkg: CMakeLists.txt(6): warning: variable CMAKE_C_FLAGS is modified"
    issues = cltp.parse_output(package, output)
    assert not issues
def test_clang_format_tool_plugin_scan_valid():
    """Integration test: Make sure the clang_format output hasn't changed."""
    cftp = setup_clang_format_tool_plugin(do_raise=True)
    if not cftp.command_exists("clang-format"):
        pytest.skip("Missing clang-format executable.")
    package = Package("valid_package",
                      os.path.join(os.path.dirname(__file__), "valid_package"))
    # Copy the latest clang_format over
    shutil.copyfile(
        cftp.plugin_context.resources.get_file("_clang-format"),
        os.path.join(os.path.expanduser("~"), "_clang-format"),
    )
    package["make_targets"] = []
    package["make_targets"].append({})
    package["make_targets"][0]["src"] = [
        os.path.join(os.path.dirname(__file__), "valid_package", "indents.c")
    ]
    package["headers"] = [
        os.path.join(os.path.dirname(__file__), "valid_package", "indents.h")
    ]
    issues = cftp.scan(package, "level")
    assert len(issues) == 1

    cftp = setup_clang_format_tool_plugin()
    issues = cftp.scan(package, "level")
    assert not issues

    if os.path.exists(os.path.join(os.path.expanduser("~"), "_clang-format")):
        os.remove(os.path.join(os.path.expanduser("~"), "_clang-format"))
Example #6
0
def test_stylelint_tool_plugin_scan_calledprocesserror(
        mock_subprocess_check_output):
    """
    Test what happens when a CalledProcessError is raised (usually means stylelint hit an error).

    Expected result: issues is None
    """
    mock_subprocess_check_output.side_effect = subprocess.CalledProcessError(
        0, '', output="mocked error")
    plugin = setup_stylelint_tool_plugin()
    package = Package('valid_package',
                      os.path.join(os.path.dirname(__file__), 'valid_package'))
    package['html_src'] = [
        os.path.join(os.path.dirname(__file__), 'valid_package', 'test.html')
    ]
    package['css_src'] = [
        os.path.join(os.path.dirname(__file__), 'valid_package', 'test.css')
    ]
    issues = plugin.scan(package, 'level')
    assert issues is None

    mock_subprocess_check_output.side_effect = subprocess.CalledProcessError(
        1, '', output="mocked error")
    issues = plugin.scan(package, 'level')
    assert not issues
def test_clang_tidy_tool_plugin_scan_custom_version():
    """Test that issues are found when a custom version is specified."""
    cttp = setup_clang_tidy_tool_plugin()
    if not cttp.command_exists("cmake"):
        pytest.skip("Can't find CMake, unable to test clang_tidy plugin")
    elif not cttp.command_exists("clang-tidy"):
        pytest.skip("Can't find clang-tidy, unable to test clang_tidy plugin")
    package = Package(
        "valid_package", os.path.join(os.path.dirname(__file__), "valid_package")
    )

    package["make_targets"] = []
    package["make_targets"].append({})
    package["make_targets"][0]["src"] = [
        os.path.join(os.path.dirname(__file__), "valid_package", "test.c")
    ]
    with TemporaryDirectory() as bin_dir:
        package["bin_dir"] = bin_dir
    package["src_dir"] = os.path.join(os.path.dirname(__file__), "valid_package")
    issues = cttp.scan(package, "unit_tests")
    assert len(issues) == 1
    assert issues[0].line_number == "6"
    assert issues[0].tool == "clang-tidy"
    assert issues[0].issue_type == "warning/clang-analyzer-deadcode.DeadStores"
    assert issues[0].severity == "3"
    assert issues[0].message == "Value stored to 'si' is never read"
Example #8
0
    def get_level(self, path: str, args: argparse.Namespace) -> Optional[str]:
        """Get level to scan package at."""
        path = os.path.abspath(path)

        profile_filename = "profile.yaml"
        if args.profile is not None:
            profile_filename = args.profile
        profile_resource = self.resources.get_file(profile_filename)
        if profile_resource is None:
            logging.error("Could not find profile file %s!", profile_filename)
            return None
        try:
            profile = Profile(profile_resource)
        except OSError as ex:
            # This isn't quite redundant with the profile_resource check: it's possible
            # that something else triggers an OSError, like permissions.
            logging.error("Failed to access profile file %s: %s",
                          profile_filename, ex)
            return None
        except ValueError as ex:
            logging.error("Profile file %s has errors: %s", profile_filename,
                          ex)
            return None

        package = Package(os.path.basename(path), path)
        level = profile.get_package_level(package)

        return level
def test_fortify_plugin_check_oserror(check_output_mock, fortify_tool_plugin):
    """Test behavior when command_exists says maven is unavailable."""
    check_output_mock.side_effect = OSError("error")
    package = Package('test', os.path.dirname(__file__))
    with tempfile.NamedTemporaryFile() as tmp_file:
        retval = fortify_tool_plugin._scan_maven(package, tmp_file)  # pylint: disable=protected-access
    assert not retval
Example #10
0
def test_scan_package_with_issues(init_statick_ws):
    """Test running Statick via the scan_package function used in multiprocessing."""
    statick = init_statick_ws[0]
    args = init_statick_ws[1]
    sys.argv = [
        "--output-directory",
        os.path.dirname(__file__),
        "--path",
        os.path.join(os.path.dirname(__file__), "test_package"),
        "--config",
        os.path.join(os.path.dirname(__file__), "rsc",
                     "config-no-reporting-plugins.yaml"),
        "--exceptions",
        os.path.join(os.path.dirname(__file__), "rsc", "exceptions.yaml"),
    ]

    parsed_args = args.get_args(sys.argv)
    path = parsed_args.path
    statick.get_config(parsed_args)
    statick.get_exceptions(parsed_args)
    package = Package("test_package", path)

    issues = statick.scan_package(parsed_args, 1, package, 1)

    assert len(issues["pylint"]) == 1

    try:
        shutil.rmtree(
            os.path.join(os.path.dirname(__file__), "test_package-sei_cert"))
    except OSError as ex:
        print("Error: {}".format(ex))
Example #11
0
def test_shellcheck_tool_plugin_scan_calledprocesserror(
        mock_subprocess_check_output):
    """
    Test what happens when a CalledProcessError is raised (usually means shellcheck hit an error).

    Expected result: issues is None
    """
    mock_subprocess_check_output.side_effect = subprocess.CalledProcessError(
        0, "", output="mocked error")
    sctp = setup_shellcheck_tool_plugin()
    # Sanity check - make sure shellcheck exists
    if not sctp.command_exists("shellcheck"):
        pytest.skip("Couldn't find 'shellcheck' command, can't run tests")
    elif sys.platform == "win32":
        pytest.skip("Don't know how to run shellcheck on Windows.")
    package = Package("valid_package",
                      os.path.join(os.path.dirname(__file__), "valid_package"))
    package["shell_src"] = [
        os.path.join(os.path.dirname(__file__), "valid_package",
                     "shellcheck_test.sh")
    ]
    issues = sctp.scan(package, "level")
    assert not issues

    mock_subprocess_check_output.side_effect = subprocess.CalledProcessError(
        32, "", output="mocked error")
    issues = sctp.scan(package, "level")
    assert issues is None
Example #12
0
def test_filter_issues_nolint_not_abs_path():
    """
    Test that issues are not filtered based on NOLINT comment when not absolute path.

    Expected result: one issue found
    """
    package = Package(
        "valid_package", os.path.join(os.path.dirname(__file__), "valid_package")
    )
    exceptions = Exceptions(
        os.path.join(os.path.dirname(__file__), "valid_exceptions.yaml")
    )

    filename = "valid_package/x.py"
    line_number = "3"
    tool = "pylint"
    issue_type = "missing-docstring"
    severity = "3"
    message = "C0111: Missing module docstring"
    tool_issue = Issue(filename, line_number, tool, issue_type, severity, message, None)
    issues = {}
    issues["pylint"] = [tool_issue]

    issues = exceptions.filter_issues(package, issues)
    assert len(issues["pylint"]) == 1
Example #13
0
def test_scan_package(init_statick_ws):
    """Test running Statick via the scan_package function used in multiprocessing."""
    statick = init_statick_ws[0]
    args = init_statick_ws[1]
    sys.argv = [
        "--output-directory",
        os.path.dirname(__file__),
        "--path",
        "/tmp/not_a_package",
    ]

    parsed_args = args.get_args(sys.argv)
    path = parsed_args.path
    statick.get_config(parsed_args)
    statick.get_exceptions(parsed_args)
    package = Package("statick", path)

    issues = statick.scan_package(parsed_args, 1, package, 1)

    assert issues is None

    try:
        shutil.rmtree(
            os.path.join(os.path.dirname(__file__), "statick-sei_cert"))
    except OSError as ex:
        print("Error: {}".format(ex))
Example #14
0
def test_filter_issues_filename_abs_path():
    """
    Test that issues are filtered based on regex exceptions with absolute path.

    Expected result: no issues found
    """
    package = Package(
        "valid_package", os.path.join(os.path.dirname(__file__), "valid_package")
    )
    exceptions = Exceptions(
        os.path.join(os.path.dirname(__file__), "valid_exceptions.yaml")
    )

    filename = "/home/travis/build/x.py"
    line_number = "4"
    tool = "pylint"
    issue_type = "R0205(useless-object-inheritance)"
    severity = "5"
    message = "R0205: Class 'Example' inherits from object, can be safely removed from bases in python3"
    tool_issue = Issue(filename, line_number, tool, issue_type, severity, message, None)
    issues = {}
    issues["pylint"] = [tool_issue]

    issues = exceptions.filter_issues(package, issues)
    assert not issues["pylint"]
def test_cccc_tool_plugin_scan_empty_calledprocesserror(mock_subprocess_check_output):
    """
    Test what happens when a CalledProcessError is hit (such as if cccc encounters an error).

    Expected result: issues is an empty list
    """
    mock_subprocess_check_output.side_effect = subprocess.CalledProcessError(
        0, "", output=b"mocked error"
    )
    ctp = setup_cccc_tool_plugin()
    if not ctp.command_exists("cccc"):
        pytest.skip("Missing cccc executable.")
    package = Package(
        "valid_package", os.path.join(os.path.dirname(__file__), "valid_package")
    )
    package["c_src"] = [
        os.path.join(os.path.dirname(__file__), "valid_package", "example.cpp")
    ]
    issues = ctp.scan(package, "level")
    assert issues is None

    mock_subprocess_check_output.side_effect = subprocess.CalledProcessError(
        1, "", output=b"mocked error"
    )
    issues = ctp.scan(package, "level")
    assert not issues
def test_cccc_tool_plugin_parse_missing_names():
    """Verify that we can parse the output of CCCC when fields are missing."""
    ctp = setup_cccc_tool_plugin()

    # Copy the latest configuration file over.
    shutil.copyfile(
        ctp.plugin_context.resources.get_file("cccc.opt"),
        os.path.join(os.path.dirname(__file__), "cccc.opt"),
    )
    config_file = ctp.plugin_context.resources.get_file("cccc.opt")

    output_file = os.path.join(
        os.path.dirname(__file__), "valid_package", "cccc-missing-names.xml"
    )
    with open(output_file) as f:
        output = xmltodict.parse(f.read())

    package = Package(
        "valid_package", os.path.join(os.path.dirname(__file__), "valid_package")
    )
    package["c_src"] = ["tmp/not_a_file.c"]

    issues = ctp.parse_output(output, package, config_file)
    print(f"issues: {issues}")
    assert not issues
def test_catkin_discovery_plugin_scan_valid():
    """Test the behavior when the Catkin plugin scans a valid package."""
    cdp = CatkinDiscoveryPlugin()
    package = Package('valid_package',
                      os.path.join(os.path.dirname(__file__), 'valid_package'))
    cdp.scan(package, 'level')
    assert package['catkin']
Example #18
0
def test_write_jenkins_warnings_ng_reporting_plugin_report_nocert():
    """Test the output of the reporting plugin without a CERT reference."""
    with TemporaryDirectory() as tmp_dir:
        wfrp = setup_write_jenkins_warnings_ng_reporting_plugin(tmp_dir)
        package = Package(
            'valid_package',
            os.path.join(os.path.dirname(__file__), 'valid_package'))
        issues = {
            'tool_a': [
                Issue('test.txt', 1, 'tool_a', 'type', "1", 'This is a test',
                      None)
            ]
        }
        _, success = wfrp.report(package, issues, 'level')
        assert success
        with open(
                os.path.join(tmp_dir, 'valid_package-level',
                             'valid_package-level.json.statick')) as outfile:
            line = outfile.readline().strip()
    expected_dict = {
        "fileName": "test.txt",
        "severity": "NORMAL",
        "lineStart": 1,
        "message": "This is a test",
        "category": "tool_a",
        "type": "type"
    }
    output_dict = json.loads(line)
    assert output_dict == expected_dict
    assert re.match(output_regex, line)
    assert line == "{\"category\": \"tool_a\", \"fileName\": \"test.txt\", \"lineStart\": 1, \"message\": \"This is a test\", \"severity\": \"NORMAL\", \"type\": \"type\"}"
    assert line == json.dumps(expected_dict, sort_keys=True)
Example #19
0
def test_writegood_tool_plugin_scan_valid_with_issues():
    """Integration test: Make sure the writegood output hasn't changed."""
    plugin = setup_writegood_tool_plugin()
    if not plugin.command_exists("writegood"):
        pytest.skip("Missing writegood executable.")
    package = Package(
        "valid_package", os.path.join(os.path.dirname(__file__), "valid_package")
    )
    package["md_src"] = [
        os.path.join(os.path.dirname(__file__), "valid_package", "test.md"),
    ]
    issues = plugin.scan(package, "level")
    assert len(issues) == 3
    assert issues[0].filename == os.path.join(os.path.dirname(__file__), "valid_package", "test.md")
    assert issues[0].line_number == "4"
    assert issues[0].tool == "writegood"
    assert issues[0].issue_type == "suggestion"
    assert issues[0].severity == "1"
    assert issues[0].message == "\"So\" adds no meaning"
    assert issues[1].filename == os.path.join(os.path.dirname(__file__), "valid_package", "test.md")
    assert issues[1].line_number == "4"
    assert issues[1].tool == "writegood"
    assert issues[1].issue_type == "suggestion"
    assert issues[1].severity == "1"
    assert issues[1].message == "\"was stolen\" may be passive voice"
    assert issues[2].filename == os.path.join(os.path.dirname(__file__), "valid_package", "test.md")
    assert issues[2].line_number == "6"
    assert issues[2].tool == "writegood"
    assert issues[2].issue_type == "suggestion"
    assert issues[2].severity == "1"
    assert issues[2].message == "\"only\" can weaken meaning"
def test_perlcritic_tool_plugin_scan_valid():
    """Integration test: Make sure the perlcritic output hasn't changed."""
    pctp = setup_perlcritic_tool_plugin()
    if not pctp.command_exists("perlcritic"):
        pytest.skip("perlcritic command not available, can't test its output")
    package = Package("valid_package",
                      os.path.join(os.path.dirname(__file__), "valid_package"))

    # Do not include perl_src
    issues = pctp.scan(package, "level")
    assert not issues

    # Pass in empty perl_src
    package["perl_src"] = []
    issues = pctp.scan(package, "level")
    assert not issues

    package["perl_src"] = [
        os.path.join(os.path.dirname(__file__), "valid_package", "test.pl")
    ]
    issues = pctp.scan(package, "level")
    assert len(issues) == 1
    assert issues[0].line_number == "2"
    assert issues[0].tool == "perlcritic"
    assert issues[0].issue_type == "InputOutput::ProhibitBarewordFileHandles"
    assert issues[0].severity == "5"
    assert issues[0].message == "Bareword file handle opened"
def test_shell_discovery_plugin_scan_exceptions():
    """Test that the shell discovery plugin properly respects exceptions."""
    shdp = ShellDiscoveryPlugin()
    if not shdp.file_command_exists():
        pytest.skip(
            "File command does not exist. Skipping test that requires it.")
    package = Package("valid_package",
                      os.path.join(os.path.dirname(__file__), "valid_package"))
    exceptions = Exceptions(
        os.path.join(os.path.dirname(__file__), "exceptions.yaml"))
    shdp.scan(package, "level", exceptions)
    expected_src = [
        "test.sh",
        "oddextensionsh.source",
        "oddextensionbash.source",
        "oddextensionzsh.source",
        "oddextensioncsh.source",
        "oddextensionksh.source",
        "oddextensiondash.source",
    ]
    # We have to add the path to each of the above...yuck
    expected_src_fullpath = [
        os.path.join(package.path, filename) for filename in expected_src
    ]
    # Neat trick to verify that two unordered lists are the same
    assert set(package["shell_src"]) == set(expected_src_fullpath)
def test_clang_format_tool_plugin_scan_oserror_raise_bin(
        mock_subprocess_check_output):
    """
    Test what happens when an OSError is raised (usually means clang-format doesn't exist).

    Expected result: issues is None
    """
    mock_subprocess_check_output.side_effect = OSError("mocked error")
    cftp = setup_clang_format_tool_plugin_non_default()
    shutil.copyfile(
        cftp.plugin_context.resources.get_file("_clang-format"),
        os.path.join(os.path.expanduser("~"), "_clang-format"),
    )
    package = Package("valid_package",
                      os.path.join(os.path.dirname(__file__), "valid_package"))
    package["make_targets"] = []
    package["make_targets"].append({})
    package["make_targets"][0]["src"] = [
        os.path.join(os.path.dirname(__file__), "valid_package", "indents.c")
    ]
    package["headers"] = [
        os.path.join(os.path.dirname(__file__), "valid_package", "indents.h")
    ]
    issues = cftp.scan(package, "level")
    assert not issues
def test_eslint_tool_plugin_scan_calledprocesserror(mock_subprocess_check_output):
    """
    Test what happens when a CalledProcessError is raised (usually means eslint hit an error).

    Expected result: issues is None
    """
    mock_subprocess_check_output.side_effect = subprocess.CalledProcessError(
        0, "", output="mocked error"
    )
    plugin = setup_eslint_tool_plugin()
    package = Package(
        "valid_package", os.path.join(os.path.dirname(__file__), "valid_package")
    )
    package["html_src"] = [
        os.path.join(os.path.dirname(__file__), "valid_package", "test.html")
    ]
    package["javascript_src"] = [
        os.path.join(os.path.dirname(__file__), "valid_package", "test.js")
    ]
    issues = plugin.scan(package, "level")
    assert issues is None

    mock_subprocess_check_output.side_effect = subprocess.CalledProcessError(
        2, "", output="mocked error"
    )
    issues = plugin.scan(package, "level")
    assert not issues
def test_cppcheck_tool_plugin_scan_valid():
    """Integration test: Make sure the cppcheck output hasn't changed."""
    cctp = setup_cppcheck_tool_plugin()
    if not cctp.command_exists('cppcheck'):
        pytest.skip("Can't find cppcheck, unable to test cppcheck plugin")
    package = Package('valid_package',
                      os.path.join(os.path.dirname(__file__), 'valid_package'))

    package['make_targets'] = []
    package['make_targets'].append({})
    package['make_targets'][0]['src'] = [
        os.path.join(os.path.dirname(__file__), 'valid_package', 'test.c')
    ]
    package['make_targets'][0]['include_dirs'] = [
        os.path.join(os.path.dirname(__file__), 'valid_package')
    ]
    package['headers'] = []
    package['path'] = os.path.join(os.path.dirname(__file__), 'valid_package')
    issues = cctp.scan(package, 'level')
    assert len(issues) == 1
    assert issues[0].filename == os.path.join(os.path.dirname(__file__),
                                              'valid_package', 'test.c')
    assert issues[0].line_number == '4'
    assert issues[0].tool == 'cppcheck'
    assert issues[0].issue_type == 'error/uninitvar'
    assert issues[0].severity == '5'
    assert issues[0].message == "Uninitialized variable: si"
def test_cppcheck_tool_plugin_scan_valid():
    """Integration test: Make sure the cppcheck output hasn't changed."""
    cctp = setup_cppcheck_tool_plugin()
    if not cctp.command_exists("cppcheck"):
        pytest.skip("Can't find cppcheck, unable to test cppcheck plugin")
    package = Package(
        "valid_package", os.path.join(os.path.dirname(__file__), "valid_package")
    )

    package["make_targets"] = []
    package["make_targets"].append({})
    package["make_targets"][0]["src"] = [
        os.path.join(os.path.dirname(__file__), "valid_package", "test.c")
    ]
    package["make_targets"][0]["include_dirs"] = [
        os.path.join(os.path.dirname(__file__), "valid_package")
    ]
    package["headers"] = []
    package["path"] = os.path.join(os.path.dirname(__file__), "valid_package")
    issues = cctp.scan(package, "level")
    assert len(issues) == 1
    assert issues[0].filename == os.path.join(
        os.path.dirname(__file__), "valid_package", "test.c"
    )
    assert issues[0].line_number == "4"
    assert issues[0].tool == "cppcheck"
    assert issues[0].issue_type == "error/uninitvar"
    assert issues[0].severity == "5"
    assert issues[0].message == "Uninitialized variable: si"
Example #26
0
def test_cmake_discovery_plugin_empty_cmake_flags():
    """Test the CMake discovery plugin without custom CMake flags."""
    cmdp = setup_cmake_discovery_plugin(True, None)
    package = Package("valid_package",
                      os.path.join(os.path.dirname(__file__), "valid_package"))
    cmdp.scan(package, "level")
    assert "cmake" in package
Example #27
0
def test_cccc_tool_plugin_parse_valid():
    """Verify that we can parse the normal output of CCCC."""
    ctp = setup_cccc_tool_plugin()

    # Copy the latest configuration file over.
    shutil.copyfile(ctp.plugin_context.resources.get_file("cccc.opt"),
                    os.path.join(os.path.dirname(__file__), 'cccc.opt'))
    config_file = ctp.plugin_context.resources.get_file('cccc.opt')

    output_file = os.path.join(os.path.dirname(__file__), 'valid_package',
                               'cccc.xml')
    with open(output_file) as f:
        output = xmltodict.parse(f.read())

    package = Package('valid_package',
                      os.path.join(os.path.dirname(__file__), 'valid_package'))
    package['c_src'] = ['tmp/not_a_file.c']

    issues = ctp.parse_output(output, package, config_file)
    print('issues: {}'.format(issues))
    assert len(issues) == 1
    assert issues[0].filename == 'tmp/not_a_file.c'
    assert issues[0].line_number == 0
    assert issues[0].tool == 'cccc'
    assert issues[0].issue_type == 'warn'
    assert issues[0].severity == 3
    assert issues[
        0].message == 'Example1 - Fan in (concrete uses only) - value: 7.0, theshold: 6.0'
Example #28
0
def test_spotbugs_tool_plugin_scan_no_plugin_context():
    """Test that issues are None when no plugin context is provided."""
    sbtp = setup_spotbugs_tool_plugin(False)
    # Sanity check - make sure mvn exists
    if not sbtp.command_exists("mvn"):
        pytest.skip("Couldn't find 'mvn' command, can't run Spotbugs tests")
    elif sys.platform == "win32":
        pytest.skip("Don't know how to run spotbugs on Windows.")

    package = Package("valid_package",
                      os.path.join(os.path.dirname(__file__), "valid_package"))
    # Have to compile the package first
    try:
        subprocess.check_output(["mvn", "clean", "compile"],
                                universal_newlines=True,
                                cwd=package.path)
    except subprocess.CalledProcessError as ex:
        print("Problem running Maven! Returncode = {}".format(
            str(ex.returncode)))
        print("{}".format(ex.output))
        pytest.fail("Failed running Maven")

    package["top_poms"] = [os.path.join(package.path, "pom.xml")]
    package["all_poms"] = [os.path.join(package.path, "pom.xml")]
    issues = sbtp.scan(package, "level")
    assert issues is None
Example #29
0
def test_spotbugs_tool_plugin_scan_valid():
    """Integration test: Make sure the spotbugs output hasn't changed."""
    sbtp = setup_spotbugs_tool_plugin()
    # Sanity check - make sure mvn exists
    if not sbtp.command_exists("mvn"):
        pytest.skip("Couldn't find 'mvn' command, can't run Spotbugs tests")
    elif sys.platform == "win32":
        pytest.skip("Don't know how to run spotbugs on Windows.")

    package = Package("valid_package",
                      os.path.join(os.path.dirname(__file__), "valid_package"))
    # Have to compile the package first
    try:
        subprocess.check_output(["mvn", "clean", "compile"],
                                universal_newlines=True,
                                cwd=package.path)
    except subprocess.CalledProcessError as ex:
        print("Problem running Maven! Returncode = {}".format(
            str(ex.returncode)))
        print("{}".format(ex.output))
        pytest.fail("Failed running Maven")

    package["top_poms"] = [os.path.join(package.path, "pom.xml")]
    package["all_poms"] = [os.path.join(package.path, "pom.xml")]
    issues = sbtp.scan(package, "level")
    assert len(issues) == 1
    assert issues[0].line_number == "4"
    assert issues[0].tool == "spotbugs"
    assert issues[0].issue_type == "MS_MUTABLE_COLLECTION_PKGPROTECT"
    assert issues[0].severity == "1"
    assert (issues[0].message ==
            "Test.h is a mutable collection which should be package protected")
def test_clang_format_tool_plugin_scan_calledprocesserror(
        mock_subprocess_check_output):
    """
    Test what happens when a CalledProcessError is raised (usually means clang-format hit an error).

    Expected result: issues is empty (no raise) or None (raise)
    """
    mock_subprocess_check_output.side_effect = subprocess.CalledProcessError(
        1, "", output="mocked error")
    cftp = setup_clang_format_tool_plugin()
    shutil.copyfile(
        cftp.plugin_context.resources.get_file("_clang-format"),
        os.path.join(os.path.expanduser("~"), "_clang-format"),
    )
    package = Package("valid_package",
                      os.path.join(os.path.dirname(__file__), "valid_package"))
    package["make_targets"] = []
    package["make_targets"].append({})
    package["make_targets"][0]["src"] = [
        os.path.join(os.path.dirname(__file__), "valid_package", "indents.c")
    ]
    package["headers"] = []
    issues = cftp.scan(package, "level")
    assert not issues

    cftp = setup_clang_format_tool_plugin(do_raise=True)
    issues = cftp.scan(package, "level")
    assert issues is None

    if os.path.exists(os.path.join(os.path.expanduser("~"), "_clang-format")):
        os.remove(os.path.join(os.path.expanduser("~"), "_clang-format"))