Skip to content

Commit

Permalink
Add checks for unreferenced files in the directory
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonrclarke committed Aug 5, 2021
1 parent e9e6e4e commit 0b489f5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
21 changes: 20 additions & 1 deletion hepdata_validator/full_submission_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ def validate(self, directory=None, file=None, zipfile=None):
))
return False

self.included_files = [self.submission_file_path]

# Open the submission.yaml file and load all YAML documents.
with open(self.submission_file_path, 'r') as stream:
try:
Expand Down Expand Up @@ -156,6 +158,15 @@ def validate(self, directory=None, file=None, zipfile=None):
type = SchemaType.SINGLE_YAML if self.single_yaml_file else SchemaType.SUBMISSION
self.valid_files[type] = [self.submission_file_path]

# Check all files in directory are in included_files
if not self.single_yaml_file:
for f in os.listdir(self.directory):
file_path = os.path.join(self.directory, f)
if file_path not in self.included_files:
self.add_validation_message(ValidationMessage(
file=file_path, message='%s is not referenced in the submission.' % file_path
))

return len(self.messages) == 0
finally:
if temp_directory:
Expand Down Expand Up @@ -183,6 +194,7 @@ def _check_doc(self, doc):
for resource in doc['additional_resources']:
if not resource['location'].startswith('http'):
location = os.path.join(self.directory, resource['location'])
self.included_files.append(location)
if not os.path.isfile(location):
self.add_validation_message(ValidationMessage(
file=self.submission_file_path, message='%s is missing.' % location
Expand All @@ -205,7 +217,14 @@ def _check_doc(self, doc):
return False

# Extract data file from YAML document.
data_file_path = self.directory + '/' + doc['data_file'] if self.directory else doc['data_file']
if self.directory:
data_file_path = os.path.join(self.directory, doc['data_file'])
else:
data_file_path = doc['data_file']

if not self.single_yaml_file:
self.included_files.append(data_file_path)

if not os.path.isfile(data_file_path):
self.add_validation_message(ValidationMessage(
file=data_file_path, message='%s is missing.' % data_file_path
Expand Down
5 changes: 3 additions & 2 deletions testsuite/test_full_submission_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ def test_invalid_data_directory(validator_v1, data_path, capsys):
expected_file_names = [
os.path.join(dir, 'submission.yaml'),
os.path.join(dir, 'data3.yaml'),
os.path.join(dir, 'data8.yaml')
os.path.join(dir, 'data8.yaml'),
os.path.join(dir, 'figFigure8B.png')
]
assert set(errors.keys()) == set(expected_file_names)
assert errors[expected_file_names[0]][0].message == 'mydirectory/data2.yaml should not contain "/".'
Expand All @@ -184,7 +185,7 @@ def test_invalid_data_directory(validator_v1, data_path, capsys):
in "{dir}/data8.yaml", line 1, column 1
did not find expected key
in "{dir}/data8.yaml", line 9, column 3"""

assert errors[expected_file_names[3]][0].message == f"{dir}/figFigure8B.png is not referenced in the submission."

def test_invalid_syntax_submission(validator_v1, data_path, capsys):
file = os.path.join(data_path, 'invalid_syntax_submission.yaml')
Expand Down

0 comments on commit 0b489f5

Please sign in to comment.