1#!/usr/bin/env python 2# -*- coding: ascii -*- 3# 4# Copyright 2014 5# Andr\xe9 Malo or his licensors, as applicable 6# 7# Licensed under the Apache License, Version 2.0 (the "License"); 8# you may not use this file except in compliance with the License. 9# You may obtain a copy of the License at 10# 11# http://www.apache.org/licenses/LICENSE-2.0 12# 13# Unless required by applicable law or agreed to in writing, software 14# distributed under the License is distributed on an "AS IS" BASIS, 15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16# See the License for the specific language governing permissions and 17# limitations under the License. 18""" 19=========== 20 Run tests 21=========== 22 23Run tests. 24""" 25__author__ = "Andr\xe9 Malo" 26__author__ = getattr(__author__, 'decode', lambda x: __author__)('latin-1') 27__docformat__ = "restructuredtext en" 28 29import os as _os 30import re as _re 31import sys as _sys 32 33from _setup import shell 34from _setup import term 35 36 37def run_tests(basedir, libdir): 38 """ Run output based tests """ 39 import rcssmin as _rcssmin 40 py_cssmin = _rcssmin._make_cssmin(python_only=True) 41 c_cssmin = _rcssmin._make_cssmin(python_only=False) 42 43 def run_test(example, output_file): 44 """ Run it """ 45 try: 46 fp = open(example, 'r') 47 except IOError: 48 return 49 else: 50 try: 51 input = fp.read() 52 finally: 53 fp.close() 54 55 def load_output(filename): 56 try: 57 fp = open(filename, 'r') 58 except IOError: 59 return None 60 else: 61 try: 62 output = fp.read() 63 finally: 64 fp.close() 65 output = output.strip() 66 if _re.search(r'(?<!\\)(?:\\\\)*\\[0-9a-zA-Z]{1,6}$', output): 67 output += ' ' 68 return output 69 70 output = load_output(output_file) 71 output_b = load_output(output_file + '.b') 72 73 def do_test(cssmin, output, **options): 74 try: 75 genout = cssmin(input, **options) 76 except (KeyboardInterrupt, SystemExit): 77 raise 78 except: 79 return 1, "%(RED)s exc%(NORMAL)s " 80 else: 81 if output is None: 82 return 1, "%(RED)smiss%(NORMAL)s " 83 elif genout == output or genout == output.rstrip(): 84 return 0, "%(GREEN)sOK%(NORMAL)s " 85 else: 86 return 1, "%(RED)sfail%(NORMAL)s " 87 88 erred, out = do_test(py_cssmin, output) 89 erred, c_out = do_test(c_cssmin, output) 90 erred, out_b = do_test(py_cssmin, output_b, keep_bang_comments=True) 91 erred, c_out_b = do_test(c_cssmin, output_b, keep_bang_comments=True) 92 93 term.write( 94 "%(out)s %(out_b)s | %(c_out)s %(c_out_b)s - %%(example)s\n" 95 % locals(), 96 example=_os.path.basename(example), 97 ) 98 return erred 99 100 # end 101 # begin main test code 102 103 erred = 0 104 basedir = shell.native(basedir) 105 strip = len(basedir) - len(_os.path.basename(basedir)) 106 for dirname, dirs, files in shell.walk(basedir): 107 dirs[:] = [ 108 item for item in dirs if item not in ('.svn', '.git', 'out') 109 ] 110 dirs.sort() 111 files = [item for item in files if item.endswith('.css')] 112 if not files: 113 continue 114 if not _os.path.isdir(_os.path.join(basedir, dirname, 'out')): 115 continue 116 term.yellow("---> %s" % (dirname[strip:],)) 117 files.sort() 118 for filename in files: 119 if run_test( 120 _os.path.join(dirname, filename), 121 _os.path.join(dirname, 'out', filename[:-4] + '.out'), 122 ): erred = 1 123 term.yellow("<--- %s" % (dirname[strip:],)) 124 return erred 125 126 127def main(): 128 """ Main """ 129 basedir, libdir = None, None 130 accept_opts = True 131 args = [] 132 for arg in _sys.argv[1:]: 133 if accept_opts: 134 if arg == '--': 135 accept_opts = False 136 continue 137 elif arg == '-q': 138 term.write = term.green = term.red = term.yellow = \ 139 term.announce = \ 140 lambda fmt, **kwargs: None 141 continue 142 elif arg == '-p': 143 info = {} 144 for key in term.terminfo(): 145 info[key] = '' 146 info['ERASE'] = '\n' 147 term.terminfo.info = info 148 continue 149 elif arg.startswith('-'): 150 _sys.stderr.write("Unrecognized option %r\n" % (arg,)) 151 return 2 152 args.append(arg) 153 if len(args) > 2: 154 _sys.stderr.write("Too many arguments\n") 155 return 2 156 elif len(args) < 1: 157 _sys.stderr.write("Missing arguments\n") 158 return 2 159 basedir = args[0] 160 if len(args) > 1: 161 libdir = args[1] 162 return run_tests(basedir, libdir) 163 164 165if __name__ == '__main__': 166 _sys.exit(main()) 167