tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

proctest.py (2195B)


      1 import os
      2 import sys
      3 import unittest
      4 
      5 from mozprocess import ProcessHandler
      6 
      7 here = os.path.dirname(os.path.abspath(__file__))
      8 
      9 
     10 class ProcTest(unittest.TestCase):
     11    @classmethod
     12    def setUpClass(cls):
     13        cls.proclaunch = os.path.join(here, "proclaunch.py")
     14        cls.python = sys.executable
     15 
     16    def determine_status(self, proc, isalive=False, expectedfail=()):
     17        """
     18        Use to determine if the situation has failed.
     19        Parameters:
     20            proc -- the processhandler instance
     21            isalive -- Use True to indicate we pass if the process exists; however, by default
     22                       the test will pass if the process does not exist (isalive == False)
     23            expectedfail -- Defaults to [], used to indicate a list of fields
     24                            that are expected to fail
     25        """
     26        returncode = proc.proc.returncode
     27        didtimeout = proc.didTimeout
     28        detected = ProcessHandler.pid_exists(proc.pid)
     29        output = ""
     30        # ProcessHandler has output when store_output is set to True in the constructor
     31        # (this is the default)
     32        if getattr(proc, "output"):
     33            output = proc.output
     34 
     35        if "returncode" in expectedfail:
     36            self.assertTrue(
     37                returncode, "Detected an unexpected return code of: %s" % returncode
     38            )
     39        elif isalive:
     40            self.assertEqual(
     41                returncode, None, "Detected not None return code of: %s" % returncode
     42            )
     43        else:
     44            self.assertNotEqual(
     45                returncode, None, "Detected unexpected None return code of"
     46            )
     47 
     48        if "didtimeout" in expectedfail:
     49            self.assertTrue(didtimeout, "Detected that process didn't time out")
     50        else:
     51            self.assertTrue(not didtimeout, "Detected that process timed out")
     52 
     53        if isalive:
     54            self.assertTrue(
     55                detected,
     56                "Detected process is not running, process output: %s" % output,
     57            )
     58        else:
     59            self.assertTrue(
     60                not detected,
     61                "Detected process is still running, process output: %s" % output,
     62            )