[ssci] Defined Date metadata field

Bug: b:277147404
Change-Id: I26fbc670ff402c2ba26ba62b344fc3f3ccd61d24
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4776729
Reviewed-by: Rachael Newitt <renewitt@google.com>
Auto-Submit: Anne Redulla <aredulla@google.com>
Commit-Queue: Anne Redulla <aredulla@google.com>
changes/29/4776729/7
Anne Redulla 2 years ago committed by LUCI CQ
parent 51690612da
commit 378fe5fb91

@ -0,0 +1,36 @@
#!/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 re
import sys
from typing import Union
_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.fields.types as field_types
import metadata.fields.util as util
import metadata.validation_result as vr
_PATTERN_DATE = re.compile(r"^\d{4}-(0|1)\d-[0-3]\d$")
class DateField(field_types.MetadataField):
"""Custom field for the date when the package was updated."""
def __init__(self):
super().__init__(name="Date", one_liner=True)
def validate(self, value: str) -> Union[vr.ValidationResult, None]:
"""Checks the given value is a YYYY-MM-DD date."""
if util.matches(_PATTERN_DATE, value):
return None
return vr.ValidationError(
f"{self._name} is '{value}' - must use format YYYY-MM-DD.")

@ -14,6 +14,7 @@ _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.fields.custom.date
import metadata.fields.types as field_types
# Freeform text fields.
@ -30,9 +31,13 @@ SHIPPED = field_types.YesNoField("Shipped")
LICENSE_ANDROID_COMPATIBLE = field_types.YesNoField(
"License Android Compatible")
# Custom fields.
DATE = metadata.fields.custom.date.DateField()
ALL_FIELDS = (
NAME,
SHORT_NAME,
DATE,
REVISION,
SECURITY_CRITICAL,
SHIPPED,

@ -15,6 +15,7 @@ _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.fields.known as known_fields
import metadata.fields.types as field_types
import metadata.validation_result as vr
@ -65,6 +66,13 @@ class FieldValidationTest(unittest.TestCase):
warning_values=["Yes?", "not"],
)
def test_date_validation(self):
self._run_field_validation(
field=known_fields.DATE,
valid_values=["2012-03-04"],
error_values=["", "\n", "April 3, 2012", "2012/03/04"],
)
if __name__ == "__main__":
unittest.main()

Loading…
Cancel
Save