Bug 1427210: Use mozlog for outputting check-sync test results; r?Fallen draft
authorTom Prince <mozilla@hocat.ca>
Tue, 26 Dec 2017 21:54:26 -0700
changeset 23606 f27580cf64d9d9fe7bfd8b50ef45f010b1dcbf23
parent 23605 89c1dea4c3878f659d355dca921e28498721c835
child 23607 67b9f4fc642c5258aceda5c877f9cc6a52c1d1b2
push id193
push userbmo:mozilla@hocat.ca
push dateWed, 27 Dec 2017 22:25:50 +0000
reviewersFallen
bugs1427210
Bug 1427210: Use mozlog for outputting check-sync test results; r?Fallen MozReview-Commit-ID: JBuQCtQej0k
mail/check-sync-dirs.py
--- a/mail/check-sync-dirs.py
+++ b/mail/check-sync-dirs.py
@@ -15,27 +15,23 @@
 # 'check-sync-exceptions', which lists files in COPY that need not be
 # the same as the corresponding file in ORIGINAL, or exist at all in
 # ORIGINAL.  (The 'check-sync-exceptions' file itself is always
 # treated as exceptional.)  Blank lines and '#' comments in the file
 # are ignored.
 
 import sys
 import os
-from os.path import join
+from os.path import join, relpath
 import filecmp
-import textwrap
 import fnmatch
+import argparse
 
-if len(sys.argv) != 3:
-    print >> sys.stderr, 'TEST-UNEXPECTED-FAIL | check-sync-dirs.py | Usage: %s COPY ORIGINAL' % sys.argv[0]
-    sys.exit(1)
 
-copy = os.path.abspath(sys.argv[1])
-original = os.path.abspath(sys.argv[2])
+from mozlog import commandline
 
 
 def read_exceptions(filename):
     """
     Return the contents of ``filename``, a 'check-sync-exceptions' file, as a
     set of filenames, along with the basename of ``filename`` itself.  If
     ``filename`` does not exist, return the empty set.
     """
@@ -59,63 +55,80 @@ def fnmatch_any(filename, patterns):
     patterns ``patterns``.
     """
     for pattern in patterns:
         if fnmatch.fnmatch(filename, pattern):
             return True
     return False
 
 
-def check(copy, original):
+def check(logger, copy, original):
     """
     Check the contents of the directory tree ``copy`` against ``original``.  For each
     file that differs, apply REPORT to ``copy``, ``original``, and the file's
     relative path.  ``copy`` and ``original`` should be absolute.  Ignore files
     that match patterns given in files named ``check-sync-exceptions``.
     """
-    os.chdir(copy)
-    for (dirpath, dirnames, filenames) in os.walk('.'):
+    test_name = "check-sync-dirs.py::{copy}".format(copy=copy)
+
+    def report(relative_name, status='FAIL', message=None):
+        logger.test_status(
+            test=test_name,
+            subtest=relative_name,
+            status=status,
+            message=message,
+        )
+
+    logger.test_start(test=test_name)
+    differences_found = False
+    for (dirpath, dirnames, filenames) in os.walk(copy):
         exceptions = read_exceptions(join(dirpath, 'check-sync-exceptions'))
         for dirname in dirnames:
             if fnmatch_any(dirname, exceptions):
                 dirnames.remove(dirname)
                 break
         for filename in filenames:
-            if fnmatch_any(filename, exceptions):
-                continue
-            relative_name = join(dirpath, filename)
+            copy_name = join(dirpath, filename)
+            relative_name = relpath(copy_name, copy)
             original_name = join(original, relative_name)
-            if (os.path.exists(original_name)
-                and filecmp.cmp(relative_name, original_name, False)):
-                continue
-            report(copy, original, relative_name)
 
+            if fnmatch_any(filename, exceptions):
+                report(relative_name, 'SKIP')
+            elif (os.path.exists(original_name)
+                  and filecmp.cmp(copy_name, original_name, False)):
+                report(relative_name, 'PASS')
+            else:
+                report(relative_name, 'FAIL',
+                       message="differs from: {file}".format(file=join(original, relative_name)),
+                       )
+                differences_found = True
 
-differences_found = False
+    logger.test_end(
+        test=test_name,
+        status='FAIL' if differences_found else 'PASS',
+        expected='PASS',
+    )
+    return differences_found
 
 
-def report(copy, original, differing):
-    """
-    Print an error message for ``differing``, which was found to differ between
-    ``copy`` and ``original``.  Set the global variable ``differences_found``.
-    """
-    global differences_found
-    if not differences_found:
-        print >> sys.stderr, 'TEST-UNEXPECTED-FAIL | check-sync-dirs.py | build file copies are not in sync\n' \
-                             'TEST-INFO | check-sync-dirs.py | file(s) found in:               %s\n' \
-                             'TEST-INFO | check-sync-dirs.py | differ from their originals in: %s' \
-                             % (copy, original)
-    print >> sys.stderr, 'TEST-INFO | check-sync-dirs.py | differing file:                 %s' % differing
-    differences_found = True
+def get_parser():
+    parser = argparse.ArgumentParser()
+    parser.add_argument('copy')
+    parser.add_argument('original')
+    return parser
 
 
-check(copy, original)
+def main():
+    parser = get_parser()
+    commandline.add_logging_group(parser)
+
+    args = parser.parse_args()
+
+    logger = commandline.setup_logging("check-sync-dirs", args, {"tbpl": sys.stdout})
 
-if differences_found:
-    msg = '''In general, the files in '%s' should always be exact copies of
-originals in '%s'.  A change made to one should also be made to the
-other.  See 'check-sync-dirs.py' for more details.''' \
-         % (copy, original)
-    print >> sys.stderr, textwrap.fill(msg, 75)
-    sys.exit(1)
+    logger.suite_start(tests=[])
+    result = check(logger, args.copy, args.original)
+    logger.suite_end()
+    return result
 
-print >> sys.stderr, 'TEST-PASS | check-sync-dirs.py | %s <= %s' % (copy, original)
-sys.exit(0)
+
+if __name__ == '__main__':
+    sys.exit(main())