From 52460a39122477c6bda34a58d7f1dfcb77dfd003 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Fri, 25 Oct 2019 12:05:19 -0700 Subject: [PATCH] clean up how we check that the process runs... This is better as it allows to check output, and prevents spamming to test console. It also sets up the environment closer to a real run. --- ntunnel.py | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/ntunnel.py b/ntunnel.py index aff73ef..1cc1de4 100644 --- a/ntunnel.py +++ b/ntunnel.py @@ -316,21 +316,34 @@ class TestMain(unittest.TestCase): shutil.rmtree(self.basetempdir) self.tempdir = None - def test_noargs(self): - sys.argv = [ 'prog' ] + @async_test + async def test_noargs(self): + proc = await self.run_with_args() - with self.assertRaises(SystemExit) as cm: - main() + await proc.wait() # XXX - not checking error message # And that it exited w/ the correct code - self.assertEqual(5, cm.exception.code) + self.assertEqual(proc.returncode, 5) + + def run_with_args(self, *args): + return asyncio.create_subprocess_exec(sys.executable, + __file__, *args, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE) - def test_genkey(self): + def test_both(self): + pass + + @async_test + async def test_genkey(self): # that it can generate a key - sys.argv = [ 'prog', 'genkey', 'somefile' ] - main() + proc = await self.run_with_args('genkey', 'somefile') + + await proc.wait() + + self.assertEqual(proc.returncode, 0) with open('somefile.pub', encoding='ascii') as fp: lines = fp.readlines() @@ -347,13 +360,17 @@ class TestMain(unittest.TestCase): self.assertIsInstance(key, x448.X448PrivateKey) # that a second call fails - with self.assertRaises(SystemExit) as cm: - main() + proc = await self.run_with_args('genkey', 'somefile') - # XXX - not checking error message + await proc.wait() + + stdoutdata, stderrdata = await proc.communicate() + + self.assertFalse(stdoutdata) + self.assertEqual(b'failed to create somefile.pub, file exists.\n', stderrdata) # And that it exited w/ the correct code - self.assertEqual(1, cm.exception.code) + self.assertEqual(proc.returncode, 1) class TestNoiseFowarder(unittest.TestCase): def setUp(self):