Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | #!/usr/bin/env python |
michael@0 | 2 | |
michael@0 | 3 | import io |
michael@0 | 4 | import os |
michael@0 | 5 | import unittest |
michael@0 | 6 | import proctest |
michael@0 | 7 | from mozprocess import processhandler |
michael@0 | 8 | |
michael@0 | 9 | here = os.path.dirname(os.path.abspath(__file__)) |
michael@0 | 10 | |
michael@0 | 11 | class ProcTestOutput(proctest.ProcTest): |
michael@0 | 12 | """ Class to test operations related to output handling """ |
michael@0 | 13 | |
michael@0 | 14 | def test_process_output_nonewline(self): |
michael@0 | 15 | """ |
michael@0 | 16 | Process is started, outputs data with no newline |
michael@0 | 17 | """ |
michael@0 | 18 | p = processhandler.ProcessHandler([self.python, "procnonewline.py"], |
michael@0 | 19 | cwd=here) |
michael@0 | 20 | |
michael@0 | 21 | p.run() |
michael@0 | 22 | p.processOutput(timeout=5) |
michael@0 | 23 | p.wait() |
michael@0 | 24 | |
michael@0 | 25 | detected, output = proctest.check_for_process("procnonewline.py") |
michael@0 | 26 | self.determine_status(detected, |
michael@0 | 27 | output, |
michael@0 | 28 | p.proc.returncode, |
michael@0 | 29 | p.didTimeout, |
michael@0 | 30 | False, |
michael@0 | 31 | ()) |
michael@0 | 32 | |
michael@0 | 33 | def test_stream_process_output(self): |
michael@0 | 34 | """ |
michael@0 | 35 | Process output stream does not buffer |
michael@0 | 36 | """ |
michael@0 | 37 | expected = '\n'.join([str(n) for n in range(0,10)]) |
michael@0 | 38 | |
michael@0 | 39 | stream = io.BytesIO() |
michael@0 | 40 | buf = io.BufferedRandom(stream) |
michael@0 | 41 | |
michael@0 | 42 | p = processhandler.ProcessHandler([self.python, "proccountfive.py"], |
michael@0 | 43 | cwd=here, |
michael@0 | 44 | stream=buf) |
michael@0 | 45 | |
michael@0 | 46 | p.run() |
michael@0 | 47 | p.wait() |
michael@0 | 48 | for i in range(5, 10): |
michael@0 | 49 | stream.write(str(i)+'\n') |
michael@0 | 50 | |
michael@0 | 51 | buf.flush() |
michael@0 | 52 | self.assertEquals(stream.getvalue().strip(), expected) |
michael@0 | 53 | |
michael@0 | 54 | # make sure mozprocess doesn't close the stream |
michael@0 | 55 | # since mozprocess didn't create it |
michael@0 | 56 | self.assertFalse(buf.closed) |
michael@0 | 57 | buf.close() |
michael@0 | 58 | |
michael@0 | 59 | detected, output = proctest.check_for_process("proccountfive.py") |
michael@0 | 60 | self.determine_status(detected, |
michael@0 | 61 | output, |
michael@0 | 62 | p.proc.returncode, |
michael@0 | 63 | p.didTimeout, |
michael@0 | 64 | False, |
michael@0 | 65 | ()) |
michael@0 | 66 | |
michael@0 | 67 | if __name__ == '__main__': |
michael@0 | 68 | unittest.main() |