Skip to content

Commit

Permalink
Write lines/text with os specific line ending
Browse files Browse the repository at this point in the history
* fix: write lines/text with os specific line ending

* chores: update poetry.lock

* chores: fix black errors in ci

* Revert "chores: fix black errors in ci"

This reverts commit bf36544.

* chores: fix black errors in ci

* chores: fix black errors in ci

* fix: tests using line endings from settings module

* chores: fix black errors in ci

* test: add unittest for line endings
  • Loading branch information
ckolumbus committed Apr 12, 2022
1 parent 44a2751 commit fba23e8
Show file tree
Hide file tree
Showing 9 changed files with 458 additions and 330 deletions.
2 changes: 1 addition & 1 deletion doorstop/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def _write(self, text, path):
"""
if not self._exists:
raise DoorstopError("cannot save to deleted: {}".format(self))
common.write_text(text, path)
common.write_text(text, path, end=settings.WRITE_LINESEPERATOR)

@staticmethod
def _dump(data):
Expand Down
4 changes: 3 additions & 1 deletion doorstop/core/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,9 @@ def index(self, value):
assert self.path
path = os.path.join(self.path, Document.INDEX)
log.info("creating {} index...".format(self))
common.write_lines(self._lines_index(self.items), path)
common.write_lines(
self._lines_index(self.items), path, end=settings.WRITE_LINESEPERATOR
)

@index.deleter
def index(self):
Expand Down
2 changes: 1 addition & 1 deletion doorstop/core/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def export(obj, path, ext=None, **kwargs):
log.info("exporting to {}...".format(path2))
if ext in FORMAT_LINES:
lines = export_lines(obj2, ext, **kwargs)
common.write_lines(lines, path2)
common.write_lines(lines, path2, end=settings.WRITE_LINESEPERATOR)
else:
export_file(obj2, path2, ext, **kwargs)

Expand Down
13 changes: 9 additions & 4 deletions doorstop/core/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,24 @@ def publish(
wrapper.append("\\section*{Traceability}")
wrapper.append("\\input{traceability.tex}")
wrapper.append("\\end{document}")
common.write_lines(wrapper, path3)
common.write_lines(wrapper, path3, end=settings.WRITE_LINESEPERATOR)

# Publish content to the specified path
log.info("publishing to {}...".format(path2))
lines = publish_lines(
obj2, ext, linkify=linkify, template=template, toc=toc, **kwargs
)
common.write_lines(lines, path2)
common.write_lines(lines, path2, end=settings.WRITE_LINESEPERATOR)
if obj2.copy_assets(assets_dir):
log.info("Copied assets from %s to %s", obj.assets, assets_dir)

if ext == ".tex":
common.write_lines(compile_files, compile_path, executable=True)
common.write_lines(
compile_files,
compile_path,
end=settings.WRITE_LINESEPERATOR,
executable=True,
)
msg = "You can now execute the file 'compile.sh' twice in the exported folder to produce the PDFs!"
utilities.show(msg, flush=True)

Expand Down Expand Up @@ -266,7 +271,7 @@ def _index(directory, index=INDEX, extensions=(".html",), tree=None):
path = os.path.join(directory, index)
log.info("creating an {}...".format(index))
lines = _lines_index(sorted(filenames), tree=tree)
common.write_lines(lines, path)
common.write_lines(lines, path, end=settings.WRITE_LINESEPERATOR)
else:
log.warning("no files for {}".format(index))

Expand Down
2 changes: 1 addition & 1 deletion doorstop/core/publisher_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,4 +556,4 @@ def _matrix_latex(table, path):
traceability.append("\\hline")
# End the table.
traceability.append("\\end{longtable}")
common.write_lines(traceability, file)
common.write_lines(traceability, file, end=settings.WRITE_LINESEPERATOR)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
active: true
derived: false
header: |
UTF-8
level: 1.7.0
links: []
normative: true
ref: ''
reviewed: null
text: |
UTF-8 is supported. Verified by this file.
première is first
première is slightly different
Кириллица is Cyrillic
𐐀 am Deseret
33 changes: 32 additions & 1 deletion doorstop/core/tests/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from typing import List
from unittest.mock import MagicMock, Mock, patch

from doorstop import common
from doorstop import common, settings
from doorstop.common import DoorstopError
from doorstop.core.item import Item, UnknownItem
from doorstop.core.tests import (
Expand Down Expand Up @@ -1055,6 +1055,13 @@ def test_stamp(self):
class TestUTF8(unittest.TestCase):
"""Unit tests for reading UTF8 formatted files."""

def setUp(self):
"""This test suite uses `item.save()` which is based on os dependent settings.
In order to make golden sample tests work here, one harmonized line ending mus
be configured.
"""
settings.WRITE_LINESEPERATOR = "\n"

def test_load_cyrillic(self):
"""Verify that cyrillic and other UTF-8 characters are correltly loaded and written."""
ITEM = "doorstop/core/tests/test_fixtures/002-utf8-characters/REQ-CYRILLIC.yml"
Expand All @@ -1078,3 +1085,27 @@ def test_load_mit(self):
self.maxDiff = None
common.write_text(backup, ITEM)
self.assertEqual(backup, text)


class TestOSLineSep(unittest.TestCase):
"""Unit tests os dependent line end handling."""

def setUp(self):
settings.WRITE_LINESEPERATOR = os.linesep

def test_write_os_dependend_line_seperator(self):
"""Verify an item file is correctly loaded and written with appropriate line endings."""
if os.name == "nt":
ITEM = "doorstop/core/tests/test_fixtures/002-utf8-characters/REQ-CYRILLIC_crlf.yml"
else:
ITEM = (
"doorstop/core/tests/test_fixtures/002-utf8-characters/REQ-CYRILLIC.yml"
)
backup = common.read_text(ITEM)
item = Item(None, ITEM)
item.load()
item.save()
text = common.read_text(ITEM)
self.maxDiff = None
common.write_text(backup, ITEM)
self.assertEqual(backup, text)
2 changes: 2 additions & 0 deletions doorstop/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""Settings for the Doorstop package."""

import logging
import os

# Logging settings
DEFAULT_LOGGING_FORMAT = "%(message)s"
Expand Down Expand Up @@ -49,6 +50,7 @@
PUBLISH_BODY_LEVELS = True # include levels on non-header items
PUBLISH_HEADING_LEVELS = True # include levels on header items
ENABLE_HEADERS = True # use headers if defined
WRITE_LINESEPERATOR = os.linesep

# Version control settings
ADDREMOVE_FILES = True # automatically add/remove new/changed files
Expand Down

0 comments on commit fba23e8

Please sign in to comment.