test_main.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Test that twisted scripts can be invoked as modules.
  5. """
  6. from __future__ import division, absolute_import
  7. import sys
  8. from twisted.application.twist._options import TwistOptions
  9. from twisted.internet import defer, reactor
  10. from twisted.python.compat import NativeStringIO as StringIO
  11. from twisted.test.test_process import Accumulator
  12. from twisted.trial.unittest import TestCase
  13. class MainTests(TestCase):
  14. """Test that twisted scripts can be invoked as modules."""
  15. def test_twisted(self):
  16. """Invoking python -m twisted should execute twist."""
  17. cmd = sys.executable
  18. p = Accumulator()
  19. d = p.endedDeferred = defer.Deferred()
  20. reactor.spawnProcess(p, cmd, [cmd, '-m', 'twisted', '--help'], env=None)
  21. p.transport.closeStdin()
  22. def processEnded(ign):
  23. f = p.outF
  24. output = f.getvalue().replace(b'\r\n', b'\n')
  25. options = TwistOptions()
  26. message = '{}\n'.format(options).encode('utf-8')
  27. self.assertEqual(output, message)
  28. return d.addCallback(processEnded)
  29. def test_twisted_import(self):
  30. """Importing twisted.__main__ does not execute twist."""
  31. output = StringIO()
  32. monkey = self.patch(sys, 'stdout', output)
  33. import twisted.__main__
  34. self.assertTrue(twisted.__main__) # Appease pyflakes
  35. monkey.restore()
  36. self.assertEqual(output.getvalue(), "")