Merging test scripts and output demo

Matt Mackall mpm at selenic.com
Fri Jun 11 16:30:40 CDT 2010


I started with this test script I had lying around:

-------------
# Use stable hg
alias hg=hgs

# Create a repo
rm -rf a
hg init a
cd a

# Make a new branch
hg branch b1
hg branch
echo foo > foo
hg ci -Am0
hg branch

# Merge b1 with null
hg merge -r null
hg branch

# Merge null with b1
hg update -r null
hg branch
hg merge -r 0
hg branch
hg ci -mhuh
hg branch
--------

And ran it through this little script:

-----------
#!/usr/bin/python

import os, sys, re, time

src = sys.argv[1]
dst = src + "-decorated"

fin = open(src)
fout = open(dst, "w")

salt = "SALT" + str(time.time())
script = fin.readlines()

for n, l in enumerate(script):
    n += 1
    if not l.startswith("#"):
        fout.write("echo %s %d\n" % (salt, n))
        fout.write(l)
fout.close()

print "sh %s > %s.out" % (dst, src)
r = os.system("sh %s > %s.out 2>&1" % (dst, src))
print "done", r

res = open("%s.out" % src)
current = ""
line = 0
perline = {}

for l in res:
    if l.startswith(salt):
        perline[line] = current
        line = int(l.split()[1])
        current = ""
    else:
        current += l

perline[line] = current

merged = open("%s.merged" % src, "w")

incomment = 1
blank = 0
for n, l in enumerate(script):
    if not l.strip():
        blank = 1
        merged.write("\n")
        continue
    if l.startswith("#!"):
        continue
    if l.startswith("#"): # doc-like
        if not incomment:
            if not blank:
                merged.write("\n")
            incomment = 1
            blank = 0
        merged.write(l[1:].lstrip())
    else:
        if incomment:
            if not blank:
                merged.write("\n")
            incomment = 0
            blank = 0
        merged.write("  $ " + l) # indent
        for ol in perline.get(n, "").splitlines(True):
            merged.write("  " + ol)
----------

And got this output:

-------------
Use stable hg

  $ alias hg=hgs

Create a repo

  $ rm -rf a
  $ hg init a
  $ cd a

Make a new branch

  $ hg branch b1
  $ hg branch
  marked working directory as branch b1
  $ echo foo > foo
  b1
  $ hg ci -Am0
  $ hg branch
  adding foo

Merge b1 with null

  $ hg merge -r null
  $ hg branch
  abort: can't merge with ancestor

Merge null with b1

  $ hg update -r null
  $ hg branch
  0 files updated, 0 files merged, 1 files removed, 0 files unresolved
  $ hg merge -r 0
  default
  $ hg branch
  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
  (branch merge, don't forget to commit)
  $ hg ci -mhuh
  default
  $ hg branch

---------------

(oh look, there's an off-by one there that I'm not going to fix right
now)

It shouldn't be too hard to teach run-tests to decorate/undecorate tests
using my "salt trick" and to convert the existing tests to this format.
Volunteers?


-- 
Mathematics is the supreme nostalgia of our time.




More information about the Mercurial-devel mailing list