1331722Seadler#!/usr/bin/env python 21590Srgrimes# -*- coding: utf-8 -*- 31590Srgrimes 41590Srgrimes# 51590Srgrimes# Copyright 2017, Data61 61590Srgrimes# Commonwealth Scientific and Industrial Research Organisation (CSIRO) 71590Srgrimes# ABN 41 687 119 230. 81590Srgrimes# 91590Srgrimes# This software may be distributed and modified according to the terms of 101590Srgrimes# the BSD 2-Clause license. Note that NO WARRANTY is provided. 111590Srgrimes# See "LICENSE_BSD2.txt" for details. 121590Srgrimes# 131590Srgrimes# @TAG(DATA61_BSD) 141590Srgrimes# 151590Srgrimes 161590Srgrimes''' 171590SrgrimesTests that look for incorrect Python idioms in the templates. 181590Srgrimes''' 191590Srgrimes 201590Srgrimesfrom __future__ import absolute_import, division, print_function, \ 211590Srgrimes unicode_literals 221590Srgrimes 231590Srgrimesimport os, re, subprocess, sys, unittest 241590Srgrimes 251590SrgrimesME = os.path.abspath(__file__) 261590Srgrimes 271590Srgrimes# Make CAmkES importable 281590Srgrimessys.path.append(os.path.join(os.path.dirname(ME), '../../..')) 291590Srgrimes 301590Srgrimesfrom camkes.internal.tests.utils import CAmkESTest 3187203Smarkm 321590Srgrimesclass TestBadIdioms(CAmkESTest): 3391839Sobrien pass 3491839Sobrien 351590Srgrimesdef _string_addition(self, path): 361590Srgrimes ''' 371590Srgrimes Look for string addition instead of using format strings. 38131954Stjr ''' 391590Srgrimes regex = re.compile(r'(\'.*?\'\s*\+|\+\s*\'.*?\')') 40131954Stjr with open(path, 'rt') as f: 41132253Sjohan for lineno, line in enumerate(f, 1): 42131954Stjr if regex.search(line) is not None: 43132253Sjohan self.fail('%s:%d: %s\nstring addition instead of format string?' 44131954Stjr % (path, lineno, line)) 45131954Stjr 461590Srgrimesregex = re.compile(r'[^\w]') 471590Srgrimestemplate_dir = os.path.abspath(os.path.join(os.path.dirname(ME), '..')) 481590Srgrimestests_dir = os.path.dirname(ME) 49131954Stjr 501590Srgrimes# Find all the templates. 5191839Sobrienfor root, _, filenames in os.walk(template_dir): 5291839Sobrien 53131954Stjr if root.startswith(tests_dir): 54131954Stjr # Don't analyse the test files. 55131954Stjr continue 56228636Sdim 57328500Skevans # For each template, monkey patch a test for it onto the test class. 581590Srgrimes for f in filenames: 59328500Skevans if f.endswith('.swp') or f.endswith('.py'): 60328500Skevans # Skip vim lock files. 61131954Stjr continue 62131954Stjr name = 'test_%s' % regex.sub('_', f) 63131954Stjr path = os.path.join(root, f) 64131954Stjr setattr(TestBadIdioms, name, lambda self, path=path: _string_addition(self, path)) 65131954Stjr 66131954Stjrif __name__ == '__main__': 671590Srgrimes unittest.main() 681590Srgrimes