Пример #1
0
def test_run_returns_nonzero_errorlevel():
    prefix = make_temp_prefix(name="test")
    with make_temp_env(prefix=prefix) as prefix:
        stdout, stderr, result = run_inprocess_conda_command(
            'conda run -p "{}" exit 5'.format(prefix))

        assert result == 5
Пример #2
0
def test_run_returns_int():
    prefix = make_temp_prefix(name="test")
    with make_temp_env(prefix=prefix):
        stdout, stderr, result = run_inprocess_conda_command(
            "conda run -p {} echo hi".format(prefix))

        assert isinstance(result, int)
Пример #3
0
def test_clean_logfiles(clear_cache):
    """Logfiles are found in pkgs_dir/.logs.

    Since these log files are uniquely implemented for the experimental libmamba release we will
    mock the log files.
    """
    pkg = "bzip2"

    with make_temp_package_cache() as pkgs_dir:
        # logfiles don't exist ahead of time
        assert not _get_logfiles(pkgs_dir)

        with make_temp_env(pkg):
            # mimic logfiles being created
            logs = join(pkgs_dir, CONDA_LOGS_DIR)
            mkdir_p(logs)
            path = join(logs, f"{datetime.utcnow():%Y%m%d-%H%M%S-%f}.log")
            with open(path, "w"):
                pass

            # logfiles exist
            assert path in _get_logfiles(pkgs_dir)

            # --json flag is regression test for #5451
            stdout, _, _ = run_command(Commands.CLEAN, "", "--logfiles",
                                       "--yes", "--json")
            json_loads(stdout)  # assert valid json

            # logfiles removed
            assert not _get_logfiles(pkgs_dir)

        # logfiles still removed
        assert not _get_logfiles(pkgs_dir)
Пример #4
0
    def test_multi_channel_export(self):
        """
            When try to import from txt
            every package should come from same channel
        """
        with make_temp_env("python=3.5") as prefix:
            assert exists(join(prefix, PYTHON_BINARY))
            assert package_is_installed(prefix, 'python=3')

            run_command(Commands.INSTALL, prefix, "six", "-c", "conda-forge")
            assert package_is_installed(prefix, "six")

            output, error, _ = run_command(Commands.LIST, prefix, "-e")
            self.assertIn("conda-forge", output)

            try:
                with Utf8NamedTemporaryFile(mode="w",
                                            suffix="txt",
                                            delete=False) as env_txt:
                    env_txt.write(output)
                    env_txt.close()
                    prefix2 = make_temp_prefix()
                    run_command(Commands.CREATE, prefix2, "--file",
                                env_txt.name)

                    assert package_is_installed(prefix2, "python")
                output2, error, _ = run_command(Commands.LIST, prefix2, "-e")
                self.assertEqual(output, output2)
            finally:
                rm_rf(env_txt.name)
Пример #5
0
def test_clean_and_packages(clear_cache):
    pkg = "bzip2"

    with make_temp_package_cache() as pkgs_dir:
        # pkg doesn't exist ahead of time
        assert_not_pkg(pkg, _get_pkgs(pkgs_dir))

        with make_temp_env(pkg) as prefix:
            # pkg exists
            assert_any_pkg(pkg, _get_pkgs(pkgs_dir))

            # --json flag is regression test for #5451
            stdout, _, _ = run_command(Commands.CLEAN, "", "--packages",
                                       "--yes", "--json")
            json_loads(stdout)  # assert valid json

            # pkg still exists since its in use by temp env
            assert_any_pkg(pkg, _get_pkgs(pkgs_dir))

            run_command(Commands.REMOVE, prefix, pkg, "--yes", "--json")
            stdout, _, _ = run_command(Commands.CLEAN, "", "--packages",
                                       "--yes", "--json")
            json_loads(stdout)  # assert valid json

            # pkg is removed
            assert_not_pkg(pkg, _get_pkgs(pkgs_dir))

        # pkg is still removed
        assert_not_pkg(pkg, _get_pkgs(pkgs_dir))
Пример #6
0
def test_run_returns_zero_errorlevel():
    prefix = make_temp_prefix(name="test")
    with make_temp_env(prefix=prefix):
        stdout, stderr, result = run_inprocess_conda_command(
            "conda run -p {} exit 0".format(prefix))

        assert result == 0
Пример #7
0
def test_run_readonly_env(request):
    prefix = make_temp_prefix(name="test")
    with make_temp_env(prefix=prefix) as prefix:
        # Remove write permissions
        current = stat.S_IMODE(os.lstat(prefix).st_mode)
        os.chmod(prefix, current & ~stat.S_IWRITE)

        # reset permissions in case something goes wrong
        def reset_permissions():
            if os.path.exists(prefix):
                os.chmod(prefix, current)

        request.addfinalizer(reset_permissions)

        # Confirm we do not have write access.
        raise_ok = False
        try:
            open(os.path.join(prefix, "test.txt"), "w+")
        except PermissionError:
            raise_ok = True

        assert raise_ok

        stdout, stderr, result = run_inprocess_conda_command(
            "conda run -p {} exit 0".format(prefix))

        # Reset permissions in case all goes according to plan
        reset_permissions()

        assert result == 0
Пример #8
0
    def test_search_2(self):
        with make_temp_env() as prefix:
            stdout, stderr, _ = run_command(Commands.SEARCH,
                                            prefix,
                                            "nose",
                                            use_exception_handler=True)
            result = stdout.replace("Loading channels: ...working... done", "")

            assert "nose                           1.3.7          py37_2  pkgs/main" in result
Пример #9
0
def test_clean_all(clear_cache):
    pkg = "bzip2"

    with make_temp_package_cache() as pkgs_dir:
        # pkg, tarball, & index cache doesn't exist ahead of time
        pkgs, tars, cache = _get_all(pkgs_dir)
        assert_not_pkg(pkg, pkgs)
        assert_not_pkg(pkg, tars)
        assert not cache

        with make_temp_env(pkg) as prefix:
            # pkg, tarball, & index cache exists
            pkgs, tars, cache = _get_all(pkgs_dir)
            assert_any_pkg(pkg, pkgs)
            assert_any_pkg(pkg, tars)
            assert cache

            stdout, _, _ = run_command(Commands.CLEAN, "", "--all", "--yes",
                                       "--json")
            json_loads(stdout)  # assert valid json

            # pkg still exists since its in use by temp env
            # tarball is removed
            # index cache is cleared
            pkgs, tars, cache = _get_all(pkgs_dir)
            assert_any_pkg(pkg, pkgs)
            assert_not_pkg(pkg, tars)
            assert not cache

            run_command(Commands.REMOVE, prefix, pkg, "--yes", "--json")
            stdout, _, _ = run_command(Commands.CLEAN, "", "--packages",
                                       "--yes", "--json")
            json_loads(stdout)  # assert valid json

            # pkg is removed
            # tarball is still removed
            # index cache is still cleared
            pkgs, tars, index_cache = _get_all(pkgs_dir)
            assert_not_pkg(pkg, pkgs)
            assert_not_pkg(pkg, tars)
            assert not cache

        # pkg is still removed
        # tarball is still removed
        # index cache is still cleared
        pkgs, tars, index_cache = _get_all(pkgs_dir)
        assert_not_pkg(pkg, pkgs)
        assert_not_pkg(pkg, tars)
        assert not cache
Пример #10
0
 def test_search_3(self):
     with make_temp_env() as prefix:
         stdout, stderr, _ = run_command(
             Commands.SEARCH,
             prefix,
             "*/linux-64::nose==1.3.7[build=py37_2]",
             "--info",
             use_exception_handler=True,
         )
         result = stdout.replace("Loading channels: ...working... done", "")
         assert "file name   : nose-1.3.7-py37_2" in result
         assert "name        : nose" in result
         assert (
             "url         : https://repo.anaconda.com/pkgs/main/linux-64/nose-1.3.7-py37_2"
             in result)
Пример #11
0
def test_run_uncaptured(capfd):
    prefix = make_temp_prefix(name="test")
    with make_temp_env(prefix=prefix):
        random_text = uuid.uuid4().hex
        stdout, stderr, result = run_inprocess_conda_command(
            "conda run -p {} --no-capture-output echo {}".format(
                prefix, random_text))

        assert result == 0
        # Output is not captured
        assert stdout == ""

        # Check that the expected output is somewhere between the conda logs
        captured = capfd.readouterr()
        assert random_text in captured.out
Пример #12
0
def test_clean_force_pkgs_dirs(clear_cache):
    pkg = "bzip2"

    with make_temp_package_cache() as pkgs_dir:
        # pkgs_dir is a directory
        assert isdir(pkgs_dir)

        with make_temp_env(pkg):
            stdout, _, _ = run_command(Commands.CLEAN, "", "--force-pkgs-dirs",
                                       "--yes", "--json")
            json_loads(stdout)  # assert valid json

            # pkgs_dir is removed
            assert not exists(pkgs_dir)

        # pkgs_dir is still removed
        assert not exists(pkgs_dir)
Пример #13
0
    def test_basic(self):
        with make_temp_env("python=3.5") as prefix:
            assert exists(join(prefix, PYTHON_BINARY))
            assert package_is_installed(prefix, 'python=3')

            output, error, _ = run_command(Commands.LIST, prefix, "-e")

            with Utf8NamedTemporaryFile(mode="w", suffix="txt",
                                        delete=False) as env_txt:
                env_txt.write(output)
                env_txt.flush()
                env_txt.close()
                prefix2 = make_temp_prefix()
                run_command(Commands.CREATE, prefix2, "--file", env_txt.name)

                assert package_is_installed(prefix2, "python")

            output2, error, _ = run_command(Commands.LIST, prefix2, "-e")
            self.assertEqual(output, output2)
Пример #14
0
def test_clean_index_cache(clear_cache):
    pkg = "bzip2"

    with make_temp_package_cache():
        # index cache doesn't exist ahead of time
        assert not _get_index_cache()

        with make_temp_env(pkg):
            # index cache exists
            assert _get_index_cache()

            stdout, _, _ = run_command(Commands.CLEAN, "", "--index-cache",
                                       "--yes", "--json")
            json_loads(stdout)  # assert valid json

            # index cache is cleared
            assert not _get_index_cache()

        # index cache is still cleared
        assert not _get_index_cache()
Пример #15
0
    def test_channel_order_channel_priority_true(self):
        # This is broken, make_temp_env will reset the context. We get away with it, but really
        # we need a function that does both these at the same time.
        with env_var("CONDA_PINNED_PACKAGES",
                     "python=3.6",
                     stack_callback=conda_tests_ctxt_mgmt_def_pol):
            with make_temp_env("pycosat==0.6.2") as prefix:
                assert package_is_installed(prefix, 'python=3.6')
                assert package_is_installed(prefix, 'pycosat')

                # add conda-forge channel
                o, e, _ = run_command(Commands.CONFIG, prefix, "--prepend",
                                      "channels", "conda-forge", '--json')
                assert context.channels == ("conda-forge", "defaults"), o + e
                # update --all
                update_stdout, _, _ = run_command(Commands.UPDATE, prefix,
                                                  '--all')

                # this assertion works with the pinned_packages config to make sure
                # conda update --all still respects the pinned python version
                assert package_is_installed(prefix, 'python=3.6')

                # pycosat should be in the SUPERSEDED list
                # after the 4.4 solver work, looks like it's in the DOWNGRADED list
                # This language needs changed anyway here.
                # For packages that CHANGE because they're being moved to a higher-priority channel
                # the message should be
                #
                # The following packages will be UPDATED to a higher-priority channel:
                #
                installed_str, x = update_stdout.split('UPDATED')
                assert re.search(
                    r'pkgs/main::pycosat-0.6.2-py36h[^\s]+ --> conda-forge::pycosat',
                    x)

                # python sys.version should show conda-forge python
                python_tuple = get_conda_list_tuple(prefix, "python")
                assert python_tuple[3] == 'conda-forge'
                # conda list should show pycosat coming from conda-forge
                pycosat_tuple = get_conda_list_tuple(prefix, "pycosat")
                assert pycosat_tuple[3] == 'conda-forge'
Пример #16
0
def test_clean_tarballs(clear_cache):
    pkg = "bzip2"

    with make_temp_package_cache() as pkgs_dir:
        # tarball doesn't exist ahead of time
        assert_not_pkg(pkg, _get_tars(pkgs_dir))

        with make_temp_env(pkg):
            # tarball exists
            assert_any_pkg(pkg, _get_tars(pkgs_dir))

            # --json flag is regression test for #5451
            stdout, _, _ = run_command(Commands.CLEAN, "", "--tarballs",
                                       "--yes", "--json")
            json_loads(stdout)  # assert valid json

            # tarball is removed
            assert_not_pkg(pkg, _get_tars(pkgs_dir))

        # tarball is still removed
        assert_not_pkg(pkg, _get_tars(pkgs_dir))
Пример #17
0
    def test_multi_channel_explicit(self):
        """
            When try to import from txt
            every package should come from same channel
        """
        with make_temp_env("python=3.5") as prefix:
            assert exists(join(prefix, PYTHON_BINARY))
            assert package_is_installed(prefix, 'python=3')

            run_command(Commands.INSTALL, prefix, "six", "-c", "conda-forge")
            assert package_is_installed(prefix, "conda-forge::six")

            output, error, _ = run_command(Commands.LIST, prefix, "--explicit")
            assert not error
            assert "conda-forge" in output

            urls1 = set(url for url in output.split()
                        if url.startswith("http"))

            try:
                with Utf8NamedTemporaryFile(mode="w",
                                            suffix="txt",
                                            delete=False) as env_txt:
                    env_txt.write(output)
                    env_txt.close()
                    prefix2 = make_temp_prefix()
                    run_command(Commands.CREATE, prefix2, "--file",
                                env_txt.name)

                    assert package_is_installed(prefix2, "python")
                    assert package_is_installed(prefix2, "six")
                output2, error2, _ = run_command(Commands.LIST, prefix2,
                                                 "--explicit")
                assert not error2
                urls2 = set(url for url in output2.split()
                            if url.startswith("http"))
                assert urls1 == urls2
            finally:
                rm_rf(env_txt.name)
Пример #18
0
def test_clean_tempfiles(clear_cache):
    """Tempfiles are either suffixed with .c~ or .trash.

    .c~ is used to indicate that conda is actively using that file. If the conda process is
    terminated unexpectedly these .c~ files may remain and hence can be cleaned up after the fact.

    .trash appears to be a legacy suffix that is no longer used by conda.

    Since the presence of .c~ and .trash files are dependent upon irregular termination we create
    our own temporary files to confirm they get cleaned up.
    """
    pkg = "bzip2"

    with make_temp_package_cache() as pkgs_dir:
        # tempfiles don't exist ahead of time
        assert not _get_tempfiles(pkgs_dir)

        with make_temp_env(pkg):
            # mimic tempfiles being created
            path = _get_tars(pkgs_dir)[0]  # grab any tarball
            for ext in CONDA_TEMP_EXTENSIONS:
                copy(path, f"{path}{ext}")

            # tempfiles exist
            assert len(_get_tempfiles(pkgs_dir)) == len(CONDA_TEMP_EXTENSIONS)

            # --json flag is regression test for #5451
            stdout, _, _ = run_command(Commands.CLEAN, "", "--tempfiles",
                                       pkgs_dir, "--yes", "--json")
            json_loads(stdout)  # assert valid json

            # tempfiles removed
            assert not _get_tempfiles(pkgs_dir)

        # tempfiles still removed
        assert not _get_tempfiles(pkgs_dir)
Пример #19
0
    def test_channel_priority_update(self):
        """
            This case will fail now
        """
        with make_temp_env("python=3.6.5", "pycosat") as prefix:
            assert package_is_installed(prefix, 'python')

            # add conda-forge channel
            o, e, _ = run_command(Commands.CONFIG, prefix, "--prepend",
                                  "channels", "conda-forge", '--json')
            assert context.channels == ("conda-forge", "defaults"), o + e

            # update python
            update_stdout, _, _ = run_command(Commands.UPDATE, prefix,
                                              'python')

            # pycosat should be in the SUPERSEDED list
            superceded_split = update_stdout.split('UPDATED')
            assert len(superceded_split) == 2
            assert 'conda-forge' in superceded_split[1]

            # python sys.version should show conda-forge python
            python_tuple = get_conda_list_tuple(prefix, "python")
            assert python_tuple[3] == 'conda-forge'