def test_create_no_auto_do_not_ask_to_sync(mocker): mocker.patch("conda_env_tracker.main.os.environ", {}) sync_mock = mocker.patch("conda_env_tracker.main.sync") mocker.patch("conda_env_tracker.main.Environment.create") mocker.patch("conda_env_tracker.main.Environment.export") mocker.patch("conda_env_tracker.main.jupyter_kernel_install_query") name = "test_env_name" create(name=name, specs=["python=3.7"]) sync_mock.assert_not_called()
def test_create_yes_auto_do_not_ask_to_sync(mocker): mocker.patch("conda_env_tracker.main.os.environ", {"CET_AUTO": "0"}) mocker.patch("conda_env_tracker.main.prompt_yes_no", mocker.Mock(return_value=True)) sync_mock = mocker.patch("conda_env_tracker.main.sync") mocker.patch("conda_env_tracker.main.Environment.create") mocker.patch("conda_env_tracker.main.Environment.export") mocker.patch("conda_env_tracker.main.jupyter_kernel_install_query") name = "test_env_name" create(name=name, specs=["python=3.7"], yes=True) sync_mock.assert_not_called()
def create(packages, name, channel, sync_arg, infer, yes, strict_channel_priority): """Create the software environment.""" if infer: main.infer(name=name, specs=packages, channels=channel) else: main.create( name=name, specs=packages, channels=channel, yes=yes, strict_channel_priority=strict_channel_priority, ) if sync_arg: main.setup_remote(name=name, yes=yes) main.push(name=name)
def test_name_cleaning(mocker): create_mock = mocker.patch("conda_env_tracker.main.Environment.create") mocker.patch("conda_env_tracker.main.Environment.export") mocker.patch("conda_env_tracker.main.jupyter_kernel_install_query") mocker.patch("conda_env_tracker.main.prompt_yes_no", mocker.Mock(return_value=False)) create(name="test", specs=["one", "TWO", "thRee=1.0"]) create_mock.assert_called_once_with( name="test", packages=Packages.from_specs(["one", "two", "three=1.0"]), channels=None, yes=False, strict_channel_priority=True, )
def setup_env(mocker, request): """Setup and teardown for tests with parameterized inputs. Even yes is passed in commands like create and install, the option itself don't show up in actions and logs as it provide no additional value for reproducibility.""" env_dir = USER_ENVS_DIR / ENV_NAME mocker.patch( "conda_env_tracker.history.debug.get_pip_version", mocker.Mock(return_value="18.1"), ) mocker.patch("conda_env_tracker.env.get_all_existing_environment") mocker.patch("conda_env_tracker.main._ask_user_to_sync") mocker.patch("conda_env_tracker.gateways.conda.run_command") initial_conda_packages = { "pandas": Package("pandas", "pandas", "0.23", "py_36") } get_package_mock = mocker.patch( "conda_env_tracker.env.get_dependencies", mocker.Mock(return_value={ "conda": initial_conda_packages, "pip": {} }), ) condarc_channels = ["conda-forge", "main"] mocker.patch( "conda_env_tracker.env.get_conda_channels", mocker.Mock(return_value=condarc_channels), ) if "channels" in request.param["input"]: channels = request.param["input"]["channels"] else: channels = condarc_channels id_mock = mocker.patch("conda_env_tracker.history.history.uuid4") id_mock.return_value = "my_unique_id" env = main.create(name=ENV_NAME, **request.param["input"]) env_io = EnvIO(env_directory=env_dir) yield { "channels": channels, "env": env, "env_io": env_io, "expected": request.param["expected"], "get_package_mock": get_package_mock, "initial_conda_packages": initial_conda_packages, "id": id_mock, } if env_dir.exists(): shutil.rmtree(env_dir)
def end_to_end_setup(request): """Setup and teardown for tests.""" name = "end_to_end_test" channels = ["defaults"] env_dir = USER_ENVS_DIR / name remote_path = Path(__file__).parent.absolute() / "remote_test_dir" if remote_path.exists(): shutil.rmtree(remote_path) remote_path.mkdir() def teardown(): delete_conda_environment(name=name) if env_dir.is_dir(): shutil.rmtree(env_dir) if remote_path.is_dir(): shutil.rmtree(remote_path) request.addfinalizer(teardown) try: env = create( name=name, specs=["python=3.6", "colorama"], channels=channels, yes=True ) setup_remote(name=name, remote_dir=remote_path, yes=True) except CondaEnvTrackerCondaError as err: teardown() raise err channel_command = ( "--override-channels --strict-channel-priority --channel " + " --channel ".join(channels) ) return { "name": name, "env": env, "env_dir": env_dir, "channels": channels, "channel_command": channel_command, "remote_dir": remote_path, }
def r_end_to_end_setup(request): """Setup and teardown for R end to end tests.""" name = "r_end_to_end_test" channels = ["r", "defaults"] env_dir = USER_ENVS_DIR / name remote_path = Path(__file__).parent.absolute() / "remote_test_dir" if remote_path.exists(): shutil.rmtree(remote_path) remote_path.mkdir() def teardown(): delete_conda_environment(name=name) if env_dir.is_dir(): shutil.rmtree(env_dir) if remote_path.is_dir(): shutil.rmtree(remote_path) request.addfinalizer(teardown) try: env = create(name=name, specs=["r-base", "r-devtools"], channels=channels, yes=True) setup_remote(name=name, remote_dir=remote_path, yes=True) except CondaEnvTrackerCondaError as err: teardown() raise err return { "name": name, "env": env, "env_dir": env_dir, "channels": channels, "remote_dir": remote_path, }
def setup_env(request): """Creating cet remotes to test cet auto shell script.""" # pylint: disable=too-many-statements env_dir = USER_ENVS_DIR / CET_ENV_NAME unseen_local_dir = USER_ENVS_DIR / UNSEEN_ENV_NAME empty_dir = Path.home() / ".cet" / "empty_dir" cet_dir = Path.home() / ".cet" / "cet_dir" identical_history_dir = Path.home() / ".cet" / "identical_history_dir" unseen_cet_dir = Path.home() / ".cet" / "unseen_cet_dir" remote_path = cet_dir / ".cet" identical_remote_path = identical_history_dir / ".cet" unseen_remote = unseen_cet_dir / ".cet" def teardown(): conda.delete_conda_environment(name=CET_ENV_NAME) conda.delete_conda_environment(name=UNSEEN_ENV_NAME) if env_dir.is_dir(): shutil.rmtree(env_dir) if cet_dir.is_dir(): shutil.rmtree(cet_dir) if unseen_cet_dir.is_dir(): shutil.rmtree(unseen_cet_dir) if identical_history_dir.is_dir(): shutil.rmtree(identical_history_dir) if empty_dir.is_dir(): shutil.rmtree(empty_dir) if unseen_local_dir.is_dir(): shutil.rmtree(unseen_local_dir) request.addfinalizer(teardown) empty_dir.mkdir() cet_dir.mkdir() identical_history_dir.mkdir() unseen_cet_dir.mkdir() if request.param: subprocess.run(f"cd {empty_dir}; git init --quiet", shell=True) subprocess.run(f"cd {cet_dir}; git init --quiet", shell=True) subprocess.run(f"cd {identical_history_dir}; git init --quiet", shell=True) subprocess.run(f"cd {unseen_cet_dir}; git init --quiet", shell=True) remote_path.mkdir() identical_remote_path.mkdir() unseen_remote.mkdir() try: env = main.create(name=CET_ENV_NAME, specs=["python"], channels=["defaults"], yes=True) main.setup_remote(name=CET_ENV_NAME, remote_dir=remote_path, yes=True) main.push(name=env.name) except errors.CondaEnvTrackerCondaError as err: teardown() raise err remote_history_file = remote_path / "history.yaml" content = remote_history_file.read_text() # We add newline characters to the remote history file so that the unix utility `wc -m` will find more # characters in the remote history.yaml file than in the local history.yaml file. This prompts the cet_auto # shell function to ask the user to pull the changes from remote into local. additional_characters = "\n\n\n" remote_history_file.write_text(content + additional_characters) identical_remote_history_file = identical_remote_path / "history.yaml" identical_remote_history_file.write_text(content) unseen_history_file = unseen_remote / "history.yaml" unseen_history_file.write_text( content.replace(CET_ENV_NAME, UNSEEN_ENV_NAME)) local_conda_env = env_dir / "conda-env.yaml" conda_env_content = local_conda_env.read_text() unseen_conda_env = unseen_remote / "conda-env.yaml" unseen_conda_env.write_text( conda_env_content.replace(CET_ENV_NAME, UNSEEN_ENV_NAME)) return { "cet": cet_dir, "empty_cet": empty_dir, "identical_cet": identical_history_dir, "unseen_cet": unseen_cet_dir, "remote_history_file": remote_history_file, "local_history_file": env_dir / "history.yaml", }