Skip to content

Commit c39eeff

Browse files
authored
Add integration with pre-commit.ci (xmlsec#230)
* add pre-commit.ci badge Signed-off-by: oleg.hoefling <[email protected]> * fix pre-commit warnings Signed-off-by: oleg.hoefling <[email protected]> Signed-off-by: oleg.hoefling <[email protected]>
1 parent 8240843 commit c39eeff

13 files changed

+26
-87
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# See https://pre-commit.com for more information
22
# See https://pre-commit.com/hooks.html for more hooks
3-
exclude: ".*.diff" # exclude patches
43
repos:
54
- repo: https://github.com/psf/black
65
rev: 22.6.0
@@ -21,6 +20,7 @@ repos:
2120
- id: check-merge-conflict
2221
- id: check-json
2322
- id: detect-private-key
23+
exclude: ^.*/rsakey.pem$
2424
- id: mixed-line-ending
2525
- id: pretty-format-json
2626
args: [--autofix]

README.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ python-xmlsec
33

44
.. image:: https://img.shields.io/pypi/v/xmlsec.svg?logo=python&logoColor=white
55
:target: https://pypi.python.org/pypi/xmlsec
6-
.. image:: https://img.shields.io/travis/com/mehcode/python-xmlsec/master.svg?logo=travis&logoColor=white&label=Travis%20CI
7-
:target: https://travis-ci.org/mehcode/python-xmlsec
6+
.. image:: https://results.pre-commit.ci/badge/github/xmlsec/python-xmlsec/master.svg
7+
:target: https://results.pre-commit.ci/latest/github/xmlsec/python-xmlsec/master
8+
:alt: pre-commit.ci status
89
.. image:: https://img.shields.io/appveyor/ci/hoefling/xmlsec/master.svg?logo=appveyor&logoColor=white&label=AppVeyor
910
:target: https://ci.appveyor.com/project/hoefling/xmlsec
1011
.. image:: https://github.com/mehcode/python-xmlsec/actions/workflows/manylinux.yml/badge.svg

doc/source/conf.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
# -*- coding: utf-8 -*-
2-
31
from __future__ import annotations
42

5-
import urllib.request
63
import importlib.metadata
4+
import urllib.request
75

86
import lxml
97
from docutils.nodes import Text, reference
@@ -13,7 +11,6 @@
1311
from sphinx.environment import BuildEnvironment
1412
from sphinx.errors import ExtensionError
1513

16-
1714
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode', 'sphinx.ext.intersphinx']
1815

1916
intersphinx_mapping = {'python': ('https://docs.python.org/3/', None)}

doc/source/examples/sign_binary.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from lxml import etree
2-
31
import xmlsec
42

53
ctx = xmlsec.SignatureContext()

doc/source/examples/verify_binary.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from lxml import etree
2-
31
import xmlsec
42

53
ctx = xmlsec.SignatureContext()

doc/source/sphinx-pr-6916.diff

-46
This file was deleted.

setup.cfg

+3
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@ upload_dir = doc/build/html
1818
[flake8]
1919
per-file-ignores =
2020
*.pyi: E301, E302, E305, E501, E701, F401, F822
21+
doc/source/conf.py: D1
22+
doc/source/examples/*.py: D1, E501
23+
tests/*.py: D1
2124
exclude = .venv*,.git,*_pb2.pyi,build,dist,libs,.eggs,.direnv*
2225
max-line-length = 130

tests/base.py

+5-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import gc
22
import os
3+
import resource
34
import sys
45
import unittest
56

@@ -12,20 +13,8 @@
1213
ns = {'dsig': xmlsec.constants.DSigNs, 'enc': xmlsec.constants.EncNs}
1314

1415

15-
try:
16-
import resource
17-
18-
def get_memory_usage():
19-
return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
20-
21-
except ImportError:
22-
resource = None
23-
24-
def get_memory_usage():
25-
return 0
26-
27-
2816
def get_iterations():
17+
"""Parse iterations amount."""
2918
if sys.platform in ('win32',):
3019
return 0
3120

@@ -114,9 +103,9 @@ def assertXmlEqual(self, first, second, msg=None): # noqa: N802
114103
for name in second.attrib.keys():
115104
if name not in first.attrib:
116105
self.fail('x2 has an attribute x1 is missing: {}. {}'.format(name, msg))
117-
if not xml_text_compare(first.text, second.text):
106+
if not _xml_text_compare(first.text, second.text):
118107
self.fail('text: {!r} != {!r}. {}'.format(first.text, second.text, msg))
119-
if not xml_text_compare(first.tail, second.tail):
108+
if not _xml_text_compare(first.tail, second.tail):
120109
self.fail('tail: {!r} != {!r}. {}'.format(first.tail, second.tail, msg))
121110
cl1 = sorted(first.getchildren(), key=lambda x: x.tag)
122111
cl2 = sorted(second.getchildren(), key=lambda x: x.tag)
@@ -128,7 +117,7 @@ def assertXmlEqual(self, first, second, msg=None): # noqa: N802
128117
self.assertXmlEqual(c1, c2)
129118

130119

131-
def xml_text_compare(t1, t2):
120+
def _xml_text_compare(t1, t2):
132121
if not t1 and not t2:
133122
return True
134123
if t1 == '*' or t2 == '*':

tests/conftest.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
def pytest_collection_modifyitems(items):
22
"""
3-
Put the module init test first to implicitly check whether
4-
any subsequent test fails because of module reinitialization.
3+
Put the module init test first.
4+
5+
This way, we implicitly check whether any subsequent test fails because of module reinitialization.
56
"""
67

78
def module_init_tests_first(item):

tests/test_ds.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ def test_sign_case3(self):
132132

133133
def test_sign_case4(self):
134134
"""Should sign a file using a dynamically created template, key from PEM and an X509 cert with custom ns."""
135-
136135
root = self.load_xml("sign4-in.xml")
137136
xmlsec.tree.add_ids(root, ["ID"])
138137
elem_id = root.get('ID', None)
@@ -249,7 +248,7 @@ def test_verify_case_5(self):
249248
self.check_verify(5)
250249

251250
def check_verify(self, i):
252-
root = self.load_xml("sign%d-out.xml" % i)
251+
root = self.load_xml("sign{}-out.xml".format(i))
253252
xmlsec.tree.add_ids(root, ["ID"])
254253
sign = xmlsec.tree.find_node(root, consts.NodeSignature)
255254
self.assertIsNotNone(sign)

tests/test_enc.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
import tempfile
32

43
from lxml import etree
@@ -125,7 +124,7 @@ def test_encrypt_binary(self):
125124

126125
encrypted = ctx.encrypt_binary(enc_data, b'test')
127126
self.assertIsNotNone(encrypted)
128-
self.assertEqual("{%s}%s" % (consts.EncNs, consts.NodeEncryptedData), encrypted.tag)
127+
self.assertEqual("{{{}}}{}".format(consts.EncNs, consts.NodeEncryptedData), encrypted.tag)
129128

130129
enc_method = xmlsec.tree.find_child(enc_data, consts.NodeEncryptionMethod, consts.EncNs)
131130
self.assertIsNotNone(enc_method)
@@ -170,7 +169,7 @@ def test_encrypt_uri(self):
170169

171170
encrypted = ctx.encrypt_binary(enc_data, 'file://' + tmpfile.name)
172171
self.assertIsNotNone(encrypted)
173-
self.assertEqual("{%s}%s" % (consts.EncNs, consts.NodeEncryptedData), encrypted.tag)
172+
self.assertEqual("{{{}}}{}".format(consts.EncNs, consts.NodeEncryptedData), encrypted.tag)
174173

175174
enc_method = xmlsec.tree.find_child(enc_data, consts.NodeEncryptionMethod, consts.EncNs)
176175
self.assertIsNotNone(enc_method)
@@ -219,7 +218,7 @@ def test_decrypt_key(self):
219218
self.assertEqual(self.load_xml("enc3-in.xml"), decrypted)
220219

221220
def check_decrypt(self, i):
222-
root = self.load_xml('enc%d-out.xml' % i)
221+
root = self.load_xml('enc{}-out.xml'.format(i))
223222
enc_data = xmlsec.tree.find_child(root, consts.NodeEncryptedData, consts.EncNs)
224223
self.assertIsNotNone(enc_data)
225224

@@ -228,7 +227,7 @@ def check_decrypt(self, i):
228227
ctx = xmlsec.EncryptionContext(manager)
229228
decrypted = ctx.decrypt(enc_data)
230229
self.assertIsNotNone(decrypted)
231-
self.assertEqual(self.load_xml("enc%d-in.xml" % i), root)
230+
self.assertEqual(self.load_xml("enc{}-in.xml".format(i)), root)
232231

233232
def test_decrypt_bad_args(self):
234233
ctx = xmlsec.EncryptionContext()

tests/test_main.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def test_sign_data_not_first_callback(self):
112112
def match_cb(filename):
113113
nonlocal bad_match_calls
114114
bad_match_calls += 1
115-
False
115+
return False
116116

117117
for _ in range(2):
118118
self._register_mismatch_callbacks(match_cb)
@@ -132,7 +132,7 @@ def test_failed_sign_because_default_callbacks(self):
132132
def mismatch_cb(filename):
133133
nonlocal mismatch_calls
134134
mismatch_calls += 1
135-
False
135+
return False
136136

137137
# NB: These first two sets of callbacks should never get called,
138138
# because the default callbacks always match beforehand:

tests/test_xmlsec.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
class TestModule(base.TestMemoryLeaks):
66
def test_reinitialize_module(self):
77
"""
8-
This doesn't explicitly test anything, but will
9-
be invoked first in the suite, so if the subsequent
10-
tests don't fail, we know that the ``init()``/``shutdown()``
8+
This test doesn't explicitly verify anything, but will be invoked first in the suite.
9+
10+
So if the subsequent tests don't fail, we know that the ``init()``/``shutdown()``
1111
function pair doesn't break anything.
1212
"""
1313
xmlsec.shutdown()

0 commit comments

Comments
 (0)