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_show_env_no_envvars(): ## uninstalled hg19_gaps() testing uninstall_hg19_gaps_v1() parser = () args = Namespace(command='show-env', pattern=None) env_var_name = "$ggd_hg19_gaps_v1" ## Test a normal run temp_stdout = StringIO() with redirect_stdout(temp_stdout): show_env.show_env(parser,args) output = temp_stdout.getvalue().strip() assert (env_var_name not in output)
def test_show_env_with_pattern(): """ Test that adding the pattern parameter to show-env properly filters the results """ env_var_name = "$ggd_hg19_gaps_v1" parser = () ## Good pattern should have "ggd_hg19_gaps" in the results args = Namespace(command='show-env', pattern="gaps") temp_stdout = StringIO() with redirect_stdout(temp_stdout): show_env.show_env(parser,args) output = temp_stdout.getvalue().strip() assert (env_var_name in output) ## Bad pattern should return "No matching recipe variables found for this environment" args = Namespace(command='show-env', pattern="NONE") temp_stdout = StringIO() with redirect_stdout(temp_stdout): show_env.show_env(parser,args) output = temp_stdout.getvalue().strip() assert (env_var_name not in output) assert ("No matching recipe variables found for this environment" in output) ## invalid pattern should exit args = Namespace(command='show-env', pattern=")()(") with pytest.raises(SystemExit) as pytest_wrapped_e: show_env.show_env(parser,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
def test_show_env_goodrun(): """ Test that show_env functoin properly provides the environment variable for an installed package """ ## Install hg19_gaps() try: install_hg19_gaps_v1() except: pass parser = () args = Namespace(command='show-env', pattern=None) env_var_name = "$ggd_hg19_gaps_v1" ## Test a normal run temp_stdout = StringIO() with redirect_stdout(temp_stdout): show_env.show_env(parser,args) output = temp_stdout.getvalue().strip() assert (env_var_name in output) ## Test active environment variables sp.check_call(["activate", "base"]) temp_stdout = StringIO() with redirect_stdout(temp_stdout): show_env.show_env(parser,args) output = temp_stdout.getvalue().strip() newout = "" active = False for line in output.strip().split("\n"): if "Active environment variables:" in line: active = True if "Inactive or out-of-date environment variables:" in line: active = False if active: newout += line assert (env_var_name in output)
def test_check_for_installation(): """ Test the check_for_installation function to check if the ggd package is installed using the ggd file handling information """ pytest_enable_socket() ## Test a not installed ggd recipe ggd_recipe = "grch37-reference-genome-1000g-v1" ggd_channel = "genomics" jdict = uninstall.get_channeldata(ggd_recipe,ggd_channel) temp_stdout = StringIO() with redirect_stdout(temp_stdout): uninstall.check_for_installation([ggd_recipe],jdict) output = temp_stdout.getvalue().strip() assert "{} is not in the ggd recipe storage".format(ggd_recipe) in output ## Test installed package ggd_recipe = "hg19-gaps-ucsc-v1" ggd_channel = "genomics" jdict = uninstall.get_channeldata(ggd_recipe,ggd_channel) temp_stdout = StringIO() with redirect_stdout(temp_stdout): uninstall.check_for_installation([ggd_recipe],jdict) output = temp_stdout.getvalue().strip() assert "Removing {} version {} file(s) from ggd recipe storage".format(ggd_recipe,jdict["packages"][ggd_recipe]["version"]) in output ## Update local metadata utils.update_installed_pkg_metadata(prefix=conda_root()) args = Namespace(command="show-env", pattern=None) temp_stdout = StringIO() with redirect_stdout(temp_stdout): show_env.show_env((),args) output = temp_stdout.getvalue().strip() assert "$ggd_hg19_gaps_v1_file" not in output assert "$ggd_hg19_gaps_v1_dir" not in output
def test_check_for_installation_different_prefix(): """ Test that check_for_installation when a prefix is provided """ ## Test installed package ggd_recipe = "hg19-pfam-domains-ucsc-v1" ggd_channel = "genomics" ### Uninstall ggd recipe uninstall_args = Namespace(channel='genomics', command='uninstall', name=ggd_recipe) try: uninstall.uninstall((),uninstall_args) except: pass ### Install ggd recipe install_args = Namespace(channel='genomics', command='install', debug=False, name=[ggd_recipe], file=[], prefix = conda_root()) try: install.install((), install_args) except: pass ## jdict and info jdict = uninstall.get_channeldata(ggd_recipe,ggd_channel) species = jdict["packages"][ggd_recipe]["identifiers"]["species"] build = jdict["packages"][ggd_recipe]["identifiers"]["genome-build"] version = jdict["packages"][ggd_recipe]["version"] ## Test the package in "conda_root" exists args = Namespace(command='list', pattern=None, prefix=conda_root()) temp_stdout = StringIO() with redirect_stdout(temp_stdout): list_installed_pkgs.list_installed_packages((), args) output = temp_stdout.getvalue().strip() assert ggd_recipe in output path = os.path.join(conda_root(),"share","ggd",species,build,ggd_recipe,version,"*") files = glob.glob(path) assert len(files) == 2 ## Create temp envi temp_env = os.path.join(utils.conda_root(), "envs", "temp_env10") ### Remove temp env if it already exists sp.check_output(["conda", "env", "remove", "--name", "temp_env10"]) try: shutil.rmtree(temp_env) except Exception: pass ### Create conda environmnet sp.check_output(["conda", "create", "--name", "temp_env10"]) ## Test prefix ### Install ggd recipe using conda into temp_env install_args = Namespace(channel='genomics', command='install', debug=False, name=[ggd_recipe],file=[], prefix = temp_env) assert install.install((), install_args) == True ## Test that the files are removed temp_stdout = StringIO() with redirect_stdout(temp_stdout): uninstall.check_for_installation([ggd_recipe],jdict,prefix=temp_env) output = temp_stdout.getvalue().strip() assert "Removing {} version {} file(s) from ggd recipe storage".format(ggd_recipe,jdict["packages"][ggd_recipe]["version"]) in output ## Update local metadata utils.update_installed_pkg_metadata(prefix=temp_env) ## Test the package was removed from the ggd info list args = Namespace(command='list', pattern=None, prefix=temp_env) temp_stdout = StringIO() with redirect_stdout(temp_stdout): list_installed_pkgs.list_installed_packages((), args) output = temp_stdout.getvalue().strip() assert ggd_recipe not in output path = os.path.join(temp_env,"share","ggd",species,build,ggd_recipe,version,"*") files = glob.glob(path) assert len(files) == 0 ## Test the package in "conda_root" was not removed args = Namespace(command='list', pattern=None, prefix=conda_root()) temp_stdout = StringIO() with redirect_stdout(temp_stdout): list_installed_pkgs.list_installed_packages((), args) output = temp_stdout.getvalue().strip() assert ggd_recipe in output path = os.path.join(conda_root(),"share","ggd",species,build,ggd_recipe,version,"*") files = glob.glob(path) assert len(files) == 2 ## Remove prefix sp.check_output(["conda", "env", "remove", "--name", "temp_env10"]) try: shutil.rmtree(temp_env) except Exception: pass assert os.path.exists(temp_env) == False ## Test the current environment (conda_root) temp_stdout = StringIO() with redirect_stdout(temp_stdout): uninstall.check_for_installation([ggd_recipe],jdict,prefix=utils.conda_root()) output = temp_stdout.getvalue().strip() assert "Removing {} version {} file(s) from ggd recipe storage".format(ggd_recipe,jdict["packages"][ggd_recipe]["version"]) in output ## Update local metadata utils.update_installed_pkg_metadata(prefix=conda_root()) ## Test environment variables removed args = Namespace(command="show-env", pattern=None) temp_stdout = StringIO() with redirect_stdout(temp_stdout): show_env.show_env((),args) output = temp_stdout.getvalue().strip() assert "hg19_pfam_domains_ucsc_v1_file" not in output assert "hg19_pfam_domains_ucsc_v1_dir" not in output args = Namespace(command='list', pattern=None, prefix=conda_root()) temp_stdout = StringIO() with redirect_stdout(temp_stdout): list_installed_pkgs.list_installed_packages((), args) output = temp_stdout.getvalue().strip() assert ggd_recipe not in output path = os.path.join(conda_root(),"share","ggd",species,build,ggd_recipe,version,"*") files = glob.glob(path) assert len(files) == 0