From 4e70c4192937bfc084dcc63bc8738c5fbad481f0 Mon Sep 17 00:00:00 2001 From: fkromer Date: Mon, 25 Apr 2016 20:25:13 +0200 Subject: [PATCH 01/20] change: refactoring self->cls --- test_proxy.py | 86 ++++++++++++++++++++++++++------------------------- 1 file changed, 44 insertions(+), 42 deletions(-) diff --git a/test_proxy.py b/test_proxy.py index e7250ab7e..7604f8f2e 100644 --- a/test_proxy.py +++ b/test_proxy.py @@ -11,93 +11,95 @@ else: import unittest + class ProxyTest(unittest.TestCase): @classmethod - def setUpClass(self): + def setUpClass(cls): """ Class scope setup. """ - self.p = Proxy() + cls.p = Proxy() - def setUp(self): + def setUp(cls): """ Function/test case scope setup. """ - self.output = StringIO() - self.saved_stdout = sys.stdout - sys.stdout = self.output + cls.output = StringIO() + cls.saved_stdout = sys.stdout + sys.stdout = cls.output - def tearDown(self): + def tearDown(cls): """ Function/test case scope teardown. """ - self.output.close() - sys.stdout = self.saved_stdout + cls.output.close() + sys.stdout = cls.saved_stdout - def test_sales_manager_shall_work_through_proxy_with_delay(self): - self.p.busy = 'No' + def test_sales_manager_shall_work_through_proxy_with_delay(cls): + cls.p.busy = 'No' start_time = time() - self.p.work() + cls.p.work() end_time = time() execution_time = end_time - start_time - print_output = self.output.getvalue() + print_output = cls.output.getvalue() expected_print_output = 'Proxy checking for Sales Manager availability\n\ Sales Manager ready to talk\n' - self.assertEqual(print_output, expected_print_output) + cls.assertEqual(print_output, expected_print_output) expected_execution_time = 2 - self.assertEqual(int(execution_time), expected_execution_time) + cls.assertEqual(int(execution_time), expected_execution_time) - def test_sales_manager_shall_respond_through_proxy_with_delay(self): - self.p.busy = 'Yes' + def test_sales_manager_shall_respond_through_proxy_with_delay(cls): + cls.p.busy = 'Yes' start_time = time() - self.p.work() + cls.p.work() end_time = time() execution_time = end_time - start_time - print_output = self.output.getvalue() + print_output = cls.output.getvalue() expected_print_output = 'Proxy checking for Sales Manager availability\n\ Sales Manager is busy\n' - self.assertEqual(print_output, expected_print_output) + cls.assertEqual(print_output, expected_print_output) expected_execution_time = 2 - self.assertEqual(int(execution_time), expected_execution_time) + cls.assertEqual(int(execution_time), expected_execution_time) + class NoTalkProxyTest(unittest.TestCase): @classmethod - def setUpClass(self): + def setUpClass(cls): """ Class scope setup. """ - self.ntp = NoTalkProxy() + cls.ntp = NoTalkProxy() - def setUp(self): + def setUp(cls): """ Function/test case scope setup. """ - self.output = StringIO() - self.saved_stdout = sys.stdout - sys.stdout = self.output + cls.output = StringIO() + cls.saved_stdout = sys.stdout + sys.stdout = cls.output - def tearDown(self): + def tearDown(cls): """ Function/test case scope teardown. """ - self.output.close() - sys.stdout = self.saved_stdout + cls.output.close() + sys.stdout = cls.saved_stdout - def test_sales_manager_shall_not_work_through_proxy_with_delay(self): - self.ntp.busy = 'No' + def test_sales_manager_shall_not_work_through_proxy_with_delay(cls): + cls.ntp.busy = 'No' start_time = time() - self.ntp.work() + cls.ntp.work() end_time = time() execution_time = end_time - start_time - print_output = self.output.getvalue() + print_output = cls.output.getvalue() expected_print_output = 'Proxy checking for Sales Manager availability\n\ This Sales Manager will not talk to you whether he/she is busy or not\n' - self.assertEqual(print_output, expected_print_output) + cls.assertEqual(print_output, expected_print_output) expected_execution_time = 2 - self.assertEqual(int(execution_time), expected_execution_time) + cls.assertEqual(int(execution_time), expected_execution_time) - def test_sales_manager_shall_not_respond_through_proxy_with_delay(self): - self.ntp.busy = 'Yes' + def test_sales_manager_shall_not_respond_through_proxy_with_delay(cls): + cls.ntp.busy = 'Yes' start_time = time() - self.ntp.work() + cls.ntp.work() end_time = time() execution_time = end_time - start_time - print_output = self.output.getvalue() + print_output = cls.output.getvalue() expected_print_output = 'Proxy checking for Sales Manager availability\n\ This Sales Manager will not talk to you whether he/she is busy or not\n' - self.assertEqual(print_output, expected_print_output) + cls.assertEqual(print_output, expected_print_output) expected_execution_time = 2 - self.assertEqual(int(execution_time), expected_execution_time) + cls.assertEqual(int(execution_time), expected_execution_time) if __name__ == "__main__": unittest.main() From 070c2cfc1f2dca84b2b0c76347f8ecef299316fe Mon Sep 17 00:00:00 2001 From: fkromer Date: Fri, 13 May 2016 19:43:20 +0200 Subject: [PATCH 02/20] change: refactor (work -> talk) --- test_proxy.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test_proxy.py b/test_proxy.py index 7604f8f2e..fdf9188ab 100644 --- a/test_proxy.py +++ b/test_proxy.py @@ -30,10 +30,10 @@ def tearDown(cls): cls.output.close() sys.stdout = cls.saved_stdout - def test_sales_manager_shall_work_through_proxy_with_delay(cls): + def test_sales_manager_shall_talk_through_proxy_with_delay(cls): cls.p.busy = 'No' start_time = time() - cls.p.work() + cls.p.talk() end_time = time() execution_time = end_time - start_time print_output = cls.output.getvalue() @@ -46,7 +46,7 @@ def test_sales_manager_shall_work_through_proxy_with_delay(cls): def test_sales_manager_shall_respond_through_proxy_with_delay(cls): cls.p.busy = 'Yes' start_time = time() - cls.p.work() + cls.p.talk() end_time = time() execution_time = end_time - start_time print_output = cls.output.getvalue() @@ -75,10 +75,10 @@ def tearDown(cls): cls.output.close() sys.stdout = cls.saved_stdout - def test_sales_manager_shall_not_work_through_proxy_with_delay(cls): + def test_sales_manager_shall_not_talk_through_proxy_with_delay(cls): cls.ntp.busy = 'No' start_time = time() - cls.ntp.work() + cls.ntp.talk() end_time = time() execution_time = end_time - start_time print_output = cls.output.getvalue() @@ -91,7 +91,7 @@ def test_sales_manager_shall_not_work_through_proxy_with_delay(cls): def test_sales_manager_shall_not_respond_through_proxy_with_delay(cls): cls.ntp.busy = 'Yes' start_time = time() - cls.ntp.work() + cls.ntp.talk() end_time = time() execution_time = end_time - start_time print_output = cls.output.getvalue() From b4993f30af555b008a4f7519781a361268dd1dac Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Tue, 17 May 2016 11:31:45 -0400 Subject: [PATCH 03/20] BF(PY2): use mock module if no unittest.mock is available --- test_abstract_factory.py | 5 ++++- test_bridge.py | 6 +++++- test_hsm.py | 5 ++++- test_observer.py | 7 +++++-- test_publish_subscribe.py | 7 +++++-- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/test_abstract_factory.py b/test_abstract_factory.py index ff228a410..c43b655a0 100644 --- a/test_abstract_factory.py +++ b/test_abstract_factory.py @@ -9,7 +9,10 @@ else: import unittest -from unittest.mock import patch +try: + from unittest.mock import patch +except ImportError: + from mock import patch class TestPetShop(unittest.TestCase): diff --git a/test_bridge.py b/test_bridge.py index 4c4b5220d..6b49c0181 100644 --- a/test_bridge.py +++ b/test_bridge.py @@ -9,7 +9,11 @@ else: import unittest -from unittest.mock import patch +try: + from unittest.mock import patch +except ImportError: + from mock import patch + class BridgeTest(unittest.TestCase): diff --git a/test_hsm.py b/test_hsm.py index be468d8ef..3345aa73e 100644 --- a/test_hsm.py +++ b/test_hsm.py @@ -7,7 +7,10 @@ else: import unittest -from unittest.mock import patch +try: + from unittest.mock import patch +except ImportError: + from mock import patch class HsmMethodTest(unittest.TestCase): diff --git a/test_observer.py b/test_observer.py index 473603bd5..733ee6333 100644 --- a/test_observer.py +++ b/test_observer.py @@ -7,11 +7,14 @@ if sys.version_info < (2, 7): import unittest2 as unittest - else: import unittest -from unittest.mock import patch +try: + from unittest.mock import patch +except ImportError: + from mock import patch + class TestSubject(unittest.TestCase): diff --git a/test_publish_subscribe.py b/test_publish_subscribe.py index 4d154a157..7edd2896b 100644 --- a/test_publish_subscribe.py +++ b/test_publish_subscribe.py @@ -6,11 +6,14 @@ if version_info < (2, 7): import unittest2 as unittest - else: import unittest -from unittest.mock import patch, call +try: + from unittest.mock import patch, call +except ImportError: + from mock import patch, call + class TestProvider(unittest.TestCase): """ From 74fc6931a549c865b84799398b47124abc2084c6 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Tue, 17 May 2016 11:36:05 -0400 Subject: [PATCH 04/20] BF: test_strategy -- use OS specific os.linesep + shebang --- test_strategy.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/test_strategy.py b/test_strategy.py index a5f3ee3ee..0116dc705 100644 --- a/test_strategy.py +++ b/test_strategy.py @@ -1,14 +1,18 @@ +#!/usr/bin/env python """ Tests for strategy.py """ -import subprocess, sys +import os +import subprocess +import sys if sys.version_info < (2, 7): import unittest2 as unittest else: import unittest + class StrategyTest(unittest.TestCase): def test_print_output(self): @@ -18,9 +22,12 @@ def test_print_output(self): line when running 'python strategy.py'. """ output = subprocess.check_output(["python", "strategy.py"]) - expected_output = 'Strategy Example 0\r\n\ -Strategy Example 1 from execute 1\r\n\ -Strategy Example 2 from execute 2\r\n' + expected_output = os.linesep.join([ + 'Strategy Example 0', + 'Strategy Example 1 from execute 1', + 'Strategy Example 2 from execute 2', + '' + ]) # byte representation required due to EOF returned subprocess expected_output_as_bytes = expected_output.encode(encoding='UTF-8') self.assertEqual(output, expected_output_as_bytes) From ebbacb4cc7ddb6ab6e15372f59d18635219d830b Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Tue, 17 May 2016 11:58:08 -0400 Subject: [PATCH 05/20] ENH: have shebangs consistently in every file with __name__ == "__main__" --- decorator.py | 1 + state.py | 1 + strategy.py | 1 + test_adapter.py | 1 + test_borg.py | 1 + test_command.py | 1 + test_hsm.py | 3 ++- test_state.py | 1 + 8 files changed, 9 insertions(+), 1 deletion(-) diff --git a/decorator.py b/decorator.py index c90749875..e0a6fa46c 100644 --- a/decorator.py +++ b/decorator.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python """https://docs.python.org/2/library/functools.html#functools.wraps""" """https://stackoverflow.com/questions/739654/how-can-i-make-a-chain-of-function-decorators-in-python/739665#739665""" diff --git a/state.py b/state.py index 45cc30a3b..ef7bfb560 100644 --- a/state.py +++ b/state.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python """Implementation of the state pattern""" # http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/ diff --git a/strategy.py b/strategy.py index 8c7a9b5a4..b7808ceac 100644 --- a/strategy.py +++ b/strategy.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python # http://stackoverflow.com/questions/963965/how-is-this-strategy-pattern # -written-in-python-the-sample-in-wikipedia """ diff --git a/test_adapter.py b/test_adapter.py index 2af0926cf..1961127d0 100644 --- a/test_adapter.py +++ b/test_adapter.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python from adapter import Dog, Cat, Human, Car, Adapter import sys diff --git a/test_borg.py b/test_borg.py index c08fd19d5..97470eaf9 100644 --- a/test_borg.py +++ b/test_borg.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python from borg import Borg, YourBorg import sys diff --git a/test_command.py b/test_command.py index d3b42f6c1..584b273b5 100644 --- a/test_command.py +++ b/test_command.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python from command import MoveFileCommand import os, shutil, subprocess, sys diff --git a/test_hsm.py b/test_hsm.py index 3345aa73e..ac9a14b86 100644 --- a/test_hsm.py +++ b/test_hsm.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python from hsm import HierachicalStateMachine, UnsupportedMessageType,\ UnsupportedState, UnsupportedTransition, Active, Standby, Suspect, Failed from sys import version_info @@ -91,4 +92,4 @@ def test_given_standby_on_message_operator_inservice_shall_raise_exception_and_k if __name__ == "__main__": - unittest.main() + unittest.main() \ No newline at end of file diff --git a/test_state.py b/test_state.py index 079602b0e..e96f8fbed 100644 --- a/test_state.py +++ b/test_state.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python from state import Radio import sys From a0d3711b08c3fb298269da65a87e467a98626de7 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Tue, 17 May 2016 12:02:36 -0400 Subject: [PATCH 06/20] BF: fixing docstring to pass doctest in adapter.py and import print_function from future for chaining_method.py --- adapter.py | 2 ++ chaining_method.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/adapter.py b/adapter.py index 25cbec973..b0bc1f238 100644 --- a/adapter.py +++ b/adapter.py @@ -46,8 +46,10 @@ class Adapter(object): >>> objects = [] >>> dog = Dog() >>> print(dog.__dict__) + {'name': 'Dog'} >>> objects.append(Adapter(dog, make_noise=dog.bark)) >>> print(objects[0].original_dict()) + {'name': 'Dog'} >>> cat = Cat() >>> objects.append(Adapter(cat, make_noise=cat.meow)) >>> human = Human() diff --git a/chaining_method.py b/chaining_method.py index e03747618..de9c115d1 100644 --- a/chaining_method.py +++ b/chaining_method.py @@ -1,6 +1,8 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +from __future__ import print_function + class Person(object): def __init__(self, name, action): From 29dc431e9a2306d6d72e44ae6bcd8a1d787bae8e Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Tue, 17 May 2016 12:04:45 -0400 Subject: [PATCH 07/20] rudimentary .travis.yml which also ignores flake8 failures for now --- .travis.yml | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..1d0f5a5d3 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,29 @@ +# vim ft=yaml +# travis-ci.org definition for python-patterns build +language: python + +sudo: false + +python: + - "2.7" + - "3.3" + - "3.4" + - "3.5" + - "pypy" + - "pypy3" + +cache: + - pip + +install: + - travis_retry pip install -q coveralls codecov + - pip install flake8 # eventually worth + +script: + - PYTHONPATH=. nosetests -s -v --with-doctest --with-cov --cover-package . --logging-level=INFO -v . + # for now failure in flaking is ignored + - flake8 *py || echo "PEP8 the code" + +after_success: + - coveralls + - codecov From ef3dbe3b17b10f9926607f98fa10b1913c61476f Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Tue, 17 May 2016 12:05:58 -0400 Subject: [PATCH 08/20] for uniformity -- no exec bit on delegation_pattern.py --- delegation_pattern.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 delegation_pattern.py diff --git a/delegation_pattern.py b/delegation_pattern.py old mode 100755 new mode 100644 From 8cc592483c7db5f276cb26943a6616efa92c0c95 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Tue, 17 May 2016 14:19:11 -0400 Subject: [PATCH 09/20] BF: minor typo unit[t]est --- test_strategy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_strategy.py b/test_strategy.py index 0116dc705..22bfe43d7 100644 --- a/test_strategy.py +++ b/test_strategy.py @@ -33,4 +33,4 @@ def test_print_output(self): self.assertEqual(output, expected_output_as_bytes) if __name__ == "__main__": - unitest.main() + unittest.main() From c5ce565ff75f6620d9b6f7352d8d1cefa85390c0 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Tue, 17 May 2016 14:19:31 -0400 Subject: [PATCH 10/20] BF/ENH: assure that foo.txt exists and no conflicts for demonstration --- command.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/command.py b/command.py index 310432cdb..27c255558 100644 --- a/command.py +++ b/command.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- import os - +from os.path import lexists class MoveFileCommand(object): @@ -28,13 +28,23 @@ def main(): command_stack.append(MoveFileCommand('foo.txt', 'bar.txt')) command_stack.append(MoveFileCommand('bar.txt', 'baz.txt')) - # they can be executed later on - for cmd in command_stack: - cmd.execute() - - # and can also be undone at will - for cmd in reversed(command_stack): - cmd.undo() + # verify that none of the target files exist + assert(not lexists("foo.txt")) + assert(not lexists("bar.txt")) + assert(not lexists("baz.txt")) + try: + with open("foo.txt", "w"): # Creating the file + pass + + # they can be executed later on + for cmd in command_stack: + cmd.execute() + + # and can also be undone at will + for cmd in reversed(command_stack): + cmd.undo() + finally: + os.unlink("foo.txt") if __name__ == "__main__": main() From b36e630b12b81fb4c4470bdc58bb56c1c55fdeb5 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Tue, 17 May 2016 14:19:38 -0400 Subject: [PATCH 11/20] BF: descriptors must be bound to class, not instance -- Revert "Adjustment to UI and Business logic for injecting dependencies" This reverts commit 69b507b87edd49896ff8877f15010e4f8d30d9e9. --- 3-tier.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/3-tier.py b/3-tier.py index 8b52216aa..625870a33 100644 --- a/3-tier.py +++ b/3-tier.py @@ -19,8 +19,7 @@ def __get__(self, obj, klas): class BusinessLogic(object): """ Business logic holding data store instances """ - def __init__(self, data): - self.data = data + data = Data() def product_list(self): return self.data['products'].keys() @@ -32,8 +31,8 @@ def product_information(self, product): class Ui(object): """ UI interaction class """ - def __init__(self, logic): - self.business_logic = logic + def __init__(self): + self.business_logic = BusinessLogic() def get_product_list(self): print('PRODUCT LIST:') @@ -54,9 +53,7 @@ def get_product_information(self, product): def main(): - data = Data() - logic = BusinessLogic(data) - ui = Ui(logic) + ui = Ui() ui.get_product_list() ui.get_product_information('cheese') ui.get_product_information('eggs') @@ -72,7 +69,7 @@ def main(): # cheese # eggs # milk -# +# # (Fetching from Data Store) # PRODUCT INFORMATION: # Name: Cheese, Price: 2.00, Quantity: 10 From 187c3bc156f3db9d251754eb5189b93f15b8f997 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Tue, 17 May 2016 14:24:58 -0400 Subject: [PATCH 12/20] ENH: reduce sleep to 0.1 for consistency --- facade.py | 2 +- proxy.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/facade.py b/facade.py index a92e4d25b..b314b1b3c 100644 --- a/facade.py +++ b/facade.py @@ -3,7 +3,7 @@ import time -SLEEP = 0.5 +SLEEP = 0.1 # Complex Parts diff --git a/proxy.py b/proxy.py index 4934b0bc9..9aea8a8b4 100644 --- a/proxy.py +++ b/proxy.py @@ -18,17 +18,17 @@ def talk(self): print("Proxy checking for Sales Manager availability") if self.busy == 'No': self.sales = SalesManager() - time.sleep(2) + time.sleep(0.1) self.sales.talk() else: - time.sleep(2) + time.sleep(0.1) print("Sales Manager is busy") class NoTalkProxy(Proxy): def talk(self): print("Proxy checking for Sales Manager availability") - time.sleep(2) + time.sleep(0.1) print("This Sales Manager will not talk to you whether he/she is busy or not") From 6b2d7d163c4b5b2d17bcec0f3a4c96bf3a627749 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Tue, 17 May 2016 14:25:43 -0400 Subject: [PATCH 13/20] ENH(TST): actually run all the scripts while testing on travis --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 1d0f5a5d3..38b011451 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,7 +20,10 @@ install: - pip install flake8 # eventually worth script: + # Run tests - PYTHONPATH=. nosetests -s -v --with-doctest --with-cov --cover-package . --logging-level=INFO -v . + # Actually run all the scripts, contributing to coverage + - bash -c 'failed=""; for f in [^_]*py; do python `which coverage` run -a $f || failed+=" $f"; echo "I: done $f. Exit code $?"; done; [ -z "$failed" ] || { echo "Failed: $failed"; exit 1; }' # for now failure in flaking is ignored - flake8 *py || echo "PEP8 the code" From 63b9759ca7ac0fde2a3c4171fb251df5d41d2ddf Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Tue, 17 May 2016 14:36:04 -0400 Subject: [PATCH 14/20] BF: moved running all the scripts via coverage into a separate helper tool --- .travis.yml | 2 +- run_all.sh | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100755 run_all.sh diff --git a/.travis.yml b/.travis.yml index 38b011451..cd122c489 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,7 +23,7 @@ script: # Run tests - PYTHONPATH=. nosetests -s -v --with-doctest --with-cov --cover-package . --logging-level=INFO -v . # Actually run all the scripts, contributing to coverage - - bash -c 'failed=""; for f in [^_]*py; do python `which coverage` run -a $f || failed+=" $f"; echo "I: done $f. Exit code $?"; done; [ -z "$failed" ] || { echo "Failed: $failed"; exit 1; }' + - ./run_all.sh # for now failure in flaking is ignored - flake8 *py || echo "PEP8 the code" diff --git a/run_all.sh b/run_all.sh new file mode 100755 index 000000000..1a90150dc --- /dev/null +++ b/run_all.sh @@ -0,0 +1,22 @@ +#!/bin/bash +# +# Little helper to run all the scripts, under python coverage if coverage is available +# + +set -eu +failed="" + +if which coverage > /dev/null; then + COVERAGE="`which coverage` run -a" +else + COVERAGE='' +fi +for f in [^_]*py; do + python $COVERAGE $f || failed+=" $f" + echo "I: done $f. Exit code $?" +done; + +if [ ! -z "$failed" ]; then + echo "Failed: $failed"; + exit 1 +fi \ No newline at end of file From 1e82c2d966055434bea086faf01c007a04709d2a Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Tue, 17 May 2016 14:57:05 -0400 Subject: [PATCH 15/20] BF(PY3): use next() instead of .next() (PY3 only) --- chain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chain.py b/chain.py index 4e087ec66..c037a4552 100644 --- a/chain.py +++ b/chain.py @@ -57,7 +57,7 @@ def delegate(self, requests): def coroutine(func): def start(*args, **kwargs): cr = func(*args, **kwargs) - cr.next() + next(cr) return cr return start From 9a654014b539afe727bde93c6eb475a199d2d1c0 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Tue, 17 May 2016 15:00:38 -0400 Subject: [PATCH 16/20] TEMP workaround: flyweight is not compatible with Python3 --- flyweight.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/flyweight.py b/flyweight.py index 557f12720..0e7d71e81 100644 --- a/flyweight.py +++ b/flyweight.py @@ -74,6 +74,11 @@ def __init__(self, *args, **kwargs): if __name__ == '__main__': + import sys + if sys.version_info[0] > 2: + sys.stderr.write("!!! This example is compatible only with Python 2 ATM !!!\n") + raise SystemExit(0) + # comment __new__ and uncomment __init__ to see the difference c1 = Card('9', 'h') c2 = Card('9', 'h') From e2a12b46c5fd02fa115188428f9d1455c70d9753 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Tue, 17 May 2016 15:01:22 -0400 Subject: [PATCH 17/20] BF(TEMP): Disabled pypy for now -- gets stuck --- .travis.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index cd122c489..3e4d7015c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,8 +9,9 @@ python: - "3.3" - "3.4" - "3.5" - - "pypy" - - "pypy3" +# Disabled for now since cause more pain than gain +# - "pypy" +# - "pypy3" cache: - pip From 8edfdcdcd43c28e78b50c5ab3c762186a27b28d5 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Tue, 17 May 2016 15:25:15 -0400 Subject: [PATCH 18/20] ENH: pragma: no cover for conditioning of mock imports --- test_bridge.py | 2 +- test_flyweight.py | 2 +- test_hsm.py | 2 +- test_publish_subscribe.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test_bridge.py b/test_bridge.py index 6b49c0181..c4db5e8c8 100644 --- a/test_bridge.py +++ b/test_bridge.py @@ -4,7 +4,7 @@ from bridge import DrawingAPI1, DrawingAPI2, CircleShape from sys import version_info -if version_info < (2, 7): +if version_info < (2, 7): # pragma: no cover import unittest2 as unittest else: import unittest diff --git a/test_flyweight.py b/test_flyweight.py index 5cb62f143..11017ad58 100644 --- a/test_flyweight.py +++ b/test_flyweight.py @@ -4,7 +4,7 @@ from flyweight import Card from sys import version_info -if version_info < (2, 7): +if version_info < (2, 7): # pragma: no cover import unittest2 as unittest else: import unittest diff --git a/test_hsm.py b/test_hsm.py index ac9a14b86..1712ff2d1 100644 --- a/test_hsm.py +++ b/test_hsm.py @@ -3,7 +3,7 @@ UnsupportedState, UnsupportedTransition, Active, Standby, Suspect, Failed from sys import version_info -if version_info < (2, 7): +if version_info < (2, 7): # pragma: no cover import unittest2 as unittest else: import unittest diff --git a/test_publish_subscribe.py b/test_publish_subscribe.py index 7edd2896b..962a12990 100644 --- a/test_publish_subscribe.py +++ b/test_publish_subscribe.py @@ -4,7 +4,7 @@ from sys import version_info from publish_subscribe import Provider, Publisher, Subscriber -if version_info < (2, 7): +if version_info < (2, 7): # pragma: no cover import unittest2 as unittest else: import unittest From 2893a039fb0fca8823a2c2160c181b42ed6f59df Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Tue, 17 May 2016 15:26:12 -0400 Subject: [PATCH 19/20] BF: dedent few assertions out of with assertRaises cm --- test_hsm.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test_hsm.py b/test_hsm.py index 1712ff2d1..79e38440e 100644 --- a/test_hsm.py +++ b/test_hsm.py @@ -78,17 +78,17 @@ def test_given_standby_on_message_fault_trigger_shall_set_suspect(cls): def test_given_standby_on_message_diagnostics_failed_shall_raise_exception_and_keep_in_state(cls): with cls.assertRaises(UnsupportedTransition) as context: cls.hsm.on_message('diagnostics failed') - cls.assertEqual(isinstance(cls.hsm._current_state, Standby), True) + cls.assertEqual(isinstance(cls.hsm._current_state, Standby), True) def test_given_standby_on_message_diagnostics_passed_shall_raise_exception_and_keep_in_state(cls): with cls.assertRaises(UnsupportedTransition) as context: cls.hsm.on_message('diagnostics passed') - cls.assertEqual(isinstance(cls.hsm._current_state, Standby), True) + cls.assertEqual(isinstance(cls.hsm._current_state, Standby), True) def test_given_standby_on_message_operator_inservice_shall_raise_exception_and_keep_in_state(cls): with cls.assertRaises(UnsupportedTransition) as context: cls.hsm.on_message('operator inservice') - cls.assertEqual(isinstance(cls.hsm._current_state, Standby), True) + cls.assertEqual(isinstance(cls.hsm._current_state, Standby), True) if __name__ == "__main__": From 126bbd93ce351a8ef91dd99f524a726896d5e894 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Tue, 17 May 2016 16:48:19 -0400 Subject: [PATCH 20/20] BF(TST): fixing test_proxy for recent decrease of sleep, but also py version specific import of StringIO (breaks on 2.x with io.StringIO) --- test_proxy.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) mode change 100644 => 100755 test_proxy.py diff --git a/test_proxy.py b/test_proxy.py old mode 100644 new mode 100755 index fdf9188ab..839b4130b --- a/test_proxy.py +++ b/test_proxy.py @@ -2,10 +2,14 @@ # -*- coding: utf-8 -*- from proxy import Proxy, NoTalkProxy -from io import StringIO import sys from time import time +if sys.version_info[0] == 2: + from StringIO import StringIO +else: + from io import StringIO + if sys.version_info < (2, 7): import unittest2 as unittest else: @@ -40,8 +44,8 @@ def test_sales_manager_shall_talk_through_proxy_with_delay(cls): expected_print_output = 'Proxy checking for Sales Manager availability\n\ Sales Manager ready to talk\n' cls.assertEqual(print_output, expected_print_output) - expected_execution_time = 2 - cls.assertEqual(int(execution_time), expected_execution_time) + expected_execution_time = 1 + cls.assertEqual(int(execution_time*10), expected_execution_time) def test_sales_manager_shall_respond_through_proxy_with_delay(cls): cls.p.busy = 'Yes' @@ -53,8 +57,8 @@ def test_sales_manager_shall_respond_through_proxy_with_delay(cls): expected_print_output = 'Proxy checking for Sales Manager availability\n\ Sales Manager is busy\n' cls.assertEqual(print_output, expected_print_output) - expected_execution_time = 2 - cls.assertEqual(int(execution_time), expected_execution_time) + expected_execution_time = 1 + cls.assertEqual(int(execution_time*10), expected_execution_time) class NoTalkProxyTest(unittest.TestCase): @@ -85,8 +89,8 @@ def test_sales_manager_shall_not_talk_through_proxy_with_delay(cls): expected_print_output = 'Proxy checking for Sales Manager availability\n\ This Sales Manager will not talk to you whether he/she is busy or not\n' cls.assertEqual(print_output, expected_print_output) - expected_execution_time = 2 - cls.assertEqual(int(execution_time), expected_execution_time) + expected_execution_time = 1 + cls.assertEqual(int(execution_time*10), expected_execution_time) def test_sales_manager_shall_not_respond_through_proxy_with_delay(cls): cls.ntp.busy = 'Yes' @@ -98,8 +102,8 @@ def test_sales_manager_shall_not_respond_through_proxy_with_delay(cls): expected_print_output = 'Proxy checking for Sales Manager availability\n\ This Sales Manager will not talk to you whether he/she is busy or not\n' cls.assertEqual(print_output, expected_print_output) - expected_execution_time = 2 - cls.assertEqual(int(execution_time), expected_execution_time) + expected_execution_time = 1 + cls.assertEqual(int(execution_time*10), expected_execution_time) if __name__ == "__main__": unittest.main()