def test_get_pkg_info_internet_free():
    """
    Test that get_pkg_info in pkg-info in a internet free context
        (pkg-info)
    """

    ## Install hg19-gaps
    try:
        pytest_enable_socket()
        install_hg19_gaps_ucsc_v1()
    except:
        pass

    ## Check show-env in an internet free context
    pytest_disable_socket()
    ### Check that there is no interent
    assert utils.check_for_internet_connection() == False

    ## Test a normal run that should pass
    ggd_package = "hg19-gaps-ucsc-v1"
    ggd_channel = "genomics"
    assert list_pkg_info.get_pkg_info(ggd_package, ggd_channel, False) == True

    ## Test that a uninstalled package is handled correctly
    ggd_package = "Bad_package"
    ggd_channel = "genomics"

    temp_stdout = StringIO()
    with redirect_stdout(temp_stdout):
        list_pkg_info.get_pkg_info(ggd_package, ggd_channel, False)
    output = temp_stdout.getvalue().strip()
    assert ggd_package + " is not downloaded on your system, or was downloaded incorrectly" in output
    assert list_pkg_info.get_pkg_info(ggd_package, ggd_channel, False) == False
Beispiel #2
0
def test_get_similar_pkg_installed_by_conda():
    """
    Test the get_similar_pkg_installed_by_conda function 
    """
    pytest_enable_socket()

    ## Test a non similar package name
    not_installed = "Not_an_installed_package"
    assert uninstall.get_similar_pkg_installed_by_conda(not_installed) == ""

    
    installed_requirement = "request"
    output = uninstall.get_similar_pkg_installed_by_conda(installed_requirement)
    assert "requests" in output
    assert "pyyaml" not in output

    installed_requirement = "yaml"
    output = uninstall.get_similar_pkg_installed_by_conda(installed_requirement)
    assert "pyyaml" in output
    assert "oyaml" in output
    assert "requests" not in output
    
    ## Make sure hg19-gaps-v1 is installed
    ggd_recipe = "hg19-gaps-ucsc-v1"
    list_files_args = Namespace(channel='genomics', command='list-files', genome_build=None, name=ggd_recipe, pattern=None, prefix=None, species=None, version=None)
    try:
        list_files.list_files((),list_files_args)
    except SystemExit as e:
        if str(e) == "1": ## If exit code is 1, implying that there were not files found
            install_hg19_gaps_ucsc_v1()

    ggd_recipe = "hg19-gaps"
    output = uninstall.get_similar_pkg_installed_by_conda(ggd_recipe)
    assert "hg19-gaps-ucsc-v1" in output
def test_show_env_internet_free():
    """
    test the show-env ggd call can run an internet free context
        (show-env)
    """

    ## Install hg19-gaps
    try:
        pytest_enable_socket()
        install_hg19_gaps_ucsc_v1()
    except:
        pass

    ## Check show-env in an internet free context
    pytest_disable_socket()
    ### Check that there is no interent
    assert utils.check_for_internet_connection() == False

    args = Namespace(command='show-env', pattern=None)
    dir_env_var_name = "$ggd_hg19_gaps_ucsc_v1_dir"
    file_env_var_name = "$ggd_hg19_gaps_ucsc_v1_file"

    ## Test show_env
    temp_stdout = StringIO()
    with redirect_stdout(temp_stdout):
        show_env.show_env((), args)
    output = temp_stdout.getvalue().strip()
    assert (dir_env_var_name in output)
    assert (file_env_var_name in output)
def test_uninstall_internet_free():
    """
    Test the uninstall method in an internet free context 
        (uninstall)
    """

    ## Install hg19-gaps
    try:
        pytest_enable_socket()
        install_hg19_gaps_ucsc_v1()
    except:
        pass

    ## Check show-env in an internet free context
    pytest_disable_socket()
    ### Check that there is no interent
    assert utils.check_for_internet_connection() == False

    ## Check non-failure uninstall command
    ggd_recipe = "hg19-gaps-ucsc-v1"
    args = Namespace(channel='genomics',
                     command='uninstall',
                     names=[ggd_recipe])
    assert uninstall.uninstall((), args) == True

    #### Get jdict
    ggd_channel = "genomics"
    jdict = uninstall.get_channeldata([ggd_recipe], ggd_channel)
    conda_root = utils.conda_root()

    ### Check that the files are not in the conda root
    species = jdict["packages"][ggd_recipe]["identifiers"]["species"]
    build = jdict["packages"][ggd_recipe]["identifiers"]["genome-build"]
    version = jdict["packages"][ggd_recipe]["version"]
    path = os.path.join(conda_root, "share", "ggd", species, build, ggd_recipe,
                        version)
    check_list = sp.check_output([
        "find", conda_root, "-name", ggd_recipe + "-" + str(version) + "*"
    ]).decode("utf8").strip().split("\n")
    for f in check_list:
        if conda_root in f:
            if conda_root + "/envs/" not in path:
                assert ggd_recipe + "-" + str(version) not in path

    #### Check data files were removed
    assert glob.glob(path) == []
    try:
        os.listdir(path)
        assert False
    except OSError as e:
        if "No such file or directory" in str(e):
            pass
        else:
            assert False

    #### Check that the ggd package is no longer in the list of conda packages
    output = sp.check_output(["conda", "list", ggd_recipe])
    assert ggd_recipe not in str(output)
Beispiel #5
0
def test_get_channeldata():
    """
    Test the get_channeldata method properly finds a good ggd package and handles a bad ggd package 
    """
    pytest_enable_socket()

    ## Uninstall hg19-gaps-v1
    try:
        uninstall_hg19_gaps_ucsc_v1()
    except:
        pass

    ## Install hg19-gaps-v1
    try:
        install_hg19_gaps_ucsc_v1()
    except:
        pass
    
    ## Test normal run
    ggd_recipe = "hg19-gaps-ucsc-v1"
    ggd_channel = "genomics"
    jdict = uninstall.get_channeldata([ggd_recipe],ggd_channel)
    assert ggd_recipe in jdict["packages"].keys()

    ## Similar installed package
    ggd_recipe = "hg19-gaps"
    ggd_channel = "genomics"
    temp_stdout = StringIO()
    with redirect_stdout(temp_stdout):
        uninstall.get_channeldata([ggd_recipe],ggd_channel)
    output = temp_stdout.getvalue().strip() 
    assert "Packages installed on your system that are similar include:" in output
    assert "hg19-gaps-ucsc-v1" in output
    assert uninstall.get_channeldata([ggd_recipe],ggd_channel) == False

    ## Test bad recipe 
    bad_recipe = "BadRecipe"
    ggd_channel = "genomics"
    temp_stdout = StringIO()
    with redirect_stdout(temp_stdout):
        uninstall.get_channeldata([bad_recipe],ggd_channel)
    output = temp_stdout.getvalue().strip() 
    print("OUTPUT: '''", output, "''''")
    assert "{} is not in the ggd-{} channel".format(bad_recipe,ggd_channel)
    assert "Unable to find any package similar to the package entered. Use 'ggd search' or 'conda find' to identify the right package" in output
    assert "This package may not be installed on your system" in output
    assert uninstall.get_channeldata([bad_recipe],ggd_channel) == False

    ## Test bad channel 
    ggd_recipe = "hg19-gaps-ucsc-v1"
    bad_channel = "BadChannel"
    temp_stdout = StringIO()
    with redirect_stdout(temp_stdout):
        uninstall.get_channeldata([ggd_recipe],bad_channel) 
    output = temp_stdout.getvalue().strip() 
    assert "The 'BadChannel' channel is not a ggd conda channel" in output
Beispiel #6
0
def test_get_channeldata():
    """
    Test the get_channeldata method properly finds a good ggd package and handles a bad ggd package 
    """
    pytest_enable_socket()

    ## Uninstall hg19-gaps-v1
    try:
        uninstall_hg19_gaps_ucsc_v1()
    except:
        pass

    ## Install hg19-gaps-v1
    try:
        install_hg19_gaps_ucsc_v1()
    except:
        pass
    
    ## Test normal run
    ggd_recipe = "hg19-gaps-ucsc-v1"
    ggd_channel = "genomics"
    jdict = uninstall.get_channeldata(ggd_recipe,ggd_channel)
    assert ggd_recipe in jdict["packages"].keys()

    ## Similar installed package
    ggd_recipe = "hg19-gaps"
    ggd_channel = "genomics"
    temp_stdout = StringIO()
    with redirect_stdout(temp_stdout):
        uninstall.get_channeldata(ggd_recipe,ggd_channel)
    output = temp_stdout.getvalue().strip() 
    assert "Packages installed on your system that are similar include:" in output
    assert "hg19-gaps-ucsc-v1" in output
    assert uninstall.get_channeldata(ggd_recipe,ggd_channel) == False

    ## Test bad recipe 
    bad_recipe = "BadRecipe"
    ggd_channel = "genomics"
    with redirect_stdout(temp_stdout):
        uninstall.get_channeldata(bad_recipe,ggd_channel)
    output = temp_stdout.getvalue().strip() 
    assert "Packages installed on your system that are similar include:" in output
    assert "{} is not in the ggd-{} channel".format(bad_recipe,ggd_channel)
    assert "Unable to find any package similar to the package entered. Use 'ggd search' or 'conda find' to identify the right package" in output
    assert "This package may not be installed on your system" in output
    assert uninstall.get_channeldata(bad_recipe,ggd_channel) == False

    ## Test bad channel 
    ggd_recipe = "hg19-gaps-ucsc-v1"
    bad_channel = "BadChannel"
    with pytest.raises(SystemExit) as pytest_wrapped_e:
        uninstall.get_channeldata(ggd_recipe,bad_channel) ## Exit due to bad url from ggd.search
    assert "SystemExit" in str(pytest_wrapped_e.exconly()) ## test that SystemExit was raised by sys.exit() 
    assert pytest_wrapped_e.match("The 'BadChannel' channel is not a ggd conda channel") ## Check that the exit code is 1
def test_info_main_internet_free():
    """
    test the pkg-info main funtion in an internet free context
        (pkg-info)
    """

    ## Install hg19-gaps
    try:
        pytest_enable_socket()
        install_hg19_gaps_ucsc_v1()
    except:
        pass

    ## Check show-env in an internet free context
    pytest_disable_socket()
    ### Check that there is no interent
    assert utils.check_for_internet_connection() == False

    ## Normal run
    ggd_package = "hg19-gaps-ucsc-v1"
    ggd_channel = "genomics"
    args = Namespace(all_versions=False,
                     channel=ggd_channel,
                     command='pkg-info',
                     name=ggd_package,
                     show_recipe=False,
                     prefix=None)
    assert list_pkg_info.info((), args) == True

    temp_stdout = StringIO()
    with redirect_stdout(temp_stdout):
        list_pkg_info.info((), args)
    output = temp_stdout.getvalue().strip()
    lines = output.strip().split("\n")
    assert lines[2] == "\t\x1b[1mGGD-Package:\x1b[0m {}".format(ggd_package)
    assert lines[4] == "\t\x1b[1mGGD-Channel:\x1b[0m ggd-{}".format(
        ggd_channel)
    assert lines[6] == "\t\x1b[1mGGD Pkg Version:\x1b[0m 1"
    assert lines[
        8] == "\t\x1b[1mSummary:\x1b[0m Assembly gaps from UCSC in bed fromat. Scaffoldings that are not contained in the hg19.genome file are removed"
    assert lines[10] == "\t\x1b[1mSpecies:\x1b[0m Homo_sapiens"
    assert lines[12] == "\t\x1b[1mGenome Build:\x1b[0m hg19"
    assert lines[
        14] == "\t\x1b[1mKeywords:\x1b[0m gaps, regions, gap-locations, Assembly-Gaps, clone-gaps, contig-gaps, centromere-gaps, telomere-gaps, heterochromatin-gaps, short-arm-gaps"
    assert lines[16] == "\t\x1b[1mCached:\x1b[0m uploaded_to_aws"
def test_in_ggd_channel_internet_free():
    """
    Test the in_ggd_channel function in list-files in an internet free context
        (list-files)
    """

    ## Install hg19-gaps
    try:
        pytest_enable_socket()
        install_hg19_gaps_ucsc_v1()
    except:
        pass

    ## Check show-env in an internet free context
    pytest_disable_socket()
    ### Check that there is no interent
    assert utils.check_for_internet_connection() == False

    ## Test that in_ggd_channel properly returns the species, genome-build, and versoin if it is in the channel
    ggd_package = "hg19-gaps-ucsc-v1"
    channel = "genomics"
    species, build, version = list_files.in_ggd_channel([ggd_package], channel,
                                                        utils.conda_root())
    assert species == "Homo_sapiens"
    assert build == "hg19"
    assert version == "1"

    ## test that in_ggd_channel properly handels bad channels
    ggd_package = "hg19-gaps-ucsc-v1"
    channel = "not_a_real_channel"
    with pytest.raises(SystemExit) as pytest_wrapped_e:
        list_files.in_ggd_channel([ggd_package], channel, utils.conda_root())
    assert "SystemExit" in str(pytest_wrapped_e.exconly(
    ))  ## test that systemexit was raised by sys.exit()
    assert pytest_wrapped_e.match("2")  ## check that the exit code is 1

    ## test that in_ggd_channel properly handels bad recipe name
    ggd_package = "NOT_A_REAL_PACKAGE_NAME"
    channel = "genomics"
    with pytest.raises(SystemExit) as pytest_wrapped_e:
        list_files.in_ggd_channel([ggd_package], channel, utils.conda_root())
    assert "SystemExit" in str(pytest_wrapped_e.exconly(
    ))  ## test that systemexit was raised by sys.exit()
    assert pytest_wrapped_e.match("2")  ## check that the exit code is 1
def test_list_installed_packages_internet_free():
    """
    Test the main function of ggd list in an internet free context
    """

    ## Install hg19-gaps
    try:
        pytest_enable_socket()
        install_hg19_gaps_ucsc_v1()
    except:
        pass

    ## Check show-env in an internet free context
    pytest_disable_socket()
    ### Check that there is no interent
    assert utils.check_for_internet_connection() == False

    args = Namespace(command='list', pattern=None, prefix=None, reset=False)
    temp_stdout = StringIO()
    with redirect_stdout(temp_stdout):
        list_installed_pkgs.list_installed_packages((), args)
    output = temp_stdout.getvalue().strip()
    assert "hg19-gaps-ucsc-v1" in output
    assert "Name" in output and "Pkg-Version" in output and "Pkg-Build" in output and "Channel" in output and "Environment-Variables" in output
    assert "To use the environment variables run `source activate base" in output
    assert "You can see the available ggd data package environment variables by running `ggd show-env" in output

    args = Namespace(command='list',
                     pattern=None,
                     prefix=utils.conda_root(),
                     reset=False)
    temp_stdout = StringIO()
    with redirect_stdout(temp_stdout):
        list_installed_pkgs.list_installed_packages((), args)
    output = temp_stdout.getvalue().strip()
    assert "hg19-gaps-ucsc-v1" in output
    assert "Name" in output and "Pkg-Version" in output and "Pkg-Build" in output and "Channel" in output and "Environment-Variables" in output
    assert "The environment variables are only available when you are using the '{}' conda environment".format(
        utils.conda_root()) in output
Beispiel #10
0
def test_get_channeldata_internet_free():
    """
    Test the get_channeldata function in uninstall in an internet free context
        (uninstall)
    """

    ## Install hg19-gaps
    try:
        pytest_enable_socket()
        install_hg19_gaps_ucsc_v1()
    except:
        pass

    ## Check show-env in an internet free context
    pytest_disable_socket()
    ### Check that there is no interent
    assert utils.check_for_internet_connection() == False

    ## Test normal run
    ggd_recipe = "hg19-gaps-ucsc-v1"
    ggd_channel = "genomics"
    jdict = uninstall.get_channeldata([ggd_recipe], ggd_channel)
    assert ggd_recipe in jdict["packages"].keys()
Beispiel #11
0
def test_uninstall():
    """
    Test that a package is properly uninstalled using the main uninstall funciton
    """
    pytest_enable_socket()

    ## Test handling of a package not installed
    ggd_recipe = "not-a-real-recipe"
    args = Namespace(channel='genomics', command='uninstall', name=ggd_recipe)
    with pytest.raises(SystemExit) as pytest_wrapped_e:
        uninstall.uninstall((),args)
    assert "SystemExit" in str(pytest_wrapped_e.exconly()) ## test that SystemExit was raised by sys.exit() 
    assert pytest_wrapped_e.match("") ## Check that the exit code is ""


    ## Test a good uninstall
    #### Install hg19-gaps-ucsc-v1
    try:
        install_hg19_gaps_ucsc_v1()
    except:
        pass

    #### Check non-failure uninstall command
    ggd_recipe = "hg19-gaps-ucsc-v1"

    #### Get conda list pkg info
    pkg_info = get_conda_package_list(utils.conda_root(),ggd_recipe)
    assert ggd_recipe in pkg_info.keys()

    #### Test uninstall 
    args = Namespace(channel='genomics', command='uninstall', name=ggd_recipe)
    assert uninstall.uninstall((),args) == True

    #### Get jdict 
    ggd_channel = "genomics"
    jdict = uninstall.get_channeldata(ggd_recipe,ggd_channel)
    conda_root = utils.conda_root()

    ### Check that the files are not in the conda root
    version = jdict["packages"][ggd_recipe]["version"]
    check_list = sp.check_output(["find", conda_root, "-name", ggd_recipe+"-"+str(version)+"*"]).decode("utf8").strip().split("\n")
    for f in check_list:
        if conda_root in f:
            if conda_root+"/envs/" not in path:
                assert ggd_recipe+"-"+str(version) not in path 

    #### Check data files were removed
    species = jdict["packages"][ggd_recipe]["identifiers"]["species"]
    build = jdict["packages"][ggd_recipe]["identifiers"]["genome-build"]
    version = jdict["packages"][ggd_recipe]["version"]
    path = os.path.join(conda_root,"share","ggd",species,build,ggd_recipe,version)
    assert glob.glob(path) == []
    try:
        os.listdir(path) 
        assert False
    except OSError as e:
        if "No such file or directory" in str(e):
            pass
        else:
            assert False

    #### Check that the ggd package is no longer in the list of conda packages
    output = sp.check_output(["conda", "list", ggd_recipe])
    assert ggd_recipe not in str(output)

    ### Test that the ggd_info metadata is updated with ggd pkg
    version = pkg_info[ggd_recipe]["version"]
    build = pkg_info[ggd_recipe]["build"]
    assert os.path.exists(os.path.join(utils.conda_root(),"share","ggd_info","noarch"))
    assert os.path.exists(os.path.join(utils.conda_root(),"share","ggd_info","noarch",ggd_recipe+"-{}-{}.tar.bz2".format(version,build))) == False
    assert os.path.exists(os.path.join(utils.conda_root(),"share","ggd_info","channeldata.json"))
    with open(os.path.join(utils.conda_root(),"share","ggd_info","channeldata.json")) as jfile:
        channeldata = json.load(jfile)
        assert ggd_recipe not in channeldata["packages"]
Beispiel #12
0
def test_list_files_internet_free():
    """
    test the list_files function in list-files in an internet free context
        (list-files)
    """

    ## Install hg19-gaps
    try:
        pytest_enable_socket()
        install_hg19_gaps_ucsc_v1()
    except:
        pass

    ## Check show-env in an internet free context
    pytest_disable_socket()
    ### Check that there is no interent
    assert utils.check_for_internet_connection() == False

    ## Test list-files
    ggd_package = "hg19-gaps-ucsc-v1"
    file1 = "{}.bed.gz".format(ggd_package)
    file2 = "{}.bed.gz.tbi".format(ggd_package)

    ##Test that the correct file paths are returned
    args = Namespace(channel='genomics',
                     command='list-files',
                     genome_build=None,
                     name=ggd_package,
                     pattern=None,
                     prefix=None,
                     species=None,
                     version=None)

    temp_stdout = StringIO()
    with redirect_stdout(temp_stdout):
        list_files.list_files((), args)
    output = str(temp_stdout.getvalue().strip())
    assert re.search(file1 + "$", sorted(output.split("\n"))[0])
    assert re.search(file2 + "$", sorted(output.split("\n"))[1])
    assert len(output.split("\n")) == 2

    ##Test that the correct file paths are returned with the genome_build key set
    args = Namespace(channel='genomics',
                     command='list-files',
                     genome_build="hg19",
                     name=ggd_package,
                     pattern=None,
                     prefix=None,
                     species=None,
                     version=None)

    temp_stdout = StringIO()
    with redirect_stdout(temp_stdout):
        list_files.list_files((), args)
    output = str(temp_stdout.getvalue().strip())
    assert re.search(file1 + "$", sorted(output.split("\n"))[0])
    assert re.search(file2 + "$", sorted(output.split("\n"))[1])
    assert len(output.split("\n")) == 2

    ##Test that the correct file paths are returned with the species key set
    args = Namespace(channel='genomics',
                     command='list-files',
                     genome_build=None,
                     name=ggd_package,
                     pattern=None,
                     prefix=None,
                     species="Homo_sapiens",
                     version=None)

    temp_stdout = StringIO()
    with redirect_stdout(temp_stdout):
        list_files.list_files((), args)
    output = str(temp_stdout.getvalue().strip())
    assert re.search(file1 + "$", sorted(output.split("\n"))[0])
    assert re.search(file2 + "$", sorted(output.split("\n"))[1])
    assert len(output.split("\n")) == 2

    ##Test that the correct file paths are returned with version  key set
    args = Namespace(channel='genomics',
                     command='list-files',
                     genome_build=None,
                     name=ggd_package,
                     pattern=None,
                     prefix=None,
                     species=None,
                     version="1")

    temp_stdout = StringIO()
    with redirect_stdout(temp_stdout):
        list_files.list_files((), args)
    output = str(temp_stdout.getvalue().strip())
    assert re.search(file1 + "$", sorted(output.split("\n"))[0])
    assert re.search(file2 + "$", sorted(output.split("\n"))[1])
    assert len(output.split("\n")) == 2

    ## Test that nothing is returned when a bad ggd package name is given
    args = Namespace(channel='genomics',
                     command='list-files',
                     genome_build=None,
                     name="NOT_a_real_package_name",
                     pattern=None,
                     prefix=None,
                     species=None,
                     version=None)

    with pytest.raises(SystemExit) as pytest_wrapped_e:
        list_files.list_files((), args)
    assert "SystemExit" in str(pytest_wrapped_e.exconly(
    ))  ## test that systemexit was raised by sys.exit()
    assert pytest_wrapped_e.match("2")  ## check that the exit code is 1

    ##Test that the function exits if a bad genome build is given
    args = Namespace(channel='genomics',
                     command='list-files',
                     genome_build="Bad_Build",
                     name=ggd_package,
                     pattern=None,
                     species=None,
                     prefix=None,
                     version=None)

    with pytest.raises(SystemExit) as pytest_wrapped_e:
        list_files.list_files((), args)
    assert "SystemExit" in str(pytest_wrapped_e.exconly(
    ))  ## test that systemexit was raised by sys.exit()
    assert pytest_wrapped_e.match("3")  ## check that the exit code is 1

    ##Test that the function exits if a bad species is given
    args = Namespace(channel='genomics',
                     command='list-files',
                     genome_build=None,
                     name=ggd_package,
                     pattern=None,
                     prefix=None,
                     species="Mus_musculus",
                     version=None)

    with pytest.raises(SystemExit) as pytest_wrapped_e:
        list_files.list_files((), args)
    assert "SystemExit" in str(pytest_wrapped_e.exconly(
    ))  ## test that systemexit was raised by sys.exit()
    assert pytest_wrapped_e.match("3")  ## check that the exit code is 1

    ##Test that the function exits if a bad version is given
    args = Namespace(channel='genomics',
                     command='list-files',
                     genome_build=None,
                     name=ggd_package,
                     pattern=None,
                     prefix=None,
                     species=None,
                     version="99999")

    with pytest.raises(SystemExit) as pytest_wrapped_e:
        list_files.list_files((), args)
    assert "SystemExit" in str(pytest_wrapped_e.exconly(
    ))  ## test that systemexit was raised by sys.exit()
    assert pytest_wrapped_e.match("1")  ## check that the exit code is 1

    ##Test that the function exits if a bad pattern is given
    args = Namespace(channel='genomics',
                     command='list-files',
                     genome_build=None,
                     name=ggd_package,
                     pattern="BAD_PATTERN",
                     prefix=None,
                     species=None,
                     version=None)

    with pytest.raises(SystemExit) as pytest_wrapped_e:
        list_files.list_files((), args)
    assert "SystemExit" in str(pytest_wrapped_e.exconly(
    ))  ## test that systemexit was raised by sys.exit()
    assert pytest_wrapped_e.match("1")  ## check that the exit code is 1

    ##Test that the function exits if a bad channel is given
    args = Namespace(channel='bad-channel',
                     command='list-files',
                     genome_build=None,
                     name=ggd_package,
                     pattern=None,
                     prefix=None,
                     species=None,
                     version=None)

    with pytest.raises(SystemExit) as pytest_wrapped_e:
        list_files.list_files((), args)
    assert "SystemExit" in str(pytest_wrapped_e.exconly(
    ))  ## test that systemexit was raised by sys.exit()
    assert pytest_wrapped_e.match("2")  ## check that the exit code is 1
Beispiel #13
0
def test_update_installed_pkg_metadata():
    """
    Test that the update_installed_pkg_metadata method correctly updates the ggd info metadata with installed packages
    """

    ## enable socket
    pytest_enable_socket()

    ggd_package = "hg19-gaps-ucsc-v1"

    ## Try to install ggd_package. If fails, most likely due to the fact that it is already installed
    try:
        install_hg19_gaps_ucsc_v1()
    except Exception:
        pass

    ggd_info_dir = os.path.join(utils.conda_root(),"share","ggd_info")

    ## Test normal run
    if os.path.isdir(ggd_info_dir):
        shutil.rmtree(ggd_info_dir)
    assert os.path.exists(ggd_info_dir) == False
    
    assert utils.update_installed_pkg_metadata() == True
    matches = [re.search(ggd_package+".+",x).group() for x in os.listdir(os.path.join(ggd_info_dir,"noarch")) if re.search(ggd_package,x) != None]
    assert len(matches) > 0
    with open(os.path.join(ggd_info_dir,"channeldata.json")) as jsonFile:
        jdict = json.load(jsonFile)
        assert ggd_package in jdict["packages"]
    assert os.path.exists(ggd_info_dir) == True


    ## Test add_package != None and remove_old == True
    ### add_package != None and remove_old == True should never happen. This would result in only 1 package in the metadata
    temp_stdout = StringIO()
    with redirect_stdout(temp_stdout):
        utils.update_installed_pkg_metadata(add_packages=["hg19-gaps-ucsc-v1"],remove_old=True)
    output = temp_stdout.getvalue().strip() 
    assert ("Warning: You indicated to add a single package to ggd info metadata but also indicated to re-build the metadata. This would result in the single indicated package being the only package in the metadata" in output)
    assert ("The ggd info metadata will be re-built and all ggd packages will be added" in output)


    ## Test prefix not set:
    ### Temp conda environment 
    temp_env = os.path.join(utils.conda_root(), "envs", "temp_env9")
    ### Remove temp env if it already exists
    sp.check_output(["conda", "env", "remove", "--name", "temp_env9"])
    try: 
        shutil.rmtree(temp_env)
    except Exception:
        pass 
    ### Create conda environmnet 
    sp.check_output(["conda", "create", "--name", "temp_env9"])

    ### Install ggd recipe using conda into temp_env
    ggd_package2 = "hg19-pfam-domains-ucsc-v1"
    install_args = Namespace(channel='genomics', command='install', debug=False, name=[ggd_package2], file=[], prefix = temp_env)
    assert install.install((), install_args) == True 

    ggd_info_dir2 = os.path.join(temp_env,"share","ggd_info")

    ## Test the installed created the ggd info metadata 
    matches = [re.search(ggd_package2+".+",x).group() for x in os.listdir(os.path.join(ggd_info_dir2,"noarch")) if re.search(ggd_package2,x) != None]
    assert len(matches) > 0
    with open(os.path.join(ggd_info_dir2,"channeldata.json")) as jsonFile:
        jdict = json.load(jsonFile)
        assert ggd_package2 in jdict["packages"]
    assert os.path.exists(ggd_info_dir2) == True

    ## remove ggd info metadata
    if os.path.isdir(ggd_info_dir2):
        shutil.rmtree(ggd_info_dir2)
    assert os.path.exists(ggd_info_dir2) == False

    ## Run the update with prefix set and check the prefix
    assert utils.update_installed_pkg_metadata(prefix=temp_env) == True
    matches = [re.search(ggd_package2+".+",x).group() for x in os.listdir(os.path.join(ggd_info_dir2,"noarch")) if re.search(ggd_package2,x) != None]
    assert len(matches) > 0
    with open(os.path.join(ggd_info_dir2,"channeldata.json")) as jsonFile:
        jdict = json.load(jsonFile)
        assert ggd_package2 in jdict["packages"]
    assert os.path.exists(ggd_info_dir2) == True

    ### Remove temp env
    sp.check_output(["conda", "env", "remove", "--name", "temp_env9"])
    try:
        shutil.rmtree(temp_env)
    except Exception:
        pass
    assert os.path.exists(temp_env) == False