Skip to content

Have repository_tool.get_filepaths_in_directory use absolute paths #774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions tests/test_repository_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,21 +377,50 @@ def test_get_filepaths_in_directory(self):
repo = repo_tool.Repository
metadata_directory = os.path.join('repository_data',
'repository', 'metadata')

# Verify the expected filenames. get_filepaths_in_directory() returns
# a list of absolute paths.
metadata_files = repo.get_filepaths_in_directory(metadata_directory)
expected_files = ['1.root.json', 'root.json',
'targets.json', 'snapshot.json',
'timestamp.json', 'role1.json', 'role2.json']

basenames = []
for filepath in metadata_files:
basenames.append(os.path.basename(filepath))
self.assertEqual(sorted(expected_files), sorted(basenames))
# Construct list of file paths expected, determining absolute paths.
expected_files = []
for filepath in ['1.root.json', 'root.json', 'targets.json',
'snapshot.json', 'timestamp.json', 'role1.json', 'role2.json']:
expected_files.append(os.path.abspath(os.path.join(
'repository_data', 'repository', 'metadata', filepath)))

self.assertEqual(sorted(expected_files), sorted(metadata_files))


# Test when the 'recursive_walk' argument is True.
# In this case, recursive walk should yield the same results as the
# previous, non-recursive call.
metadata_files = repo.get_filepaths_in_directory(metadata_directory,
recursive_walk=True)
self.assertEqual(sorted(expected_files), sorted(metadata_files))

# And this recursive call from the directory above should yield the same
# results as well, plus extra files.
metadata_files = repo.get_filepaths_in_directory(
os.path.join('repository_data', 'repository'), recursive_walk=True)
for expected_file in expected_files:
self.assertIn(expected_file, metadata_files)
# self.assertEqual(sorted(expected_files), sorted(metadata_files))

# Now let's check it against the full list of expected files for the parent
# directory.... We'll add to the existing list. Expect the same files in
# metadata.staged/ as in metadata/, and a few target files in targets/
# This is somewhat redundant with the previous test, but together they're
# probably more future-proof.
for filepath in ['file1.txt', 'file2.txt', 'file3.txt']:
expected_files.append(os.path.abspath(os.path.join(
'repository_data', 'repository', 'targets', filepath)))
for filepath in [ '1.root.json', 'root.json', 'targets.json',
'snapshot.json', 'timestamp.json', 'role1.json', 'role2.json']:
expected_files.append(os.path.abspath(os.path.join(
'repository_data', 'repository', 'metadata.staged', filepath)))

self.assertEqual(sorted(expected_files), sorted(metadata_files))

# Test improperly formatted arguments.
self.assertRaises(securesystemslib.exceptions.FormatError, repo.get_filepaths_in_directory,
Expand Down
2 changes: 1 addition & 1 deletion tuf/repository_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ def get_filepaths_in_directory(files_directory, recursive_walk=False,
for dirpath, dirnames, filenames in os.walk(files_directory,
followlinks=followlinks):
for filename in filenames:
full_target_path = os.path.join(dirpath, filename)
full_target_path = os.path.join(os.path.abspath(dirpath), filename)
targets.append(full_target_path)

# Prune the subdirectories to walk right now if we do not wish to
Expand Down