Skip to content

Commit

Permalink
fix(mark2confluence): Skip files beginning with Space header when inj…
Browse files Browse the repository at this point in the history
…ecting default parents (#19)

* Ensure file BEGINS with mark headers

* ignore case

* Skip files with space header

* Ensure file BEGINS with mark headers

---------

Co-authored-by: Manuel Bovo <manuel.bovo@sysdig.com>
  • Loading branch information
gmarraff and mbovo committed Jan 31, 2024
1 parent 45d0dd1 commit 04c7b22
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 6 deletions.
5 changes: 4 additions & 1 deletion mark2confluence/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ def begins_with_mark_headers(path: str, headers: List[str] = ["Space", "Parent",
return True
return False

def begins_with_mark_space_header(path: str):
return begins_with_mark_headers(path, ["Space"])

class MultilineCommentIsOpenException(Exception):
pass

Expand Down Expand Up @@ -240,7 +243,7 @@ def get_default_parents(parents_string: str) -> List[ParentCfg]:
def inject_default_parents(path: str, default_parents_cfg: List[ParentCfg]):
file_dir = f"{os.path.dirname(os.path.abspath(path))}"
for parent_cfg in default_parents_cfg:
if parent_cfg.is_directory_included(file_dir):
if parent_cfg.is_directory_included(file_dir) and not begins_with_mark_space_header(path):
header = parent_cfg.get_header()
with open(path, 'r') as f:
file_content = f.read()
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<!-- Parent: BAZ -->
<!-- Title: BIM -->
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!-- Space: FOO -->
<!-- Parent: BAR -->
<!-- Parent: BAZ -->
<!-- Title: BIM -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- Space: BAR -->
<!-- Parent: BARED -->
<!-- Title: BORED -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- Space: BAR -->
<!-- Parent: BARED -->
<!-- Title: BORED -->
18 changes: 13 additions & 5 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,22 @@ def test_get_default_parents(string, expected_parents_count):
def test_ParentCfg_get_header(cfg: main.ParentCfg, expected_header):
assert cfg.get_header() == expected_header

def test_inject_default_parents(monkeypatch):
@pytest.mark.parametrize(
ids=("skip file with space mark header", "properly inject default headers"),
argnames="tested_file_name, expected_file_name",
argvalues=[
("0-input.md", "0-output.md"),
("1-input.md", "1-output.md"),
],
)
def test_inject_default_parents(monkeypatch, tested_file_name, expected_file_name):
monkeypatch.setattr('mark2confluence.main.cfg', dot.dotify({"github": {"WORKSPACE": WORKSPACE}}))

base_dir = f"{RESOURCE_DIR}/markdown/test_inject_default_parents"
source_file_path = f"{base_dir}/0-input.md"
expected_file_path = f"{base_dir}/0-output.md"
parsed_file_dir = f"{WORKSPACE}/tests/foo"
parsed_file_path = f"{parsed_file_dir}/parsed_file.md"
source_file_path = os.path.join(base_dir, tested_file_name)
expected_file_path = os.path.join(base_dir, expected_file_name)
parsed_file_dir = os.path.join(WORKSPACE, "tests", "foo")
parsed_file_path = os.path.join(parsed_file_dir, "parsed_file.md")
cfgs = [
main.ParentCfg(directory="tests/foo/bar", space="FOO", parents=["BAZ"]),
main.ParentCfg(directory="tests/foo/*", space="FOO", parents=["BAR"]),
Expand Down

0 comments on commit 04c7b22

Please sign in to comment.