def test_xsoar_linter_errors(mocker, file, python_version, support_level, long_running, exit_code, error_msgs): """ Given - file to run the linter on. - Python version of the file. - expected exit code of the xsoar linter function. - expected error messages of the xosar linter. When - Running xsoar linter using demisto lint. Then - Ensure valid files pass with the correct exit code. - Ensure valid files pass with no error messages. - Ensure invalid files fail with the correct exit code. - Ensure invalid files fail with the correct error messages. """ mocker.patch.object(linter.Linter, '_docker_login') mocker.patch.object(linter.Linter, '_update_support_level') linter.Linter._docker_login.return_value = False test_path = Path(f"{GIT_ROOT}/demisto_sdk/tests/test_files") runner = linter.Linter(content_repo=test_path, pack_dir=test_path, req_2=[], req_3=[], docker_engine=True) runner._facts['support_level'] = support_level runner._facts['is_long_running'] = long_running exit_code_actual, output = runner._run_xsoar_linter(python_version, [file]) assert exit_code == exit_code_actual for msg in error_msgs: assert msg in output
def initiate_linter(demisto_content, integration_path, docker_engine=False): return linter.Linter(content_repo=demisto_content, pack_dir=Path(integration_path), req_2=[], req_3=[], docker_engine=docker_engine, docker_timeout=60)
def test_valid_yaml_key_script_is_dict(self, demisto_content, create_integration: Callable): integration_path: Path = create_integration( content_path=demisto_content, type_script_key=True) runner = linter.Linter(content_repo=demisto_content, pack_dir=integration_path, req_2=[], req_3=[], docker_engine=False) assert not runner._gather_facts(modules={})
def test_package_is_not_python_pack(self, demisto_content: Callable, create_integration: Callable): integration_path: Path = create_integration( content_path=demisto_content, js_type=True) runner = linter.Linter(content_repo=demisto_content, pack_dir=integration_path, req_2=[], req_3=[], docker_engine=False) assert runner._gather_facts(modules={})
def test_package_is_not_python_pack(self, demisto_content: Callable, create_integration: Callable): from demisto_sdk.commands.lint import linter from wcmatch.pathlib import Path integration_path: Path = create_integration(content_path=demisto_content, js_type=True) runner = linter.Linter(content_repo=demisto_content, pack_dir=integration_path, req_2=[], req_3=[], docker_engine=False) assert runner._gather_facts(modules={})
def test_valid_yaml_key_script_is_not_dict(self, demisto_content: Callable, create_integration: Callable): from demisto_sdk.commands.lint import linter from wcmatch.pathlib import Path integration_path: Path = create_integration(content_path=demisto_content, type_script_key=False) runner = linter.Linter(content_repo=demisto_content, pack_dir=integration_path, req_2=[], req_3=[], docker_engine=False) assert not runner._gather_facts(modules={})
def test_package_is_python_pack(self, demisto_content: Callable, create_integration: Callable, mocker): integration_path: Path = create_integration( content_path=demisto_content, js_type=False) mocker.patch.object(linter.Linter, '_update_support_level') runner = linter.Linter(content_repo=demisto_content, pack_dir=integration_path, req_2=[], req_3=[], docker_engine=False) assert not runner._gather_facts(modules={})
def test_test_requirements_not_exists(self, mocker, demisto_content: Callable, create_integration: Callable): from demisto_sdk.commands.lint import linter from wcmatch.pathlib import Path mocker.patch.object(linter.Linter, '_docker_login') linter.Linter._docker_login.return_value = False integration_path: Path = create_integration(content_path=demisto_content) runner = linter.Linter(content_repo=demisto_content, pack_dir=integration_path, req_2=[], req_3=[], docker_engine=True) runner._gather_facts(modules={}) assert not runner._facts["additional_requirements"]
def test_lint_files_not_exists(self, mocker, demisto_content: Callable, create_integration: Callable): mocker.patch.object(linter.Linter, '_docker_login') linter.Linter._docker_login.return_value = False integration_path: Path = create_integration( content_path=demisto_content, no_lint_file=True) runner = linter.Linter(content_repo=demisto_content, pack_dir=integration_path, req_2=[], req_3=[], docker_engine=True) runner._gather_facts(modules={}) assert not runner._facts["lint_files"]
def test_lint_files_exists(self, mocker, demisto_content: Callable, create_integration: Callable): from demisto_sdk.commands.lint import linter from wcmatch.pathlib import Path mocker.patch.object(linter.Linter, '_docker_login') linter.Linter._docker_login.return_value = False integration_path: Path = create_integration(content_path=demisto_content, no_lint_file=False) runner = linter.Linter(content_repo=demisto_content, pack_dir=integration_path, req_2=[], req_3=[], docker_engine=True) runner._gather_facts(modules={}) assert runner._facts["lint_files"][0] == integration_path / f'{integration_path.name}.py' assert runner._facts['lint_unittest_files'][0] == integration_path / f'{integration_path.name}_test.py'
def test_test_requirements_exists(self, mocker, demisto_content: Callable, create_integration: Callable): mocker.patch.object(linter.Linter, '_docker_login') linter.Linter._docker_login.return_value = False integration_path: Path = create_integration( content_path=demisto_content, test_reqs=True) runner = linter.Linter(content_repo=demisto_content, pack_dir=integration_path, req_2=[], req_3=[], docker_engine=True) runner._gather_facts(modules={}) assert runner._facts["additional_requirements"] test_requirements = ['mock', 'pre-commit', 'pytest'] for test_req in test_requirements: assert test_req in runner._facts["additional_requirements"]
def test_docker_images_not_exists(self, mocker, demisto_content: Callable, create_integration: Callable): exp_image = "demisto/python:1.3-alpine" exp_py_num = 2.7 mocker.patch.object(linter.Linter, '_docker_login') linter.Linter._docker_login.return_value = False integration_path: Path = create_integration( content_path=demisto_content, image="", image_py_num=exp_py_num) runner = linter.Linter(content_repo=demisto_content, pack_dir=integration_path, req_2=[], req_3=[], docker_engine=True) runner._gather_facts(modules={}) assert runner._facts["images"][0][0] == exp_image assert runner._facts["images"][0][1] == exp_py_num
def test_docker_images_exists(self, mocker, demisto_content: Callable, create_integration: Callable): from demisto_sdk.commands.lint import linter from wcmatch.pathlib import Path exp_image = "test-image:12.0" exp_py_num = 2.7 mocker.patch.object(linter.Linter, '_docker_login') linter.Linter._docker_login.return_value = False integration_path: Path = create_integration(content_path=demisto_content, image=exp_image, image_py_num=exp_py_num) runner = linter.Linter(content_repo=demisto_content, pack_dir=integration_path, req_2=[], req_3=[], docker_engine=True) runner._gather_facts(modules={}) assert runner._facts["images"][0][0] == exp_image assert runner._facts["images"][0][1] == exp_py_num
def test_checks_common_server_python(self, repo): """ Given - Repo with CommonServerPython script When - Running lint on a repo with a change to CommonServerPython Then - Validate that the CommonServerPython is in the file list to check """ pack = repo.create_pack('Base') script = pack.create_script('CommonServerPython') script.create_default_script() script_path = Path(script.path) runner = linter.Linter(content_repo=pack.path, pack_dir=script_path, req_2=[], req_3=[], docker_engine=False) runner._gather_facts(modules={}) common_server_python_path = runner._facts.get('lint_files')[0] assert 'Packs/Base/Scripts/CommonServerPython/CommonServerPython.py' in str( common_server_python_path)