def test_setenv(): monkeypatch = MonkeyPatch() monkeypatch.setenv('XYZ123', 2) import os assert os.environ['XYZ123'] == "2" monkeypatch.undo() assert 'XYZ123' not in os.environ
def test_setenv(): monkeypatch = MonkeyPatch() monkeypatch.setenv("XYZ123", 2) import os assert os.environ["XYZ123"] == "2" monkeypatch.undo() assert "XYZ123" not in os.environ
def test_default_xdg_config_home(monkeypatch: MonkeyPatch, tmp_path: Path): home_dir = tmp_path monkeypatch.delenv(ENV_HTTPIE_CONFIG_DIR, raising=False) monkeypatch.delenv(ENV_XDG_CONFIG_HOME, raising=False) monkeypatch.setenv('HOME', str(home_dir)) expected_config_dir = (home_dir / DEFAULT_RELATIVE_XDG_CONFIG_HOME / DEFAULT_CONFIG_DIRNAME) assert get_default_config_dir() == expected_config_dir
def test_explicit_xdg_config_home(monkeypatch: MonkeyPatch, tmp_path: Path): home_dir = tmp_path monkeypatch.delenv(ENV_HTTPIE_CONFIG_DIR, raising=False) monkeypatch.setenv('HOME', str(home_dir)) custom_xdg_config_home = home_dir / 'custom_xdg_config_home' monkeypatch.setenv(ENV_XDG_CONFIG_HOME, str(custom_xdg_config_home)) expected_config_dir = custom_xdg_config_home / DEFAULT_CONFIG_DIRNAME assert get_default_config_dir() == expected_config_dir
def test_fetcher_bad_uri(tmp_trestle_dir: pathlib.Path, uri: str, monkeypatch: MonkeyPatch) -> None: """Test fetcher factory with bad URI.""" if 'https' in uri: monkeypatch.setenv('myusername', 'user123') monkeypatch.setenv('mypassword', 'somep4ss') with pytest.raises(TrestleError): cache.FetcherFactory.get_fetcher(tmp_trestle_dir, uri)
def test_sftp_fetcher_bad_ssh_key(tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch) -> None: """Test the sftp fetcher when the loaded SSH_KEY env var contains a bad SSH key.""" uri = 'sftp://*****:*****@some.host/path/to/file.json' fetcher = cache.FetcherFactory.get_fetcher(tmp_trestle_dir, uri) monkeypatch.setenv('SSH_KEY', 'blah') with pytest.raises(err.TrestleError): fetcher._update_cache()
def test_setenv_non_str_warning(self, monkeypatch: MonkeyPatch) -> None: value = 2 msg = ( "Value of environment variable PYTEST_INTERNAL_MY_VAR type should be str, " "but got 2 (type: int); converted to str implicitly") with pytest.warns(pytest.PytestWarning, match=re.escape(msg)): monkeypatch.setenv(str(self.VAR_NAME), value) # type: ignore[arg-type]
def engine(monkeypatch_module: MonkeyPatch) -> sa.engine.Engine: sqlalchemy_database_uri = "sqlite:///./test.db" monkeypatch_module.setenv("DB_SQLALCHEMY_URI", sqlalchemy_database_uri) monkeypatch_module.setenv("DB_LOG_SQLALCHEMY_SQL_STATEMENTS", "1") clear_caches() return get_engine()
def test_delete_using_lru_if_cache_exceeds_size( tmp_path: Path, monkeypatch: MonkeyPatch, default_model_storage: ModelStorage): monkeypatch.setenv(CACHE_LOCATION_ENV, str(tmp_path)) # Pretend we have a cache of certain size monkeypatch.setenv(CACHE_SIZE_ENV, "5") cache = LocalTrainingCache() # Cache an item fingerprint_key1 = uuid.uuid4().hex output1 = TestCacheableOutput({"something to cache": "dasdaasda"}, size_in_mb=2) output_fingerprint1 = uuid.uuid4().hex cache.cache_output(fingerprint_key1, output1, output_fingerprint1, default_model_storage) # Cache an non cacheable item to spice it up 🔥 fingerprint_key2 = uuid.uuid4().hex output2 = TestCacheableOutput(None) output_fingerprint2 = uuid.uuid4().hex cache.cache_output(fingerprint_key2, output2, output_fingerprint2, default_model_storage) # Cache another item fingerprint_key3 = uuid.uuid4().hex output3 = TestCacheableOutput({"something to cache": "dasdaasda"}, size_in_mb=2) output_fingerprint3 = uuid.uuid4().hex cache.cache_output(fingerprint_key3, output3, output_fingerprint3, default_model_storage) # Assert both are there for output_fingerprint in [output_fingerprint1, output_fingerprint2]: assert cache.get_cached_result(output_fingerprint, "some_node", default_model_storage) # Checkout the first item as this updates `last_used` and hence affects LRU cache.get_cached_output_fingerprint(fingerprint_key1) # Now store something which requires a deletion fingerprint_key4 = uuid.uuid4().hex output4 = TestCacheableOutput({"something to cache": "dasdaasda"}, size_in_mb=2) output_fingerprint4 = uuid.uuid4().hex cache.cache_output(fingerprint_key4, output4, output_fingerprint4, default_model_storage) # Assert cached result 1 and 3 are there for output_fingerprint in [output_fingerprint1, output_fingerprint4]: assert cache.get_cached_result(output_fingerprint, "some_node", default_model_storage) # Cached result 2 and 3 were deleted assert cache.get_cached_output_fingerprint(fingerprint_key2) is None assert (cache.get_cached_result(output_fingerprint3, "some_node", default_model_storage) is None)
async def test_events_schema(monkeypatch: MonkeyPatch, default_agent: Agent, config_path: Text): # this allows us to patch the printing part used in debug mode to collect the # reported events monkeypatch.setenv("RASA_TELEMETRY_DEBUG", "true") monkeypatch.setenv("RASA_TELEMETRY_ENABLED", "true") mock = Mock() monkeypatch.setattr(telemetry, "print_telemetry_event", mock) with open(TELEMETRY_EVENTS_JSON) as f: schemas = json.load(f)["events"] initial = asyncio.Task.all_tasks() # Generate all known backend telemetry events, and then use events.json to # validate their schema. training_data = TrainingDataImporter.load_from_config(config_path) async with telemetry.track_model_training(training_data, "rasa"): await asyncio.sleep(1) telemetry.track_telemetry_disabled() telemetry.track_data_split(0.5, "nlu") telemetry.track_validate_files(True) telemetry.track_data_convert("yaml", "nlu") telemetry.track_tracker_export(5, TrackerStore(domain=None), EventBroker()) telemetry.track_interactive_learning_start(True, False) telemetry.track_server_start([CmdlineInput()], None, None, 42, True) telemetry.track_project_init("tests/") telemetry.track_shell_started("nlu") telemetry.track_rasa_x_local() telemetry.track_visualization() telemetry.track_core_model_test(5, True, default_agent) telemetry.track_nlu_model_test(TrainingData()) pending = asyncio.Task.all_tasks() - initial await asyncio.gather(*pending) assert mock.call_count == 15 for call in mock.call_args_list: event = call.args[0] # `metrics_id` automatically gets added to all event but is # not part of the schema so we need to remove it before validation del event["properties"]["metrics_id"] jsonschema.validate(instance=event["properties"], schema=schemas[event["event"]])
def test_setenv_prepend(): import os monkeypatch = MonkeyPatch() monkeypatch.setenv('XYZ123', 2, prepend="-") assert os.environ['XYZ123'] == "2" monkeypatch.setenv('XYZ123', 3, prepend="-") assert os.environ['XYZ123'] == "3-2" monkeypatch.undo() assert 'XYZ123' not in os.environ
def test_setenv(): monkeypatch = MonkeyPatch() with pytest.warns(pytest.PytestWarning): monkeypatch.setenv("XYZ123", 2) import os assert os.environ["XYZ123"] == "2" monkeypatch.undo() assert "XYZ123" not in os.environ
def test_settings(mock_service_envs: None, monkeypatch: MonkeyPatch): monkeypatch.delenv("DASK_START_AS_SCHEDULER") settings = Settings.create_from_envs() assert settings.as_worker() monkeypatch.setenv("DASK_START_AS_SCHEDULER", "1") settings = Settings.create_from_envs() assert settings.as_scheduler()
def test_cli_run_config( runner: click.testing.CliRunner, cache_dir: str, monkeypatch: MonkeyPatch ) -> None: monkeypatch.setenv("SKJOLD_CACHE_DIR", cache_dir) config = Configuration() config.use({"report_only": False, "report_format": "cli", "sources": ["pyup"]}) result = runner.invoke(cli, args=["config"]) assert result.exit_code == 0
def test_gunicorn_conf_workers_custom_cores( self, monkeypatch: MonkeyPatch) -> None: """Test custom Gunicorn worker process calculation.""" monkeypatch.setenv("WORKERS_PER_CORE", "0.5") assert os.getenv("WORKERS_PER_CORE") == "0.5" cores: int = multiprocessing.cpu_count() assert gunicorn_conf.calculate_workers( None, "2", str(os.getenv("WORKERS_PER_CORE")), cores=cores) == max(int(cores * 0.5), 2)
def project(self, project): Project.deactivate() monkeypatch = MonkeyPatch() monkeypatch.setenv(PROJECT_READONLY_ENV, "true") yield project monkeypatch.undo()
def test_setenv() -> None: monkeypatch = MonkeyPatch() with pytest.warns(pytest.PytestWarning): monkeypatch.setenv("XYZ123", 2) # type: ignore[arg-type] import os assert os.environ["XYZ123"] == "2" monkeypatch.undo() assert "XYZ123" not in os.environ
def init_ndb_env_vars(monkeypatch: MonkeyPatch) -> None: """ Initializing an ndb Client in a test env requires some environment variables to be set For now, these are just garbage values intended to give the library _something_ (we don't expect them to actually work yet) """ monkeypatch.setenv("DATASTORE_EMULATOR_HOST", "localhost") monkeypatch.setenv("DATASTORE_DATASET", "datastore-stub-test")
def test_pytest_addopts_before_pytester(request, monkeypatch: MonkeyPatch) -> None: orig = os.environ.get("PYTEST_ADDOPTS", None) monkeypatch.setenv("PYTEST_ADDOPTS", "--orig-unused") pytester: Pytester = request.getfixturevalue("pytester") assert "PYTEST_ADDOPTS" not in os.environ pytester._finalize() assert os.environ.get("PYTEST_ADDOPTS") == "--orig-unused" monkeypatch.undo() assert os.environ.get("PYTEST_ADDOPTS") == orig
def test_dask_clients_pool_missing_raises_configuration_error( minimal_dask_config: None, monkeypatch: MonkeyPatch): monkeypatch.setenv("DIRECTOR_V2_DASK_CLIENT_ENABLED", "0") settings = AppSettings.create_from_envs() app = init_app(settings) with TestClient(app, raise_server_exceptions=True) as client: with pytest.raises(ConfigurationError): DaskClientsPool.instance(client.app)
def test_cache_creates_location_if_missing(tmp_path: Path, monkeypatch: MonkeyPatch): cache_location = tmp_path / "directory does not exist yet" monkeypatch.setenv(CACHE_LOCATION_ENV, str(cache_location)) _ = LocalTrainingCache() assert cache_location.is_dir()
def test_setenv_prepend() -> None: import os monkeypatch = MonkeyPatch() monkeypatch.setenv("XYZ123", "2", prepend="-") monkeypatch.setenv("XYZ123", "3", prepend="-") assert os.environ["XYZ123"] == "3-2" monkeypatch.undo() assert "XYZ123" not in os.environ
async def dask_local_cluster(monkeypatch: MonkeyPatch) -> LocalCluster: async with LocalCluster(n_workers=2, threads_per_worker=1, asynchronous=True) as cluster: scheduler_address = URL(cluster.scheduler_address) monkeypatch.setenv("DASK_SCHEDULER_HOST", scheduler_address.host or "invalid") monkeypatch.setenv("DASK_SCHEDULER_PORT", f"{scheduler_address.port}") yield cluster
def test_get_rules_dirs_with_custom_rules( user_ruledirs: List[str], use_default: bool, expected: List[str], monkeypatch: MonkeyPatch, ) -> None: """Test it returns expected dir lists when custom rules exist.""" monkeypatch.setenv(constants.CUSTOM_RULESDIR_ENVVAR, str(_CUSTOM_RULESDIR)) assert get_rules_dirs(user_ruledirs, use_default) == expected
def test_https_fetcher_fails(tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch) -> None: """Test the HTTPS fetcher failing.""" monkeypatch.setenv('myusername', 'user123') monkeypatch.setenv('mypassword', 'somep4ss') # This syntactically valid uri points to nothing and should ConnectTimeout. uri = 'https://{{myusername}}:{{mypassword}}@127.0.0.1/path/to/file.json' fetcher = cache.FetcherFactory.get_fetcher(tmp_trestle_dir, uri) with pytest.raises(TrestleError): fetcher._update_cache()
def test_init__sets_base_url_and_timeout(monkeypatch: MonkeyPatch): host = "tcp://localhost:1234" monkeypatch.setenv("QCS_SETTINGS_APPLICATIONS_PYQUIL_QUILC_URL", host) client_configuration = QCSClientConfiguration.load() compiler_client = CompilerClient(client_configuration=client_configuration, request_timeout=3.14) assert compiler_client.base_url == host assert compiler_client.timeout == 3.14
def test_setenv_prepend(): import os monkeypatch = MonkeyPatch() monkeypatch.setenv("XYZ123", 2, prepend="-") assert os.environ["XYZ123"] == "2" monkeypatch.setenv("XYZ123", 3, prepend="-") assert os.environ["XYZ123"] == "3-2" monkeypatch.undo() assert "XYZ123" not in os.environ
def test_config_connector_already_in_jars(monkeypatch: MonkeyPatch) -> None: d = Path(tempfile.mkdtemp()) jars = d.joinpath("jars") monkeypatch.setenv("SPARK_HOME", d.as_posix()) jars.mkdir() jars.joinpath("gcs-connector-hadoop2-2.1.1.jar").touch() c = get_gcs_enabled_config("foobar") assert c.contains("spark.hadoop.fs.gs.impl") assert c.get("spark.hadoop.fs.gs.project.id") == "foobar" assert not c.contains("spark.jars")
def test_cache_reportheader(env, pytester: Pytester, monkeypatch: MonkeyPatch) -> None: pytester.makepyfile("""def test_foo(): pass""") if env: monkeypatch.setenv(*env) expected = os.path.join(env[1], ".pytest_cache") else: monkeypatch.delenv("TOX_ENV_DIR", raising=False) expected = ".pytest_cache" result = pytester.runpytest("-v") result.stdout.fnmatch_lines(["cachedir: %s" % expected])
def test_valid_network_names(simcore_services_network_name: str, monkeypatch: MonkeyPatch) -> None: monkeypatch.setenv("DYNAMIC_SIDECAR_IMAGE", "local/dynamic-sidecar:MOCKED") monkeypatch.setenv("SIMCORE_SERVICES_NETWORK_NAME", simcore_services_network_name) monkeypatch.setenv("TRAEFIK_SIMCORE_ZONE", "test_traefik_zone") monkeypatch.setenv("SWARM_STACK_NAME", "test_swarm_name") monkeypatch.setenv("R_CLONE_S3_PROVIDER", "MINIO") dynamic_sidecar_settings = DynamicSidecarSettings.create_from_envs() assert dynamic_sidecar_settings
def test_should_raise_an_exception_if_missing_env( monkeypatch: MonkeyPatch) -> None: # given monkeypatch.setenv("FTP_HOST", "test_host") monkeypatch.setenv("FTP_PASSWORD", "test_password") monkeypatch.setenv("FTP_PATH", "test/path/foo/bar") # then with pytest.raises(FTPConfigError): FTPConfig.from_env_for_website()
def test_custom_cache_dir_with_env_var(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> None: monkeypatch.setenv("env_var", "custom_cache_dir") pytester.makeini(""" [pytest] cache_dir = {cache_dir} """.format(cache_dir="$env_var")) pytester.makepyfile(test_errored="def test_error():\n assert False") pytester.runpytest() assert pytester.path.joinpath("custom_cache_dir").is_dir()
def test_setenv_prepend(): import os monkeypatch = MonkeyPatch() with pytest.warns(pytest.PytestWarning): monkeypatch.setenv("XYZ123", 2, prepend="-") assert os.environ["XYZ123"] == "2" with pytest.warns(pytest.PytestWarning): monkeypatch.setenv("XYZ123", 3, prepend="-") assert os.environ["XYZ123"] == "3-2" monkeypatch.undo() assert "XYZ123" not in os.environ
def test_setenv_deleted_meanwhile(before): key = "qwpeoip123" if before: os.environ[key] = "world" monkeypatch = MonkeyPatch() monkeypatch.setenv(key, "hello") del os.environ[key] monkeypatch.undo() if before: assert os.environ[key] == "world" del os.environ[key] else: assert key not in os.environ
def test_delenv(): name = "xyz1234" assert name not in os.environ monkeypatch = MonkeyPatch() pytest.raises(KeyError, "monkeypatch.delenv(%r, raising=True)" % name) monkeypatch.delenv(name, raising=False) monkeypatch.undo() os.environ[name] = "1" try: monkeypatch = MonkeyPatch() monkeypatch.delenv(name) assert name not in os.environ monkeypatch.setenv(name, "3") assert os.environ[name] == "3" monkeypatch.undo() assert os.environ[name] == "1" finally: if name in os.environ: del os.environ[name]
def inline_run(self, *args, **kwargs): """Run ``pytest.main()`` in-process, returning a HookRecorder. Runs the :py:func:`pytest.main` function to run all of pytest inside the test process itself. This means it can return a :py:class:`HookRecorder` instance which gives more detailed results from that run than can be done by matching stdout/stderr from :py:meth:`runpytest`. :param args: command line arguments to pass to :py:func:`pytest.main` :param plugin: (keyword-only) extra plugin instances the ``pytest.main()`` instance should use :return: a :py:class:`HookRecorder` instance """ finalizers = [] try: # Do not load user config. monkeypatch = MonkeyPatch() monkeypatch.setenv("HOME", str(self.tmpdir)) monkeypatch.setenv("USERPROFILE", str(self.tmpdir)) finalizers.append(monkeypatch.undo) # When running pytest inline any plugins active in the main test # process are already imported. So this disables the warning which # will trigger to say they can no longer be rewritten, which is # fine as they have already been rewritten. orig_warn = AssertionRewritingHook._warn_already_imported def revert_warn_already_imported(): AssertionRewritingHook._warn_already_imported = orig_warn finalizers.append(revert_warn_already_imported) AssertionRewritingHook._warn_already_imported = lambda *a: None # Any sys.module or sys.path changes done while running pytest # inline should be reverted after the test run completes to avoid # clashing with later inline tests run within the same pytest test, # e.g. just because they use matching test module names. finalizers.append(self.__take_sys_modules_snapshot().restore) finalizers.append(SysPathsSnapshot().restore) # Important note: # - our tests should not leave any other references/registrations # laying around other than possibly loaded test modules # referenced from sys.modules, as nothing will clean those up # automatically rec = [] class Collect(object): def pytest_configure(x, config): rec.append(self.make_hook_recorder(config.pluginmanager)) plugins = kwargs.get("plugins") or [] plugins.append(Collect()) ret = pytest.main(list(args), plugins=plugins) if len(rec) == 1: reprec = rec.pop() else: class reprec(object): pass reprec.ret = ret # typically we reraise keyboard interrupts from the child run # because it's our user requesting interruption of the testing if ret == EXIT_INTERRUPTED and not kwargs.get("no_reraise_ctrlc"): calls = reprec.getcalls("pytest_keyboard_interrupt") if calls and calls[-1].excinfo.type == KeyboardInterrupt: raise KeyboardInterrupt() return reprec finally: for finalizer in finalizers: finalizer()