def test_run_package_is_ignored(init_statick): """ Test that ignored package is ignored. Expected results: issues is empty and success is True """ args = Args("Statick tool") args.parser.add_argument("--path", help="Path of package to scan") statick = Statick(args.get_user_paths()) statick.gather_args(args.parser) sys.argv = [ "--path", os.path.join(os.path.dirname(__file__), "test_package"), "--exceptions", os.path.join(os.path.dirname(__file__), "rsc", "exceptions-test.yaml"), ] parsed_args = args.get_args(sys.argv) path = parsed_args.path statick.get_config(parsed_args) statick.get_exceptions(parsed_args) issues, success = statick.run(path, parsed_args) assert not issues assert success try: shutil.rmtree( os.path.join(os.path.dirname(__file__), "statick-sei_cert")) except OSError as ex: print("Error: {}".format(ex))
def test_run_output_is_not_directory(mocked_mkdir, init_statick): """Test running Statick against a missing directory.""" mocked_mkdir.side_effect = OSError("error") args = Args("Statick tool") args.parser.add_argument("--path", help="Path of package to scan") statick = Statick(args.get_user_paths()) statick.gather_args(args.parser) sys.argv = [ "--output-directory", "/tmp/not_a_directory", "--path", os.path.dirname(__file__), ] parsed_args = args.get_args(sys.argv) path = parsed_args.path statick.get_config(parsed_args) statick.get_exceptions(parsed_args) issues, success = statick.run(path, parsed_args) assert issues is None assert not success try: shutil.rmtree( os.path.join(os.path.dirname(__file__), "statick-sei_cert")) except OSError as ex: print("Error: {}".format(ex))
def test_run_force_tool_list(init_statick): """Test running Statick against a missing directory.""" args = Args("Statick tool") args.parser.add_argument("--path", help="Path of package to scan") statick = Statick(args.get_user_paths()) statick.gather_args(args.parser) sys.argv = [ "--path", os.path.dirname(__file__), "--force-tool-list", "bandit" ] args.output_directory = os.path.dirname(__file__) parsed_args = args.get_args(sys.argv) path = parsed_args.path statick.get_config(parsed_args) statick.get_exceptions(parsed_args) issues, success = statick.run(path, parsed_args) for tool in issues: assert not issues[tool] assert success try: shutil.rmtree( os.path.join(os.path.dirname(__file__), "statick-sei_cert")) except OSError as ex: print("Error: {}".format(ex))
def test_run_called_process_error(mock_subprocess_check_output): """ Test running Statick when each plugin has a CalledProcessError. Expected result: issues is None """ mock_subprocess_check_output.side_effect = subprocess.CalledProcessError( 1, "", output="mocked error" ) args = Args("Statick tool") args.parser.add_argument("--path", help="Path of package to scan") statick = Statick(args.get_user_paths()) statick.gather_args(args.parser) sys.argv = [ "--output-directory", os.path.dirname(__file__), "--path", os.path.dirname(__file__), ] parsed_args = args.get_args(sys.argv) path = parsed_args.path statick.get_config(parsed_args) statick.get_exceptions(parsed_args) issues, _ = statick.run(path, parsed_args) for tool in issues: assert not issues[tool] try: shutil.rmtree(os.path.join(os.path.dirname(__file__), "statick-sei_cert")) except OSError as ex: print(f"Error: {ex}")
def test_run_mkdir_oserror(mocked_mkdir, init_statick): """ Test the behavior when mkdir in run throws an OSError. Expected results: issues is None and success is False """ mocked_mkdir.side_effect = OSError("error") args = Args("Statick tool") args.parser.add_argument("--path", help="Path of package to scan") statick = Statick(args.get_user_paths()) statick.gather_args(args.parser) sys.argv = [ "--path", os.path.dirname(__file__), "--output-directory", os.path.dirname(__file__), ] parsed_args = args.get_args(sys.argv) path = parsed_args.path statick.get_config(parsed_args) statick.get_exceptions(parsed_args) issues, success = statick.run(path, parsed_args) assert issues is None assert not success try: shutil.rmtree(os.path.join(os.path.dirname(__file__), "statick-sei_cert")) except OSError as ex: print(f"Error: {ex}")
def test_run_file_cmd_does_not_exist(init_statick): """ Test when file command does not exist. Expected results: no issues found even though Python file without extension does have issues """ with modified_environ(PATH=""): args = Args("Statick tool") args.parser.add_argument("--path", help="Path of package to scan") statick = Statick(args.get_user_paths()) statick.gather_args(args.parser) sys.argv = [ "--path", os.path.join(os.path.dirname(__file__), "test_package"), "--output-directory", os.path.dirname(__file__), "--force-tool-list", "pylint", ] parsed_args = args.get_args(sys.argv) path = parsed_args.path statick.get_config(parsed_args) statick.get_exceptions(parsed_args) issues, success = statick.run(path, parsed_args) for tool in issues: assert not issues[tool] assert success try: shutil.rmtree(os.path.join(os.path.dirname(__file__), "test_package-sei_cert")) except OSError as ex: print(f"Error: {ex}")
def test_run_invalid_level(init_statick): """ Test that invalid profile results in invalid level. Expected results: issues is None and success is False """ args = Args("Statick tool") args.parser.add_argument("--path", help="Path of package to scan") statick = Statick(args.get_user_paths()) statick.gather_args(args.parser) sys.argv = [ "--path", os.path.dirname(__file__), "--profile", os.path.join(os.path.dirname(__file__), "rsc", "nonexistent.yaml"), ] args.output_directory = os.path.dirname(__file__) parsed_args = args.get_args(sys.argv) path = parsed_args.path statick.get_config(parsed_args) statick.get_exceptions(parsed_args) issues, success = statick.run(path, parsed_args) assert issues is None assert not success try: shutil.rmtree(os.path.join(os.path.dirname(__file__), "statick-sei_cert")) except OSError as ex: print(f"Error: {ex}")
def test_run_invalid_reporting_plugins(init_statick): """ Test that invalid reporting plugins returns unsuccessful. Expected results: issues is None and success is False """ args = Args("Statick tool") args.parser.add_argument("--path", help="Path of package to scan") statick = Statick(args.get_user_paths()) statick.gather_args(args.parser) sys.argv = [ "--path", os.path.dirname(__file__), "--profile", os.path.join( os.path.dirname(__file__), "rsc", "profile-missing-reporting-plugin.yaml" ), "--config", os.path.join( os.path.dirname(__file__), "rsc", "config-invalid-reporting-plugins.yaml" ), ] args.output_directory = os.path.dirname(__file__) parsed_args = args.get_args(sys.argv) path = parsed_args.path statick.get_config(parsed_args) statick.get_exceptions(parsed_args) issues, success = statick.run(path, parsed_args) assert issues is None assert not success try: shutil.rmtree(os.path.join(os.path.dirname(__file__), "statick-custom")) except OSError as ex: print(f"Error: {ex}")
def test_run_discovery_dependency(init_statick): """ Test that a discovery plugin can run its dependencies. Expected results: issues is None and success is False """ args = Args("Statick tool") args.parser.add_argument("--path", help="Path of package to scan") statick = Statick(args.get_user_paths()) statick.gather_args(args.parser) sys.argv = [ "--path", os.path.dirname(__file__), "--profile", os.path.join(os.path.dirname(__file__), "rsc", "profile-custom.yaml"), "--config", os.path.join( os.path.dirname(__file__), "rsc", "config-discovery-dependency.yaml" ), ] args.output_directory = os.path.dirname(__file__) parsed_args = args.get_args(sys.argv) path = parsed_args.path statick.get_config(parsed_args) statick.get_exceptions(parsed_args) _, success = statick.run(path, parsed_args) assert success try: shutil.rmtree(os.path.join(os.path.dirname(__file__), "statick-custom")) except OSError as ex: print(f"Error: {ex}")
def test_run_invalid_tool_plugin(init_statick): """ Test that a non-existent tool plugin results in failure. Expected results: issues is None and success is False """ args = Args("Statick tool") args.parser.add_argument("--path", help="Path of package to scan") statick = Statick(args.get_user_paths()) statick.gather_args(args.parser) sys.argv = [ "--path", os.path.dirname(__file__), "--profile", os.path.join(os.path.dirname(__file__), "rsc", "profile-missing-tool.yaml"), "--config", os.path.join(os.path.dirname(__file__), "rsc", "config-missing-tool.yaml"), ] parsed_args = args.get_args(sys.argv) path = parsed_args.path statick.get_config(parsed_args) statick.get_exceptions(parsed_args) issues, success = statick.run(path, parsed_args) assert issues is None assert not success try: shutil.rmtree( os.path.join(os.path.dirname(__file__), "statick-sei_cert")) except OSError as ex: print("Error: {}".format(ex))
def test_run(): """Test running Statick.""" args = Args("Statick tool") args.parser.add_argument("--path", help="Path of package to scan") statick = Statick(args.get_user_paths()) statick.gather_args(args.parser) sys.argv = [ "--output-directory", os.path.dirname(__file__), "--path", os.path.dirname(__file__), ] parsed_args = args.get_args(sys.argv) path = parsed_args.path statick.get_config(parsed_args) statick.get_exceptions(parsed_args) issues, success = statick.run(path, parsed_args) for tool in issues: assert not issues[tool] try: shutil.rmtree( os.path.join(os.path.dirname(__file__), "statick-sei_cert")) except OSError as ex: print("Error: {}".format(ex))
def test_run_missing_path(init_statick): """Test running Statick against a package that does not exist.""" args = Args("Statick tool") statick = Statick(args.get_user_paths()) statick.gather_args(args.parser) sys.argv = ["--output-directory", os.path.dirname(__file__)] parsed_args = args.get_args(sys.argv) path = "/tmp/invalid" statick.get_config(parsed_args) issues, success = statick.run(path, parsed_args) assert issues is None assert not success
def test_run_output_is_not_directory(init_statick): """Test running Statick against a missing directory.""" args = Args("Statick tool") args.parser.add_argument("--path", help="Path of package to scan") statick = Statick(args.get_user_paths()) statick.gather_args(args.parser) sys.argv = ["--output-directory", "/tmp/not_a_directory", "--path", os.path.dirname(__file__)] parsed_args = args.get_args(sys.argv) path = parsed_args.path statick.get_config(parsed_args) statick.get_exceptions(parsed_args) issues, success = statick.run(path, parsed_args) assert issues is None assert not success
def test_run_missing_path(init_statick): """Test running Statick against a package that does not exist.""" args = Args("Statick tool") statick = Statick(args.get_user_paths()) statick.gather_args(args.parser) sys.argv = ["--output-directory", os.path.dirname(__file__)] parsed_args = args.get_args(sys.argv) path = "/tmp/invalid" statick.get_config(parsed_args) issues, success = statick.run(path, parsed_args) assert issues is None assert not success try: shutil.rmtree(os.path.join(os.path.dirname(__file__), "statick-sei_cert")) except OSError as ex: print(f"Error: {ex}")
def test_run(): """Test running Statick.""" args = Args("Statick tool") args.parser.add_argument("--path", help="Path of package to scan") statick = Statick(args.get_user_paths()) statick.gather_args(args.parser) sys.argv = ["--output-directory", os.path.dirname(__file__), "--path", os.path.dirname(__file__)] parsed_args = args.get_args(sys.argv) path = parsed_args.path statick.get_config(parsed_args) statick.get_exceptions(parsed_args) issues, success = statick.run(path, parsed_args) for tool in issues: assert not issues[tool] assert not success
def test_run_tool_dependency(init_statick): """ Test that a tool plugin can run its dependencies. Expected results: issues is None and success is False """ cttp = ClangTidyToolPlugin() if not cttp.command_exists("clang-tidy"): pytest.skip("Can't find clang-tidy, unable to test clang-tidy plugin") args = Args("Statick tool") args.parser.add_argument("--path", help="Path of package to scan") statick = Statick(args.get_user_paths()) statick.gather_args(args.parser) sys.argv = [ "--path", os.path.dirname(__file__), "--profile", os.path.join(os.path.dirname(__file__), "rsc", "profile-custom.yaml"), "--config", os.path.join( os.path.dirname(__file__), "rsc", "config-enabled-dependency.yaml" ), "--force-tool-list", "clang-tidy", ] args.output_directory = os.path.dirname(__file__) parsed_args = args.get_args(sys.argv) path = parsed_args.path statick.get_config(parsed_args) statick.get_exceptions(parsed_args) issues, success = statick.run(path, parsed_args) for tool in issues: assert not issues[tool] assert success try: shutil.rmtree(os.path.join(os.path.dirname(__file__), "statick-custom")) except OSError as ex: print(f"Error: {ex}")
def test_run_missing_tool_dependency(init_statick): """ Test that a tool plugin results in failure when its dependency is not configured to run. Expected results: issues is None and success is False """ cttp = ClangTidyToolPlugin() if not cttp.command_exists("clang-tidy"): pytest.skip("Can't find clang-tidy, unable to test clang-tidy plugin") args = Args("Statick tool") args.parser.add_argument("--path", help="Path of package to scan") statick = Statick(args.get_user_paths()) statick.gather_args(args.parser) sys.argv = [ "--path", os.path.dirname(__file__), "--force-tool-list", "clang-tidy", "--config", os.path.join(os.path.dirname(__file__), "rsc", "config-missing-tool-dependency.yaml"), ] args.output_directory = os.path.dirname(__file__) parsed_args = args.get_args(sys.argv) path = parsed_args.path statick.get_config(parsed_args) statick.get_exceptions(parsed_args) issues, success = statick.run(path, parsed_args) assert issues is None assert not success try: shutil.rmtree( os.path.join(os.path.dirname(__file__), "statick-sei_cert")) except OSError as ex: print("Error: {}".format(ex))