Skip to content

Commit

Permalink
Merge pull request #452 from gitlabform/fix_451
Browse files Browse the repository at this point in the history
Don't strip trailing new lines from files (fix #451)
  • Loading branch information
gdubicki committed Nov 29, 2022
2 parents b0b63c5 + 4116051 commit c3195b4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 31 deletions.
12 changes: 10 additions & 2 deletions gitlabform/processors/project/files_processor.py
Expand Up @@ -104,7 +104,13 @@ def _process_configuration(self, project_and_group: str, configuration: dict):
)
new_content = effective_path.read_text()

if configuration.get("files|" + file + "|template", True):
# templating is documented to be enabled by default,
# see https://gitlabform.github.io/gitlabform/reference/files/#files
templating_enabled = True

if configuration.get(
"files|" + file + "|template", templating_enabled
):
new_content = self.get_file_content_as_template(
new_content,
project_and_group,
Expand Down Expand Up @@ -253,7 +259,9 @@ def just_modify_file(
def get_file_content_as_template(self, template, project_and_group, **kwargs):
# Use jinja with variables project and group
rtemplate = Environment(
loader=FileSystemLoader("."), autoescape=True
loader=FileSystemLoader("."),
autoescape=True,
keep_trailing_newline=True,
).from_string(template)
return rtemplate.render(
project=self.get_project(project_and_group),
Expand Down
5 changes: 3 additions & 2 deletions tests/acceptance/standard/test_files.py
@@ -1,5 +1,6 @@
import os
import pytest
import re

from gitlabform.gitlab import AccessLevel
from gitlabform.gitlab.core import NotFoundException
Expand Down Expand Up @@ -279,7 +280,7 @@ def test__set_file_with_chinese_characters(self, gitlab, group_and_project, bran
run_gitlabform(set_file_chinese_characters, group_and_project)

file_content = gitlab.get_file(group_and_project, branch, "README.md")
assert file_content == "Hanzi (Traditional): 丕不丈丁\nHanzi (Simplified): 丏丅丙两"
assert file_content == "Hanzi (Traditional): 丕不丈丁\nHanzi (Simplified): 丏丅丙两\n"

def test__set_external_file_with_chinese_characters(
self, gitlab, group_and_project, branch, file
Expand Down Expand Up @@ -317,4 +318,4 @@ def test__set_external_file_with_utf8_characters(
run_gitlabform(set_file_chinese_characters, group_and_project)

file_content = gitlab.get_file(group_and_project, branch, "README.md")
assert file_content == "LICENSE\n\n“This is a license file”"
assert file_content == "LICENSE\n\n“This is a license file”\n"
71 changes: 44 additions & 27 deletions tests/acceptance/standard/test_files_templates.py
@@ -1,42 +1,48 @@
from tests.acceptance import run_gitlabform, DEFAULT_README
import os
import pytest

from tests.acceptance import run_gitlabform


@pytest.fixture(scope="class")
def file3(request):
f = open("file3.txt", "a")
f.write("{{ group }}/{{ project }}\n\n")
f.close()

def fin():
os.remove("file3.txt")

request.addfinalizer(fin)


class TestFilesTemplates:
def test__default_variables(self, gitlab, group_and_project):

config = (
"""
config = f"""
projects_and_groups:
"""
+ group_and_project
+ """:
{group_and_project}:
files:
"README.md":
overwrite: true
branches: all
content: |
This is a text that contains the default variables: {{ group }}/{{ project }}
This is a text that contains the default variables: {{{{ group }}}}/{{{{ project }}}}
"""
)

run_gitlabform(config, group_and_project)

file_content = gitlab.get_file(group_and_project, "main", "README.md")
# TODO: the file_content should actually end with a new line, as this is how the original content above ends
assert (
file_content
== f"This is a text that contains the default variables: "
+ group_and_project
== f"This is a text that contains the default variables: {group_and_project}\n"
)

def test__custom_variables(self, gitlab, group_and_project):

config = (
"""
config = f"""
projects_and_groups:
"""
+ group_and_project
+ """:
{group_and_project}:
files:
"README.md":
overwrite: true
Expand All @@ -45,35 +51,46 @@ def test__custom_variables(self, gitlab, group_and_project):
foo: "fooz"
bar: "barz"
content: |
This is a text that contains custom variables: {{ foo }}, {{ bar }}
This is a text that contains custom variables: {{{{ foo }}}}, {{{{ bar }}}}
"""
)

run_gitlabform(config, group_and_project)

file_content = gitlab.get_file(group_and_project, "main", "README.md")
# TODO: the file_content should actually end with a new line, as this is how the original content above ends
assert (
file_content == "This is a text that contains custom variables: fooz, barz"
file_content
== "This is a text that contains custom variables: fooz, barz\n"
)

def test__trailing_new_lines(self, gitlab, group_and_project, file3):
set_file_chinese_characters = f"""
projects_and_groups:
{group_and_project}:
files:
"README.md":
overwrite: true
branches: all
file: file3.txt
"""

run_gitlabform(set_file_chinese_characters, group_and_project)

file_content = gitlab.get_file(group_and_project, "main", "README.md")
assert file_content == f"{group_and_project}\n\n"

def test__disabled_templating(self, gitlab, group_and_project):

config = (
"""
config = f"""
projects_and_groups:
"""
+ group_and_project
+ """:
{group_and_project}:
files:
"README.md":
overwrite: true
branches: all
template: no
content: |
This is a text containing literals: {{ group }}/{{ project }}
This is a text containing literals: {{{{ group }}}}/{{{{ project }}}}
"""
)

run_gitlabform(config, group_and_project)

Expand Down

0 comments on commit c3195b4

Please sign in to comment.