Repository: cekit/cekit

Issue:
expand `cekit test behave --steps-url` to additionally support selecting branch/ref
**Describe the solution you'd like**
`cekit test behave --steps-url` lets you specify  an alternative Git repository from which to fetch behave-test-steps, replacing https://github.com/cekit/behave-test-steps.git

This is useful when, for example, adding some new steps or debugging something in a clone of behave-test-steps

However there is no way to specify what ref to check out of the cloned git repo. I presume it goes with whatever the default branch is. But if I've forked behave-test-steps in order to raise a PR, I'm going to be using an alternative to the default branch name.

I can change my fork's default branch, but that doesn't scale past one PR at  time.

I suggest adding `--steps-ref` to let you specify a ref that cekit then creates a local branch targetting after cloning the repo, i.e.

`cekit test behave --steps-url some-uri --steps-ref some-ref` and cekit does something like `git clone some-uri --branch some-ref $wherever_cekit_clones_to` (note that I believe `--branch`, despite its name, can also reference tags)

Discussion:
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/cekit/cekit/pull/877?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cekit) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`376b663`)](https://app.codecov.io/gh/cekit/cekit/commit/376b6630407f72238a18102e35602121828ed9be?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cekit) 89.97% compared to head [(`cac7204`)](https://app.codecov.io/gh/cekit/cekit/pull/877?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cekit) 89.97%.

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff            @@
##           develop     #877   +/-   ##
========================================
  Coverage    89.97%   89.97%           
========================================
  Files           42       42           
  Lines         3232     3232           
========================================
  Hits          2908     2908           
  Misses         324      324           
```


| [Files](https://app.codecov.io/gh/cekit/cekit/pull/877?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cekit) | Coverage Δ | |
|---|---|---|
| [cekit/cli.py](https://app.codecov.io/gh/cekit/cekit/pull/877?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cekit#diff-Y2VraXQvY2xpLnB5) | `89.59% <100.00%> (+0.06%)` | :arrow_up: |
| [cekit/test/behave\_tester.py](https://app.codecov.io/gh/cekit/cekit/pull/877?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cekit#diff-Y2VraXQvdGVzdC9iZWhhdmVfdGVzdGVyLnB5) | `97.36% <ø> (-0.07%)` | :arrow_down: |
| [cekit/test/collector.py](https://app.codecov.io/gh/cekit/cekit/pull/877?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cekit#diff-Y2VraXQvdGVzdC9jb2xsZWN0b3IucHk=) | `92.85% <100.00%> (ø)` | |


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/cekit/cekit/pull/877?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cekit).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cekit).


Patch:
diff --git a/cekit/cli.py b/cekit/cli.py
index dcbd360..6a4cb1e 100644
--- a/cekit/cli.py
+++ b/cekit/cli.py
@@ -13,7 +13,7 @@ from cekit.config import Config, default_work_dir
 from cekit.errors import CekitError
 from cekit.log import setup_logging
 from cekit.tools import Map
-from cekit.version import __version__
+from cekit.version import __version__, schema_version
 
 if TYPE_CHECKING:
     from cekit.builder import Command
@@ -352,6 +352,12 @@ def test(image, overrides):
     default="https://github.com/cekit/behave-test-steps.git",
     show_default=True,
 )
+@click.option(
+    "--steps-ref",
+    help="Behave steps library reference (branch or tag)",
+    default="v%s" % schema_version,
+    show_default=True,
+)
 @click.option("--wip", help="Run test scenarios tagged with @wip only.", is_flag=True)
 @click.option("--include-re", help="Run only those features which match this regex")
 @click.option("--exclude-re", help="Do not run those features which match this regex")
@@ -362,7 +368,7 @@ def test(image, overrides):
     multiple=True,
 )
 @click.pass_context
-def test_behave(ctx, steps_url, wip, names, include_re, exclude_re):
+def test_behave(ctx, steps_url, steps_ref, wip, names, include_re, exclude_re):
     """
     DESCRIPTION
 


Test patch:
diff --git a/cekit/test/behave_tester.py b/cekit/test/behave_tester.py
index 6aee335..d411626 100644
--- a/cekit/test/behave_tester.py
+++ b/cekit/test/behave_tester.py
@@ -5,7 +5,6 @@ from cekit.builder import Command
 from cekit.generator.behave import BehaveGenerator
 from cekit.test.behave_runner import BehaveTestRunner
 from cekit.test.collector import BehaveTestCollector
-from cekit.version import schema_version
 
 LOGGER = logging.getLogger("cekit")
 
@@ -44,10 +43,8 @@ class BehaveTester(Command):
 
         self.generator.generate()
 
-        # The branch to clone is determined by the schema_version in the descriptor prefixed with `v`
-        # It defaults to the CEKIt current schema_version.
         self.collected = self.test_collector.collect(
-            self.generator.image.get("schema_version", schema_version),
+            self.params.steps_ref,
             self.params.steps_url,
         )
 
diff --git a/cekit/test/collector.py b/cekit/test/collector.py
index 2fd9f88..1d209a2 100644
--- a/cekit/test/collector.py
+++ b/cekit/test/collector.py
@@ -41,7 +41,7 @@ class BehaveTestCollector(object):
 
     def _fetch_steps(self, version, url):
         """Method fetches common steps"""
-        logger.info("Fetching common steps from '{}'.".format(url))
+        logger.info("Fetching common steps from '{}', ref '{}'.".format(url, version))
         cmd = [
             "git",
             "clone",
@@ -50,7 +50,7 @@ class BehaveTestCollector(object):
             url,
             self.test_dir,
             "-b",
-            "v%s" % version,
+            "%s" % version,
         ]
         run_wrapper(cmd, False, f"Could not fetch steps from {url}")
 
diff --git a/docs/handbook/testing/behave.rst b/docs/handbook/testing/behave.rst
index 85fa491..38bcf2e 100644
--- a/docs/handbook/testing/behave.rst
+++ b/docs/handbook/testing/behave.rst
@@ -122,6 +122,7 @@ Options
 
 * ``--wip`` -- Only run tests tagged with the ``@wip`` tag.
 * ``--steps-url`` -- A git repository url containing `steps <https://pythonhosted.org/behave/tutorial.html#python-step-implementations>`_ for tests.
+* ``--steps-ref`` -- A reference (branch or tag) in the steps repository to check out
 * ``--name`` -- *Scenario* name to be executed
 * ``--include-re`` -- Regex of feature files which will be executed only
 * ``--exclude-re`` -- Regex of feature files which will not be executed
diff --git a/tests/test_unit_args.py b/tests/test_unit_args.py
index 124e7f7..7de9ffd 100644
--- a/tests/test_unit_args.py
+++ b/tests/test_unit_args.py
@@ -286,6 +286,7 @@ def _get_class_by_name(clazz):
                 "overrides": (),
                 "image": "image:1.0",
                 "steps_url": "https://github.com/cekit/behave-test-steps.git",
+                "steps_ref": "v1",
                 "wip": False,
                 "include_re": None,
                 "exclude_re": None,