Skip to content

Commit

Permalink
Merge pull request #372 from razzeee/always-create-description-tag
Browse files Browse the repository at this point in the history
Always create description tag
  • Loading branch information
wjt authored May 20, 2024
2 parents 6899aeb + 7cefc3a commit 075cb89
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 10 deletions.
17 changes: 17 additions & 0 deletions src/lib/appdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ def add_release(
releases.insert(0, release)
_fill_padding(release)

description = ElementTree.Element("description")

# Give <description> a closing </description> rather than it being
# self-closing
description.text = ""

# Indent the opening <description> tag one level
# deeper than the <release> tag.
if releases.text:
release.text = "\n" + ((releases.text[1::2]) * 3)

# Indent the closing </release> tag by the same amount as the opening
# <release> tag (which we know to be the first child of <releases> since
# we just prepended it above)
description.tail = releases.text
release.append(description)

tree.write(
dst,
# XXX: lxml uses single quotes for doctype line if generated with
Expand Down
113 changes: 103 additions & 10 deletions tests/test_appdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,90 @@ def test_simple(self):
<?xml version="1.0" encoding="UTF-8"?>
<component type="desktop">
<releases>
<release version="4.5.6" date="2020-02-02"/>
<release version="4.5.6" date="2020-02-02">
<description></description>
</release>
<release version="1.2.3" date="2019-01-01"/>
</releases>
</component>
""".strip(),
)

@unittest.expectedFailure
def test_four_space_no_releases_element(self):
# FIXME: This ends up indenting <releases> correctly, but
# <release> and <description> incorrectly get the default 2-space
# indent.
self._do_test(
"""
<?xml version="1.0" encoding="UTF-8"?>
<component type="desktop">
<id>com.example.Workaround</id>
<name>My history is a mystery</name>
</component>
""".strip(),
"""
<?xml version="1.0" encoding="UTF-8"?>
<component type="desktop">
<id>com.example.Workaround</id>
<name>My history is a mystery</name>
<releases>
<release version="4.5.6" date="2020-02-02">
<description></description>
</release>
</releases>
</component>
""".strip(),
)

def test_four_space_one_prior_release(self):
self._do_test(
"""
<?xml version="1.0" encoding="UTF-8"?>
<component type="desktop">
<releases>
<release version="1.2.3" date="2019-01-01"/>
</releases>
</component>
""".strip(),
"""
<?xml version="1.0" encoding="UTF-8"?>
<component type="desktop">
<releases>
<release version="4.5.6" date="2020-02-02">
<description></description>
</release>
<release version="1.2.3" date="2019-01-01"/>
</releases>
</component>
""".strip(),
)

def test_four_space_many_prior_releases(self):
self._do_test(
"""
<?xml version="1.0" encoding="UTF-8"?>
<component type="desktop">
<releases>
<release version="1.2.3" date="2019-01-01"/>
<release version="1.1.2" date="2018-01-01"/>
</releases>
</component>
""".strip(),
"""
<?xml version="1.0" encoding="UTF-8"?>
<component type="desktop">
<releases>
<release version="4.5.6" date="2020-02-02">
<description></description>
</release>
<release version="1.2.3" date="2019-01-01"/>
<release version="1.1.2" date="2018-01-01"/>
</releases>
</component>
""".strip(),
)

def test_mixed_indentation(self):
"""This input uses 3-space indentation for one existing <release> and 4-space
for another. Match the top one."""
Expand All @@ -70,7 +147,9 @@ def test_mixed_indentation(self):
<?xml version="1.0" encoding="UTF-8"?>
<component type="desktop">
<releases>
<release version="4.5.6" date="2020-02-02"/>
<release version="4.5.6" date="2020-02-02">
<description></description>
</release>
<release version="1.2.3" date="2019-01-01"/>
<release version="1.2.3" date="2019-01-01"/>
</releases>
Expand Down Expand Up @@ -119,7 +198,9 @@ def test_comment(self):
<component type="desktop">
<!-- I am the walrus -->
<releases>
<release version="4.5.6" date="2020-02-02"/>
<release version="4.5.6" date="2020-02-02">
<description></description>
</release>
<release version="1.2.3" date="2019-01-01"/>
</releases>
</component>
Expand All @@ -137,14 +218,16 @@ def test_no_releases(self):
<?xml version="1.0" encoding="UTF-8"?>
<component type="desktop">
<releases>
<release version="4.5.6" date="2020-02-02"/>
<release version="4.5.6" date="2020-02-02">
<description></description>
</release>
</releases>
</component>
""".strip(),
)

def test_empty_releases(self):
"""No whitespace is generated between <release /> and </releases>."""
"""No whitespace is generated between </release> and </releases>."""
self._do_test(
"""
<?xml version="1.0" encoding="UTF-8"?>
Expand All @@ -156,7 +239,9 @@ def test_empty_releases(self):
<?xml version="1.0" encoding="UTF-8"?>
<component type="desktop">
<releases>
<release version="4.5.6" date="2020-02-02"/>
<release version="4.5.6" date="2020-02-02">
<description></description>
</release>
</releases>
</component>
""".strip(),
Expand Down Expand Up @@ -186,7 +271,9 @@ def test_double_comment_within_root(self):
-->
<name>First element needed</name>
<releases>
<release version="4.5.6" date="2020-02-02"/>
<release version="4.5.6" date="2020-02-02">
<description></description>
</release>
</releases>
</application>
""".strip(),
Expand Down Expand Up @@ -219,7 +306,9 @@ def test_comment_outside_root(self):
-->
<application>
<releases>
<release version="4.5.6" date="2020-02-02"/>
<release version="4.5.6" date="2020-02-02">
<description></description>
</release>
</releases>
</application>
""".strip(),
Expand All @@ -238,7 +327,9 @@ def test_amp_as_amp(self):
<component type="desktop">
<name>🍦 &amp; 🎂</name>
<releases>
<release version="4.5.6" date="2020-02-02"/>
<release version="4.5.6" date="2020-02-02">
<description></description>
</release>
</releases>
</component>
""".strip(),
Expand All @@ -259,7 +350,9 @@ def test_amp_as_codepoint(self):
<component type="desktop">
<name>🦝 &#38; 🍒</name>
<releases>
<release version="4.5.6" date="2020-02-02"/>
<release version="4.5.6" date="2020-02-02">
<description></description>
</release>
</releases>
</component>
""".strip(),
Expand Down

0 comments on commit 075cb89

Please sign in to comment.