You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_main.py 2.4KB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.scripts import trial
  10. from twisted.internet import defer, reactor
  11. from twisted.python.compat import NativeStringIO as StringIO
  12. from twisted.test.test_process import Accumulator
  13. from twisted.trial.unittest import TestCase
  14. class MainTests(TestCase):
  15. """Test that twisted scripts can be invoked as modules."""
  16. def test_twisted(self):
  17. """Invoking python -m twisted should execute twist."""
  18. cmd = sys.executable
  19. p = Accumulator()
  20. d = p.endedDeferred = defer.Deferred()
  21. reactor.spawnProcess(p, cmd, [cmd, '-m', 'twisted', '--help'], env=None)
  22. p.transport.closeStdin()
  23. # Fix up our sys args to match the command we issued
  24. from twisted import __main__
  25. self.patch(sys, 'argv', [__main__.__file__, '--help'])
  26. def processEnded(ign):
  27. f = p.outF
  28. output = f.getvalue().replace(b'\r\n', b'\n')
  29. options = TwistOptions()
  30. message = '{}\n'.format(options).encode('utf-8')
  31. self.assertEqual(output, message)
  32. return d.addCallback(processEnded)
  33. def test_trial(self):
  34. """Invoking python -m twisted.trial should execute trial."""
  35. cmd = sys.executable
  36. p = Accumulator()
  37. d = p.endedDeferred = defer.Deferred()
  38. reactor.spawnProcess(p, cmd, [cmd, '-m', 'twisted.trial', '--help'], env=None)
  39. p.transport.closeStdin()
  40. # Fix up our sys args to match the command we issued
  41. from twisted.trial import __main__
  42. self.patch(sys, 'argv', [__main__.__file__, '--help'])
  43. def processEnded(ign):
  44. f = p.outF
  45. output = f.getvalue().replace(b'\r\n', b'\n')
  46. options = trial.Options()
  47. message = '{}\n'.format(options).encode('utf-8')
  48. self.assertEqual(output, message)
  49. return d.addCallback(processEnded)
  50. def test_twisted_import(self):
  51. """Importing twisted.__main__ does not execute twist."""
  52. output = StringIO()
  53. monkey = self.patch(sys, 'stdout', output)
  54. import twisted.__main__
  55. self.assertTrue(twisted.__main__) # Appease pyflakes
  56. monkey.restore()
  57. self.assertEqual(output.getvalue(), "")