def test_execute_escapes_quotes(self, mock_popen): mock_popen.return_value.wait.return_value = 0 mock_popen.return_value.stdout = StringIO("") execute("mycommand -a {arg} {}", "; rm;", arg="'quote tacular'") mock_popen.assert_called_with( "mycommand -a ''\"'\"'quote tacular'\"'\"'' '; rm;'", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
def execute(self, command, *args, **kwargs): def capture(command, *cargs, **ckwargs): self.commands.append(command) result = mock.Mock() result.wait.return_value = 0 result.stdout = StringIO(self.execute_output) return result with mock.patch("subprocess.Popen") as mock_popen: mock_popen.side_effect = capture return execute(command, *args, **kwargs)
def test_execute(self, mock_popen): def side_effect(command, shell, stdout, stderr): self.assertEqual("foobar thing", command) self.assertTrue(shell) self.assertEqual(subprocess.PIPE, stdout) self.assertEqual(subprocess.STDOUT, stderr) result = mock.Mock() result.wait.return_value = 0 result.stdout = StringIO("FOOBAR!") return result mock_popen.side_effect = side_effect output = execute("foobar thing") self.assertEqual("FOOBAR!", output)
def test_execute_fails(self, mock_popen): mock_popen.return_value.wait.return_value = 1 mock_popen.return_value.stdout = StringIO("") with self.assertRaises(RuntimeError): execute("whatever")