Skip to content

Commit

Permalink
Now there is a way to run tests in frozen version
Browse files Browse the repository at this point in the history
  • Loading branch information
ak2ls2py committed Dec 13, 2016
1 parent 6873a90 commit 36682ab
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 89 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,5 @@ src/screenshot-*.jpg

log.txt
prof.out
output.txt

2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ include freeze.py
include setupdata.py


global-exclude *.py[cod] __pycache__ *.so *.dylib
global-exclude *.py[cod] __pycache__ *.so *.dylib output.txt
recursive-exclude junk *
recursive-exclude src *.rpt
recursive-exclude src/rrpam_wds/tests ?.inp
Expand Down
22 changes: 0 additions & 22 deletions src/rrpam_wds/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,3 @@ def __init__(self, args=None):
@pyqtSlot()
def show_application(self):
sys.exit(self.app.exec_())

# @pyqtSlot(str)

# def screenshot(self, filename):
# if (getattr(sys, 'frozen', False)):
# p = os.path.dirname(sys.executable)
# else:
# p = os.getcwd()
#
# filename=os.path.join(p,filename)
# msg = QMessageBox()
# msg.setIcon(QMessageBox.Information)
# msg.setText("Screenshot will be saved at : %s" % (filename))
# QTimer.singleShot(5000, msg.accept)
# msg.exec_()
#
# now take a screenshot
# qs = QApplication.primaryScreen()
# if (qs):
# qs.grabWindow(self.win.winId()).save(filename, 'jpg')
# else:
# QPixmap().save(filename, 'jpg')
4 changes: 2 additions & 2 deletions src/rrpam_wds/tests/test_closable_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import time
import unittest

from gui_test_tools import uniquestring
from guiqwt.label import LabelItem
from PyQt5.QtCore import Qt
from PyQt5.QtTest import QTest
from PyQt5.QtWidgets import QApplication
from test_network_map import draw_a_network

from rrpam_wds.examples import examples as ex
from rrpam_wds.gui.dialogs import CurveDialogWithClosable
from rrpam_wds.gui.dialogs import MainWindow
from rrpam_wds.tests.gui_test_tools import uniquestring
from rrpam_wds.tests.test_network_map import draw_a_network


class mdi_graph_test(unittest.TestCase):
Expand Down
107 changes: 43 additions & 64 deletions src/rrpamwds.py
Original file line number Diff line number Diff line change
@@ -1,72 +1,51 @@
from rrpam_wds.gui import set_pyqt_api # isort:skip # NOQA
from rrpam_wds import setpath # isort:skip # NOQA
import os
# ^ above line is needed to make sure the path is correctly set (under frozen conditions)
import sys
import time
from contextlib import contextmanager
from contextlib import redirect_stdout

from PyQt5.QtCore import QObject
from PyQt5.QtCore import QThread
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication
import pytest

from rrpam_wds.cli import Main

from rrpam_wds.gui import set_pyqt_api # isort:skip # NOQA
from rrpam_wds import setpath # isort:skip # NOQA

class Demo(QObject):
finished = pyqtSignal()
addAWindow = pyqtSignal(int)
timetogo = pyqtSignal()
recordMe = pyqtSignal(str)

def __init__(self, mainwindow):
self.mainwindow = mainwindow
super(Demo, self).__init__()

@pyqtSlot()
def do_some_demos(self): # A slot takes no params

for i in range(1, 11):
time.sleep(1)
self.addAWindow.emit(i)
print(i)

self.finished.emit()
time.sleep(.1)
# this is a hack. If this is not here, the main (gui) thread will freeze.
# Need to find why.
self.now_wait()

@pyqtSlot()
def now_wait(self):
self.recordMe.emit("screenshot-1.jpg")
print("Work done .. now biding time")
for i in range(1, 11):
time.sleep(1)
print(11 - i)
print(".. and done!")

time.sleep(.1)
QApplication.processEvents()
if(not len(self.mainwindow.mdi.subWindowList()) == 10):
raise Exception
self.timetogo.emit()


if (len(sys.argv) == 1): # plain run
main = Main()
sys.exit(main.app.exec_())

else: # run as a test. Open, run tests and close.
main = Main()
tester = Demo(main.win)
thread = QThread()
tester.moveToThread(thread)
tester.addAWindow.connect(main.win.new_window)
tester.finished.connect(thread.quit)
# ^ this is also needed to prevent gui from freezing upon finishing the
# thread.
thread.started.connect(tester.do_some_demos)
tester.timetogo.connect(main.app.exit)
thread.start()
main.show_application()
def fileno(file_or_fd):
fd = getattr(file_or_fd, 'fileno', lambda: file_or_fd)()
if not isinstance(fd, int):
raise ValueError("Expected a file (`.fileno()`) or a file descriptor")
return fd


@contextmanager
def stdout_redirected(to=os.devnull, stdout=None):
if stdout is None:
stdout = sys.stdout

stdout_fd = fileno(stdout)
# copy stdout_fd before it is overwritten
# NOTE: `copied` is inheritable on Windows when duplicating a standard stream
with os.fdopen(os.dup(stdout_fd), 'wb') as copied:
stdout.flush() # flush library buffers that dup2 knows nothing about
try:
os.dup2(fileno(to), stdout_fd) # $ exec >&to
except ValueError: # filename
with open(to, 'wb') as to_file:
os.dup2(to_file.fileno(), stdout_fd) # $ exec > to
try:
yield stdout # allow code to be run with the redirected stdout
finally:
# restore stdout to its previous value
# NOTE: dup2 makes stdout_fd inheritable unconditionally
stdout.flush()
os.dup2(copied.fileno(), stdout_fd) # $ exec >&copied

if (len(sys.argv) > 1): # first run tests
output = ""
with open('output.txt', 'w') as f, redirect_stdout(f):
pytest.main(sys.argv[1:])
sys.argv = [sys.argv[0]]
main = Main()
sys.exit(main.app.exec_())

0 comments on commit 36682ab

Please sign in to comment.