Skip to content

Commit 9ebf77e

Browse files
committed
fix use of --copy-ec with a single argument, assume copy to current working directory (fixes #3224)
1 parent 44b804f commit 9ebf77e

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

easybuild/main.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,12 @@ def main(args=None, logfile=None, do_build=None, testing=False, modtool=None):
291291
eb_file = find_easybuild_easyconfig()
292292
orig_paths.append(eb_file)
293293

294-
# last path is target when --copy-ec is used, so remove that from the list
295-
target_path = orig_paths.pop() if options.copy_ec else None
294+
if len(orig_paths) == 1:
295+
# if only one easyconfig file is specified, use current directory as target directory
296+
target_path = os.getcwd()
297+
elif orig_paths:
298+
# last path is target when --copy-ec is used, so remove that from the list
299+
target_path = orig_paths.pop() if options.copy_ec else None
296300

297301
categorized_paths = categorize_files_by_type(orig_paths)
298302

@@ -310,8 +314,12 @@ def main(args=None, logfile=None, do_build=None, testing=False, modtool=None):
310314
if options.copy_ec:
311315
if len(determined_paths) == 1:
312316
copy_file(determined_paths[0], target_path)
313-
else:
317+
print_msg("%s copied to %s" % (os.path.basename(determined_paths[0]), target_path), prefix=False)
318+
elif len(determined_paths) > 1:
314319
copy_files(determined_paths, target_path)
320+
print_msg("%d file(s) copied to %s" % (len(determined_paths), target_path), prefix=False)
321+
else:
322+
raise EasyBuildError("One of more files to copy should be specified!")
315323

316324
elif options.fix_deprecated_easyconfigs:
317325
fix_deprecated_easyconfigs(determined_paths)

test/framework/options.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,16 @@ def test_show_ec(self):
836836
def test_copy_ec(self):
837837
"""Test --copy-ec."""
838838

839+
def mocked_main(args):
840+
self.mock_stderr(True)
841+
self.mock_stdout(True)
842+
self.eb_main(args, raise_error=True)
843+
stderr, stdout = self.get_stderr(), self.get_stdout()
844+
self.mock_stderr(False)
845+
self.mock_stdout(False)
846+
self.assertEqual(stderr, '')
847+
return stdout.strip()
848+
839849
topdir = os.path.dirname(os.path.abspath(__file__))
840850
test_easyconfigs_dir = os.path.join(topdir, 'easyconfigs', 'test_ecs')
841851

@@ -845,7 +855,8 @@ def test_copy_ec(self):
845855
# basic test: copying one easyconfig file to a non-existing absolute path
846856
test_ec = os.path.join(self.test_prefix, 'test.eb')
847857
args = ['--copy-ec', 'toy-0.0.eb', test_ec]
848-
self.eb_main(args)
858+
stdout = mocked_main(args)
859+
self.assertEqual(stdout, 'toy-0.0.eb copied to %s' % test_ec)
849860

850861
self.assertTrue(os.path.exists(test_ec))
851862
self.assertEqual(toy_ec_txt, read_file(test_ec))
@@ -858,7 +869,8 @@ def test_copy_ec(self):
858869
self.assertFalse(os.path.exists(target_fn))
859870

860871
args = ['--copy-ec', 'toy-0.0.eb', target_fn]
861-
self.eb_main(args)
872+
stdout = mocked_main(args)
873+
self.assertEqual(stdout, 'toy-0.0.eb copied to test.eb')
862874

863875
change_dir(cwd)
864876

@@ -869,7 +881,8 @@ def test_copy_ec(self):
869881
test_target_dir = os.path.join(self.test_prefix, 'test_target_dir')
870882
mkdir(test_target_dir)
871883
args = ['--copy-ec', 'toy-0.0.eb', test_target_dir]
872-
self.eb_main(args)
884+
stdout = mocked_main(args)
885+
self.assertEqual(stdout, 'toy-0.0.eb copied to %s' % test_target_dir)
873886

874887
copied_toy_ec = os.path.join(test_target_dir, 'toy-0.0.eb')
875888
self.assertTrue(os.path.exists(copied_toy_ec))
@@ -890,7 +903,8 @@ def check_copied_files():
890903

891904
# copying multiple easyconfig files to a non-existing target directory (which is created automatically)
892905
args = ['--copy-ec', 'toy-0.0.eb', 'bzip2-1.0.6-GCC-4.9.2.eb', test_target_dir]
893-
self.eb_main(args)
906+
stdout = mocked_main(args)
907+
self.assertEqual(stdout, '2 file(s) copied to %s' % test_target_dir)
894908

895909
check_copied_files()
896910

@@ -901,7 +915,8 @@ def check_copied_files():
901915
args[-1] = os.path.basename(test_target_dir)
902916
self.assertFalse(os.path.exists(args[-1]))
903917

904-
self.eb_main(args)
918+
stdout = mocked_main(args)
919+
self.assertEqual(stdout, '2 file(s) copied to test_target_dir')
905920

906921
check_copied_files()
907922

@@ -912,6 +927,24 @@ def check_copied_files():
912927
error_pattern = ".*/test.eb exists but is not a directory"
913928
self.assertErrorRegex(EasyBuildError, error_pattern, self.eb_main, args, raise_error=True)
914929

930+
# test use of --copy-ec with only one argument: copy to current working directory
931+
test_working_dir = os.path.join(self.test_prefix, 'test_working_dir')
932+
mkdir(test_working_dir)
933+
change_dir(test_working_dir)
934+
self.assertEqual(len(os.listdir(os.getcwd())), 0)
935+
args = ['--copy-ec', 'toy-0.0.eb']
936+
stdout = mocked_main(args)
937+
regex = re.compile('toy-0.0.eb copied to .*/%s' % os.path.basename(test_working_dir))
938+
self.assertTrue(regex.match(stdout), "Pattern '%s' found in: %s" % (regex.pattern, stdout))
939+
copied_toy_cwd = os.path.join(test_working_dir, 'toy-0.0.eb')
940+
self.assertTrue(os.path.exists(copied_toy_cwd))
941+
self.assertEqual(read_file(copied_toy_cwd), toy_ec_txt)
942+
943+
# --copy-ec without arguments results in a proper error
944+
args = ['--copy-ec']
945+
error_pattern = "One of more files to copy should be specified!"
946+
self.assertErrorRegex(EasyBuildError, error_pattern, self.eb_main, args, raise_error=True)
947+
915948
def test_dry_run(self):
916949
"""Test dry run (long format)."""
917950
fd, dummylogfn = tempfile.mkstemp(prefix='easybuild-dummy', suffix='.log')

0 commit comments

Comments
 (0)