on xenial help sign in
Add test to check the sdk versions are aligned
9df1b7fAlbert Astals Cid
tests/CMakeLists.txt+1
@@ -126,3 +126,4 @@ add_subdirectory(libs)
126add_subdirectory(plugins)
126add_subdirectory(plugins)
127add_subdirectory(qmltests)
127add_subdirectory(qmltests)
128add_subdirectory(whitespace)
128add_subdirectory(whitespace)
129+add_subdirectory(sdk)
tests/sdk/CMakeLists.txtnew file+6
@@ -0,0 +1,6 @@
1+
2+#
3+# Test that all imports for Ubuntu.Components use the wanted version
4+#
5+
6+add_test(Whitespace ${CMAKE_CURRENT_SOURCE_DIR}/check_sdk.py ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR})
tests/sdk/check_sdk.pynew file+114
@@ -0,0 +1,114 @@
1+#! /usr/bin/env python3
2+
3+#
4+# Copyright (C) 2015 Canonical Ltd
5+#
6+# This program is free software: you can redistribute it and/or modify
7+# it under the terms of the GNU General Public License version 3 as
8+# published by the Free Software Foundation.
9+#
10+# This program is distributed in the hope that it will be useful,
11+# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+# GNU General Public License for more details.
14+#
15+# You should have received a copy of the GNU General Public License
16+# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+#
18+# Authored by: Michi Henning <michi.henning@canonical.com>
19+#
20+
21+#
22+# Little helper program to test that all source files import
23+# the wanted SDK version
24+#
25+# Usage: check_sdk.py directory [ignore_prefix]
26+#
27+# The directory specifies the (recursive) location of the source files. Any
28+# files with a path that starts with ignore_prefix are not checked. This is
29+# useful to exclude files that are generated into the build directory.
30+#
31+# See the file_pat definition below for a list of files that are checked.
32+#
33+
34+from __future__ import print_function
35+
36+import argparse
37+import os
38+import re
39+import sys
40+
41+
42+# Print msg on stderr, preceded by program name and followed by newline
43+def error(msg):
44+ print(os.path.basename(sys.argv[0]) + ": " + msg, file=sys.stderr)
45+
46+
47+# Function to raise errors encountered by os.walk
48+def raise_error(e):
49+ raise e
50+
51+# Scan lines in file_path for bad SDK version. For each file,
52+# print the line numbers that have SDK version issues
53+
54+ubuntu_components_pat = re.compile(r'.*import Ubuntu.Components.*')
55+ubuntu_good_components_pat = re.compile(r'.*import Ubuntu.Components.*1\.3.*')
56+
57+def scan_for_bad_sdk(file_path):
58+ global ubuntu_components_pat
59+ errors = []
60+ with open(file_path, 'rt', encoding='utf-8') as ifile:
61+ for lino, line in enumerate(ifile, start=1):
62+ if ubuntu_components_pat.match(line) and not ubuntu_good_components_pat.match(line):
63+ errors.append(lino)
64+ if 0 < len(errors) <= 10:
65+ if len(errors) > 1:
66+ plural = 's'
67+ else:
68+ plural = ''
69+ print(
70+ "%s: bad SDK Version in line%s %s" % (
71+ file_path, plural, ", ".join((str(i) for i in errors))))
72+ elif errors:
73+ print("%s: bad SDK Version in multiple lines" % file_path)
74+ return bool(errors)
75+
76+# Parse args
77+
78+parser = argparse.ArgumentParser(
79+ description='Test that source files contain the wanted SDK version.')
80+parser.add_argument(
81+ 'dir', nargs=1,
82+ help='The directory to (recursively) search for source files')
83+parser.add_argument(
84+ 'ignore_prefix', nargs='?', default=None,
85+ help='Ignore source files with a path that starts with the given prefix.')
86+args = parser.parse_args()
87+
88+# Files we want to check for SDK version.
89+
90+file_pat = (
91+ r'(.*\.(js|qml)$)')
92+pat = re.compile(file_pat)
93+
94+# Find all the files with matching file extension in the specified
95+# directory and check them
96+
97+directory = os.path.abspath(args.dir[0])
98+ignore = args.ignore_prefix and os.path.abspath(args.ignore_prefix) or None
99+
100+found_bad_sdk = False
101+try:
102+ for root, dirs, files in os.walk(directory, onerror=raise_error):
103+ for file in files:
104+ path = os.path.join(root, file)
105+ if not (ignore and path.startswith(ignore)) and pat.match(file):
106+ if scan_for_bad_sdk(path):
107+ found_bad_sdk = True
108+
109+except OSError as e:
110+ error("cannot create file list for \"" + dir + "\": " + e.strerror)
111+ sys.exit(1)
112+
113+if found_bad_sdk:
114+ sys.exit(1)