[pp:metadata] add 'newline' option (#8439)
* Add configuration setting to control newline characters in metadata. * update docs/configuration * introduce 'open()' method * add 'newline' test
This commit is contained in:
@@ -453,7 +453,7 @@ class MetadataTest(BasePostprocessorTest):
|
||||
self._trigger()
|
||||
|
||||
path = f"{self.pathfmt.realpath}.JSON"
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8")
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None)
|
||||
|
||||
self.assertEqual(self._output(m), """{
|
||||
"category": "test",
|
||||
@@ -473,6 +473,7 @@ class MetadataTest(BasePostprocessorTest):
|
||||
"indent" : None,
|
||||
"open" : "a",
|
||||
"encoding" : "UTF-8",
|
||||
"newline" : "\r\n",
|
||||
"extension" : "JSON",
|
||||
}, {
|
||||
"public" : "hello ワールド",
|
||||
@@ -487,7 +488,9 @@ class MetadataTest(BasePostprocessorTest):
|
||||
self._trigger()
|
||||
|
||||
path = f"{self.pathfmt.realpath}.JSON"
|
||||
m.assert_called_once_with(path, "a", encoding="UTF-8")
|
||||
m.assert_called_once_with(path, "a", encoding="UTF-8", newline='\r\n')
|
||||
# Since we mocked the call to open,
|
||||
# we don't actually see the effect of setting newline.
|
||||
self.assertEqual(self._output(m), """{\
|
||||
"_private" : "foo \\u30d0\\u30fc",\
|
||||
"category" : "test",\
|
||||
@@ -508,7 +511,7 @@ class MetadataTest(BasePostprocessorTest):
|
||||
self._trigger()
|
||||
|
||||
path = f"{self.pathfmt.realpath}.txt"
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8")
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None)
|
||||
self.assertEqual(self._output(m), "foo\nbar\nbaz\n")
|
||||
|
||||
def test_metadata_tags_split_1(self):
|
||||
@@ -599,7 +602,7 @@ class MetadataTest(BasePostprocessorTest):
|
||||
self._trigger()
|
||||
|
||||
path = f"{self.pathfmt.realdirectory}file.json"
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8")
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None)
|
||||
|
||||
def test_metadata_extfmt_2(self):
|
||||
self._create({
|
||||
@@ -611,7 +614,7 @@ class MetadataTest(BasePostprocessorTest):
|
||||
self._trigger()
|
||||
|
||||
path = f"{self.pathfmt.realdirectory}file.2.EXT-data:tESt"
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8")
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None)
|
||||
|
||||
def test_metadata_directory(self):
|
||||
self._create({
|
||||
@@ -622,7 +625,7 @@ class MetadataTest(BasePostprocessorTest):
|
||||
self._trigger()
|
||||
|
||||
path = f"{self.pathfmt.realdirectory}metadata/file.ext.json"
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8")
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None)
|
||||
|
||||
def test_metadata_directory_2(self):
|
||||
self._create({
|
||||
@@ -634,7 +637,7 @@ class MetadataTest(BasePostprocessorTest):
|
||||
self._trigger()
|
||||
|
||||
path = f"{self.pathfmt.realdirectory}metadata/file.json"
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8")
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None)
|
||||
|
||||
def test_metadata_directory_format(self):
|
||||
self._create(
|
||||
@@ -646,7 +649,7 @@ class MetadataTest(BasePostprocessorTest):
|
||||
self._trigger()
|
||||
|
||||
path = f"{self.pathfmt.realdirectory}../json/12500/file.ext.json"
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8")
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None)
|
||||
|
||||
def test_metadata_directory_empty(self):
|
||||
self._create(
|
||||
@@ -657,7 +660,7 @@ class MetadataTest(BasePostprocessorTest):
|
||||
self._trigger()
|
||||
|
||||
path = f"{self.pathfmt.realdirectory}./file.ext.json"
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8")
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None)
|
||||
|
||||
def test_metadata_basedirectory(self):
|
||||
self._create({"base-directory": True})
|
||||
@@ -666,7 +669,7 @@ class MetadataTest(BasePostprocessorTest):
|
||||
self._trigger()
|
||||
|
||||
path = f"{self.pathfmt.basedirectory}file.ext.json"
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8")
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None)
|
||||
|
||||
def test_metadata_basedirectory_custom(self):
|
||||
self._create({
|
||||
@@ -678,7 +681,7 @@ class MetadataTest(BasePostprocessorTest):
|
||||
self._trigger()
|
||||
|
||||
path = "/home/test/meta/file.ext.json"
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8")
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None)
|
||||
|
||||
def test_metadata_filename(self):
|
||||
self._create({
|
||||
@@ -690,7 +693,7 @@ class MetadataTest(BasePostprocessorTest):
|
||||
self._trigger()
|
||||
|
||||
path = f"{self.pathfmt.realdirectory}test_file__meta_.data"
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8")
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None)
|
||||
|
||||
def test_metadata_meta_path(self):
|
||||
self._create({
|
||||
@@ -790,7 +793,7 @@ class MetadataTest(BasePostprocessorTest):
|
||||
self.assertGreater(len(self._output(m)), 0)
|
||||
|
||||
path = f"{self.pathfmt.realdirectory}file.ext.json"
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8")
|
||||
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None)
|
||||
|
||||
def test_metadata_option_skip_false(self):
|
||||
self._create({"skip": False})
|
||||
@@ -802,6 +805,28 @@ class MetadataTest(BasePostprocessorTest):
|
||||
self.assertTrue(not e.called)
|
||||
self.assertTrue(m.called)
|
||||
|
||||
def test_metadata_option_newline(self):
|
||||
self._create({
|
||||
"newline": "\r\n",
|
||||
"filename" : "data.json",
|
||||
"directory" : "",
|
||||
"base-directory": self.dir.name,
|
||||
})
|
||||
|
||||
self._trigger()
|
||||
|
||||
path = os.path.join(self.dir.name, "data.json")
|
||||
with open(path, newline="") as fp:
|
||||
content = fp.read()
|
||||
|
||||
self.assertEqual(content, """\
|
||||
{\r\n\
|
||||
"category": "test",\r\n\
|
||||
"filename": "file",\r\n\
|
||||
"extension": "ext"\r\n\
|
||||
}\r\n\
|
||||
""")
|
||||
|
||||
def test_metadata_option_include(self):
|
||||
self._create(
|
||||
{"include": ["_private", "filename", "foo"], "sort": True},
|
||||
|
||||
Reference in New Issue
Block a user