[ssci] Added CheckChromiumMetadataFiles in presubmit_canned_checks
Bug: b:277147404 Change-Id: I14a2f11b256bc85fdfe225443ef533c38463ca3e Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4796694 Reviewed-by: Gavin Mak <gavinmak@google.com> Reviewed-by: Rachael Newitt <renewitt@google.com> Commit-Queue: Anne Redulla <aredulla@google.com>changes/94/4796694/7
parent
2c3875ea1c
commit
a1cfc693af
@ -0,0 +1,4 @@
|
|||||||
|
# Validation for Chromium's Third Party Metadata Files
|
||||||
|
|
||||||
|
This directory contains the code to validate Chromium's third party metadata
|
||||||
|
files, i.e. `README.chromium` files.
|
@ -0,0 +1,32 @@
|
|||||||
|
Name: Test-A README for Chromium metadata
|
||||||
|
Short Name: metadata-test-valid
|
||||||
|
URL: https://www.example.com/metadata,
|
||||||
|
https://www.example.com/parser
|
||||||
|
Version: 1.0.12
|
||||||
|
Date: 2020-12-03
|
||||||
|
License: Apache, 2.0 and MIT
|
||||||
|
License File: LICENSE
|
||||||
|
Security Critical: yes
|
||||||
|
Shipped: yes
|
||||||
|
CPEPrefix: unknown
|
||||||
|
This line should be ignored because CPEPrefix is a one-liner field.
|
||||||
|
Description:
|
||||||
|
A test metadata file, with a
|
||||||
|
multi-line description.
|
||||||
|
|
||||||
|
Local Modifications:
|
||||||
|
None,
|
||||||
|
EXCEPT:
|
||||||
|
* nothing.
|
||||||
|
|
||||||
|
-------------------- DEPENDENCY DIVIDER --------------------
|
||||||
|
|
||||||
|
Name: Test-B README for Chromium metadata
|
||||||
|
Short Name: metadata-test-valid-again
|
||||||
|
URL: https://www.example.com/metadata
|
||||||
|
Version: 1.0.12
|
||||||
|
Date: 2020-12-03
|
||||||
|
License: Apache, 2.0 and MIT
|
||||||
|
License File: LICENSE
|
||||||
|
Security Critical: yes
|
||||||
|
Shipped: yes
|
@ -0,0 +1,20 @@
|
|||||||
|
Name: Test-A README for Chromium metadata
|
||||||
|
Short Name: metadata-test-valid
|
||||||
|
URL: https://www.example.com/metadata,
|
||||||
|
https://www.example.com/parser
|
||||||
|
Version: 1.0.12
|
||||||
|
Date: 2020-12-03
|
||||||
|
License: Apache, 2.0 and MIT
|
||||||
|
License File: LICENSE
|
||||||
|
Security Critical: yes
|
||||||
|
Shipped: yes
|
||||||
|
CPEPrefix: unknown
|
||||||
|
This line should be ignored because CPEPrefix is a one-liner field.
|
||||||
|
Description:
|
||||||
|
A test metadata file, with a
|
||||||
|
multi-line description.
|
||||||
|
|
||||||
|
Local Modifications:
|
||||||
|
None,
|
||||||
|
EXCEPT:
|
||||||
|
* nothing.
|
@ -0,0 +1,76 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# Copyright 2023 The Chromium Authors. All rights reserved.
|
||||||
|
# Use of this source code is governed by a BSD-style license that can be
|
||||||
|
# found in the LICENSE file.
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
_THIS_DIR = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
# The repo's root directory.
|
||||||
|
_ROOT_DIR = os.path.abspath(os.path.join(_THIS_DIR, "..", ".."))
|
||||||
|
|
||||||
|
# Add the repo's root directory for clearer imports.
|
||||||
|
sys.path.insert(0, _ROOT_DIR)
|
||||||
|
|
||||||
|
import metadata.validate
|
||||||
|
|
||||||
|
|
||||||
|
class ValidateTest(unittest.TestCase):
|
||||||
|
def test_validate_file(self):
|
||||||
|
# Validate a valid file (no errors or warnings).
|
||||||
|
test_filepath = os.path.join(_THIS_DIR, "data",
|
||||||
|
"README.chromium.test.multi-valid")
|
||||||
|
results = metadata.validate.validate_file(
|
||||||
|
filepath=test_filepath,
|
||||||
|
repo_root_dir=_THIS_DIR,
|
||||||
|
)
|
||||||
|
self.assertEqual(len(results), 0)
|
||||||
|
|
||||||
|
# Validate an invalid file (both errors and warnings).
|
||||||
|
test_filepath = os.path.join(_THIS_DIR, "data",
|
||||||
|
"README.chromium.test.multi-invalid")
|
||||||
|
results = metadata.validate.validate_file(
|
||||||
|
filepath=test_filepath,
|
||||||
|
repo_root_dir=_THIS_DIR,
|
||||||
|
)
|
||||||
|
self.assertEqual(len(results), 11)
|
||||||
|
error_count = 0
|
||||||
|
warning_count = 0
|
||||||
|
for result in results:
|
||||||
|
if result.is_fatal():
|
||||||
|
error_count += 1
|
||||||
|
else:
|
||||||
|
warning_count += 1
|
||||||
|
self.assertEqual(error_count, 9)
|
||||||
|
self.assertEqual(warning_count, 2)
|
||||||
|
|
||||||
|
def test_check_file(self):
|
||||||
|
# Check a valid file (no errors or warnings).
|
||||||
|
test_filepath = os.path.join(_THIS_DIR, "data",
|
||||||
|
"README.chromium.test.multi-valid")
|
||||||
|
errors, warnings = metadata.validate.check_file(
|
||||||
|
filepath=test_filepath,
|
||||||
|
repo_root_dir=_THIS_DIR,
|
||||||
|
)
|
||||||
|
self.assertEqual(len(errors), 0)
|
||||||
|
self.assertEqual(len(warnings), 0)
|
||||||
|
|
||||||
|
# Check an invalid file (both errors and warnings).
|
||||||
|
test_filepath = os.path.join(_THIS_DIR, "data",
|
||||||
|
"README.chromium.test.multi-invalid")
|
||||||
|
errors, warnings = metadata.validate.check_file(
|
||||||
|
filepath=test_filepath,
|
||||||
|
repo_root_dir=_THIS_DIR,
|
||||||
|
)
|
||||||
|
# TODO(aredulla): update this test once validation errors can be returned
|
||||||
|
# as errors.
|
||||||
|
# self.assertEqual(len(errors), 9)
|
||||||
|
# self.assertEqual(len(warnings), 2)
|
||||||
|
self.assertEqual(len(errors), 0)
|
||||||
|
self.assertEqual(len(warnings), 11)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
@ -0,0 +1,80 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# Copyright 2023 The Chromium Authors. All rights reserved.
|
||||||
|
# Use of this source code is governed by a BSD-style license that can be
|
||||||
|
# found in the LICENSE file.
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from typing import List, Tuple
|
||||||
|
|
||||||
|
_THIS_DIR = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
# The repo's root directory.
|
||||||
|
_ROOT_DIR = os.path.abspath(os.path.join(_THIS_DIR, ".."))
|
||||||
|
|
||||||
|
# Add the repo's root directory for clearer imports.
|
||||||
|
sys.path.insert(0, _ROOT_DIR)
|
||||||
|
|
||||||
|
import metadata.parse
|
||||||
|
import metadata.validation_result as vr
|
||||||
|
|
||||||
|
|
||||||
|
def validate_file(filepath: str,
|
||||||
|
repo_root_dir: str) -> List[vr.ValidationResult]:
|
||||||
|
"""Validate the metadata file."""
|
||||||
|
if not os.path.exists(filepath):
|
||||||
|
result = vr.ValidationError(f"'{filepath}' not found.")
|
||||||
|
result.set_tag(tag="reason", value="file not found")
|
||||||
|
return [result]
|
||||||
|
|
||||||
|
# Get the directory the metadata file is in.
|
||||||
|
parent_dir = os.path.dirname(filepath)
|
||||||
|
|
||||||
|
results = []
|
||||||
|
dependencies = metadata.parse.parse_file(filepath)
|
||||||
|
for dependency in dependencies:
|
||||||
|
results.extend(
|
||||||
|
dependency.validate(
|
||||||
|
source_file_dir=parent_dir,
|
||||||
|
repo_root_dir=repo_root_dir,
|
||||||
|
))
|
||||||
|
|
||||||
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
def check_file(filepath: str,
|
||||||
|
repo_root_dir: str) -> Tuple[List[str], List[str]]:
|
||||||
|
"""Run metadata validation on the given file, and return all validation
|
||||||
|
errors and validation warnings.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
filepath: the path to a metadata file,
|
||||||
|
e.g. "/chromium/src/third_party/libname/README.chromium"
|
||||||
|
repo_root_dir: the repository's root directory; this is needed to construct
|
||||||
|
file paths to license files.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
error_messages: the fatal validation issues present in the file;
|
||||||
|
i.e. presubmit should fail.
|
||||||
|
warning_messages: the non-fatal validation issues present in the file;
|
||||||
|
i.e. presubmit should still pass.
|
||||||
|
"""
|
||||||
|
error_messages = []
|
||||||
|
warning_messages = []
|
||||||
|
for result in validate_file(filepath, repo_root_dir):
|
||||||
|
# Construct the message.
|
||||||
|
if result.get_tag("reason") == "file not found":
|
||||||
|
message = result.get_message(postscript="", width=60)
|
||||||
|
else:
|
||||||
|
message = result.get_message(width=60)
|
||||||
|
|
||||||
|
# TODO(aredulla): Actually distinguish between validation errors and
|
||||||
|
# warnings. The quality of metadata is currently being uplifted, but is not
|
||||||
|
# yet guaranteed to pass validation. So for now, all validation results will
|
||||||
|
# be returned as warnings so CLs are not blocked by invalid metadata in
|
||||||
|
# presubmits yet.
|
||||||
|
# if result.is_fatal():
|
||||||
|
# error_messages.append(message)
|
||||||
|
# else:
|
||||||
|
warning_messages.append(message)
|
||||||
|
|
||||||
|
return error_messages, warning_messages
|
Loading…
Reference in New Issue