| 1 |
|
| 2 |
''' |
| 3 |
Tests for the 'cheetah' command. |
| 4 |
|
| 5 |
Besides unittest usage, recognizes the following command-line options: |
| 6 |
--list CheetahWrapper.py |
| 7 |
List all scenarios that are tested. The argument is the path |
| 8 |
of this script. |
| 9 |
--nodelete |
| 10 |
Don't delete scratch directory at end. |
| 11 |
--output |
| 12 |
Show the output of each subcommand. (Normally suppressed.) |
| 13 |
''' |
| 14 |
|
| 15 |
import os |
| 16 |
import os.path |
| 17 |
import re |
| 18 |
import shutil |
| 19 |
import sys |
| 20 |
import tempfile |
| 21 |
import unittest |
| 22 |
import pickle |
| 23 |
import json |
| 24 |
|
| 25 |
|
| 26 |
from optparse import OptionParser |
| 27 |
from Cheetah.CheetahWrapper import CheetahWrapper |
| 28 |
|
| 29 |
try: |
| 30 |
from subprocess import Popen, PIPE, STDOUT |
| 31 |
|
| 32 |
class Popen4(Popen): |
| 33 |
def __init__(self, cmd, bufsize=-1, shell=True, close_fds=True, |
| 34 |
stdin=PIPE, stdout=PIPE, stderr=STDOUT, **kwargs): |
| 35 |
|
| 36 |
super(Popen4, self).__init__( |
| 37 |
cmd, bufsize=bufsize, shell=shell, close_fds=close_fds, |
| 38 |
stdin=stdin, stdout=stdout, stderr=stderr, **kwargs) |
| 39 |
|
| 40 |
self.tochild = self.stdin |
| 41 |
self.fromchild = self.stdout |
| 42 |
self.childerr = self.stderr |
| 43 |
except ImportError: |
| 44 |
from popen2 import Popen4 |
| 45 |
|
| 46 |
DELETE = True |
| 47 |
OUTPUT = False |
| 48 |
|
| 49 |
BACKUP_SUFFIX = CheetahWrapper.BACKUP_SUFFIX |
| 50 |
|
| 51 |
|
| 52 |
def warn(msg): |
| 53 |
sys.stderr.write(msg + '\n') |
| 54 |
|
| 55 |
|
| 56 |
class CFBase(unittest.TestCase): |
| 57 |
"""Base class for "cheetah compile" and "cheetah fill" unit tests. |
| 58 |
""" |
| 59 |
srcDir = '' |
| 60 |
subdirs = ('child', 'child/grandkid') |
| 61 |
srcFiles = ('a.tmpl', 'child/a.tmpl', 'child/grandkid/a.tmpl') |
| 62 |
expectError = False |
| 63 |
|
| 64 |
def inform(self, message): |
| 65 |
if self.verbose: |
| 66 |
print(message) |
| 67 |
|
| 68 |
def setUp(self): |
| 69 |
"""Create the top-level directories, subdirectories and .tmpl |
| 70 |
files. |
| 71 |
""" |
| •72 |
self.cmd = sys.executable + " " + self.locate_cheetah('cheetah') |
| 73 |
cwd = os.getcwd() |
| 74 |
if not os.environ.get('PYTHONPATH'): |
| 75 |
os.environ['PYTHONPATH'] = cwd |
| 76 |
else: |
| 77 |
pythonPath = os.environ['PYTHONPATH'] |
| 78 |
if (pythonPath != cwd) and \ |
| 79 |
not pythonPath.endswith(':%s' % cwd): |
| 80 |
os.environ['PYTHONPATH'] = '%s:%s' % (pythonPath, cwd) |
| 81 |
|
| 82 |
self.scratchDir = scratchDir = tempfile.mkdtemp() |
| 83 |
self.origCwd = os.getcwd() |
| 84 |
os.chdir(scratchDir) |
| 85 |
if self.srcDir: |
| 86 |
os.mkdir(self.srcDir) |
| 87 |
|
| 88 |
for dir in self.subdirs: |
| 89 |
os.mkdir(dir) |
| 90 |
|
| 91 |
for fil in self.srcFiles: |
| 92 |
f = open(fil, 'w') |
| 93 |
f.write("Hello, world!\n") |
| 94 |
f.close() |
| 95 |
|
| 96 |
def tearDown(self): |
| 97 |
os.chdir(self.origCwd) |
| 98 |
if DELETE: |
| 99 |
shutil.rmtree(self.scratchDir, True) |
| 100 |
if os.path.exists(self.scratchDir): |
| 101 |
warn("Warning: unable to delete scratch directory %s" |
| 102 |
% self.scratchDir) |
| 103 |
else: |
| 104 |
warn("Warning: not deleting scratch directory %s" |
| 105 |
% self.scratchDir) |
| 106 |
|
| 107 |
def _checkDestFileHelper(self, path, expected, |
| 108 |
allowSurroundingText, errmsg): |
| 109 |
"""Low-level helper to check a destination file. |
| 110 |
|
| 111 |
in : path, string, the destination path. |
| 112 |
expected, string, the expected contents. |
| 113 |
allowSurroundingtext, bool, allow the result to contain |
| 114 |
additional text around the 'expected' substring? |
| 115 |
errmsg, string, the error message. It may contain the |
| 116 |
following "%"-operator keys: path, expected, result. |
| 117 |
out: None |
| 118 |
""" |
| 119 |
path = os.path.abspath(path) |
| 120 |
exists = os.path.exists(path) |
| 121 |
msg = "destination file missing: %s" % path |
| 122 |
self.assertTrue(exists, msg) |
| 123 |
f = open(path, 'r') |
| 124 |
result = f.read() |
| 125 |
f.close() |
| 126 |
if allowSurroundingText: |
| 127 |
success = result.find(expected) != -1 |
| 128 |
else: |
| 129 |
success = result == expected |
| 130 |
msg = errmsg % locals() |
| 131 |
self.assertTrue(success, msg) |
| 132 |
|
| 133 |
def checkCompile(self, path): |
| 134 |
|
| 135 |
|
| 136 |
expected = "Hello, world!" |
| 137 |
errmsg = """\ |
| 138 |
destination file %(path)s doesn't contain expected substring: |
| 139 |
%(expected)r""" |
| 140 |
self._checkDestFileHelper(path, expected, True, errmsg) |
| 141 |
|
| 142 |
def checkFill(self, path): |
| 143 |
expected = "Hello, world!\n" |
| 144 |
errmsg = """\ |
| 145 |
destination file %(path)s contains wrong result. |
| 146 |
Expected %(expected)r |
| 147 |
Found %(result)r""" |
| 148 |
self._checkDestFileHelper(path, expected, False, errmsg) |
| 149 |
|
| 150 |
def checkSubdirPyInit(self, path): |
| 151 |
"""Verify a destination subdirectory exists and contains an |
| 152 |
__init__.py file. |
| 153 |
""" |
| 154 |
exists = os.path.exists(path) |
| 155 |
msg = "destination subdirectory %s misssing" % path |
| 156 |
self.assertTrue(exists, msg) |
| 157 |
initPath = os.path.join(path, "__init__.py") |
| 158 |
exists = os.path.exists(initPath) |
| 159 |
msg = "destination init file missing: %s" % initPath |
| 160 |
self.assertTrue(exists, msg) |
| 161 |
|
| 162 |
def checkNoBackup(self, path): |
| 163 |
"""Verify 'path' does not exist. (To check --nobackup.) |
| 164 |
""" |
| 165 |
exists = os.path.exists(path) |
| 166 |
msg = "backup file exists in spite of --nobackup: %s" % path |
| 167 |
self.assertFalse(exists, msg) |
| 168 |
|
| 169 |
def locate_cheetah(self, cmd): |
| 170 |
paths = os.getenv('PATH') |
| 171 |
if not paths: |
| 172 |
return cmd |
| 173 |
paths = paths.split(':') |
| 174 |
for p in paths: |
| 175 |
p = os.path.join(p, cmd) |
| 176 |
p = os.path.abspath(p) |
| 177 |
if os.path.isfile(p): |
| 178 |
return p |
| 179 |
return cmd |
| 180 |
|
| 181 |
def assertWin32Subprocess(self, cmd): |
| 182 |
try: |
| 183 |
from subprocess import Popen, PIPE, STDOUT |
| 184 |
except ImportError: |
| 185 |
_in, _out = os.popen4(cmd) |
| 186 |
_in.close() |
| 187 |
output = _out.read() |
| 188 |
status = _out.close() |
| 189 |
if status is None: |
| 190 |
status = 0 |
| 191 |
else: |
| 192 |
process = Popen(cmd, shell=True, |
| 193 |
stdin=PIPE, stdout=PIPE, stderr=STDOUT) |
| 194 |
process.stdin.close() |
| 195 |
output = process.stdout.read() |
| 196 |
status = process.wait() |
| 197 |
process.stdout.close() |
| 198 |
return status, output |
| 199 |
|
| 200 |
def assertPosixSubprocess(self, cmd): |
| 201 |
cmd = cmd.replace('cheetah', self.cmd) |
| 202 |
process = Popen4(cmd, env=os.environ) |
| 203 |
process.tochild.close() |
| 204 |
output = process.fromchild.read() |
| 205 |
status = process.wait() |
| 206 |
process.fromchild.close() |
| 207 |
return status, output |
| 208 |
|
| 209 |
def assertSubprocess(self, cmd, nonzero=False): |
| 210 |
status, output = None, None |
| 211 |
if sys.platform == 'win32': |
| 212 |
status, output = self.assertWin32Subprocess(cmd) |
| 213 |
else: |
| 214 |
status, output = self.assertPosixSubprocess(cmd) |
| 215 |
|
| 216 |
if not nonzero: |
| 217 |
self.assertEqual(status, 0, |
| 218 |
'''Subprocess exited with a non-zero status (%d) |
| 219 |
%s''' % (status, output)) |
| 220 |
else: |
| 221 |
self.assertNotEqual(status, 0, |
| 222 |
'''Subprocess exited with a zero status (%d) |
| 223 |
%s''' % (status, output)) |
| 224 |
return output |
| 225 |
|
| 226 |
def go(self, cmd, expectedStatus=0, expectedOutputSubstring=None): |
| 227 |
"""Run a "cheetah compile" or "cheetah fill" subcommand. |
| 228 |
|
| 229 |
in : cmd, string, the command to run. |
| 230 |
expectedStatus, int, subcommand's expected output status. |
| 231 |
0 if the subcommand is expected to succeed, 1-255 otherwise. |
| 232 |
expectedOutputSubstring, string, substring which much appear |
| 233 |
in the standard output or standard error. None to skip this |
| 234 |
test. |
| 235 |
out: None. |
| 236 |
""" |
| 237 |
output = self.assertSubprocess(cmd) |
| 238 |
if expectedOutputSubstring is not None: |
| 239 |
msg = "substring %r not found in subcommand output: %s" % \ |
| 240 |
(expectedOutputSubstring, cmd) |
| 241 |
substringTest = output.find(expectedOutputSubstring) != -1 |
| 242 |
self.assertTrue(substringTest, msg) |
| 243 |
|
| 244 |
|
| 245 |
class CFIdirBase(CFBase): |
| 246 |
"""Subclass for tests with --idir. |
| 247 |
""" |
| 248 |
srcDir = 'SRC' |
| 249 |
subdirs = ('SRC/child', 'SRC/child/grandkid') |
| 250 |
srcFiles = ('SRC/a.tmpl', 'SRC/child/a.tmpl', 'SRC/child/grandkid/a.tmpl') |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
class OneFile(CFBase): |
| 258 |
def testCompile(self): |
| 259 |
self.go("cheetah compile a.tmpl") |
| 260 |
self.checkCompile("a.py") |
| 261 |
|
| 262 |
def testFill(self): |
| 263 |
self.go("cheetah fill a.tmpl") |
| 264 |
self.checkFill("a.html") |
| 265 |
|
| 266 |
def testText(self): |
| 267 |
self.go("cheetah fill --oext txt a.tmpl") |
| 268 |
self.checkFill("a.txt") |
| 269 |
|
| 270 |
|
| 271 |
class OneFileNoExtension(CFBase): |
| 272 |
def testCompile(self): |
| 273 |
self.go("cheetah compile a") |
| 274 |
self.checkCompile("a.py") |
| 275 |
|
| 276 |
def testFill(self): |
| 277 |
self.go("cheetah fill a") |
| 278 |
self.checkFill("a.html") |
| 279 |
|
| 280 |
def testText(self): |
| 281 |
self.go("cheetah fill --oext txt a") |
| 282 |
self.checkFill("a.txt") |
| 283 |
|
| 284 |
|
| 285 |
class SplatTmpl(CFBase): |
| 286 |
def testCompile(self): |
| 287 |
self.go("cheetah compile *.tmpl") |
| 288 |
self.checkCompile("a.py") |
| 289 |
|
| 290 |
def testFill(self): |
| 291 |
self.go("cheetah fill *.tmpl") |
| 292 |
self.checkFill("a.html") |
| 293 |
|
| 294 |
def testText(self): |
| 295 |
self.go("cheetah fill --oext txt *.tmpl") |
| 296 |
self.checkFill("a.txt") |
| 297 |
|
| 298 |
|
| 299 |
class ThreeFilesWithSubdirectories(CFBase): |
| 300 |
def testCompile(self): |
| 301 |
self.go("cheetah compile a.tmpl child/a.tmpl child/grandkid/a.tmpl") |
| 302 |
self.checkCompile("a.py") |
| 303 |
self.checkCompile("child/a.py") |
| 304 |
self.checkCompile("child/grandkid/a.py") |
| 305 |
|
| 306 |
def testFill(self): |
| 307 |
self.go("cheetah fill a.tmpl child/a.tmpl child/grandkid/a.tmpl") |
| 308 |
self.checkFill("a.html") |
| 309 |
self.checkFill("child/a.html") |
| 310 |
self.checkFill("child/grandkid/a.html") |
| 311 |
|
| 312 |
def testText(self): |
| 313 |
self.go("cheetah fill --oext txt " |
| 314 |
"a.tmpl child/a.tmpl child/grandkid/a.tmpl") |
| 315 |
self.checkFill("a.txt") |
| 316 |
self.checkFill("child/a.txt") |
| 317 |
self.checkFill("child/grandkid/a.txt") |
| 318 |
|
| 319 |
|
| 320 |
class ThreeFilesWithSubdirectoriesNoExtension(CFBase): |
| 321 |
def testCompile(self): |
| 322 |
self.go("cheetah compile a child/a child/grandkid/a") |
| 323 |
self.checkCompile("a.py") |
| 324 |
self.checkCompile("child/a.py") |
| 325 |
self.checkCompile("child/grandkid/a.py") |
| 326 |
|
| 327 |
def testFill(self): |
| 328 |
self.go("cheetah fill a child/a child/grandkid/a") |
| 329 |
self.checkFill("a.html") |
| 330 |
self.checkFill("child/a.html") |
| 331 |
self.checkFill("child/grandkid/a.html") |
| 332 |
|
| 333 |
def testText(self): |
| 334 |
self.go("cheetah fill --oext txt a child/a child/grandkid/a") |
| 335 |
self.checkFill("a.txt") |
| 336 |
self.checkFill("child/a.txt") |
| 337 |
self.checkFill("child/grandkid/a.txt") |
| 338 |
|
| 339 |
|
| 340 |
class SplatTmplWithSubdirectories(CFBase): |
| 341 |
def testCompile(self): |
| 342 |
self.go("cheetah compile *.tmpl child/*.tmpl child/grandkid/*.tmpl") |
| 343 |
self.checkCompile("a.py") |
| 344 |
self.checkCompile("child/a.py") |
| 345 |
self.checkCompile("child/grandkid/a.py") |
| 346 |
|
| 347 |
def testFill(self): |
| 348 |
self.go("cheetah fill *.tmpl child/*.tmpl child/grandkid/*.tmpl") |
| 349 |
self.checkFill("a.html") |
| 350 |
self.checkFill("child/a.html") |
| 351 |
self.checkFill("child/grandkid/a.html") |
| 352 |
|
| 353 |
def testText(self): |
| 354 |
self.go("cheetah fill --oext txt " |
| 355 |
"*.tmpl child/*.tmpl child/grandkid/*.tmpl") |
| 356 |
self.checkFill("a.txt") |
| 357 |
self.checkFill("child/a.txt") |
| 358 |
self.checkFill("child/grandkid/a.txt") |
| 359 |
|
| 360 |
|
| 361 |
class OneFileWithOdir(CFBase): |
| 362 |
def testCompile(self): |
| 363 |
self.go("cheetah compile --odir DEST a.tmpl") |
| 364 |
self.checkSubdirPyInit("DEST") |
| 365 |
self.checkCompile("DEST/a.py") |
| 366 |
|
| 367 |
def testFill(self): |
| 368 |
self.go("cheetah fill --odir DEST a.tmpl") |
| 369 |
self.checkFill("DEST/a.html") |
| 370 |
|
| 371 |
def testText(self): |
| 372 |
self.go("cheetah fill --odir DEST --oext txt a.tmpl") |
| 373 |
self.checkFill("DEST/a.txt") |
| 374 |
|
| 375 |
|
| 376 |
class VarietyWithOdir(CFBase): |
| 377 |
def testCompile(self): |
| 378 |
self.go("cheetah compile --odir DEST " |
| 379 |
"a.tmpl child/a child/grandkid/*.tmpl") |
| 380 |
self.checkSubdirPyInit("DEST") |
| 381 |
self.checkSubdirPyInit("DEST/child") |
| 382 |
self.checkSubdirPyInit("DEST/child/grandkid") |
| 383 |
self.checkCompile("DEST/a.py") |
| 384 |
self.checkCompile("DEST/child/a.py") |
| 385 |
self.checkCompile("DEST/child/grandkid/a.py") |
| 386 |
|
| 387 |
def testFill(self): |
| 388 |
self.go("cheetah fill --odir DEST " |
| 389 |
"a.tmpl child/a child/grandkid/*.tmpl") |
| 390 |
self.checkFill("DEST/a.html") |
| 391 |
self.checkFill("DEST/child/a.html") |
| 392 |
self.checkFill("DEST/child/grandkid/a.html") |
| 393 |
|
| 394 |
def testText(self): |
| 395 |
self.go("cheetah fill --odir DEST --oext txt " |
| 396 |
"a.tmpl child/a child/grandkid/*.tmpl") |
| 397 |
self.checkFill("DEST/a.txt") |
| 398 |
self.checkFill("DEST/child/a.txt") |
| 399 |
self.checkFill("DEST/child/grandkid/a.txt") |
| 400 |
|
| 401 |
|
| 402 |
class RecurseExplicit(CFBase): |
| 403 |
def testCompile(self): |
| 404 |
self.go("cheetah compile -R child") |
| 405 |
self.checkCompile("child/a.py") |
| 406 |
self.checkCompile("child/grandkid/a.py") |
| 407 |
|
| 408 |
def testFill(self): |
| 409 |
self.go("cheetah fill -R child") |
| 410 |
self.checkFill("child/a.html") |
| 411 |
self.checkFill("child/grandkid/a.html") |
| 412 |
|
| 413 |
def testText(self): |
| 414 |
self.go("cheetah fill -R --oext txt child") |
| 415 |
self.checkFill("child/a.txt") |
| 416 |
self.checkFill("child/grandkid/a.txt") |
| 417 |
|
| 418 |
|
| 419 |
class RecurseImplicit(CFBase): |
| 420 |
def testCompile(self): |
| 421 |
self.go("cheetah compile -R") |
| 422 |
self.checkCompile("child/a.py") |
| 423 |
self.checkCompile("child/grandkid/a.py") |
| 424 |
|
| 425 |
def testFill(self): |
| 426 |
self.go("cheetah fill -R") |
| 427 |
self.checkFill("a.html") |
| 428 |
self.checkFill("child/a.html") |
| 429 |
self.checkFill("child/grandkid/a.html") |
| 430 |
|
| 431 |
def testText(self): |
| 432 |
self.go("cheetah fill -R --oext txt") |
| 433 |
self.checkFill("a.txt") |
| 434 |
self.checkFill("child/a.txt") |
| 435 |
self.checkFill("child/grandkid/a.txt") |
| 436 |
|
| 437 |
|
| 438 |
class RecurseExplicitWIthOdir(CFBase): |
| 439 |
def testCompile(self): |
| 440 |
self.go("cheetah compile -R --odir DEST child") |
| 441 |
self.checkSubdirPyInit("DEST/child") |
| 442 |
self.checkSubdirPyInit("DEST/child/grandkid") |
| 443 |
self.checkCompile("DEST/child/a.py") |
| 444 |
self.checkCompile("DEST/child/grandkid/a.py") |
| 445 |
|
| 446 |
def testFill(self): |
| 447 |
self.go("cheetah fill -R --odir DEST child") |
| 448 |
self.checkFill("DEST/child/a.html") |
| 449 |
self.checkFill("DEST/child/grandkid/a.html") |
| 450 |
|
| 451 |
def testText(self): |
| 452 |
self.go("cheetah fill -R --odir DEST --oext txt child") |
| 453 |
self.checkFill("DEST/child/a.txt") |
| 454 |
self.checkFill("DEST/child/grandkid/a.txt") |
| 455 |
|
| 456 |
|
| 457 |
class Flat(CFBase): |
| 458 |
def testCompile(self): |
| 459 |
self.go("cheetah compile --flat child/a.tmpl") |
| 460 |
self.checkCompile("a.py") |
| 461 |
|
| 462 |
def testFill(self): |
| 463 |
self.go("cheetah fill --flat child/a.tmpl") |
| 464 |
self.checkFill("a.html") |
| 465 |
|
| 466 |
def testText(self): |
| 467 |
self.go("cheetah fill --flat --oext txt child/a.tmpl") |
| 468 |
self.checkFill("a.txt") |
| 469 |
|
| 470 |
|
| 471 |
class FlatRecurseCollision(CFBase): |
| 472 |
expectError = True |
| 473 |
|
| 474 |
def testCompile(self): |
| 475 |
self.assertSubprocess("cheetah compile -R --flat", nonzero=True) |
| 476 |
|
| 477 |
def testFill(self): |
| 478 |
self.assertSubprocess("cheetah fill -R --flat", nonzero=True) |
| 479 |
|
| 480 |
def testText(self): |
| 481 |
self.assertSubprocess("cheetah fill -R --flat", nonzero=True) |
| 482 |
|
| 483 |
|
| 484 |
class IdirRecurse(CFIdirBase): |
| 485 |
def testCompile(self): |
| 486 |
self.go("cheetah compile -R --idir SRC child") |
| 487 |
self.checkSubdirPyInit("child") |
| 488 |
self.checkSubdirPyInit("child/grandkid") |
| 489 |
self.checkCompile("child/a.py") |
| 490 |
self.checkCompile("child/grandkid/a.py") |
| 491 |
|
| 492 |
def testFill(self): |
| 493 |
self.go("cheetah fill -R --idir SRC child") |
| 494 |
self.checkFill("child/a.html") |
| 495 |
self.checkFill("child/grandkid/a.html") |
| 496 |
|
| 497 |
def testText(self): |
| 498 |
self.go("cheetah fill -R --idir SRC --oext txt child") |
| 499 |
self.checkFill("child/a.txt") |
| 500 |
self.checkFill("child/grandkid/a.txt") |
| 501 |
|
| 502 |
|
| 503 |
class IdirOdirRecurse(CFIdirBase): |
| 504 |
def testCompile(self): |
| 505 |
self.go("cheetah compile -R --idir SRC --odir DEST child") |
| 506 |
self.checkSubdirPyInit("DEST/child") |
| 507 |
self.checkSubdirPyInit("DEST/child/grandkid") |
| 508 |
self.checkCompile("DEST/child/a.py") |
| 509 |
self.checkCompile("DEST/child/grandkid/a.py") |
| 510 |
|
| 511 |
def testFill(self): |
| 512 |
self.go("cheetah fill -R --idir SRC --odir DEST child") |
| 513 |
self.checkFill("DEST/child/a.html") |
| 514 |
self.checkFill("DEST/child/grandkid/a.html") |
| 515 |
|
| 516 |
def testText(self): |
| 517 |
self.go("cheetah fill -R --idir SRC --odir DEST --oext txt child") |
| 518 |
self.checkFill("DEST/child/a.txt") |
| 519 |
self.checkFill("DEST/child/grandkid/a.txt") |
| 520 |
|
| 521 |
|
| 522 |
class IdirFlatRecurseCollision(CFIdirBase): |
| 523 |
expectError = True |
| 524 |
|
| 525 |
def testCompile(self): |
| 526 |
self.assertSubprocess( |
| 527 |
"cheetah compile -R --flat --idir SRC", nonzero=True) |
| 528 |
|
| 529 |
def testFill(self): |
| 530 |
self.assertSubprocess( |
| 531 |
"cheetah fill -R --flat --idir SRC", nonzero=True) |
| 532 |
|
| 533 |
def testText(self): |
| 534 |
self.assertSubprocess( |
| 535 |
"cheetah fill -R --flat --idir SRC --oext txt", nonzero=True) |
| 536 |
|
| 537 |
|
| 538 |
class NoBackup(CFBase): |
| 539 |
"""Run the command twice each time and verify a backup file is |
| 540 |
*not* created. |
| 541 |
""" |
| 542 |
def testCompile(self): |
| 543 |
self.go("cheetah compile --nobackup a.tmpl") |
| 544 |
self.go("cheetah compile --nobackup a.tmpl") |
| 545 |
self.checkNoBackup("a.py" + BACKUP_SUFFIX) |
| 546 |
|
| 547 |
def testFill(self): |
| 548 |
self.go("cheetah fill --nobackup a.tmpl") |
| 549 |
self.go("cheetah fill --nobackup a.tmpl") |
| 550 |
self.checkNoBackup("a.html" + BACKUP_SUFFIX) |
| 551 |
|
| 552 |
def testText(self): |
| 553 |
self.go("cheetah fill --nobackup --oext txt a.tmpl") |
| 554 |
self.go("cheetah fill --nobackup --oext txt a.tmpl") |
| 555 |
self.checkNoBackup("a.txt" + BACKUP_SUFFIX) |
| 556 |
|
| 557 |
|
| 558 |
class FileDataFill(CFBase): |
| 559 |
"""Populate searchlist variables from pickled or JSON data""" |
| 560 |
srcFiles = ('pickle.tmpl', 'json.tmpl') |
| 561 |
|
| 562 |
def setUp(self): |
| 563 |
"""Create the top-level directories and .tmpl |
| 564 |
files. |
| 565 |
""" |
| •566 |
self.cmd = sys.executable + " " + self.locate_cheetah('cheetah') |
| 567 |
cwd = os.getcwd() |
| 568 |
if not os.environ.get('PYTHONPATH'): |
| 569 |
os.environ['PYTHONPATH'] = cwd |
| 570 |
else: |
| 571 |
pythonPath = os.environ['PYTHONPATH'] |
| 572 |
if (pythonPath != cwd) and \ |
| 573 |
not pythonPath.endswith(':%s' % cwd): |
| 574 |
os.environ['PYTHONPATH'] = '%s:%s' % (pythonPath, cwd) |
| 575 |
|
| 576 |
self.scratchDir = scratchDir = tempfile.mkdtemp() |
| 577 |
self.origCwd = os.getcwd() |
| 578 |
os.chdir(scratchDir) |
| 579 |
if self.srcDir: |
| 580 |
os.mkdir(self.srcDir) |
| 581 |
|
| 582 |
for fil in self.srcFiles: |
| 583 |
with open(fil, 'w') as f: |
| 584 |
f.write("foo $bar\n") |
| 585 |
|
| 586 |
self.pickle_file = tempfile.NamedTemporaryFile(mode='wb', delete=False) |
| 587 |
pickle.dump({'bar': 'baz'}, self.pickle_file) |
| 588 |
self.pickle_file.close() |
| 589 |
self.json_file = tempfile.NamedTemporaryFile(mode='w', delete=False) |
| 590 |
json.dump({'bar': 'baz'}, self.json_file) |
| 591 |
self.json_file.close() |
| 592 |
|
| 593 |
def tearDown(self): |
| 594 |
os.chdir(self.origCwd) |
| 595 |
if DELETE: |
| 596 |
shutil.rmtree(self.scratchDir, True) |
| 597 |
if os.path.exists(self.scratchDir): |
| 598 |
warn("Warning: unable to delete scratch directory %s" |
| 599 |
% self.scratchDir) |
| 600 |
else: |
| 601 |
warn("Warning: not deleting scratch directory %s" |
| 602 |
% self.scratchDir) |
| 603 |
|
| 604 |
def checkCompile(self, path): |
| 605 |
|
| 606 |
|
| 607 |
expected = "foo" |
| 608 |
errmsg = """\ |
| 609 |
destination file %(path)s doesn't contain expected substring: |
| 610 |
%(expected)r""" |
| 611 |
self._checkDestFileHelper(path, expected, True, errmsg) |
| 612 |
|
| 613 |
def checkFill(self, path): |
| 614 |
expected = "foo baz\n" |
| 615 |
errmsg = """\ |
| 616 |
destination file %(path)s contains wrong result. |
| 617 |
Expected %(expected)r |
| 618 |
Found %(result)r""" |
| 619 |
self._checkDestFileHelper(path, expected, False, errmsg) |
| 620 |
|
| 621 |
def testCompile(self): |
| 622 |
self.go('cheetah compile json.tmpl') |
| 623 |
self.go('cheetah compile pickle.tmpl') |
| 624 |
self.checkCompile('json.py') |
| 625 |
self.checkCompile('pickle.py') |
| 626 |
|
| 627 |
def testFill(self): |
| 628 |
self.go('cheetah fill --json {jsonfile} json.tmpl' |
| 629 |
.format(jsonfile=self.json_file.name)) |
| 630 |
self.go('cheetah fill --pickle {picklefile} pickle.tmpl' |
| 631 |
.format(picklefile=self.pickle_file.name)) |
| 632 |
self.checkFill('json.html') |
| 633 |
self.checkFill('pickle.html') |
| 634 |
|
| 635 |
def testText(self): |
| 636 |
self.go('cheetah fill --json {jsonfile} --oext txt json.tmpl' |
| 637 |
.format(jsonfile=self.json_file.name)) |
| 638 |
self.go('cheetah fill --pickle {picklefile} --oext txt pickle.tmpl' |
| 639 |
.format(picklefile=self.pickle_file.name)) |
| 640 |
self.checkFill('json.txt') |
| 641 |
self.checkFill('pickle.txt') |
| 642 |
|
| 643 |
|
| 644 |
def listTests(cheetahWrapperFile): |
| 645 |
"""cheetahWrapperFile, string, path of this script. |
| 646 |
|
| 647 |
XXX TODO: don't print test where expectError is true. |
| 648 |
""" |
| 649 |
rx = re.compile(R'self\.go\("(.*?)"\)') |
| 650 |
f = open(cheetahWrapperFile) |
| 651 |
while True: |
| 652 |
lin = f.readline() |
| 653 |
if not lin: |
| 654 |
break |
| 655 |
m = rx.search(lin) |
| 656 |
if m: |
| 657 |
print(m.group(1)) |
| 658 |
f.close() |
| 659 |
|
| 660 |
|
| 661 |
def main(): |
| 662 |
global DELETE, OUTPUT |
| 663 |
parser = OptionParser() |
| 664 |
parser.add_option("--list", action="store", dest="listTests") |
| 665 |
parser.add_option("--nodelete", action="store_true") |
| 666 |
parser.add_option("--output", action="store_true") |
| 667 |
|
| 668 |
parser.add_option("-e", "--explain", action="store_true") |
| 669 |
parser.add_option("-v", "--verbose", action="store_true") |
| 670 |
parser.add_option("-q", "--quiet", action="store_true") |
| 671 |
opts, files = parser.parse_args() |
| 672 |
if opts.nodelete: |
| 673 |
DELETE = False |
| 674 |
if opts.output: |
| 675 |
OUTPUT = True |
| 676 |
if opts.listTests: |
| 677 |
listTests(opts.listTests) |
| 678 |
else: |
| 679 |
|
| 680 |
|
| 681 |
del sys.argv[1:] |
| 682 |
for opt in ("explain", "verbose", "quiet"): |
| 683 |
if getattr(opts, opt): |
| 684 |
sys.argv.append("--" + opt) |
| 685 |
sys.argv.extend(files) |
| 686 |
unittest.main() |
| 687 |
|
| 688 |
|
| 689 |
if __name__ == '__main__': |
| 690 |
main() |