Skip to content

Commit

Permalink
Merge pull request #355 from comtihon/skipif_in_loop
Browse files Browse the repository at this point in the history
fixed skipif usage for the loop's substep
  • Loading branch information
comtihon committed Jun 27, 2023
2 parents e0305be + bc7666e commit 949451a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
2 changes: 1 addition & 1 deletion catcher/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
APPNAME = 'catcher'
APPAUTHOR = 'Valerii Tikhonov, Ekaterina Belova'
APPVSN = '1.36.2'
APPVSN = '1.36.3'
9 changes: 6 additions & 3 deletions catcher/steps/loop.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import collections
import itertools
import json
from collections.abc import Iterable

from catcher.utils import logger
from catcher.steps.check import Operator
from catcher.steps.step import Step, update_variables
from catcher.steps.step import Step, update_variables, SkipException
from catcher.utils.logger import debug
from catcher.utils.misc import fill_template_str, try_get_objects

Expand Down Expand Up @@ -163,7 +162,11 @@ def __run_actions(self, includes, variables: dict) -> dict:
output = variables
for action in self.do_action:
try:
action.check_skip(variables)
output = action.action(includes, output)
except SkipException: # skip this step
debug('SubStep ' + fill_template_str(action.name, variables) + logger.yellow(' skipped'))
return output
except Exception as e:
if action.ignore_errors:
debug('{} got {} but we ignore it'.format(fill_template_str(action.name, variables), e))
Expand Down
18 changes: 18 additions & 0 deletions docs/source/tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,24 @@ New in `1.17.0` - you can now use `Wait.for` instead::
In this case `other_steps` will be executed only when `select 1;` becomes true. Test will fail after 30 seconds,
if `select 1;` is still failing.

Ignore errors and loops::

---
- loop:
foreach:
in: '{{ list }}'
do:
- echo: {from: '{{ ITEM["value"] }}', to: '{{ ITEM["key"] }}_before.output'}
- check:
ignore_errors: true
equals: {the: False, is: True}
- echo: {from: '{{ ITEM["value"] }}', to: '{{ ITEM["key"] }}_after.output'}

| You can use `ignore_errors` on the substep level as well.
| In this case second loop's substep contains an error which is marked with `ignore_errors = true` This test will run the
first step of the loop for every element of the `{{ list }}` variable. Treat it as a `continue` keyword in other programming
languages.

Skip steps
----------
| You can skip your steps based on conditions.
Expand Down
51 changes: 51 additions & 0 deletions test/steps/loop_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,54 @@ def test_loop_with_include(self):
self.assertTrue(check_file(join(self.test_dir, 'a'), '1'))
self.assertTrue(check_file(join(self.test_dir, 'b'), '2'))
self.assertTrue(check_file(join(self.test_dir, 'c'), '3'))

def test_loop_skip_if(self):
self.populate_file('main.yaml', '''---
variables:
test_data: [ "NOT_PRESENT", "NOT_PRESENT", "PRESENT" ]
steps:
- echo:
name: 'echo test items'
from: 'Items are {{ test_data }}'
- loop:
foreach:
in: '{{ test_data }}'
do:
- echo:
name: 'echo test item'
from: 'Item is {{ ITEM }}'
register: {found: '{{ ITEM == "PRESENT" }}'}
- echo:
name: 'echo flag'
from: 'Found flag is {{ found }}'
- check:
name: 'truefalse test'
skip_if: '{{ not found }}'
equals: {the: '{{ found }}', is: True}
''')
runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None)
self.assertTrue(runner.run_tests())

def test_loop_ignore_errors(self):
self.populate_file('main.yaml', '''---
variables:
list: [{'key': 'a', 'value': 1}, {'key': 'b', 'value': 2}, {'key': 'c', 'value': 3}]
steps:
- loop:
foreach:
in: '{{ list }}'
do:
- echo: {from: '{{ ITEM["value"] }}', to: '{{ ITEM["key"] }}_before.output'}
- check:
ignore_errors: true
equals: {the: False, is: True}
- echo: {from: '{{ ITEM["value"] }}', to: '{{ ITEM["key"] }}_after.output'}
''')
runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None)
self.assertTrue(runner.run_tests())
self.assertTrue(check_file(join(self.test_dir, 'a_before.output'), '1'))
self.assertTrue(check_file(join(self.test_dir, 'b_before.output'), '2'))
self.assertTrue(check_file(join(self.test_dir, 'c_before.output'), '3'))
self.assertFalse(check_file(join(self.test_dir, 'a_after.output'), '1'))
self.assertFalse(check_file(join(self.test_dir, 'b_after.output'), '1'))
self.assertFalse(check_file(join(self.test_dir, 'c_after.output'), '1'))

0 comments on commit 949451a

Please sign in to comment.