def test_toterminal_long_filenames( self, importasmod, tw_mock, monkeypatch: MonkeyPatch ) -> None: mod = importasmod( """ def f(): raise ValueError() """ ) excinfo = pytest.raises(ValueError, mod.f) path = Path(mod.__file__) monkeypatch.chdir(path.parent) repr = excinfo.getrepr(abspath=False) repr.toterminal(tw_mock) x = bestrelpath(Path.cwd(), path) if len(x) < len(str(path)): msg = tw_mock.get_write_msg(-2) assert msg == "mod.py" assert tw_mock.lines[-1] == ":3: ValueError" repr = excinfo.getrepr(abspath=True) repr.toterminal(tw_mock) msg = tw_mock.get_write_msg(-2) assert msg == str(path) line = tw_mock.lines[-1] assert line == ":3: ValueError"
def test_run_failure_plan_execute(tmp_path: pathlib.Path, sample_catalog_minimal: Catalog, monkeypatch: MonkeyPatch, caplog: pytest.LogCaptureFixture): """Test failure plan execute() in _run on RemoveCmd.""" # Plant this specific logged error for failing execution in mock_execute: logged_error = 'fail_execute' def mock_execute(*args, **kwargs): raise err.TrestleError(logged_error) # Create a temporary file as a valid arg for trestle remove: content_type = FileContentType.JSON catalog_def_dir, catalog_def_file = test_utils.prepare_trestle_project_dir( tmp_path, content_type, sample_catalog_minimal, test_utils.CATALOGS_DIR) monkeypatch.chdir(tmp_path) # Add remarks here, so it is a valid removal target, testargs = [ 'trestle', 'create', '-f', str(catalog_def_file), '-e', 'catalog.metadata.remarks' ] monkeypatch.setattr(sys, 'argv', testargs) Trestle().run() # .. then attempt to remove it here, but mocking a failed execute: testargs = [ 'trestle', 'remove', '-f', str(catalog_def_file), '-e', 'catalog.metadata.remarks' ] monkeypatch.setattr(Plan, 'execute', mock_execute) monkeypatch.setattr(sys, 'argv', testargs) caplog.clear() rc = Trestle().run() assert rc > 0
def read_setup(project_dir): """Read the setup metadata from a directory. Warning: This function imports the setup file while mocking ``setuptools.setup``; any other code in ``setup.py`` gets executed. """ import os import setuptools import pytest try: from _pytest.monkeypatch import MonkeyPatch except ImportError: # pytest < 3.0 from _pytest.monkeypatch import monkeypatch as MonkeyPatch setup = {} mp = MonkeyPatch() try: mp.chdir(project_dir) mp.setattr(setuptools, 'setup', setup.update) with open('setup.py') as f: exec(compile(f.read(), os.path.abspath(f.name), "exec"), {'__file__': f.name}) finally: mp.undo() return setup
def test_path_from_config_do_not_depend_on_cwd( monkeypatch: MonkeyPatch, ) -> None: # Issue 572 config1 = cli.load_config("test/fixtures/config-with-relative-path.yml") monkeypatch.chdir('test') config2 = cli.load_config("fixtures/config-with-relative-path.yml") assert config1['exclude_paths'].sort() == config2['exclude_paths'].sort()
def test_discover_lintables_silent(is_in_git: bool, monkeypatch: MonkeyPatch, capsys: CaptureFixture[str]) -> None: """Verify that no stderr output is displayed while discovering yaml files. (when the verbosity is off, regardless of the Git or Git-repo presence) Also checks expected number of files are detected. """ options = cli.get_config([]) test_dir = Path(__file__).resolve().parent lint_path = test_dir / '..' / 'examples' / 'roles' / 'test-role' if not is_in_git: monkeypatch.setenv('GIT_DIR', '') yaml_count = len(list(lint_path.glob('**/*.yml'))) + len( list(lint_path.glob('**/*.yaml'))) monkeypatch.chdir(str(lint_path)) files = file_utils.discover_lintables(options) stderr = capsys.readouterr().err assert not stderr, 'No stderr output is expected when the verbosity is off' assert ( len(files) == yaml_count ), "Expected to find {yaml_count} yaml files in {lint_path}".format_map( locals(), )
def rename_incident_field(content_repo: ContentGitRepo, monkeypatch: MonkeyPatch): """ Given: Incident field in HelloWorld pack. When: Renaming the entity. Then: Validate lint, secrets and validate exit code is 0 """ monkeypatch.chdir(content_git_repo.content) # type: ignore hello_world_incidentfields_path = Path("Packs/HelloWorld/IncidentFields/") curr_incident_field = hello_world_incidentfields_path / "incidentfield-Hello_World_ID.json" content_repo.run_command( f"git mv {curr_incident_field} {hello_world_incidentfields_path / 'incidentfield-new.json'}" ) runner = CliRunner(mix_stderr=False) res = runner.invoke( main, "update-release-notes -i Packs/HelloWorld -u revision") assert res.exit_code == 0, f"stdout = {res.stdout}\nstderr = {res.stderr}" try: content_repo.update_rn() except IndexError as exception: raise TestError( f"stdout = {res.stdout}\nstderr = {res.stderr}") from exception content_repo.run_validations()
def mirror_hash_index( tmpdir: Path, master: "Master", monkeypatch: MonkeyPatch ) -> "BandersnatchMirror": monkeypatch.chdir(tmpdir) from bandersnatch.mirror import BandersnatchMirror return BandersnatchMirror(tmpdir, master, hash_index=True)
def all_files_renamed(content_repo: ContentGitRepo, monkeypatch: MonkeyPatch): """ Given: HelloWorld Integration When: Renaming the all files to a new name Then: Validate lint, secrets and validate exit code is 0 """ monkeypatch.chdir(content_git_repo.content) # type: ignore path_to_hello_world_pack = Path( "Packs") / "HelloWorld" / "Integrations" / "HelloWorld" hello_world_path = content_repo.content / path_to_hello_world_pack # rename all files in dir for file in list_files(hello_world_path): new_file = file.replace('HelloWorld', 'helloworld') if not file == new_file: content_repo.run_command( f"git mv {path_to_hello_world_pack / file} {path_to_hello_world_pack / new_file}" ) runner = CliRunner(mix_stderr=False) res = runner.invoke( main, "update-release-notes -i Packs/HelloWorld -u revision") assert res.exit_code == 0, f"stdout = {res.stdout}\nstderr = {res.stderr}" try: content_repo.update_rn() except IndexError as exception: raise TestError( f"stdout = {res.stdout}\nstderr = {res.stderr}") from exception content_repo.run_validations()
def init_integration(content_repo: ContentGitRepo, monkeypatch: MonkeyPatch): """ Given: Instruction to create a new integration using the sdk. Use ID for as dir name: y When: Initiating a new integration with the init command Then: Validate lint, secrets and validate exit code is 0 """ runner = CliRunner(mix_stderr=False) hello_world_path = content_repo.content / "Packs" / "HelloWorld" / "Integrations" monkeypatch.chdir(hello_world_path) res = runner.invoke(main, "init --integration -n Sample", input="\n".join(["y", "6.0.0", "1"])) assert res.exit_code == 0, f"stdout = {res.stdout}\nstderr = {res.stderr}" content_repo.run_command("git add .") monkeypatch.chdir(content_repo.content) res = runner.invoke( main, "update-release-notes -i Packs/HelloWorld -u revision") assert res.exit_code == 0, f"stdout = {res.stdout}\nstderr = {res.stderr}" try: content_repo.update_rn() except IndexError as exception: raise TestError( f"stdout = {res.stdout}\nstderr = {res.stderr}") from exception content_repo.run_validations()
def modify_entity(content_repo: ContentGitRepo, monkeypatch: MonkeyPatch): """ Given: Modify entity description. When: Modifying an entity. Then: Validate lint, secrets and validate exit code is 0 """ runner = CliRunner(mix_stderr=False) monkeypatch.chdir(content_repo.content / "Packs" / "HelloWorld" / "Scripts" / "HelloWorldScript") # Modify the entity script = yaml.load(open("./HelloWorldScript.yml")) script['args'][0]["description"] = "new description" yaml.dump(script, open("./HelloWorldScript.yml", "w")) content_repo.run_command("git add .") monkeypatch.chdir(content_repo.content) res = runner.invoke( main, "update-release-notes -i Packs/HelloWorld -u revision") assert res.exit_code == 0, f"stdout = {res.stdout}\nstderr = {res.stderr}" content_repo.run_command("git add .") # Get the newest rn file and modify it. try: content_repo.update_rn() except IndexError as exception: raise TestError( f"stdout = {res.stdout}\nstderr = {res.stderr}") from exception content_repo.run_validations()
def test_ignores_from_ignore_file(monkeypatch: MonkeyPatch) -> None: monkeypatch.chdir(BASE_PATH) test_path = Path("tests") / "integration" / "simple" (test_path / ".bento").mkdir(exist_ok=True) (test_path / "node_modules").mkdir(exist_ok=True) fi = open_ignores(test_path, BASE_PATH / ".bentoignore") print(fi.patterns) survivors = {str(e.path) for e in fi.entries() if e.survives} assert survivors == { "tests/integration/simple/.bentoignore", "tests/integration/simple/init.js", "tests/integration/simple/package-lock.json", "tests/integration/simple/package.json", "tests/integration/simple/bar.py", "tests/integration/simple/foo.py", "tests/integration/simple/baz.py", "tests/integration/simple/jinja-template.html", } rejects = {str(e.path) for e in fi.entries() if not e.survives} assert rejects == { "tests/integration/simple/.bento", "tests/integration/simple/.gitignore", "tests/integration/simple/dist", "tests/integration/simple/node_modules", }
def init_pack(content_repo: ContentGitRepo, monkeypatch: MonkeyPatch): """ Given: Instruction to create a new pack using the sdk. Fill metadata: y Name: Sample Description: description Pack's type: 1 (xsoar) Category: 1 (Analytics & SIEM) Create integration: n When: Initiating a new pack with the init command Then: Validate lint, secrets and validate exit code is 0 """ author_image_rel_path = \ r"demisto_sdk/tests/test_files/artifacts/AuthorImageTest/SanityCheck" author_image_abs_path = os.path.abspath( f"./{author_image_rel_path}/{AUTHOR_IMAGE_FILE_NAME}") monkeypatch.chdir(content_repo.content) runner = CliRunner(mix_stderr=False) res = runner.invoke( main, f"init -a {author_image_abs_path} --pack --name Sample", input="\n".join(["y", "Sample", "description", "1", "1", "n", "6.0.0"])) assert res.exit_code == 0, f"Could not run the init command.\nstdout={res.stdout}\nstderr={res.stderr}\n" \ f"author_image_abs_path={author_image_abs_path}" content_repo.run_validations()
def test_rewrite_nonexisting_file(tmpdir: Path, monkeypatch: MonkeyPatch) -> None: monkeypatch.chdir(tmpdir) with rewrite("sample", "w") as f: f.write("csdf") with open("sample") as f: assert f.read() == "csdf"
def test_rewrite_fails(tmpdir: Path, monkeypatch: MonkeyPatch) -> None: monkeypatch.chdir(tmpdir) with open("sample", "w") as f: f.write("bsdf") with pytest.raises(Exception): with rewrite("sample") as f: # type: ignore f.write("csdf") raise Exception() assert open("sample").read() == "bsdf" # type: ignore
def test_discover_lintables_umlaut(monkeypatch: MonkeyPatch) -> None: """Verify that filenames containing German umlauts are not garbled by the discover_lintables.""" options = cli.get_config([]) test_dir = Path(__file__).resolve().parent lint_path = test_dir / '..' / 'examples' / 'playbooks' monkeypatch.chdir(str(lint_path)) files = file_utils.discover_lintables(options) assert '"with-umlaut-\\303\\244.yml"' not in files assert 'with-umlaut-ä.yml' in files
def test_rewrite(tmpdir: Path, monkeypatch: MonkeyPatch) -> None: monkeypatch.chdir(tmpdir) with open("sample", "w") as f: f.write("bsdf") with rewrite("sample") as f: # type: ignore f.write("csdf") assert open("sample").read() == "csdf" mode = os.stat("sample").st_mode # chmod doesn't work on windows machines. Permissions are pinned at 666 if not WINDOWS: assert oct(mode) == "0o100644"
def test_check_relative_path(monkeypatch: MonkeyPatch) -> None: """Validates that check works with relative specified paths when not run from project root""" monkeypatch.chdir(SIMPLE / "dist") runner = CliRunner(mix_stderr=False) result = runner.invoke(check, ["-f", "json", "--all", "../bar.py"], obj=Context(base_path=SIMPLE)) parsed = json.loads(result.stdout) print(parsed) assert len(parsed) == 1
def test_install_ignore_no_repo(tmp_path: Path, monkeypatch: MonkeyPatch) -> None: """Validates that bento installs extra ignore items when not in a git repo""" monkeypatch.chdir(tmp_path) context = Context(base_path=tmp_path, is_init=True) command = InitCommand(context) command._install_ignore_if_not_exists() context = Context(base_path=tmp_path, is_init=True) ig = context.file_ignores assert "node_modules/" in ig.patterns
def test_check_specified_ignored(monkeypatch: MonkeyPatch) -> None: """Validates that check does not check specified, ignored paths""" monkeypatch.chdir(BASE) runner = CliRunner(mix_stderr=False) result = runner.invoke( check, ["-f", "json", "--all", "tests/integration/simple/bar.py"], obj=Context(base_path=BASE), ) parsed = json.loads(result.stdout) assert not parsed
def test_check_specified_paths(monkeypatch: MonkeyPatch) -> None: """Validates that check discovers issues in specified paths""" monkeypatch.chdir(SIMPLE) runner = CliRunner(mix_stderr=False) Context(SIMPLE).cache.wipe() result = runner.invoke( check, ["--formatter", "json", "--all", "init.js", "foo.py"], obj=Context(base_path=SIMPLE), ) parsed = json.loads(result.stdout) assert len(parsed) == 3
def test_install_ignore_no_repo(tmp_path: Path, monkeypatch: MonkeyPatch) -> None: """Validates that bento installs extra ignore items when not in a git repo""" monkeypatch.chdir(tmp_path) context = Context(base_path=tmp_path, is_init=True) assert not context.ignore_file_path.exists() command = InitCommand(context) command._install_ignore_if_not_exists() assert context.ignore_file_path.exists() with context.ignore_file_path.open() as file: patterns = file.read() assert "node_modules/" in patterns
def test_compare_with_compgen(self, tmp_path: Path, monkeypatch: MonkeyPatch) -> None: from _pytest._argcomplete import FastFilesCompleter ffc = FastFilesCompleter() fc = FilesCompleter() monkeypatch.chdir(tmp_path) assert equal_with_bash("", ffc, fc, out=sys.stdout) tmp_path.cwd().joinpath("data").touch() for x in ["d", "data", "doesnotexist", ""]: assert equal_with_bash(x, ffc, fc, out=sys.stdout)
def test_train_data_in_project_dir(monkeypatch: MonkeyPatch, tmp_path: Path): """Test cache directory placement. Tests cache directories for training data are in project root, not where `rasa init` is run. """ # We would like to test CLI but can't run it with popen because we want # to be able to monkeypatch it. Solution is to call functions inside CLI # module. Initial project folder should have been created before # `init_project`, that's what we do here. monkeypatch.chdir(tmp_path) new_project_folder_path = tmp_path / "new-project-folder" new_project_folder_path.mkdir() # Simulate CLI run arguments. parser = argparse.ArgumentParser() subparsers = parser.add_subparsers() scaffold.add_subparser(subparsers, parents=[]) args = parser.parse_args([ "init", "--no-prompt", ]) # Simple config which should train fast. def mock_get_config(*args): return { "language": "en", "pipeline": [{ "name": "KeywordIntentClassifier" }], "policies": [{ "name": "RulePolicy" }], "recipe": "default.v1", } monkeypatch.setattr( "rasa.shared.importers.importer.CombinedDataImporter.get_config", mock_get_config, ) # Cache dir is auto patched to be a temp directory, this makes it # go back to local project folder so we can test it is created correctly. with enable_cache(Path(".rasa", "cache")): mock_stdin([]) scaffold.init_project(args, str(new_project_folder_path)) assert os.getcwd() == str(new_project_folder_path) assert os.path.exists(".rasa/cache")
def test_path_from_cli_depend_on_cwd(base_arguments: List[str], monkeypatch: MonkeyPatch) -> None: # Issue 572 arguments = base_arguments + [ "--exclude", "test/fixtures/config-with-relative-path.yml", ] options1 = cli.get_cli_parser().parse_args(arguments) assert 'test/test' not in str(options1.exclude_paths[0]) test_dir = 'test' monkeypatch.chdir(test_dir) options2 = cli.get_cli_parser().parse_args(arguments) assert 'test/test' in str(options2.exclude_paths[0])
def __count_simple_findings( archive: Dict[str, Set[str]], files: Optional[List[str]], monkeypatch: MonkeyPatch, path: str = "tests/integration/simple", ) -> Dict[str, Tuple[int, int]]: """ Runs __tool_parallel_results from the CLI Returns: (dict): Counts of (unfiltered, filtered) findings for each tool """ monkeypatch.chdir(os.path.join(BASE_PATH, path)) context = bento.context.Context() tools = context.tools.values() results = bento.tool_runner.Runner().parallel_results(tools, archive, files) return {tid: __violation_counts(vv) for tid, vv in results if isinstance(vv, list)}
def test_insert_missing_modules(self, monkeypatch: MonkeyPatch, tmp_path: Path) -> None: monkeypatch.chdir(tmp_path) # Use 'xxx' and 'xxy' as parent names as they are unlikely to exist and # don't end up being imported. modules = {"xxx.tests.foo": ModuleType("xxx.tests.foo")} insert_missing_modules(modules, "xxx.tests.foo") assert sorted(modules) == ["xxx", "xxx.tests", "xxx.tests.foo"] mod = ModuleType("mod", doc="My Module") modules = {"xxy": mod} insert_missing_modules(modules, "xxy") assert modules == {"xxy": mod} modules = {} insert_missing_modules(modules, "") assert modules == {}
def test_run_failure_required_element(tmp_path: pathlib.Path, sample_catalog_minimal: Catalog, monkeypatch: MonkeyPatch): """Test failure of _run on RemoveCmd in specifying a required element for removal.""" # Create a temporary catalog file with responsible-parties content_type = FileContentType.JSON catalog_def_dir, catalog_def_file = test_utils.prepare_trestle_project_dir( tmp_path, content_type, sample_catalog_minimal, test_utils.CATALOGS_DIR) # 4. simulate() fails -- Should happen if required element is target for deletion monkeypatch.chdir(tmp_path) testargs = [ 'trestle', 'remove', '-f', str(catalog_def_file), '-e', 'catalog.metadata' ] monkeypatch.setattr(sys, 'argv', testargs) exitcode = Trestle().run() assert exitcode == 1
def test_generate_no_file_output(tmp_path: Path, monkeypatch: MonkeyPatch) -> None: """Test generating and getting output as a str instead of writing to file""" monkeypatch.chdir(tmp_path) test_case_name = "basic" test_case_file_name = f"{test_case_name}.json" test_case_path = get_test_case_path("basic") schemas = get_schemas_to_render(test_case_path, None, "md") template_renderer = MagicMock(TemplateRenderer) template_renderer.render.return_value = "" template_renderer.config = GenerationConfiguration() generated = generate_schemas_doc(schemas, template_renderer) assert list(generated.keys()) == [test_case_file_name] # Ensure no file is written to current working directory assert len(list(tmp_path.glob("**"))) == 1 # glob("**") returns itself
def test_check_compare_to_head_diffs(monkeypatch: MonkeyPatch) -> None: """Validates that check shows issues in staged changes""" monkeypatch.chdir(SIMPLE) runner = CliRunner(mix_stderr=False) Context(SIMPLE).cache.wipe() edited = SIMPLE / "bar.py" with mod_file(edited): with edited.open("a") as stream: stream.write("\nprint({'a': 2}).has_key('b')") subprocess.run(["git", "add", str(edited)], check=True) result = runner.invoke( check, ["--formatter", "json", str(SIMPLE)], obj=Context(base_path=SIMPLE)) subprocess.run(["git", "reset", "HEAD", str(edited)]) parsed = json.loads(result.stdout) assert [p["check_id"] for p in parsed] == ["deprecated-has-key"]
def __count_simple_findings( archive: Dict[str, Set[str]], files: List[Path], monkeypatch: MonkeyPatch, path: str = "tests/integration/simple", ) -> Dict[str, Tuple[int, int]]: """ Runs __tool_parallel_results from the CLI Returns: (dict): Counts of (unfiltered, filtered) findings for each tool """ monkeypatch.chdir(BASE_PATH / path) context = bento.context.Context() tools = context.tools.values() paths = [Path(f) for f in files] if files is not None else [context.base_path] results = bento.tool_runner.Runner(paths=paths, use_cache=True).parallel_results( tools, archive) return { tid: __violation_counts(vv) for tid, vv in results if isinstance(vv, list) }