Skip to content

Commit 94241c8

Browse files
authored
Merge pull request #11 from nledez/master
Fix 'ModuleNotFoundError: No module named 'pkg_resources'' install error Rework tests
2 parents f0601bf + ccfcdb6 commit 94241c8

9 files changed

Lines changed: 452 additions & 30 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
jobs:
10+
test:
11+
runs-on: ${{ matrix.os }}
12+
strategy:
13+
fail-fast: false
14+
matrix:
15+
include:
16+
- python-version: "3.8"
17+
os: ubuntu-latest
18+
- python-version: "3.9"
19+
os: ubuntu-latest
20+
- python-version: "3.10"
21+
os: ubuntu-latest
22+
- python-version: "3.11"
23+
os: ubuntu-latest
24+
- python-version: "3.12"
25+
os: ubuntu-latest
26+
- python-version: "3.13"
27+
os: ubuntu-latest
28+
- python-version: "3.14"
29+
os: ubuntu-latest
30+
31+
steps:
32+
- uses: actions/checkout@v4
33+
34+
- name: Set up Python ${{ matrix.python-version }}
35+
uses: actions/setup-python@v5
36+
with:
37+
python-version: ${{ matrix.python-version }}
38+
allow-prereleases: true
39+
40+
- name: Install dependencies
41+
run: |
42+
python -m pip install --upgrade pip
43+
pip install -r requirements.txt
44+
pip install pytest pytest-cov
45+
46+
- name: Run tests with coverage
47+
run: pytest tests/ --cov=matterhook --cov-report=term-missing

‎README.rst‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,25 @@ Advanced usage
7171
'''
7272
message['text'] = markdown_msg
7373
attachments.append(message)
74-
mwh.send(attachments=attachments)
74+
mwh.send(attachments=attachments)
75+
76+
Testing
77+
=======
78+
79+
Install the test dependencies first:
80+
81+
.. code-block:: bash
82+
83+
pip install pytest pytest-cov
84+
85+
Run the tests:
86+
87+
.. code-block:: bash
88+
89+
pytest tests/
90+
91+
Run the tests with code coverage:
92+
93+
.. code-block:: bash
94+
95+
pytest tests/ --cov=matterhook --cov-report=term-missing

‎pyproject.toml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build-system]
2+
requires = ["setuptools"]
3+
build-backend = "setuptools.build_meta"

‎run_tests.sh‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
VERSIONS="3.8 3.9 3.10 3.11 3.12 3.13 3.14"
6+
FAILED=""
7+
8+
for v in $VERSIONS; do
9+
echo "=== Python $v ==="
10+
if uv run --python "$v" --with pytest,pytest-cov,requests \
11+
pytest tests/ --cov=matterhook --cov-report=term-missing; then
12+
echo "=== Python $v: OK ==="
13+
else
14+
echo "=== Python $v: FAILED ==="
15+
FAILED="$FAILED $v"
16+
fi
17+
echo
18+
done
19+
20+
if [ -n "$FAILED" ]; then
21+
echo "FAILED versions:$FAILED"
22+
exit 1
23+
else
24+
echo "All versions passed."
25+
fi

‎setup.py‎

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
"""
44

55
import os
6-
import sys
76

8-
from pkg_resources import get_distribution, parse_version
97
from setuptools import find_packages, setup
10-
from setuptools.command.test import test as TestCommand
118

129

1310
def read(fname: str):
@@ -25,29 +22,6 @@ def read(fname: str):
2522
return open(os.path.join(os.path.dirname(__file__), fname)).read()
2623

2724

28-
class PyTest(TestCommand):
29-
user_options = [("pytest-args=", "a", "Arguments to pass to py.test")]
30-
31-
def initialize_options(self):
32-
TestCommand.initialize_options(self)
33-
self.pytest_args = []
34-
35-
def finalize_options(self):
36-
TestCommand.finalize_options(self)
37-
38-
# https://bitbucket.org/pypa/setuptools/commits/cf565b6
39-
if get_distribution("setuptools").parsed_version < parse_version("18.4"):
40-
self.test_args = []
41-
self.test_suite = True
42-
43-
def run_tests(self):
44-
# import here, cause outside the eggs aren't loaded
45-
import pytest
46-
47-
errno = pytest.main(self.pytest_args)
48-
sys.exit(errno)
49-
50-
5125
setup(
5226
author="numberly",
5327
classifiers=[
@@ -60,9 +34,13 @@ def run_tests(self):
6034
"Programming Language :: Python :: 3.7",
6135
"Programming Language :: Python :: 3.8",
6236
"Programming Language :: Python :: 3.9",
37+
"Programming Language :: Python :: 3.10",
38+
"Programming Language :: Python :: 3.11",
39+
"Programming Language :: Python :: 3.12",
40+
"Programming Language :: Python :: 3.13",
41+
"Programming Language :: Python :: 3.14",
6342
"Topic :: Software Development :: Libraries :: Python Modules",
6443
],
65-
cmdclass={"test": PyTest},
6644
description="Interact with Mattermost incoming webhooks easily.",
6745
download_url="https://github.com/numberly/matterhook/tags",
6846
include_package_data=True,
@@ -72,8 +50,7 @@ def run_tests(self):
7250
name="matterhook",
7351
packages=find_packages(),
7452
platforms="any",
75-
tests_require=["pytest"],
7653
url="https://github.com/numberly/matterhook",
77-
version="0.2",
54+
version="0.3",
7855
zip_safe=True,
7956
)

‎tests/__init__.py‎

Whitespace-only changes.

‎tests/test_attachments.py‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
from matterhook.attachments import Attachment
2+
3+
4+
class TestAttachmentInit:
5+
def test_minimal(self):
6+
att = Attachment(fallback="fallback text")
7+
assert att.fallback == "fallback text"
8+
assert att.color is None
9+
assert att.pretext is None
10+
assert att.text is None
11+
assert att.author_name is None
12+
assert att.author_link is None
13+
assert att.author_icon is None
14+
assert att.title is None
15+
assert att.title_link is None
16+
assert att.fields is None
17+
assert att.image_url is None
18+
assert att.thumb_url is None
19+
20+
def test_all_params(self):
21+
fields = [{"short": True, "title": "F", "value": "V"}]
22+
att = Attachment(
23+
fallback="fb",
24+
color="#FF0000",
25+
pretext="pre",
26+
text="body",
27+
author_name="author",
28+
author_link="https://author.com",
29+
author_icon="https://author.com/icon.png",
30+
title="Title",
31+
title_link="https://title.com",
32+
fields=fields,
33+
image_url="https://image.png",
34+
thumb_url="https://thumb.png",
35+
)
36+
assert att.fallback == "fb"
37+
assert att.color == "#FF0000"
38+
assert att.pretext == "pre"
39+
assert att.text == "body"
40+
assert att.author_name == "author"
41+
assert att.author_link == "https://author.com"
42+
assert att.author_icon == "https://author.com/icon.png"
43+
assert att.title == "Title"
44+
assert att.title_link == "https://title.com"
45+
assert att.fields == fields
46+
assert att.image_url == "https://image.png"
47+
assert att.thumb_url == "https://thumb.png"
48+
49+
50+
class TestAttachmentPayload:
51+
def test_minimal_payload(self):
52+
att = Attachment(fallback="fb")
53+
assert att.payload == {"fallback": "fb"}
54+
55+
def test_full_payload(self):
56+
fields = [{"short": True, "title": "F", "value": "V"}]
57+
att = Attachment(
58+
fallback="fb",
59+
color="#FF0000",
60+
pretext="pre",
61+
text="body",
62+
author_name="author",
63+
author_link="https://author.com",
64+
author_icon="https://author.com/icon.png",
65+
title="Title",
66+
title_link="https://title.com",
67+
fields=fields,
68+
image_url="https://image.png",
69+
thumb_url="https://thumb.png",
70+
)
71+
assert att.payload == {
72+
"fallback": "fb",
73+
"color": "#FF0000",
74+
"pretext": "pre",
75+
"text": "body",
76+
"author_name": "author",
77+
"author_link": "https://author.com",
78+
"author_icon": "https://author.com/icon.png",
79+
"title": "Title",
80+
"title_link": "https://title.com",
81+
"fields": fields,
82+
"image_url": "https://image.png",
83+
"thumb_url": "https://thumb.png",
84+
}
85+
86+
def test_partial_payload_only_color(self):
87+
att = Attachment(fallback="fb", color="#00FF00")
88+
assert att.payload == {"fallback": "fb", "color": "#00FF00"}
89+
90+
def test_partial_payload_only_text(self):
91+
att = Attachment(fallback="fb", text="hello")
92+
assert att.payload == {"fallback": "fb", "text": "hello"}
93+
94+
def test_partial_payload_author_fields(self):
95+
att = Attachment(
96+
fallback="fb",
97+
author_name="a",
98+
author_link="https://a.com",
99+
author_icon="https://a.com/i.png",
100+
)
101+
assert att.payload == {
102+
"fallback": "fb",
103+
"author_name": "a",
104+
"author_link": "https://a.com",
105+
"author_icon": "https://a.com/i.png",
106+
}
107+
108+
def test_partial_payload_title_fields(self):
109+
att = Attachment(
110+
fallback="fb", title="T", title_link="https://t.com"
111+
)
112+
assert att.payload == {
113+
"fallback": "fb",
114+
"title": "T",
115+
"title_link": "https://t.com",
116+
}
117+
118+
def test_partial_payload_image_fields(self):
119+
att = Attachment(
120+
fallback="fb",
121+
image_url="https://img.png",
122+
thumb_url="https://thumb.png",
123+
)
124+
assert att.payload == {
125+
"fallback": "fb",
126+
"image_url": "https://img.png",
127+
"thumb_url": "https://thumb.png",
128+
}
129+
130+
def test_partial_payload_pretext(self):
131+
att = Attachment(fallback="fb", pretext="before")
132+
assert att.payload == {"fallback": "fb", "pretext": "before"}
133+
134+
def test_partial_payload_fields(self):
135+
fields = [{"short": False, "title": "X", "value": "Y"}]
136+
att = Attachment(fallback="fb", fields=fields)
137+
assert att.payload == {"fallback": "fb", "fields": fields}

0 commit comments

Comments
 (0)