def test_old_psutil(self, psutil):
        p1 = lambda: None  # object with settable fields
        p1.name = "python"
        p1.cmdline = ["foo", "bar"]
        p1.getcwd = lambda: "/any/dir"
        p1.pid = 42
        processes = [p1]
        psutil.process_iter.return_value = (p for p in processes)

        actual_processes = [p for p in get_processes()]

        self.assertEqual(len(actual_processes), 1)
        self.assertEqual(actual_processes[0].pid, 42)
        self.assertEqual(actual_processes[0].name(), "python")
        self.assertEqual(actual_processes[0].cmdline(), ["foo", "bar"])
        self.assertEqual(actual_processes[0].cwd(), "/any/dir")
    def test_new_psutil(self, psutil):
        p1 = Mock()
        p1.name.return_value = "python"
        p1.cmdline.return_value = ["foo", "bar"]
        p1.cwd.return_value = "/any/dir"
        p1.pid = 42
        processes = [p1]
        psutil.process_iter.return_value = (p for p in processes)

        actual_processes = [p for p in get_processes()]

        self.assertEqual(len(actual_processes), 1)
        self.assertEqual(actual_processes[0].pid, 42)
        self.assertEqual(actual_processes[0].name(), "python")
        self.assertEqual(actual_processes[0].cmdline(), ["foo", "bar"])
        self.assertEqual(actual_processes[0].cwd(), "/any/dir")
    def test_old_psutil(self, psutil):
        p1 = lambda: None  # object with settable fields
        p1.name = "python"
        p1.cmdline = ["foo", "bar"]
        p1.getcwd = lambda: "/any/dir"
        p1.pid = 42
        processes = [p1]
        psutil.process_iter.return_value = (p for p in processes)

        actual_processes = [p for p in get_processes()]

        self.assertEqual(len(actual_processes), 1)
        self.assertEqual(actual_processes[0].pid, 42)
        self.assertEqual(actual_processes[0].name(), "python")
        self.assertEqual(actual_processes[0].cmdline(), ["foo", "bar"])
        self.assertEqual(actual_processes[0].cwd(), "/any/dir")
    def test_new_psutil(self, psutil):
        p1 = Mock()
        p1.name.return_value = "python"
        p1.cmdline.return_value = ["foo", "bar"]
        p1.cwd.return_value = "/any/dir"
        p1.pid = 42
        processes = [p1]
        psutil.process_iter.return_value = (p for p in processes)

        actual_processes = [p for p in get_processes()]

        self.assertEqual(len(actual_processes), 1)
        self.assertEqual(actual_processes[0].pid, 42)
        self.assertEqual(actual_processes[0].name(), "python")
        self.assertEqual(actual_processes[0].cmdline(), ["foo", "bar"])
        self.assertEqual(actual_processes[0].cwd(), "/any/dir")
 def get_python_processes_containing(self, script_name):
     python_processes = filter(lambda p: p.name() == "python", get_processes())
     matching_processes = filter(lambda p: script_name in p.cmdline(), python_processes)
     return matching_processes