|
49 | 49 | from easybuild.tools.config import DEFAULT_MODULECLASSES |
50 | 50 | from easybuild.tools.config import find_last_log, get_build_log_path, get_module_syntax, module_classes |
51 | 51 | from easybuild.tools.environment import modify_env |
52 | | -from easybuild.tools.filetools import copy_dir, copy_file, download_file, mkdir, read_file, remove_file |
53 | | -from easybuild.tools.filetools import which, write_file |
| 52 | +from easybuild.tools.filetools import change_dir, copy_dir, copy_file, download_file, mkdir, read_file |
| 53 | +from easybuild.tools.filetools import remove_dir, remove_file, which, write_file |
54 | 54 | from easybuild.tools.github import GITHUB_RAW, GITHUB_EB_MAIN, GITHUB_EASYCONFIGS_REPO |
55 | 55 | from easybuild.tools.github import URL_SEPARATOR, fetch_github_token |
56 | 56 | from easybuild.tools.modules import Lmod |
@@ -828,6 +828,85 @@ def test_show_ec(self): |
828 | 828 | regex = re.compile(pattern, re.M) |
829 | 829 | self.assertTrue(regex.search(stdout), "Pattern '%s' found in: %s" % (regex.pattern, stdout)) |
830 | 830 |
|
| 831 | + def test_copy_ec(self): |
| 832 | + """Test --copy-ec.""" |
| 833 | + |
| 834 | + topdir = os.path.dirname(os.path.abspath(__file__)) |
| 835 | + test_easyconfigs_dir = os.path.join(topdir, 'easyconfigs', 'test_ecs') |
| 836 | + |
| 837 | + toy_ec_txt = read_file(os.path.join(test_easyconfigs_dir, 't', 'toy', 'toy-0.0.eb')) |
| 838 | + bzip2_ec_txt = read_file(os.path.join(test_easyconfigs_dir, 'b', 'bzip2', 'bzip2-1.0.6-GCC-4.9.2.eb')) |
| 839 | + |
| 840 | + # basic test: copying one easyconfig file to a non-existing absolute path |
| 841 | + test_ec = os.path.join(self.test_prefix, 'test.eb') |
| 842 | + args = ['--copy-ec', 'toy-0.0.eb', test_ec] |
| 843 | + self.eb_main(args) |
| 844 | + |
| 845 | + self.assertTrue(os.path.exists(test_ec)) |
| 846 | + self.assertEqual(toy_ec_txt, read_file(test_ec)) |
| 847 | + |
| 848 | + remove_file(test_ec) |
| 849 | + |
| 850 | + # basic test: copying one easyconfig file to a non-existing relative path |
| 851 | + cwd = change_dir(self.test_prefix) |
| 852 | + target_fn = 'test.eb' |
| 853 | + self.assertFalse(os.path.exists(target_fn)) |
| 854 | + |
| 855 | + args = ['--copy-ec', 'toy-0.0.eb', target_fn] |
| 856 | + self.eb_main(args) |
| 857 | + |
| 858 | + change_dir(cwd) |
| 859 | + |
| 860 | + self.assertTrue(os.path.exists(test_ec)) |
| 861 | + self.assertEqual(toy_ec_txt, read_file(test_ec)) |
| 862 | + |
| 863 | + # copying one easyconfig into an existing directory |
| 864 | + test_target_dir = os.path.join(self.test_prefix, 'test_target_dir') |
| 865 | + mkdir(test_target_dir) |
| 866 | + args = ['--copy-ec', 'toy-0.0.eb', test_target_dir] |
| 867 | + self.eb_main(args) |
| 868 | + |
| 869 | + copied_toy_ec = os.path.join(test_target_dir, 'toy-0.0.eb') |
| 870 | + self.assertTrue(os.path.exists(copied_toy_ec)) |
| 871 | + self.assertEqual(toy_ec_txt, read_file(copied_toy_ec)) |
| 872 | + |
| 873 | + remove_dir(test_target_dir) |
| 874 | + |
| 875 | + def check_copied_files(): |
| 876 | + """Helper function to check result of copying multiple easyconfigs.""" |
| 877 | + self.assertTrue(os.path.exists(test_target_dir)) |
| 878 | + self.assertEqual(sorted(os.listdir(test_target_dir)), ['bzip2-1.0.6-GCC-4.9.2.eb', 'toy-0.0.eb']) |
| 879 | + copied_toy_ec = os.path.join(test_target_dir, 'toy-0.0.eb') |
| 880 | + self.assertTrue(os.path.exists(copied_toy_ec)) |
| 881 | + self.assertEqual(toy_ec_txt, read_file(copied_toy_ec)) |
| 882 | + copied_bzip2_ec = os.path.join(test_target_dir, 'bzip2-1.0.6-GCC-4.9.2.eb') |
| 883 | + self.assertTrue(os.path.exists(copied_bzip2_ec)) |
| 884 | + self.assertEqual(bzip2_ec_txt, read_file(copied_bzip2_ec)) |
| 885 | + |
| 886 | + # copying multiple easyconfig files to a non-existing target directory (which is created automatically) |
| 887 | + args = ['--copy-ec', 'toy-0.0.eb', 'bzip2-1.0.6-GCC-4.9.2.eb', test_target_dir] |
| 888 | + self.eb_main(args) |
| 889 | + |
| 890 | + check_copied_files() |
| 891 | + |
| 892 | + remove_dir(test_target_dir) |
| 893 | + |
| 894 | + # same but with relative path for target dir |
| 895 | + change_dir(self.test_prefix) |
| 896 | + args[-1] = os.path.basename(test_target_dir) |
| 897 | + self.assertFalse(os.path.exists(args[-1])) |
| 898 | + |
| 899 | + self.eb_main(args) |
| 900 | + |
| 901 | + check_copied_files() |
| 902 | + |
| 903 | + # copying multiple easyconfig to an existing target file resuts in an error |
| 904 | + target = os.path.join(self.test_prefix, 'test.eb') |
| 905 | + self.assertTrue(os.path.isfile(target)) |
| 906 | + args = ['--copy-ec', 'toy-0.0.eb', 'bzip2-1.0.6-GCC-4.9.2.eb', target] |
| 907 | + error_pattern = ".*/test.eb exists but is not a directory" |
| 908 | + self.assertErrorRegex(EasyBuildError, error_pattern, self.eb_main, args, raise_error=True) |
| 909 | + |
831 | 910 | def test_dry_run(self): |
832 | 911 | """Test dry run (long format).""" |
833 | 912 | fd, dummylogfn = tempfile.mkstemp(prefix='easybuild-dummy', suffix='.log') |
|
0 commit comments