chiark / gitweb /
lint: check file extension for metadata
authorHans-Christoph Steiner <hans@eds.org>
Fri, 23 Jun 2017 19:58:46 +0000 (21:58 +0200)
committerHans-Christoph Steiner <hans@eds.org>
Wed, 28 Jun 2017 21:14:57 +0000 (23:14 +0200)
This helps keep fdroiddata clean, on @krt's request.

closes #222

fdroidserver/lint.py
tests/lint.TestCase [new file with mode: 0755]

index 3ffdf5e31b30792b4a202c2c113b490b03d8003e..200bddf6f501f9b9167f657216263f6784acc36d 100644 (file)
@@ -17,6 +17,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 from argparse import ArgumentParser
+import glob
 import os
 import re
 import sys
@@ -378,6 +379,29 @@ def check_extlib_dir(apps):
         yield "Unused extlib at %s" % os.path.join(dir_path, path)
 
 
+def check_for_unsupported_metadata_files(basedir=""):
+    """Checks whether any non-metadata files are in metadata/"""
+
+    global config
+
+    return_value = False
+    formats = config['accepted_formats']
+    for f in glob.glob(basedir + 'metadata/*') + glob.glob(basedir + 'metadata/.*'):
+        if os.path.isdir(f):
+            exists = False
+            for t in formats:
+                exists = exists or os.path.exists(f + '.' + t)
+            if not exists:
+                print('"' + f + '/" has no matching metadata file!')
+                return_value = True
+        elif not os.path.splitext(f)[1][1:] in formats:
+            print('"' + f.replace(basedir, '')
+                  + '" is not a supported file format: (' + ','.join(formats) + ')')
+            return_value = True
+
+    return return_value
+
+
 def main():
 
     global config, options
@@ -398,7 +422,7 @@ def main():
     allapps = metadata.read_metadata(xref=True)
     apps = common.read_app_args(options.appid, allapps, False)
 
-    anywarns = False
+    anywarns = check_for_unsupported_metadata_files()
 
     apps_check_funcs = []
     if len(options.appid) == 0:
diff --git a/tests/lint.TestCase b/tests/lint.TestCase
new file mode 100755 (executable)
index 0000000..158c5cb
--- /dev/null
@@ -0,0 +1,56 @@
+#!/usr/bin/env python3
+
+# http://www.drdobbs.com/testing/unit-testing-with-python/240165163
+
+import inspect
+import optparse
+import os
+import shutil
+import sys
+import tempfile
+import unittest
+
+localmodule = os.path.realpath(
+    os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..'))
+print('localmodule: ' + localmodule)
+if localmodule not in sys.path:
+    sys.path.insert(0, localmodule)
+
+import fdroidserver.common
+import fdroidserver.lint
+
+
+class LintTest(unittest.TestCase):
+    '''fdroidserver/lint.py'''
+
+    def test_check_for_unsupported_metadata_files(self):
+        config = dict()
+        fdroidserver.common.fill_config_defaults(config)
+        config['accepted_formats'] = ('txt', 'yml')
+        fdroidserver.common.config = config
+        fdroidserver.lint.config = config
+        self.assertTrue(fdroidserver.lint.check_for_unsupported_metadata_files())
+
+        tmpdir = os.path.join(localmodule, '.testfiles')
+        tmptestsdir = tempfile.mkdtemp(prefix='test_check_for_unsupported_metadata_files-',
+                                       dir=tmpdir)
+        self.assertFalse(fdroidserver.lint.check_for_unsupported_metadata_files(tmptestsdir + '/'))
+        shutil.copytree(os.path.join(localmodule, 'tests', 'metadata'),
+                        os.path.join(tmptestsdir, 'metadata'),
+                        ignore=shutil.ignore_patterns('apk', 'dump', '*.json'))
+        self.assertFalse(fdroidserver.lint.check_for_unsupported_metadata_files(tmptestsdir + '/'))
+        shutil.copy(os.path.join(localmodule, 'tests', 'metadata', 'org.adaway.json'),
+                    os.path.join(tmptestsdir, 'metadata'))
+        self.assertTrue(fdroidserver.lint.check_for_unsupported_metadata_files(tmptestsdir + '/'))
+
+
+if __name__ == "__main__":
+    parser = optparse.OptionParser()
+    parser.add_option("-v", "--verbose", action="store_true", default=False,
+                      help="Spew out even more information than normal")
+    (fdroidserver.lint.options, args) = parser.parse_args(['--verbose'])
+    fdroidserver.common.options = fdroidserver.lint.options
+
+    newSuite = unittest.TestSuite()
+    newSuite.addTest(unittest.makeSuite(LintTest))
+    unittest.main()