def test_download_000027(url: str, tmp_path: Path, capsys: pytest.CaptureFixture) -> None: ret = download(url, tmp_path) # type: ignore[func-returns-value] assert not ret # we return nothing ATM, might want to "generate" dsdir = tmp_path / "000027" assert list_paths(dsdir, dirs=True) == [ dsdir / "dandiset.yaml", dsdir / "sub-RAT123", dsdir / "sub-RAT123" / "sub-RAT123.nwb", ] # and checksum should be correct as well from ..support.digests import Digester assert (Digester(["md5"])( dsdir / "sub-RAT123" / "sub-RAT123.nwb")["md5"] == "33318fd510094e4304868b4a481d4a5a") # redownload - since already exist there should be an exception if we are # not using pyout with pytest.raises(FileExistsError): download(url, tmp_path, format="debug") assert "FileExistsError" not in capsys.readouterr().out # but no exception is raised, and rather it gets output to pyout otherwise download(url, tmp_path) assert "FileExistsError" in capsys.readouterr().out # TODO: somehow get that status report about what was downloaded and what not download(url, tmp_path, existing="skip") # TODO: check that skipped download(url, tmp_path, existing="overwrite") # TODO: check that redownloaded download(url, tmp_path, existing="refresh") # TODO: check that skipped (the same)
def test_metadata_cargo_log(capfd: CaptureFixture, monkeypatch: MonkeyPatch, hello_world_bin: RustBin) -> None: monkeypatch.setenv("CARGO_LOG", "trace") # With quiet unset, no stdout, plenty of logging stderr hello_world_bin._metadata(quiet=False) captured = capfd.readouterr() assert captured.out == "" assert "TRACE cargo::util::config" in captured.err # With quiet set, nothing will be printed hello_world_bin._metadata(quiet=True) captured = capfd.readouterr() assert captured.out == "" assert captured.err == ""
def test_json_success(self, parse_args: Mock, db_upgrade: Mock, capsys: CaptureFixture) -> None: parse_args.return_value.json = True db_upgrade.return_value = UpgradeResult( VersionResult(123, 45), VersionResult(124, 46), [FileInfo("foo.sql", "", "", 124, 46)], ) main() j = json.loads(capsys.readouterr().out) assert j == { "success": True, "oldVersion": { "version": 123, "apiLevel": 45 }, "newVersion": { "version": 124, "apiLevel": 46 }, "appliedScripts": [{ "filename": "foo.sql", "version": 124, "apiLevel": 46, }], }
def test_log_out(capfd: pytest.CaptureFixture): test_str = "Testing..." now = datetime.now().strftime("%X") c.error(test_str) out, err = capfd.readouterr() assert " " + test_str in out assert f"[{now}]:" in out
def test_validate_bbcode_command(args: argparse.Namespace, capsys: pytest.CaptureFixture): args.format = 'bbcode' args.validate = True storyconverter.main(args) captured = capsys.readouterr() assert 'Successfully validated source as BBCODE' in captured.out
def test_logging_filters_with_known_limitations( self, capfd: pytest.CaptureFixture, log_filters_input: str, mocker: MockerFixture, ) -> None: """Test known limitations of log message filters. - Filters in the input string should be separated with commas, not spaces. - Filters are applied with string matching, so a filter of `/health` will also filter out messages including `/healthy`. """ filters = logging_conf.LogFilter.set_filters(log_filters_input) mocker.patch.dict( logging_conf.LOGGING_CONFIG["filters"]["filter_log_message"], { "()": logging_conf.LogFilter, "filters": filters }, clear=True, ) logger = logging_conf.logging.getLogger( "test.logging_conf.output.filtererrors") logging_conf.configure_logging(logger=logger) logger.info(log_filters_input) logger.info("/healthy") captured = capfd.readouterr() assert log_filters_input not in captured.out if log_filters_input == "/health": assert "/healthy" not in captured.out else: assert "/healthy" in captured.out
def test_logging_filters( self, capfd: pytest.CaptureFixture, log_filters_input: str, log_filters_output: set[str], mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch, ) -> None: """Test that log message filters are applied as expected.""" monkeypatch.setenv("LOG_FILTERS", log_filters_input) mocker.patch.object(logging_conf, "LOG_FILTERS", logging_conf.LogFilter.set_filters()) mocker.patch.dict( logging_conf.LOGGING_CONFIG["filters"]["filter_log_message"], { "()": logging_conf.LogFilter, "filters": logging_conf.LOG_FILTERS }, clear=True, ) path_to_log = "/status" logger = logging_conf.logging.getLogger( "test.logging_conf.output.filters") logging_conf.configure_logging(logger=logger) logger.info(*self._uvicorn_access_log_args(path_to_log)) logger.info(log_filters_input) for log_filter in log_filters_output: logger.info(*self._uvicorn_access_log_args(log_filter)) captured = capfd.readouterr() assert logging_conf.LOG_FILTERS == log_filters_output assert path_to_log in captured.out for log_filter in log_filters_output: assert log_filter not in captured.out
def test_json_error(self, parse_args: Mock, db_upgrade: Mock, capsys: CaptureFixture) -> None: parse_args.return_value.json = True db_upgrade.return_value = UpgradeResult( VersionResult(123, 45), VersionResult(123, 45), [], FileInfo("foo.sql", "", "", 124, 46), ) with pytest.raises(SystemExit): main() j = json.loads(capsys.readouterr().out) assert j == { "success": False, "oldVersion": { "version": 123, "apiLevel": 45 }, "newVersion": { "version": 123, "apiLevel": 45 }, "appliedScripts": [], "failedScript": { "filename": "foo.sql", "version": 124, "apiLevel": 46, }, }
def test_no_log(self, capfd: pytest.CaptureFixture): test_str = "lev med det" with log.no_log: for level in LogLevels: log(test_str, level=level) out, err = capfd.readouterr() assert not out and not err
def test_create_mysql_connection( capsys: pytest.CaptureFixture, mysql_username: str, mysql_password: str, database: str, cloud_sql_conn_name: str, project_id: str, location: str, ) -> None: cloud_sql_credential = bq_connection.CloudSqlCredential( { "username": mysql_username, "password": mysql_password, } ) cloud_sql_properties = bq_connection.CloudSqlProperties( { "type_": bq_connection.CloudSqlProperties.DatabaseType.MYSQL, "database": database, "instance_id": cloud_sql_conn_name, "credential": cloud_sql_credential, } ) create_mysql_connection.create_mysql_connection( project_id=project_id, location=location, cloud_sql_properties=cloud_sql_properties, ) out, _ = capsys.readouterr() assert "Created connection successfully:" in out
def test_functional_config_loading( configuration_path: str, default_configuration: PylintConfiguration, file_to_lint_path: str, capsys: CaptureFixture, caplog: LogCaptureFixture, ): """Functional tests for configurations.""" # logging is helpful to see what's expected and why. The output of the # program is checked during the test so printing messes with the result. caplog.set_level(logging.INFO) configuration_path = str(FUNCTIONAL_DIR / configuration_path) msg = f"Wrong result with configuration {configuration_path}" expected_code, expected_output = get_expected_output( configuration_path, USER_SPECIFIC_PATH) expected_loaded_configuration = get_expected_configuration( configuration_path, default_configuration) mock_exit, _, runner = run_using_a_configuration_file( configuration_path, file_to_lint_path) mock_exit.assert_called_once_with(expected_code) out, err = capsys.readouterr() # 'rstrip()' applied, so we can have a final newline in the expected test file assert expected_output.rstrip() == out.rstrip(), msg assert sorted(expected_loaded_configuration.keys()) == sorted( runner.linter.namespace.__dict__.keys()), msg for key, expected_value in expected_loaded_configuration.items(): key_msg = f"{msg} for key '{key}':" if isinstance(expected_value, list): assert sorted(expected_value) == sorted( runner.linter.namespace.__dict__[key]), key_msg else: assert expected_value == runner.linter.namespace.__dict__[ key], key_msg assert not err, msg
def test_warn_about_old_home(capsys: CaptureFixture) -> None: """Test that we correctly warn about old_home.""" # Create old home old_home = Path(USER_HOME) / OLD_DEFAULT_PYLINT_HOME old_home.mkdir(parents=True, exist_ok=True) # Create spam prevention file ten_years_ago = datetime.datetime.now() - datetime.timedelta(weeks=520) new_prevention_file = Path(PYLINT_HOME) / ten_years_ago.strftime( "pylint_warned_about_old_cache_already_%Y-%m-%d.temp") with open(new_prevention_file, "w", encoding="utf8") as f: f.write("") # Remove current prevention file cur_prevention_file = Path(PYLINT_HOME) / datetime.datetime.now().strftime( "pylint_warned_about_old_cache_already_%Y-%m-%d.temp") if cur_prevention_file.exists(): os.remove(cur_prevention_file) _warn_about_old_home(Path(PYLINT_HOME)) assert not new_prevention_file.exists() assert cur_prevention_file.exists() out = capsys.readouterr() assert "PYLINTHOME is now" in out.err
def test_unknown_short_option_name(capsys: CaptureFixture) -> None: """Check that we correctly raise a message on an unknown short option.""" with pytest.raises(SystemExit): Run([str(EMPTY_MODULE), "-Q"], exit=False) output = capsys.readouterr() assert "usage: pylint" in output.err assert "Unrecognized option" in output.err
def test_gunicorn_config(self, capfd: pytest.CaptureFixture, gunicorn_conf_path: str, module: str) -> None: """Load Gunicorn configuration file and verify output.""" app_module = f"inboard.app.main_{module}:app" gunicorn_conf_path = gunicorn_conf.__file__ gunicorn_options = [ "gunicorn", "--print-config", "-c", gunicorn_conf_path, "-k", "uvicorn.workers.UvicornWorker", app_module, ] subprocess.run(gunicorn_options) captured = capfd.readouterr() captured_and_cleaned = captured.out.replace(" ", "").splitlines() assert app_module in captured.out assert gunicorn_conf_path in captured.out assert "INFO" in captured.out assert "uvicorn.logging.DefaultFormatter" in captured.out assert "graceful_timeout=120" in captured_and_cleaned assert "keepalive=5" in captured_and_cleaned assert "loglevel=info" in captured_and_cleaned assert "timeout=120" in captured_and_cleaned assert f"workers={max(multiprocessing.cpu_count(), 2)}" in captured_and_cleaned
def test_import_dotted_library( capsys: CaptureFixture, caplog: LogCaptureFixture, ) -> None: caplog.set_level(logging.INFO) original_module = sys.modules.pop("xml.etree.ElementTree") expected_out = "INFO (TEST): Welcome to cElementTree!" expected_err = "WARNING (TEST): Monkey-patched version of cElementTree" def function_with_stdout_and_stderr(expected_out, expected_err): def mocked_function(*args, **kwargs): print(f"{expected_out} args={args} kwargs={kwargs}") print(expected_err, file=sys.stderr) return mocked_function try: with unittest.mock.patch( "importlib.import_module", side_effect=function_with_stdout_and_stderr( expected_out, expected_err), ): modutils.load_module_from_name("xml.etree.ElementTree") out, err = capsys.readouterr() assert expected_out in caplog.text assert expected_err in caplog.text assert not out assert not err finally: sys.modules["xml.etree.ElementTree"] = original_module
def install_and_verify( capsys: pytest.CaptureFixture, caplog, monkeypatch, module_globals: ModuleGlobalsData, using_clear_path: bool, package_data: PackageData, deps: bool, ) -> Tuple[bool, Optional[bool], float]: _ = capsys.readouterr() caplog.clear() test_error_fh = io.StringIO() monkeypatch.setenv( "PATH", os.getenv("PATH_TEST" if using_clear_path else "PATH_ORIG") ) start_time = time.time() pipx_exit_code = run_pipx_cli( ["install", package_data.package_spec, "--verbose"] + (["--include-deps"] if deps else []) ) elapsed_time = time.time() - start_time captured = capsys.readouterr() pip_pass, pipx_pass, pip_error_file = verify_post_install( pipx_exit_code, captured, caplog, package_data.package_name, test_error_fh, using_clear_path=using_clear_path, deps=deps, ) if not pip_pass or not pipx_pass: print_error_report( module_globals, captured, test_error_fh, package_data.package_spec, "clear PATH" if using_clear_path else "sys PATH", pip_error_file, ) return pip_pass, pipx_pass, elapsed_time
def test_compile_ui_defaults_to_no_output( capsys: pytest.CaptureFixture, tmp_path_with_ui: pathlib.Path, ) -> None: ssst._utilities.compile_ui(directory_path=[tmp_path_with_ui]) captured = capsys.readouterr() assert captured.out == ""
def test_main_debug_file_does_not_exist(capsys: CaptureFixture): sys.argv = ['conda-debug', 'file-does-not-exist'] with pytest.raises(SystemExit): debug.main() captured = capsys.readouterr() assert valid.CONDA_PKG_OR_RECIPE_ERROR_MESSAGE in captured.err
def test_validate_markdown_command(args: argparse.Namespace, capsys: pytest.CaptureFixture): args.format = 'markdown' args.source = ['test_resources/markdown_source.md'] args.validate = True storyconverter.main(args) captured = capsys.readouterr() assert 'Successfully validated source as MARKDOWN' in captured.out
def test_markdown_to_bbcode_stdout(args: argparse.Namespace, capsys: pytest.CaptureFixture): args.stdout = True args.source = ['test_resources/markdown_source.md'] args.format = 'bbcode' storyconverter.main(args) captured = capsys.readouterr() assert 'This is a [I]sample[/I] of markdown text.\n\n' == captured.out
def test_format_help(capsys: CaptureFixture, store: MessageDefinitionStore) -> None: store.help_message([]) captured = capsys.readouterr() assert captured.out == "" store.help_message(["W1234", "E1234", "C1234"]) captured = capsys.readouterr() assert (captured.out == """:msg-symbol (W1234): *message* msg description. This message belongs to the achecker checker. :duplicate-keyword-arg (E1234): *Duplicate keyword argument %r in %s call* Used when a function call passes the same keyword argument multiple times. This message belongs to the achecker checker. It can't be emitted when using Python >= 2.6. No such message id or symbol 'C1234'. """)
def test_bbcode_to_markdown_file(args: argparse.Namespace, capsys: pytest.CaptureFixture, tmp_path: Path): args.output = tmp_path / 'output.md' storyconverter.main(args) captured = capsys.readouterr() assert args.output.exists() assert 'Determined filetype BBCODE heuristically' in captured.out assert 'Output file written' in captured.out
def test_basic_file_output(capsys: pytest.CaptureFixture, args: argparse.Namespace, tmp_path: Path): args.output = tmp_path / Path('results.txt') filewhitelist.main(args) captured = capsys.readouterr() assert args.output.exists() assert 'Whitelist file loaded' in captured.out assert 'Writing to output' in captured.out assert '3 files not in whitelist found' in captured.out
def test_versionString(capsys: pytest.CaptureFixture) -> None: """Test the output of a version string.""" with pytest.raises(expected_exception=SystemExit) as excp: runTzolkinCalendar(["--version"]) assert excp.value.args[0] == 0 # nosec captured = capsys.readouterr() assert captured.err == "" # nosec assert captured.out.find("tzolkin-calendar") == 0 # nosec
def test_cloud_kms_env_aead( capsys: pytest.CaptureFixture, kms_uri: str) -> None: credentials = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", "") # Create env_aead primitive init_tink_env_aead(kms_uri, credentials) captured = capsys.readouterr().out assert f"Created envelope AEAD Primitive using KMS URI: {kms_uri}" in captured
def test_logging_output_default(self, capfd: pytest.CaptureFixture) -> None: """Test logger output with default format.""" logger = logging_conf.logging.getLogger() logging_conf.configure_logging() logger.info("Hello, World!") captured = capfd.readouterr() assert "INFO" in captured.out assert "Hello, World!" in captured.out
def test_log_commit(self, capfd: pytest.CaptureFixture): """ Tests are assumed to be run from within the pelutils git repository root or above. If not, this test will fail. """ log.log_repo() stdout, _ = capfd.readouterr() if ".git" in os.listdir("."): assert re.search(r"\b[0-9a-f]{40}\b", stdout) else: assert re.search(r"\b[0-9a-f]{40}\b", stdout) is None
def test_multiple_loggers(self, capfd: pytest.CaptureFixture): os.remove(self.logfile) logfile2 = os.path.join(self.test_dir, "test_logging2.log") log2 = Logger().configure(logfile2, print_level=None) logs = [("logger", ), ("logger", "bogger"), ("logger", "bogger", "hogger")] for tolog in logs: log(*tolog) stdout, _ = capfd.readouterr() assert all(x in stdout for x in tolog) log2(*tolog) stdout, _ = capfd.readouterr() assert not stdout with open(self.logfile) as lf, open(logfile2) as lf2: logger_iter = chain(*logs) for line1, line2 in zip(lf, lf2): log_item = next(logger_iter) assert log_item in line1 and log_item in line2
def test_showYear(capsys: pytest.CaptureFixture) -> None: """Test the output of the help text.""" with pytest.raises(expected_exception=SystemExit) as excp: runTzolkinCalendar(["--year"]) assert excp.value.args[0] == 0 # nosec captured = capsys.readouterr() assert captured.err == "" # nosec assert captured.out.find( " 1 Imix 2 Ikʼ 3 Akʼbʼal 4 Kʼan 5 Chikchan") == 0 # nosec
def test_issue_template_on_fatal_errors(capsys: pytest.CaptureFixture) -> None: """Test that we also create an issue template if the offending exception isn't from astroid.""" with pytest.raises(SystemExit): with unittest.mock.patch("astroid.MANAGER.ast_from_file", side_effect=RecursionError()): Run([__file__]) captured = capsys.readouterr() assert "Fatal error while checking" in captured.out assert "Please open an issue" in captured.out assert "Traceback" in captured.err