Skip to content

Commit

Permalink
Add integration tests (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
NilashishC authored Jan 2, 2024
1 parent e070c6b commit 23ed05c
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 108 deletions.
1 change: 1 addition & 0 deletions .config/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
coverage
pytest
coverage[toml]
pytest-xdist
tox
1 change: 1 addition & 0 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ jobs:
- name: "tox -e ${{ matrix.passed_name }}"
continue-on-error: ${{ matrix.devel || false }}
run: python3 -m tox -e ${{ matrix.passed_name }}

- name: Archive logs
uses: actions/upload-artifact@v3
with:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,6 @@ $ tree -lla /home/ansible-dev/collections/ansible_collections

## Licensing

GNU General Public License v3.0 or later.
ansible-creator is released under the Apache License version 2.

See [LICENSE](https://www.gnu.org/licenses/gpl-3.0.txt) to see the full text.
See the [LICENSE](https://github.com/ansible/ansible-creator/blob/main/LICENSE) file for more details.
16 changes: 0 additions & 16 deletions src/ansible_creator/templar.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
except ImportError:
HAS_JINJA2 = False

from ansible_creator.utils import get_file_contents


class Templar:
"""Class representing a Jinja2 template engine."""
Expand All @@ -33,20 +31,6 @@ def __init__(self: Templar) -> None:
keep_trailing_newline=True,
)

def render(self: Templar, template_name: str, data: dict) -> str:
"""Load template from a file and render with provided data.
:param template_name: Name of the template to load.
:param data: Data to render template with.
:returns: Templated content.
"""
template_content = get_file_contents(
directory="templates",
filename=template_name,
)
return self.render_from_content(template=template_content, data=data)

def render_from_content(self: Templar, template: str, data: dict) -> str:
"""Render a template with provided data.
Expand Down
80 changes: 0 additions & 80 deletions src/ansible_creator/validators.py

This file was deleted.

32 changes: 32 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""conftest."""
import os
import subprocess
import pytest

os.environ["HOME"] = "/home/ansible"
os.environ["DEV_WORKSPACE"] = "collections/ansible_collections"


@pytest.fixture
def cli():
"""fixture to run CLI commands."""
return cli_run


def cli_run(args):
"""execute a command using subprocess."""
updated_env = os.environ.copy()
# this helps asserting stdout/stderr
updated_env.update({"LINES": "40", "COLUMNS": "300", "TERM": "xterm-256color"})
try:
result = subprocess.run(
args,
shell=True,
capture_output=True,
check=True,
text=True,
env=updated_env,
)
return result
except subprocess.CalledProcessError as err:
return err
File renamed without changes.
108 changes: 108 additions & 0 deletions tests/integration/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
"""Unit tests for ansible-creator init."""

from __future__ import annotations

import re
import sys
from textwrap import dedent


def test_run_help(cli):
result = cli("ansible-creator --help")
assert result.returncode == 0

# temporary assertion fix until we write custom helper
if sys.version_info < (3, 10):
assert (
dedent(
"""\
usage: ansible-creator [-h] [--version] {init} ...
Tool to scaffold Ansible Content. Get started by looking at the help text.
optional arguments:
-h, --help show this help message and exit
--version Print ansible-creator version and exit.
Commands:
{init} The subcommand to invoke.
init Initialize an Ansible Collection.
""",
)
in result.stdout
)
else:
assert (
dedent(
"""\
usage: ansible-creator [-h] [--version] {init} ...
Tool to scaffold Ansible Content. Get started by looking at the help text.
options:
-h, --help show this help message and exit
--version Print ansible-creator version and exit.
Commands:
{init} The subcommand to invoke.
init Initialize an Ansible Collection.
""",
)
in result.stdout
)


def test_run_no_subcommand(cli):
result = cli("ansible-creator")
assert result.returncode != 0
assert (
dedent(
"""\
usage: ansible-creator [-h] [--version] {init} ...
ansible-creator: error: the following arguments are required: subcommand
""",
)
in result.stderr
)


def test_run_init_no_input(cli):
result = cli("ansible-creator init")
assert result.returncode != 0
assert (
"ansible-creator init: error: the following arguments are required: collection"
in result.stderr
)


def test_run_init_basic(cli, tmp_path):
result = cli(f"ansible-creator init testorg.testcol --init-path {tmp_path}")
assert result.returncode == 0

# check stdout
assert (
re.search("Note: collection testorg.testcol created at", result.stdout)
is not None
)

# fail to override existing collection with force=false (default)
result = cli(f"ansible-creator init testorg.testcol --init-path {tmp_path}")
assert result.returncode != 0
assert (
re.search(
rf"Error: The directory\s+{tmp_path}/testorg/testcol\s+already exists.",
result.stderr,
flags=re.MULTILINE,
)
is not None
)
assert "You can use --force to re-initialize this directory." in result.stderr
assert "However it will delete ALL existing contents in it." in result.stderr

# override existing collection with force=true
result = cli(f"ansible-creator init testorg.testcol --init-path {tmp_path} --force")
assert result.returncode == 0
assert (
re.search("Warning: re-initializing existing directory", result.stdout)
is not None
)
6 changes: 0 additions & 6 deletions tests/units/conftest.py

This file was deleted.

6 changes: 3 additions & 3 deletions tests/units/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


@pytest.fixture()
def cli_args(tmp_path: Path) -> dict:
def cli_args(tmp_path) -> dict:
"""Create an Init class object as fixture.
:param tmp_path: App configuration object.
Expand All @@ -38,7 +38,7 @@ def cli_args(tmp_path: Path) -> dict:


@pytest.fixture()
def output(tmp_path: Path) -> Output:
def output(tmp_path) -> Output:
"""Create an Output class object as fixture.
:param tmp_path: App configuration object.
Expand Down Expand Up @@ -69,7 +69,7 @@ def test_run_success(
result = capsys.readouterr().out

# check stdout
assert re.search("Note: collection testorg.testcol created at", result) is not None
assert re.search("Note: collection testorg.testcol created", result) is not None

# recursively assert files created
dircmp(str(tmp_path), str(FIXTURES_DIR / "collection")).report_full_closure()
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ commands_pre =
commands =
coverage run -m pytest {posargs:tests}
sh -c "coverage combine -a -q --data-file=.coverage {toxworkdir}/.coverage.*"
-sh -c "COVERAGE_FILE= coverage xml --ignore-errors -q --fail-under=0"
sh -c "COVERAGE_FILE= coverage xml --ignore-errors -q --fail-under=0"
sh -c "COVERAGE_FILE= coverage report"
allowlist_externals =
sh
Expand Down

0 comments on commit 23ed05c

Please sign in to comment.