519 def escapef(m): |
519 def escapef(m): |
520 return escapemap[m.group(0)] |
520 return escapemap[m.group(0)] |
521 def stringescape(s): |
521 def stringescape(s): |
522 return escapesub(escapef, s) |
522 return escapesub(escapef, s) |
523 |
523 |
524 def transformtst(lines): |
|
525 inblock = False |
|
526 for l in lines: |
|
527 if inblock: |
|
528 if l.startswith(' $ ') or not l.startswith(' '): |
|
529 inblock = False |
|
530 yield ' > EOF\n' |
|
531 yield l |
|
532 else: |
|
533 yield ' > ' + l[2:] |
|
534 else: |
|
535 if l.startswith(' >>> '): |
|
536 inblock = True |
|
537 yield ' $ %s -m heredoctest <<EOF\n' % PYTHON |
|
538 yield ' > ' + l[2:] |
|
539 else: |
|
540 yield l |
|
541 if inblock: |
|
542 yield ' > EOF\n' |
|
543 |
|
544 def tsttest(test, wd, options, replacements): |
524 def tsttest(test, wd, options, replacements): |
545 t = open(test) |
525 t = open(test) |
546 out = [] |
526 out = [] |
547 script = [] |
527 script = [] |
548 salt = "SALT" + str(time.time()) |
528 salt = "SALT" + str(time.time()) |
549 |
529 |
550 pos = prepos = -1 |
530 pos = prepos = -1 |
551 after = {} |
531 after = {} |
552 expected = {} |
532 expected = {} |
553 for n, l in enumerate(transformtst(t)): |
533 inpython = False |
|
534 for n, l in enumerate(t): |
554 if not l.endswith('\n'): |
535 if not l.endswith('\n'): |
555 l += '\n' |
536 l += '\n' |
556 if l.startswith(' $ '): # commands |
537 if l.startswith(' >>> '): |
|
538 if not inpython: |
|
539 # we've just entered a Python block, add the header |
|
540 inpython = True |
|
541 script.append('echo %s %s $?\n' % (salt, n)) |
|
542 script.append('%s -m heredoctest <<EOF\n' % PYTHON) |
|
543 prepos = pos |
|
544 pos = n |
|
545 after.setdefault(prepos, []).append(l) |
|
546 script.append(l[2:]) |
|
547 elif l.startswith(' $ '): # commands |
|
548 if inpython: |
|
549 script.append("EOF\n") |
|
550 inpython = False |
557 after.setdefault(pos, []).append(l) |
551 after.setdefault(pos, []).append(l) |
558 prepos = pos |
552 prepos = pos |
559 pos = n |
553 pos = n |
560 script.append('echo %s %s $?\n' % (salt, n)) |
554 script.append('echo %s %s $?\n' % (salt, n)) |
561 script.append(l[4:]) |
555 script.append(l[4:]) |
562 elif l.startswith(' > '): # continuations |
556 elif l.startswith(' > '): # continuations |
563 after.setdefault(prepos, []).append(l) |
557 after.setdefault(prepos, []).append(l) |
564 script.append(l[4:]) |
558 script.append(l[4:]) |
565 elif l.startswith(' '): # results |
559 elif l.startswith(' '): # results |
566 # queue up a list of expected results |
560 if inpython: |
567 expected.setdefault(pos, []).append(l[2:]) |
561 script.append(l[2:]) |
|
562 after.setdefault(prepos, []).append(l) |
|
563 else: |
|
564 # queue up a list of expected results |
|
565 expected.setdefault(pos, []).append(l[2:]) |
568 else: |
566 else: |
|
567 if inpython: |
|
568 script.append("EOF\n") |
|
569 inpython = False |
569 # non-command/result - queue up for merged output |
570 # non-command/result - queue up for merged output |
570 after.setdefault(pos, []).append(l) |
571 after.setdefault(pos, []).append(l) |
|
572 |
|
573 if inpython: |
|
574 script.append("EOF\n") |
571 |
575 |
572 t.close() |
576 t.close() |
573 |
577 |
574 script.append('echo %s %s $?\n' % (salt, n + 1)) |
578 script.append('echo %s %s $?\n' % (salt, n + 1)) |
575 |
579 |