def install(specs, name, channel, yes, strict_channel_priority): """Conda install (or update) packages.""" name = _infer_name_if_necessary(name) main.conda_install( name=name, specs=specs, channels=channel, yes=yes, strict_channel_priority=strict_channel_priority, )
def test_history_after_install(end_to_end_setup): """Test the history.yaml file in detail after pytest has been installed.""" name = end_to_end_setup["name"] conda_install(name=name, specs=["pytest"], yes=True) env_dir = end_to_end_setup["env_dir"] channels = end_to_end_setup["channels"] channel_command = end_to_end_setup["channel_command"] log_file = env_dir / "history.yaml" actual_history_content = log_file.read_text() print(actual_history_content) actual = yaml.load(actual_history_content, Loader=yaml.FullLoader) action_install_expected_pattern = ( rf"(conda install --name {name})(\s)(pytest=)(.*)({channel_command})") expected_packages = { "conda": { "colorama": "*", "python": "3.6", "pytest": "*" } } expected_log = f"conda install --name {name} pytest" expected_debug = 2 * [{ "platform": get_platform_name(), "conda_version": CONDA_VERSION, "pip_version": get_pip_version(name=name), "timestamp": str(date.today()), }] assert actual["packages"] == expected_packages assert actual["logs"][-1] == expected_log assert len(actual["logs"]) == 2 assert actual["channels"] == channels assert re.match(action_install_expected_pattern, actual["actions"][-1]) assert len(actual["actions"]) == 2 for key, val in expected_debug[1].items(): if key == "timestamp": assert actual["debug"][1][key].startswith(val) else: assert actual["debug"][1][key] == val expected_start = [f"name: {name}", "channels:"] for channel in channels: expected_start.append(f" - {channel}") expected_history_start = "\n".join(expected_start + [ "packages:", " conda:", " python: '3.6'", " colorama: '*'", " pytest: '*'", "logs:", " - conda create --name end_to_end_test python=3.6 colorama --override-channels --strict-channel-priority", " --channel main", f" - {expected_log}", "actions:", f" - conda create --name {name} python=3.6", ]) end = actual_history_content.rfind("python=3.6") + len("python=3.6") actual_history_start = actual_history_content[:end] assert actual_history_start == expected_history_start
def test_history_after_install(end_to_end_setup): """Test the history.yaml file in detail after pytest has been installed.""" name = end_to_end_setup["name"] env = conda_install(name=name, specs=["pytest>4.0,<6.0"], yes=True) env_dir = end_to_end_setup["env_dir"] channels = end_to_end_setup["channels"] channel_command = end_to_end_setup["channel_command"] log_file = env_dir / "history.yaml" actual_history_content = log_file.read_text() print(actual_history_content) actual = yaml.load(actual_history_content, Loader=yaml.FullLoader) action_install_expected_pattern = ( rf"(conda install --name {name})(\s)(pytest=)(.*)({channel_command})") expected_packages = { "conda": { "colorama": "*", "python": "python=3.6", "pytest": "pytest>4.0,<6.0" } } expected_log = f'conda install --name {name} "pytest>4.0,<6.0"' expected_debug = 2 * [{ "platform": get_platform_name(), "conda_version": CONDA_VERSION, "pip_version": get_pip_version(name=name), "timestamp": str(date.today()), }] assert actual["packages"] == expected_packages assert len(actual["revisions"]) == 2 assert actual["revisions"][-1]["log"] == expected_log assert actual["channels"] == channels assert re.match(action_install_expected_pattern, actual["revisions"][-1]["action"]) for key, val in expected_debug[1].items(): if key == "timestamp": assert actual["revisions"][1]["debug"][key].startswith(val) else: assert actual["revisions"][1]["debug"][key] == val # The packages section at the top of the file is current expected_packages_section = "\n".join([ "packages:", " conda:", " python: python=3.6", " colorama: '*'", " pytest: pytest>4.0,<6.0", "revisions:", ]) assert expected_packages_section in actual_history_content conda_dependencies = env.dependencies["conda"] expected_second_revision = "\n".join([ " - packages:", " conda:", " python: python=3.6", " colorama: '*'", " pytest: pytest>4.0,<6.0", " diff:", " conda:", " upsert:", f" - pytest={conda_dependencies['pytest'].version}", f' log: conda install --name {name} "pytest>4.0,<6.0"', f" action: conda install --name {name} pytest", ]) index_first_revision = actual_history_content.find(" - packages:") index_second_revision_start = actual_history_content.find( " - packages:", index_first_revision + 1) second_action = f"action: conda install --name {name} pytest" index_second_action = actual_history_content.find( second_action, index_second_revision_start) + len(second_action) actual_second_revision = actual_history_content[ index_second_revision_start:index_second_action] assert actual_second_revision == expected_second_revision
def test_remote_end_to_end(end_to_end_setup, mocker): """Test setup, create, install, pull and push feature of conda_env_tracker""" env = end_to_end_setup["env"] remote_dir = end_to_end_setup["remote_dir"] channels = end_to_end_setup["channels"] channel_command = end_to_end_setup["channel_command"] setup_remote(name=env.name, remote_dir=remote_dir) local_io = EnvIO(env_directory=USER_ENVS_DIR / env.name) assert str(local_io.get_remote_dir()) == str(remote_dir) assert not list(remote_dir.glob("*")) push(name=env.name) remote_io = EnvIO(env_directory=remote_dir) assert remote_io.get_history() == local_io.get_history() assert remote_io.get_environment() == local_io.get_environment() env = conda_install(name=env.name, specs=["pytest"], yes=True) assert env.local_io.get_history() != remote_io.get_history() env = push(name=env.name) assert env.local_io.get_history() == remote_io.get_history() log_mock = mocker.patch("conda_env_tracker.pull.logging.Logger.info") env = pull(name=env.name) log_mock.assert_called_once_with("Nothing to pull.") remove_package_from_history(env, "pytest") conda_dependencies = env.dependencies["conda"] assert env.local_io.get_history() != remote_io.get_history() assert env.history.packages == { "conda": { "python": Package( "python", "python=3.6", version=conda_dependencies["python"].version, build=conda_dependencies["python"].build, ), "colorama": Package( "colorama", "colorama", version=conda_dependencies["colorama"].version, build=conda_dependencies["colorama"].build, ), } } env = pull(name=env.name) assert env.local_io.get_history() == remote_io.get_history() assert remote_io.get_environment() == local_io.get_environment() remove_package_from_history(env, "pytest") assert env.local_io.get_history() != remote_io.get_history() env = conda_install(name=env.name, specs=["pytest-cov"], channels=("main", ), yes=True) env = pull(name=env.name, yes=True) conda_dependencies = env.dependencies["conda"] assert env.history.packages == { "conda": { "python": Package( "python", "python=3.6", version=conda_dependencies["python"].version, build=conda_dependencies["python"].build, ), "colorama": Package( "colorama", "colorama", version=conda_dependencies["colorama"].version, build=conda_dependencies["colorama"].build, ), "pytest": Package( "pytest", "pytest", version=conda_dependencies["pytest"].version, build=conda_dependencies["pytest"].build, ), "pytest-cov": Package( "pytest-cov", "pytest-cov", version=conda_dependencies["pytest-cov"].version, build=conda_dependencies["pytest-cov"].build, ), } } log_list = [ f"conda create --name {env.name} python=3.6 colorama {channel_command}", f"conda install --name {env.name} pytest", f"conda install --name {env.name} pytest-cov --channel main", ] assert env.history.logs == log_list actual_env = (env.local_io.env_dir / "environment.yml").read_text() conda_dependencies = get_dependencies(name=env.name)["conda"] expected_env = [f"name: {env.name}", "channels:"] for channel in channels + ["nodefaults"]: expected_env.append(f" - {channel}") expected_env = ("\n".join(expected_env + [ "dependencies:", " - python=" + conda_dependencies["python"].version, " - colorama=" + conda_dependencies["colorama"].version, " - pytest=" + conda_dependencies["pytest"].version, " - pytest-cov=" + conda_dependencies["pytest-cov"].version, ]) + "\n") assert actual_env == expected_env expected_debug = 3 * [{ "platform": get_platform_name(), "conda_version": CONDA_VERSION, "pip_version": get_pip_version(name=env.name), "timestamp": str(date.today()), }] for i in range(len(env.history.debug)): for key, val in expected_debug[i].items(): if key == "timestamp": assert env.history.debug[i][key].startswith(val) else: assert env.history.debug[i][key] == val