Skip to content

Commit

Permalink
Add first test battery (#5)
Browse files Browse the repository at this point in the history
* Add first test battery

* Clean github env vars

* Fix CMD in Dockerfile

* Remove useless import
  • Loading branch information
gmarraff authored Mar 7, 2022
1 parent 97ef866 commit 108a736
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.venv
publish.sh
main.sh
__pycache__
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ ENV PYTHONPATH /app
ENV DOC_PREFIX /github/workspace/
ENV LOGURU_FORMAT "<lvl>{level:7} {message}</lvl>"
ENTRYPOINT [ "python" ]
CMD ["/app/main.py"]
CMD ["/app/mark2confluence/main.py"]
3 changes: 2 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ tasks:
test:
desc: Run tests
cmds:
- echo "Nothing to do"
- task: setup
- source .venv/bin/activate && pytest -v .

clean:
desc: Cleanup
Expand Down
Empty file added mark2confluence/__init__.py
Empty file.
14 changes: 9 additions & 5 deletions main.py → mark2confluence/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,20 @@ def get_files_by_doc_dir_pattern() -> list():

return filtered_files

def main()->int:
global cfg
load_vars()

def check_header_template(header_template: str):
try:
tpl = jinja2.Template(cfg.inputs.HEADER_TEMPLATE)
return jinja2.Template(header_template)
except jinja2.exceptions.TemplateError as e:
logger.error(f"Setup error, HEADER_TEMPLATE: {e}")
exit(1)


def main()->int:
global cfg
load_vars()

tpl = check_header_template(cfg.inputs.HEADER_TEMPLATE)

files = []
if cfg.inputs.FILES:
files = list(map(
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Jinja2 >= "3.0.3"
pyaml >= "20.4.0"
pytest >= "7.0.1"
loguru >= "0.5.3"
supermutes >= "0.2.5"
Empty file added tests/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions tests/resources/markdown/with_mark_headers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!-- Space: Japan -->
<!-- Parent: Football -->
<!-- Parent: New Team -->
<!-- Title: I have Mark Lenders -->

And you don't.
1 change: 1 addition & 0 deletions tests/resources/markdown/without_mark_headers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# I DON'T HAVE MARK HEADERS
44 changes: 44 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os
import pytest

import mark2confluence.main as main

RESOURCE_DIR = "tests/resources"

def clean_github_environment_variables():
if(os.getenv("CI", False)):
for env_var in os.environ.keys():
if env_var.startswith("GITHUB"):
os.environ.pop(env_var)

def test_load_vars_without_env():
clean_github_environment_variables()
main.load_vars()
assert main.cfg.inputs == main.DEFAULT_INPUTS
assert main.cfg.github == main.DEFAULT_GITHUB

def test_load_env_prefixes():
prefixes = main.ENV_PREFIXES
os.environ[f"{prefixes['inputs']}DOC_DIR_PATTERN"] = "foo"
os.environ[f"{prefixes['github']}REPOSITORY"] = "foo"
os.environ[f"{prefixes['actions']}FOO"] = "foo"
os.environ[f"{prefixes['runner']}FOO"] = "foo"
main.load_vars()
assert main.cfg.inputs["DOC_DIR_PATTERN"] == "foo"
assert main.cfg.github["REPOSITORY"] == "foo"
assert main.cfg.actions["FOO"] == "foo"
assert main.cfg.runner["FOO"] == "foo"

def test_has_mark_headers():
assert main.has_mark_headers(f"{RESOURCE_DIR}/markdown/with_mark_headers.md")
assert not main.has_mark_headers(f"{RESOURCE_DIR}/markdown/without_mark_headers.md")


def test_check_header_template():
assert main.check_header_template("Valid Jinja2 {{ var }}")
with pytest.raises(SystemExit, match="1"):
main.check_header_template("{{")


def test_default_header_template():
assert main.check_header_template(main.DEFAULT_INPUTS["HEADER_TEMPLATE"])

0 comments on commit 108a736

Please sign in to comment.