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(mark2confluence): Skip files beginning with Space header when injecting default parents #19

Merged
merged 5 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
17 changes: 10 additions & 7 deletions mark2confluence/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,18 @@ def publish(path: str)-> tuple:
return True, None


def has_mark_headers(path: str) -> bool:
space_re = re.compile("<!--.?(space|parent|title):.*-->", re.IGNORECASE)
def begins_with_mark_headers(path: str, headers: List[str] = ["Space", "Parent", "Title"]) -> bool:
with open(path, 'r+') as f:
data = f.read().split("\n")
for line in data:
if space_re.match(line):
first_row = f.readline()
for header in headers:
regex = re.compile(f"^<!--.?{header}:.*-->", re.IGNORECASE)
if regex.match(first_row):
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 Expand Up @@ -270,7 +273,7 @@ def main()->int:
default_parents = get_default_parents(cfg.inputs.DEFAULT_PARENTS)
status = {}
for path in files:
if path[-3:] == '.md' and has_mark_headers(path):
if path[-3:] == '.md' and begins_with_mark_headers(path):
logger.info(f"Processing file {path}")
inject_default_parents(path, default_parents)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# I HAVE MARK HEADERS IN THE CONTENT

```markdown
<!-- Space: Content -->
```
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 -->
31 changes: 20 additions & 11 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ def test_load_env_prefixes():
assert main.cfg.actions["FOO"] == "foo"
assert main.cfg.runner["FOO"] == "foo"

def test_has_mark_headers():
resource_directory = f"{RESOURCE_DIR}/markdown/test_has_mark_headers"
assert main.has_mark_headers(f"{resource_directory}/with_mark_space_header.md")
assert main.has_mark_headers(f"{resource_directory}/with_mark_parent_header.md")
assert main.has_mark_headers(f"{resource_directory}/with_mark_title_header.md")
assert not main.has_mark_headers(f"{resource_directory}/without_mark_headers.md")
def test_begins_with_mark_headers():
resource_directory = f"{RESOURCE_DIR}/markdown/test_begins_with_mark_headers"
assert main.begins_with_mark_headers(f"{resource_directory}/with_mark_space_header.md")
assert main.begins_with_mark_headers(f"{resource_directory}/with_mark_parent_header.md")
assert main.begins_with_mark_headers(f"{resource_directory}/with_mark_title_header.md")
assert not main.begins_with_mark_headers(f"{resource_directory}/without_mark_headers.md")
assert not main.begins_with_mark_headers(f"{resource_directory}/with_mark_headers_in_the_content.md")

def test_check_header_template():
assert main.check_header_template("Valid Jinja2 {{ var }}")
Expand Down Expand Up @@ -126,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
Loading