Skip to content

Commit

Permalink
Merge pull request #364 from projectsyn/bastjan-patch-1
Browse files Browse the repository at this point in the history
Add golden and matrix tests to component template
  • Loading branch information
simu authored Nov 8, 2021
2 parents fdd377e + b58562d commit 0ca26e3
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 21 deletions.
25 changes: 24 additions & 1 deletion commodore/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,32 @@ def component(config: Config, verbose):
show_default=True,
help="The copyright holder added to the license file.",
)
@click.option(
"--golden-tests/--no-golden-tests",
default=True,
show_default=True,
help="Add golden tests to the component.",
)
@click.option(
"--matrix-tests/--no-matrix-tests",
default=True,
show_default=True,
help="Enable test matrix for compile/golden tests.",
)
@verbosity
@pass_config
# pylint: disable=too-many-arguments
def component_new(
config: Config, slug, name, lib, pp, owner, copyright_holder, verbose
config: Config,
slug,
name,
lib,
pp,
owner,
copyright_holder,
golden_tests,
matrix_tests,
verbose,
):
config.update_verbosity(verbose)
f = ComponentTemplater(config, slug)
Expand All @@ -260,6 +281,8 @@ def component_new(
f.post_process = pp
f.github_owner = owner
f.copyright_holder = copyright_holder
f.golden_tests = golden_tests
f.matrix_tests = matrix_tests
f.create()


Expand Down
2 changes: 2 additions & 0 deletions commodore/component-template/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

"add_lib": "n",
"add_pp": "n",
"add_golden": "y",
"add_matrix": "y",

"copyright_holder": "VSHN AG <[email protected]>",
"copyright_year": "1950",
Expand Down
4 changes: 4 additions & 0 deletions commodore/component-template/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import shutil

create_lib = "{{ cookiecutter.add_lib }}" == "y"
add_golden = "{{ cookiecutter.add_golden }}" == "y"

if not create_lib:
shutil.rmtree("lib")

if not add_golden:
shutil.rmtree("tests/golden")
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ insert_final_newline = false

[Makefile]
indent_style = tab

# Don't check for trailing newlines in golden tests output
[tests/golden/**]
insert_final_newline = unset
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
branches:
- master

env:
COMPONENT_NAME: {{ cookiecutter.slug }}

jobs:
linting:
runs-on: ubuntu-latest
Expand All @@ -24,3 +27,47 @@ jobs:
- uses: snow-actions/[email protected]
with:
args: 'check'
test:
runs-on: ubuntu-latest
{%- if cookiecutter.add_matrix == "y" %}
strategy:
matrix:
instance:
- defaults
{%- endif %}
defaults:
run:
working-directory: {% raw %}${{ env.COMPONENT_NAME }}{% endraw %}
steps:
- uses: actions/checkout@v2
with:
path: {% raw %}${{ env.COMPONENT_NAME }}{% endraw %}
- name: Compile component
{%- if cookiecutter.add_matrix == "y" %}
run: make test -e instance={% raw %}${{ matrix.instance }}{% endraw %}
{%- else %}
run: make test
{%- endif %}
{%- if cookiecutter.add_golden == "y" %}
golden:
runs-on: ubuntu-latest
{%- if cookiecutter.add_matrix == "y" %}
strategy:
matrix:
instance:
- defaults
{%- endif %}
defaults:
run:
working-directory: {% raw %}${{ env.COMPONENT_NAME }}{% endraw %}
steps:
- uses: actions/checkout@v2
with:
path: {% raw %}${{ env.COMPONENT_NAME }}{% endraw %}
- name: Golden diff
{%- if cookiecutter.add_matrix == "y" %}
run: make golden-diff -e instance={% raw %}${{ matrix.instance }}{% endraw %}
{%- else %}
run: make golden-diff
{%- endif %}
{%- endif %}
11 changes: 9 additions & 2 deletions commodore/component-template/{{ cookiecutter.slug }}/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
_archive/
_public/
# Commodore
.cache/
helmcharts/
manifests/
vendor/
jsonnetfile.lock.json
crds/
compiled/

# Antora
_archive/
_public/

# Additional entries
13 changes: 13 additions & 0 deletions commodore/component-template/{{ cookiecutter.slug }}/.sync.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
:global:
componentName: {{ cookiecutter.name }}
githubUrl: {{ cookiecutter.github_url }}
feature_goldenTests: true

docs/antora.yml:
name: {{ cookiecutter.slug }}
title: {{ cookiecutter.name }}
{% if cookiecutter.add_matrix == "y" -%}

.github/workflows/test.yaml:
test_makeTarget: test -e instance={% raw %}${{ matrix.instance }}{% endraw %}
{%- if cookiecutter.add_golden == "y" %}
goldenTest_makeTarget: golden-diff -e instance={% raw %}${{ matrix.instance }}{% endraw %}
{%- endif %}
matrix:
key: instance
entries:
- defaults
{% endif -%}
14 changes: 14 additions & 0 deletions commodore/component-template/{{ cookiecutter.slug }}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ docs-serve: ## Preview the documentation
test: commodore_args += -f tests/$(instance).yml
test: .compile ## Compile the component

{%- if cookiecutter.add_golden == "y" %}
.PHONY: gen-golden
gen-golden: commodore_args += -f tests/$(instance).yml
gen-golden: .compile ## Update the reference version for target `golden-diff`.
@rm -rf tests/golden/$(instance)
@mkdir -p tests/golden/$(instance)
@cp -R compiled/. tests/golden/$(instance)/.

.PHONY: golden-diff
golden-diff: commodore_args += -f tests/$(instance).yml
golden-diff: .compile ## Diff compile output against the reference version. Review output and run `make gen-golden golden-diff` if this target fails.
@git diff --exit-code --minimal --no-index -- tests/golden/$(instance) compiled/
{%- endif %}

.PHONY: clean
clean: ## Clean the project
rm -rf compiled dependencies vendor helmcharts jsonnetfile*.json || true
4 changes: 4 additions & 0 deletions commodore/component/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class ComponentTemplater:
github_owner: str
copyright_holder: str
today: datetime.date
golden_tests: bool
matrix_tests: bool

def __init__(self, config, slug):
self.config = config
Expand Down Expand Up @@ -59,6 +61,8 @@ def cookiecutter_args(self):
return {
"add_lib": "y" if self.library else "n",
"add_pp": "y" if self.post_process else "n",
"add_golden": "y" if self.golden_tests else "n",
"add_matrix": "y" if self.matrix_tests else "n",
"copyright_holder": self.copyright_holder,
"copyright_year": self.today.strftime("%Y"),
"github_owner": self.github_owner,
Expand Down
6 changes: 6 additions & 0 deletions docs/modules/ROOT/pages/reference/cli.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,11 @@ This command doesn't have any command line options.
*--copyright* TEXT::
The copyright holder added to the license file. Defaults to "VSHN AG <[email protected]>."

*--golden-tests / --no-golden-tests*::
Enable golden tests for the component. Defaults to _yes_.

*--matrix-tests / --no-matrix-tests*::
Enable test matrix for the component compile and golden tests. Defaults to _yes_.

*--help*::
Show component new usage and options then exit.
Loading

0 comments on commit 0ca26e3

Please sign in to comment.