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.

174 lines
6.8 KiB

6 years ago
  1. import json
  2. import sys
  3. import time
  4. import unittest
  5. from concurrent import futures
  6. from os import path
  7. from gooey.gui import application
  8. from gooey.gui.lang.i18n import _
  9. from gooey.gui.util.freeze import getResourcePath
  10. from gooey.gui.util.quoting import quote
  11. class TestGooeyIntegration(unittest.TestCase):
  12. """
  13. A few quick integration tests that exercise Gooey's various run modes
  14. WX Python needs to control the main thread. So, in order to simulate a user
  15. running through the system, we have to execute the actual assertions in a
  16. different thread
  17. """
  18. LOCAL_DIR = path.dirname(__file__)
  19. def performTest(self, configPath, assertionFunction):
  20. """
  21. Primary test harness.
  22. Instantiates the WX App, and spawns the threads
  23. required to make assertions against it
  24. """
  25. with open(configPath, 'r') as f:
  26. build_spec = json.loads(f.read())
  27. # swaps the absolute path stored by Gooey at write time
  28. # for a relative one based on our current test location
  29. target_pyfile = path.split(build_spec['target'].replace('"', ''))[-1]
  30. file_path = path.join(path.dirname(__file__), target_pyfile)
  31. run_cmd = '{} -u {}'.format(quote(sys.executable), quote(file_path))
  32. build_spec['language_dir'] = getResourcePath('languages')
  33. build_spec['target'] = run_cmd
  34. app = application.build_app(build_spec=build_spec)
  35. executor = futures.ThreadPoolExecutor(max_workers=1)
  36. testResult = executor.submit(assertionFunction, app, build_spec)
  37. app.MainLoop()
  38. testResult.result()
  39. # some extra padding time between starting/stopping the wx App
  40. app.Destroy()
  41. time.sleep(1)
  42. def test_gooeyNormalRun(self):
  43. """ Tests the happy path through the default run mode of Gooey """
  44. self.performTest(path.join(self.LOCAL_DIR, 'gooey_config__normal.json'), self.gooeySanityTest)
  45. def test_gooeySubparserMode(self):
  46. """ Tests the happy path through the subparser run mode of Gooey """
  47. self.performTest(path.join(self.LOCAL_DIR, 'gooey_config__subparser.json'), self.gooeySanityTest)
  48. def test__gooeyAutoStart(self):
  49. """Verifies that issue #201 doesn't regress and auto_start skips the config
  50. screen and hops right into the client's program"""
  51. self.performTest(path.join(self.LOCAL_DIR, 'gooey_config__autostart.json'), self.verifyAutoStart)
  52. def test__gooeyValidation(self):
  53. """Verifies that custom validation routines supplied via gooey_options prevents
  54. the user from advancing past the configuration page when they fail"""
  55. self.performTest(path.join(self.LOCAL_DIR, 'gooey_config__autostart.json'), self.verifyValidators)
  56. def verifyValidators(self, app, buildSpec):
  57. time.sleep(1)
  58. try:
  59. app.TopWindow.onStart()
  60. # we should still be on the configuration page due to a validation fail
  61. title = app.TopWindow.header._header.GetLabel()
  62. subtitle = app.TopWindow.header._subheader.GetLabel()
  63. self.assertNotEqual(title, buildSpec['program_name'])
  64. self.assertNotEqual(subtitle, buildSpec['program_description'])
  65. except:
  66. app.TopWindow.Destroy()
  67. raise
  68. else:
  69. app.TopWindow.Destroy()
  70. return None
  71. def verifyAutoStart(self, app, buildSpec):
  72. """
  73. When the auto_start flag == True Gooey should skip the
  74. configuration screen
  75. """
  76. time.sleep(1)
  77. try:
  78. # Gooey should NOT be showing the name/description headers
  79. # present on the config page
  80. title = app.TopWindow.header._header.GetLabel()
  81. subtitle = app.TopWindow.header._subheader.GetLabel()
  82. self.assertNotEqual(title, buildSpec['program_name'])
  83. self.assertNotEqual(subtitle, buildSpec['program_description'])
  84. # Gooey should be showing the console messages straight away
  85. # without manually starting the program
  86. title = app.TopWindow.header._header.GetLabel()
  87. subtitle = app.TopWindow.header._subheader.GetLabel()
  88. self.assertEqual(title,_("running_title"))
  89. self.assertEqual(subtitle, _('running_msg'))
  90. # Wait for Gooey to swap the header to the final screen
  91. while app.TopWindow.header._header.GetLabel() == _("running_title"):
  92. time.sleep(.1)
  93. # verify that we've landed on the success screen
  94. title = app.TopWindow.header._header.GetLabel()
  95. subtitle = app.TopWindow.header._subheader.GetLabel()
  96. self.assertEqual(title, _("finished_title"))
  97. self.assertEqual(subtitle, _('finished_msg'))
  98. # and that output was actually written to the console
  99. self.assertIn("Success", app.TopWindow.console.textbox.GetValue())
  100. except:
  101. app.TopWindow.Destroy()
  102. raise
  103. else:
  104. app.TopWindow.Destroy()
  105. return None
  106. def gooeySanityTest(self, app, buildSpec):
  107. time.sleep(1)
  108. try:
  109. # Check out header is present and showing data
  110. title = app.TopWindow.header._header.GetLabel()
  111. subtitle = app.TopWindow.header._subheader.GetLabel()
  112. self.assertEqual(title, buildSpec['program_name'])
  113. self.assertEqual(subtitle, buildSpec['program_description'])
  114. # switch to the run screen
  115. app.TopWindow.onStart()
  116. # Should find the expected test in the header
  117. title = app.TopWindow.header._header.GetLabel()
  118. subtitle = app.TopWindow.header._subheader.GetLabel()
  119. self.assertEqual(title,_("running_title"))
  120. self.assertEqual(subtitle, _('running_msg'))
  121. # Wait for Gooey to swap the header to the final screen
  122. while app.TopWindow.header._header.GetLabel() == _("running_title"):
  123. time.sleep(.1)
  124. # verify that we've landed on the success screen
  125. title = app.TopWindow.header._header.GetLabel()
  126. subtitle = app.TopWindow.header._subheader.GetLabel()
  127. self.assertEqual(title, _("finished_title"))
  128. self.assertEqual(subtitle, _('finished_msg'))
  129. # and that output was actually written to the console
  130. self.assertIn("Success", app.TopWindow.console.textbox.GetValue())
  131. except:
  132. app.TopWindow.Destroy()
  133. raise
  134. else:
  135. app.TopWindow.Destroy()
  136. return None
  137. if __name__ == '__main__':
  138. unittest.main()