Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(aar_doc): handle tilde home-expansion #74

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions aar_doc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ def parse_config(
"""
# This will change the defaults when called with --help
# See: https://github.com/tiangolo/typer/issues/347
if config_file.exists():

config_file_full_path: pathlib.Path = config_file.expanduser()

if config_file_full_path.exists():
try:
with open(config_file, "r", encoding="utf-8") as f:
with open(config_file_full_path, "r", encoding="utf-8") as f:
content = yaml.safe_load(f)

ctx.default_map = ctx.default_map or {}
Expand Down Expand Up @@ -118,7 +121,7 @@ def write(ctx: typer.Context, content: str) -> None:
Writes a content string to the given file.
"""

output = ctx.obj["config"]["output_file"]
output = ctx.obj["config"]["output_file"].expanduser()

if "/" not in str(output):
output = ctx.obj["config"]["role_path"] / output
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/roles/home_dir_expand/.ansible-docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
output_template: |
<!-- BEGIN_ANSIBLE_DOCS -->

{{ role }} with a configuration file that lies in `~/.ansible-docs.yml` where `~ = role_path`.

<!-- END_ANSIBLE_DOCS -->
5 changes: 5 additions & 0 deletions tests/fixtures/roles/home_dir_expand/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- BEGIN_ANSIBLE_DOCS -->

home_dir_expand with a configuration file that lies in `~/.ansible-docs.yml` where `~ = role_path`.

<!-- END_ANSIBLE_DOCS -->
34 changes: 34 additions & 0 deletions tests/fixtures/roles/home_dir_expand/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
galaxy_info:
role_name: home_dir_expand
author: your name
description: Test role with a minimum amount of metadata
company: ansible-docs

license: MIT

min_ansible_version: "1.2"

platforms:
- name: Fedora
versions:
- all

argument_specs:
main:
short_description: The main entrypoint for the home_dir_expand role
description: |
A longer description of the entrypoint.

Reaching multiple lines.

This contains the various parameters one can give in argument_specs that do not get tested below.
author:
- Foo
- Bar
options:
name:
option-name: override
description: |
This one contains option-name, which should override
the name given for the option.
27 changes: 27 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,30 @@ def test_missing_doc_string(tmp_path):
== "Could not find <!-- BEGIN_ANSIBLE_DOCS --> in the output file\n"
)
assert result.exit_code == 1


def test_expand_home_path(tmp_path):
os.environ["HOME"] = str(ROLES_DIR / "home_dir_expand/")

role_path = pathlib.Path("~/")
readme_md = pathlib.Path("~/README.md").expanduser()
config_file = str(role_path / ".ansible-docs.yml")

output_dir = tmp_path
output_dir.mkdir(exist_ok=True)
output_file = output_dir / "README.md"

result = runner.invoke(
app,
[
"--config-file",
config_file,
"--output-file",
output_file,
os.environ["HOME"],
"markdown",
],
)
print(result.output)
assert result.exit_code == 0
assert filecmp.cmp(readme_md, output_file)