Commits

myfreeweb  committed 0983fe9

add backgrounding

  • Participants
  • Parent commits 6158305

Comments (0)

Files changed (3)

 * Lines beginning with two spaces, a greater than sign, and a space
   allow multi-line commands.
 
+* Lines beginning with two spaces, an ampersand, and a space start
+  background processes that will be killed at the end of the test.
+
 * All other lines beginning with two spaces are considered command
   output.
 
     indent = ' ' * indent
     cmdline = '%s$ ' % indent
     conline = '%s> ' % indent
+    bgpline = '%s& ' % indent
 
     f = open(path)
     abspath = os.path.abspath(path)
     refout, postout = [], []
     i = pos = prepos = -1
     stdin = []
+    bgprocs = []
     for i, line in enumerate(f):
         refout.append(line)
         if line.startswith(cmdline):
         elif line.startswith(conline):
             after.setdefault(prepos, []).append(line)
             stdin.append(line[len(conline):])
+        elif line.startswith(bgpline):
+            after.setdefault(pos, []).append(line)
+            cmd = line[len(bgpline):]
+            subproc = subprocess.Popen(cmd, shell=True,
+                    stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
+                    universal_newlines=True, env=env,
+                    close_fds=os.name == 'posix')
+            bgprocs.append(subproc)
         elif not line.startswith(indent):
             after.setdefault(pos, []).append(line)
     stdin.append('echo "\n%s %s $?"\n' % (salt, i + 1))
 
     output = p.communicate(input=encodeinput(''.join(stdin)))[0]
+    for bgp in bgprocs:
+        bgp.terminate()
     if p.returncode == 80:
         return (refout, None, [])
 

File tests/cram.t

   $ cram --indent=4 indent.t
   .
   # Ran 1 tests, 0 skipped, 0 failed.
+
+Background processes:
+
+  $ cat > background.t <<EOF
+  > Background server:
+  > 
+  >   & python -m SimpleHTTPServer 9099
+  >   $ sleep 1
+  >   $ curl localhost:9099 -Is
+  >   HTTP/1.0 200 OK
+  >   Server: SimpleHTTP/.* (re)
+  >   Date: .* (re)
+  >   Content-type: text/html; charset=utf-8
+  >   Content-Length: .* (re)
+  >   
+  > EOF
+  $ cram background.t
+  .
+  # Ran 1 tests, 0 skipped, 0 failed.