Skip to content

Commit 07813bd

Browse files
committed
Add pytest tests and workflow
1 parent 357af4e commit 07813bd

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

.github/workflows/python-tests.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Python tests
2+
3+
on:
4+
push:
5+
branches: ["master"]
6+
pull_request:
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Set up Python
14+
uses: actions/setup-python@v4
15+
with:
16+
python-version: '3.x'
17+
- name: Install dependencies
18+
run: |
19+
python -m pip install --upgrade pip
20+
pip install -r requirements.txt
21+
- name: Run tests
22+
run: pytest -v

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ Serve the site locally with:
5353
bundle exec jekyll serve
5454
```
5555

56+
57+
## Running tests
58+
59+
Install the Python dependencies and execute:
60+
61+
```bash
62+
pytest
63+
```
5664
GitHub Actions already runs these commands automatically during deployments.
5765

5866
# ToDo

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
python-frontmatter
22
PyYAML
3+
pytest

tests/test_fix_date.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
import tempfile
3+
import frontmatter
4+
import pytest
5+
6+
import fix_date
7+
8+
9+
def test_extract_date_from_filename():
10+
assert fix_date.extract_date_from_filename('2023-12-01-post.md') == '2023-12-01'
11+
assert fix_date.extract_date_from_filename('no-date.md') is None
12+
13+
14+
def create_markdown_file(path, front_matter):
15+
content = frontmatter.dumps(front_matter) + "\nBody"
16+
with open(path, 'w', encoding='utf-8') as f:
17+
f.write(content)
18+
19+
20+
def load_front_matter(path):
21+
with open(path, 'r', encoding='utf-8') as f:
22+
return frontmatter.load(f)
23+
24+
25+
@pytest.mark.parametrize("initial_frontmatter, expected_date", [
26+
({'title': 'Test'}, '2024-02-03'),
27+
({'title': 'Test', 'date': '2020-01-01'}, '2024-02-03'),
28+
])
29+
def test_process_markdown_file_updates_date(tmp_path, initial_frontmatter, expected_date):
30+
file_path = tmp_path / '2024-02-03-test.md'
31+
create_markdown_file(file_path, initial_frontmatter)
32+
33+
fix_date.process_markdown_file(str(file_path))
34+
35+
updated = load_front_matter(file_path)
36+
assert updated['date'] == expected_date

0 commit comments

Comments
 (0)