From b5c5b38fa8d3cb468fafc377893ebdf112174e2b Mon Sep 17 00:00:00 2001 From: Kenneth Hoste Date: Sun, 8 Mar 2020 10:35:11 +0100 Subject: [PATCH] fix determining relative paths in create_index --- easybuild/tools/filetools.py | 6 +++--- test/framework/filetools.py | 30 ++++++++++++++++-------------- test/framework/options.py | 10 ++++------ 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/easybuild/tools/filetools.py b/easybuild/tools/filetools.py index d651ab2b6b..a45382a2a7 100644 --- a/easybuild/tools/filetools.py +++ b/easybuild/tools/filetools.py @@ -611,15 +611,15 @@ def create_index(path, ignore_dirs=None): elif not os.path.isdir(path): raise EasyBuildError("Specified path is not a directory: %s", path) - for (dirpath, dirnames, filenames) in os.walk(path, topdown=True): + for (dirpath, dirnames, filenames) in os.walk(path, topdown=True, followlinks=True): for filename in filenames: # use relative paths in index - index.add(os.path.join(dirpath[len(path) + 1:], filename)) + index.add(os.path.join(os.path.relpath(dirpath, path), filename)) # do not consider (certain) hidden directories # note: we still need to consider e.g., .local ! # replace list elements using [:], so os.walk doesn't process deleted directories - # see http://stackoverflow.com/questions/13454164/os-walk-without-hidden-folders + # see https://stackoverflow.com/questions/13454164/os-walk-without-hidden-folders dirnames[:] = [d for d in dirnames if d not in ignore_dirs] return index diff --git a/test/framework/filetools.py b/test/framework/filetools.py index f4a10d4771..cd76667397 100644 --- a/test/framework/filetools.py +++ b/test/framework/filetools.py @@ -1690,20 +1690,22 @@ def test_index_functions(self): # load_index just returns None if there is no index in specified directory self.assertEqual(ft.load_index(self.test_prefix), None) - # create index for test easyconfigs - index = ft.create_index(test_ecs) - self.assertEqual(len(index), 79) - - expected = [ - os.path.join('b', 'bzip2', 'bzip2-1.0.6-GCC-4.9.2.eb'), - os.path.join('t', 'toy', 'toy-0.0.eb'), - os.path.join('s', 'ScaLAPACK', 'ScaLAPACK-2.0.2-gompi-2018a-OpenBLAS-0.2.20.eb'), - ] - for fn in expected: - self.assertTrue(fn in index) - - for fp in index: - self.assertTrue(fp.endswith('.eb')) + # create index for test easyconfigs; + # test with specified path with and without trailing '/'s + for path in [test_ecs, test_ecs + '/', test_ecs + '//']: + index = ft.create_index(path) + self.assertEqual(len(index), 79) + + expected = [ + os.path.join('b', 'bzip2', 'bzip2-1.0.6-GCC-4.9.2.eb'), + os.path.join('t', 'toy', 'toy-0.0.eb'), + os.path.join('s', 'ScaLAPACK', 'ScaLAPACK-2.0.2-gompi-2018a-OpenBLAS-0.2.20.eb'), + ] + for fn in expected: + self.assertTrue(fn in index) + + for fp in index: + self.assertTrue(fp.endswith('.eb')) # set up some files to create actual index file for ft.copy_dir(os.path.join(test_ecs, 'g'), os.path.join(self.test_prefix, 'g')) diff --git a/test/framework/options.py b/test/framework/options.py index 3b9fc440fc..a7f1d9bdad 100644 --- a/test/framework/options.py +++ b/test/framework/options.py @@ -785,13 +785,11 @@ def test_ignore_index(self): toy_ec = os.path.join(test_ecs_dir, 'test_ecs', 't', 'toy', 'toy-0.0.eb') copy_file(toy_ec, self.test_prefix) + toy_ec_list = ['toy-0.0.eb', 'toy-1.2.3.eb', 'toy-4.5.6.eb'] + # install index that list more files than are actually available, # so we can check whether it's used - index_txt = '\n'.join([ - 'toy-0.0.eb', - 'toy-1.2.3.eb', - 'toy-4.5.6.eb', - ]) + index_txt = '\n'.join(toy_ec_list) write_file(os.path.join(self.test_prefix, '.eb-path-index'), index_txt) args = [ @@ -803,7 +801,7 @@ def test_ignore_index(self): stdout = self.get_stdout() self.mock_stdout(False) - for toy_ec_fn in ['toy-0.0.eb', 'toy-1.2.3.eb', 'toy-4.5.6.eb']: + for toy_ec_fn in toy_ec_list: regex = re.compile(re.escape(os.path.join(self.test_prefix, toy_ec_fn)), re.M) self.assertTrue(regex.search(stdout), "Pattern '%s' should be found in: %s" % (regex.pattern, stdout))