def test_documentation(self): with OutputCapture(): with mocks.Subprocess( 'ls', completion=mocks.ProcessCompletion( returncode=0, stdout='file1.txt\nfile2.txt\n'), ): result = run(['ls'], capture_output=True, encoding='utf-8') assert result.returncode == 0 assert result.stdout == 'file1.txt\nfile2.txt\n' with mocks.Subprocess( 'ls', completion=mocks.ProcessCompletion( returncode=0, stdout='file1.txt\nfile2.txt\n'), ): assert subprocess.check_output(['ls' ]) == b'file1.txt\nfile2.txt\n' assert subprocess.check_call(['ls']) == 0 with mocks.Subprocess( mocks.Subprocess.CommandRoute( 'command-a', 'argument', completion=mocks.ProcessCompletion(returncode=0)), mocks.Subprocess.CommandRoute( 'command-b', completion=mocks.ProcessCompletion(returncode=-1)), ): result = run(['command-a', 'argument']) assert result.returncode == 0 result = run(['command-b']) assert result.returncode == -1
def test_implied_route(self): with mocks.Subprocess( 'command', completion=mocks.ProcessCompletion(returncode=0)): self.assertEqual(run(['command']).returncode, 0) with self.assertRaises(OSError): run(['invalid-file'])
def test_popen(self): with OutputCapture() as captured: with mocks.Subprocess(MockSubprocess.LS): result = subprocess.check_call(['ls']) self.assertEqual(result, 0) self.assertEqual(captured.stdout.getvalue(), 'file1.txt\nfile2.txt\n')
def test_popen(self): with OutputCapture() as captured: with mocks.Subprocess(MockSubprocess.LS): result = run(['ls']) self.assertEqual(result.returncode, 0) self.assertEqual(result.stdout, None) self.assertEqual(result.stderr, None) self.assertEqual(captured.stdout.getvalue(), 'file1.txt\nfile2.txt\n')
def test_cwd_priority(self): with OutputCapture(), mocks.Subprocess( mocks.Subprocess.Route( 'command', completion=mocks.ProcessCompletion(returncode=0), cwd='/example'), mocks.Subprocess.Route( 'command', completion=mocks.ProcessCompletion(returncode=1)), ): self.assertEqual(run(['command']).returncode, 1) self.assertEqual(run(['command'], cwd='/example').returncode, 0)
def test_argument_priority(self): with OutputCapture(), mocks.Subprocess( mocks.Subprocess.Route( 'command', '--help', completion=mocks.ProcessCompletion(returncode=0)), mocks.Subprocess.Route( 'command', completion=mocks.ProcessCompletion(returncode=1)), ): self.assertEqual(run(['command']).returncode, 1) self.assertEqual(run(['command', '--help']).returncode, 0)
def test_input_priority(self): with OutputCapture(), mocks.Subprocess( mocks.Subprocess.Route( 'command', completion=mocks.ProcessCompletion(returncode=0), input='stdin'), mocks.Subprocess.Route( 'command', completion=mocks.ProcessCompletion(returncode=1)), ): self.assertEqual(run(['command']).returncode, 1) self.assertEqual(run(['command'], input='stdin').returncode, 0) self.assertEqual( run(['command'], stdin=BytesIO(b'stdin')).returncode, 0)
def test_ordered(self): with OutputCapture(), mocks.Subprocess( mocks.Subprocess.Route( 'command', completion=mocks.ProcessCompletion(returncode=0)), mocks.Subprocess.Route( 'command', completion=mocks.ProcessCompletion(returncode=1)), ordered=True, ): self.assertEqual(run(['command']).returncode, 0) self.assertEqual(run(['command']).returncode, 1) with self.assertRaises(OSError): run(['command'])
def test_popen(self): with mocks.Time: with mocks.Subprocess(self.LS, self.SLEEP): ls = subprocess.Popen(['ls'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) ls.wait() self.assertEqual(0, ls.poll()) self.assertEqual(b'file1.txt\nfile2.txt\n', ls.stdout.read()) sleep = subprocess.Popen(['sleep', '1'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.assertEqual(None, sleep.poll()) sleep.wait() self.assertEqual(0, sleep.poll())
def test_no_file(self): with mocks.Subprocess(self.LS, self.SLEEP): with self.assertRaises(OSError): run(['invalid-file'])
def test_popen(self): with mocks.Subprocess(MockSubprocess.LS): result = subprocess.check_output(['ls']) self.assertEqual(result, b'file1.txt\nfile2.txt\n')