From ac1baa5a12a3ba5efb15bfffa92972ffbabcf6f0 Mon Sep 17 00:00:00 2001 From: Marcus Ottosson Date: Thu, 4 Jun 2015 15:10:37 +0100 Subject: [PATCH 1/4] Exceptions thrown by `func` passed to pyblish.logic.process is yielded and are expected to be handled by the called. --- pyblish/logic.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/pyblish/logic.py b/pyblish/logic.py index ce23a623..afafe449 100644 --- a/pyblish/logic.py +++ b/pyblish/logic.py @@ -72,7 +72,7 @@ def process(func, plugins, context, test=None): A result per complete process. If test fails, a TestFailed exception is returned, containing the variables used in the test. Finally, any exception - thrown by `process` is yielded. Note that this is + thrown by `func` is yielded. Note that this is considered a bug in *your* code as you are the one supplying it. @@ -130,22 +130,20 @@ def gen(plugin, instances): continue for instance in gen(plugin, instances): - print plugin, instance if instance is None and "instance" in args: continue - print "Made it" # Provide introspection self.next_instance = instance try: result = func(plugin, context, instance) - except Exception: - # If this happens, there is a bug - traceback.print_exc() - trace = traceback.format_exc() - assert False, trace + except Exception as exc: + # Any exception occuring within the function + # you pass is yielded, you are expected to + # handle it. + yield exc else: # Make note of the order at which From 317082c618676cddc413b52a55c674ee2d5ed34c Mon Sep 17 00:00:00 2001 From: Marcus Ottosson Date: Thu, 4 Jun 2015 15:10:55 +0100 Subject: [PATCH 2/4] Adding test for asking for unavaialble service. --- tests/test_di.py | 57 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/tests/test_di.py b/tests/test_di.py index 4af6048d..a297cc31 100644 --- a/tests/test_di.py +++ b/tests/test_di.py @@ -186,7 +186,6 @@ def process(self, context): assert_equals(count["#"], 2) -@with_setup(lib.setup_empty, lib.teardown) def test_unavailable_service(): """Asking for unavailable service throws exception""" @@ -199,6 +198,21 @@ def func(arg1, arg2): assert_raises(KeyError, provider.invoke, func) +def test_unavailable_service_logic(): + """Asking for unavailable service ..?""" + + class SelectUnavailable(pyblish.api.Selector): + def process(self, unavailable): + print "HHOH" + self.log.critical("Test") + + for result in pyblish.logic.process( + func=pyblish.plugin.process, + plugins=[SelectUnavailable], + context=pyblish.api.Context()): + assert_true(isinstance(result["error"], KeyError)) + + @with_setup(lib.setup_empty, lib.teardown) def test_test_failure(): """Failing the test yields an exception""" @@ -226,8 +240,8 @@ def process(self, context): context=context)) assert_equals(len(triggered), 1) - assert type(triggered[0]) == ValidateFailure - assert isinstance(results[-1], Exception) + assert_equals(type(triggered[0]), ValidateFailure) + assert_true(isinstance(results[-1], Exception)) @with_setup(lib.setup_empty, lib.teardown) @@ -314,3 +328,40 @@ def process(self, asset): print result assert_equals(count["#"], 3) + + +@with_setup(lib.setup_empty, lib.teardown) +def test_di_testing(): + """DI simplifies testing""" + + count = {"#": 0, "instances": []} + + class SelectCharacters(pyblish.api.Validator): + def process(self, context, host): + for char in host.ls("*_char"): + instance = context.create_instance(char, family="character") + instance.add(host.listRelatives(char)) + count["#"] += 1 + count["instances"].append(instance.name) + + class HostMock(object): + def ls(self, query): + return ["bobby_char", "rocket_char"] + + def listRelatives(self, node): + if node == "bobby_char": + return ["arm", "leg"] + if node == "rocket_char": + return ["propeller"] + return [] + + pyblish.api.register_service("host", HostMock()) + + for result in pyblish.logic.process( + func=pyblish.plugin.process, + plugins=[SelectCharacters], + context=pyblish.api.Context()): + assert_equals(result["error"], None) + + assert_equals(count["#"], 2) + assert_equals(count["instances"], ["bobby_char", "rocket_char"]) From 4cd2ba3e17b17c63865aa1ec4c3d5c242f037d2a Mon Sep 17 00:00:00 2001 From: Marcus Ottosson Date: Thu, 4 Jun 2015 15:11:53 +0100 Subject: [PATCH 3/4] Adding test --- tests/test_logic.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/test_logic.py b/tests/test_logic.py index 710c42f2..93b8500b 100644 --- a/tests/test_logic.py +++ b/tests/test_logic.py @@ -159,15 +159,15 @@ def process(self, context): count["#"] += 1 context = pyblish.api.Context() - # for result in pyblish.logic.process( - # func=pyblish.plugin.process, - # plugins=[SelectMany, ValidateContext], - # context=context): - # pass + for result in pyblish.logic.process( + func=pyblish.plugin.process, + plugins=[SelectMany, ValidateContext], + context=context): + pass - # assert_equals(count["#"], 1) + assert_equals(count["#"], 1) - # count["#"] = 0 + count["#"] = 0 # When families are wildcard, it does process class ValidateContext(pyblish.api.Validator): From 3fc6ec29a7c54caa6c02b39e74287bfc9c57ba83 Mon Sep 17 00:00:00 2001 From: Marcus Ottosson Date: Thu, 4 Jun 2015 15:14:58 +0100 Subject: [PATCH 4/4] Simplifying test --- tests/test_di.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/test_di.py b/tests/test_di.py index a297cc31..a34a3b8a 100644 --- a/tests/test_di.py +++ b/tests/test_di.py @@ -334,15 +334,14 @@ def process(self, asset): def test_di_testing(): """DI simplifies testing""" - count = {"#": 0, "instances": []} + instances = list() class SelectCharacters(pyblish.api.Validator): def process(self, context, host): for char in host.ls("*_char"): instance = context.create_instance(char, family="character") instance.add(host.listRelatives(char)) - count["#"] += 1 - count["instances"].append(instance.name) + instances.append(instance.name) class HostMock(object): def ls(self, query): @@ -363,5 +362,5 @@ def listRelatives(self, node): context=pyblish.api.Context()): assert_equals(result["error"], None) - assert_equals(count["#"], 2) - assert_equals(count["instances"], ["bobby_char", "rocket_char"]) + assert_equals(len(instances), 2) + assert_equals(instances, ["bobby_char", "rocket_char"]) \ No newline at end of file