Exemple #1
0
def test_r_install_double_quotes(setup_env, quote_mark, mocker):
    double_quote_command = "install.packages('h2o')"
    expected_command = double_quote_command.replace("'", r"\"")
    command = double_quote_command.replace("'", quote_mark)
    env = setup_env["env"]
    env_io = setup_env["env_io"]
    expected = copy.deepcopy(setup_env["expected"])

    run_mock = mocker.patch("conda_env_tracker.gateways.r.run_command")
    run_mock.configure_mock(**{
        "return_value.failed": False,
        "return_value.stderr": ""
    })
    mocker.patch(
        "conda_env_tracker.env.get_r_dependencies",
        mocker.Mock(return_value={"h2o": Package("h2o", "h2o", "3.24.0.3")}),
    )
    h2o = Package("h2o", command)
    packages = Packages([h2o])

    RHandler(env=env).install(packages=packages)
    expected["logs"].append(f'R --quiet --vanilla -e "{expected_command}"')
    expected["packages"]["r"] = {"h2o": h2o}
    actual = env_io.get_history()

    assert actual.logs == expected["logs"]
    assert actual.packages == expected["packages"]
    assert actual.actions[-1] == expected["logs"][-1]

    actual_install_r = (env_io.env_dir / "install.R").read_text()
    assert actual_install_r == command
Exemple #2
0
def test_custom_r_install(setup_env, mocker):
    command = 'install.packages("h2o")'
    env = setup_env["env"]
    env_io = setup_env["env_io"]
    expected = copy.deepcopy(setup_env["expected"])

    run_mock = mocker.patch("conda_env_tracker.gateways.r.run_command")
    run_mock.configure_mock(**{
        "return_value.failed": False,
        "return_value.stderr": ""
    })
    mocker.patch(
        "conda_env_tracker.env.get_r_dependencies",
        mocker.Mock(return_value={"h2o": "3.24.0.3"}),
    )
    h2o = Package("h2o", command)
    packages = Packages([h2o])

    RHandler(env=env).install(packages=packages)
    expected["logs"].append(f"R --quiet --vanilla -e '{command}'")
    expected["packages"]["r"] = {"h2o": h2o}
    actual = env_io.get_history()

    assert actual.logs == expected["logs"]
    assert actual.packages == expected["packages"]
    assert actual.actions[-1] == expected["logs"][-1]

    actual_install_r = (env_io.env_dir / "install.R").read_text()
    assert actual_install_r == command
Exemple #3
0
def test_two_same_package_r_install(setup_env, mocker):
    """Test that the second command replaces the first command (and updates the version of the package)"""
    command_h2o_1 = 'install.packages("h2o")'
    escaped_command_1 = command_h2o_1.replace('"', r"\"")
    command_h2o_2 = 'install.packages("h2o", type="source", repos=(c("http://h2o-release.s3.amazonaws.com/h2o/latest_stable_R")))'
    escaped_command_2 = command_h2o_2.replace('"', r"\"")
    env = setup_env["env"]
    env_io = setup_env["env_io"]
    expected = copy.deepcopy(setup_env["expected"])

    run_mock = mocker.patch("conda_env_tracker.gateways.r.run_command")
    run_mock.configure_mock(**{
        "return_value.failed": False,
        "return_value.stderr": ""
    })
    mocker.patch(
        "conda_env_tracker.env.get_r_dependencies",
        side_effect=[
            {
                "h2o": Package("h2o", "h2o", "3.4.0.1")
            },
            {
                "h2o": Package("h2o", "h2o", "3.24.0.5")
            },
        ],
    )

    h2o_1 = Package("h2o", command_h2o_1)
    h2o_2 = Package("h2o", command_h2o_2)

    packages_1 = Packages([h2o_1])
    packages_2 = Packages([h2o_2])

    RHandler(env=env).install(packages=packages_1)
    RHandler(env=env).install(packages=packages_2)

    expected["logs"].append(f'R --quiet --vanilla -e "{escaped_command_1}"')
    expected["logs"].append(f'R --quiet --vanilla -e "{escaped_command_2}"')
    expected["packages"]["r"] = {"h2o": h2o_2}
    actual = env_io.get_history()

    assert actual.logs == expected["logs"]
    assert actual.packages == expected["packages"]
    assert actual.actions[-1] == expected["logs"][-1]

    actual_install_r = (env_io.env_dir / "install.R").read_text()
    assert actual_install_r == command_h2o_2
Exemple #4
0
def r_remove(name: str, specs=ListLike, yes: bool = False) -> Environment:
    """R remove spec"""
    env = Environment.read(name=name)
    check_r_base_package(env=env)
    packages = Packages.from_specs(specs)
    RHandler(env=env).remove(packages=packages)
    _ask_user_to_sync(name=name, yes=yes)
    return env
Exemple #5
0
def _handle_r_extra_log(env: Environment, history: History,
                        index: int) -> Environment:
    log = history.logs[index]
    if "remove.packages(" in log:
        packages = history.logs.extra_removed_packages(index=index)
        RHandler(env=env).remove(packages=packages)
    else:
        package_names = [
            package_name
            for package_name in history.revisions.diffs[index]["r"]["upsert"]
        ]
        packages = Packages()
        for package_name in package_names:
            packages.append(
                history.revisions.packages[index]["r"][package_name])
        RHandler(env=env).install(packages=packages)
    return env
Exemple #6
0
def test_two_custom_r_install(setup_env, mocker):
    """Test that the second custom command replaces the first command (and updates the version of the package)"""
    custom_command_h2o = 'install.packages("h2o")'
    custom_command_trelliscopejs = (
        'library("devtools"); install_github("hafen/trelliscopejs")')
    env = setup_env["env"]
    env_io = setup_env["env_io"]
    expected = copy.deepcopy(setup_env["expected"])

    run_mock = mocker.patch("conda_env_tracker.gateways.r.run_command")
    run_mock.configure_mock(**{
        "return_value.failed": False,
        "return_value.stderr": ""
    })
    mocker.patch(
        "conda_env_tracker.env.get_r_dependencies",
        return_value={
            "h2o": Package("h2o", "h2o", "3.4.0.1"),
            "trelliscopejs": Package("trelliscopejs", "trelliscopejs",
                                     "0.1.7"),
        },
    )

    h2o = Package("h2o", custom_command_h2o)
    trelliscopejs = Package("trelliscopejs", custom_command_trelliscopejs)

    RHandler(env=env).install(packages=Packages([h2o]))
    RHandler(env=env).install(packages=Packages([trelliscopejs]))

    expected["logs"].append(f"R --quiet --vanilla -e '{custom_command_h2o}'")
    expected["logs"].append(
        f"R --quiet --vanilla -e '{custom_command_trelliscopejs}'")
    expected["packages"]["r"] = {"h2o": h2o, "trelliscopejs": trelliscopejs}
    actual = env_io.get_history()

    assert actual.logs == expected["logs"]
    assert actual.packages == expected["packages"]
    assert actual.actions[-1] == expected["logs"][-1]

    actual_install_r = (env_io.env_dir / "install.R").read_text()
    expected_install_r = "\n".join(
        [custom_command_h2o, custom_command_trelliscopejs])
    assert actual_install_r == expected_install_r
Exemple #7
0
def r_install(
    name: str, package_names: ListLike, commands: ListLike, yes: bool = False
) -> Environment:
    """Install R packages with corresponding R command."""
    env = Environment.read(name=name)
    check_r_base_package(env=env)
    packages = clean_r_specs(package_names=package_names, commands=commands)
    RHandler(env=env).install(packages=packages)
    _ask_user_to_sync(name=name, yes=yes)
    return env
Exemple #8
0
def test_remove_r_package(setup_env, mocker):
    env = setup_env["env"]
    env_io = setup_env["env_io"]
    expected = copy.deepcopy(setup_env["expected"])

    r_command = 'install.packages("h2o")'
    escaped_command = r_command.replace('"', r"\"")
    run_mock = mocker.patch("conda_env_tracker.gateways.r.run_command")
    run_mock.configure_mock(**{
        "return_value.failed": False,
        "return_value.stderr": ""
    })
    mocker.patch(
        "conda_env_tracker.env.get_r_dependencies",
        mocker.Mock(return_value={"h2o": Package("h2o", "h2o", "3.24.0.3")}),
    )

    handler = RHandler(env=env)
    h2o = Package("h2o", r_command)
    packages = Packages([h2o])
    handler.install(packages=packages)

    install_log = f'R --quiet --vanilla -e "{escaped_command}"'

    expected["logs"].append(install_log)
    expected["packages"]["r"] = {"h2o": h2o}

    actual = env_io.get_history()

    assert actual.logs == expected["logs"]
    assert actual.packages == expected["packages"]
    assert actual.actions[-1] == install_log

    expected_install_r = r_command
    actual_install_r = (env_io.env_dir / "install.R").read_text()
    assert actual_install_r == expected_install_r

    mocker.patch("conda_env_tracker.r.r_remove")
    handler.remove(packages=packages)
    actual = env_io.get_history()
    command = r"remove.packages(c(\"h2o\"))"

    expected["packages"].pop("r")
    expected["logs"].append(f'R --quiet --vanilla -e "{command}"')
    assert actual.logs == expected["logs"]
    assert actual.packages == expected["packages"]
    assert actual.actions[-1] == expected["logs"][-1]

    assert not (env_io.env_dir / "install.R").exists()
Exemple #9
0
def _install_missing_r_packages(
    env: Environment, local_history: History, remote_history: History
) -> Environment:
    local_r_packages = local_history.packages.get("r", {})
    remote_r_packages = remote_history.packages.get("r", {})
    handler = RHandler(env=env)
    packages_to_install = Packages()
    packages_to_add_to_history = Packages()
    for package_name, package in local_r_packages.items():
        if package_name not in remote_r_packages:
            if package_name in env.dependencies.get("r", {}):
                packages_to_add_to_history.append(package)
            else:
                packages_to_install.append(package)
    if packages_to_install:
        handler.install(packages_to_install)
    if packages_to_add_to_history:
        handler.update_history_install(packages_to_add_to_history)
    return env
Exemple #10
0
def _handle_r_extra_log(env: Environment, history: History, index: int) -> Environment:
    log = history.logs[index]
    if "remove.packages(" in log:
        packages = history.logs.extra_removed_packages(index=index)
        RHandler(env=env).remove(packages=packages)
    return env