Skip to content

Commit

Permalink
refactor: replace os.path/glob with pathlib
Browse files Browse the repository at this point in the history
Since Python 3.4, `pathlib` provides a nicer interface to manipulate
paths, removing the need to split and reassemble strings.

The addition of `pytest.param(..., id=fn.name)` is to set the test name
output to a human-friendly string in the event of failure or `--verbose`

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
  • Loading branch information
miketheman committed Jul 6, 2022
1 parent b92c64f commit 7ce62f3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 32 deletions.
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import pathlib

import setuptools

base_dir = os.path.dirname(__file__)
base_dir = pathlib.Path(__file__).parent

with open(os.path.join(base_dir, "readme_renderer", "__about__.py")) as f:
with open(base_dir.joinpath("readme_renderer", "__about__.py")) as f:
about = {}
exec(f.read(), about)

with open(os.path.join(base_dir, "README.rst")) as f:
with open(base_dir.joinpath("README.rst")) as f:
long_description = f.read()


Expand Down
22 changes: 6 additions & 16 deletions tests/test_markdown.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
import glob
import os
from pathlib import Path

import pytest

from readme_renderer.markdown import render, variants


MD_FIXTURES = [
(fn, os.path.splitext(fn)[0] + ".html", variant)
for variant in variants
for fn in glob.iglob(
os.path.join(
os.path.dirname(__file__),
"fixtures",
"test_" + variant + "*.md"
)
)
]


@pytest.mark.parametrize(
("md_filename", "html_filename", "variant"),
MD_FIXTURES,
[
(pytest.param(fn, fn.with_suffix(".html"), variant, id=fn.name))
for variant in variants
for fn in Path(__file__).parent.glob(f"fixtures/test_{variant}*.md")
],
)
def test_md_fixtures(md_filename, html_filename, variant):
# Get our Markup
Expand Down
9 changes: 3 additions & 6 deletions tests/test_rst.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import io
import glob
import os.path
from pathlib import Path

import pytest

Expand All @@ -10,10 +9,8 @@
@pytest.mark.parametrize(
("rst_filename", "html_filename"),
[
(fn, os.path.splitext(fn)[0] + ".html")
for fn in glob.glob(
os.path.join(os.path.dirname(__file__), "fixtures", "test_*.rst")
)
(pytest.param(fn, fn.with_suffix(".html"), id=fn.name))
for fn in Path(__file__).parent.glob("fixtures/test_*.rst")
],
)
def test_rst_fixtures(rst_filename, html_filename):
Expand Down
9 changes: 3 additions & 6 deletions tests/test_txt.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import glob
import os.path
from pathlib import Path

import pytest

Expand All @@ -9,10 +8,8 @@
@pytest.mark.parametrize(
("txt_filename", "html_filename"),
[
(fn, os.path.splitext(fn)[0] + ".html")
for fn in glob.glob(
os.path.join(os.path.dirname(__file__), "fixtures", "test_*.txt")
)
(pytest.param(fn, fn.with_suffix(".html"), id=fn.name))
for fn in Path(__file__).parent.glob("fixtures/test_*.txt")
],
)
def test_txt_fixtures(txt_filename, html_filename):
Expand Down

0 comments on commit 7ce62f3

Please sign in to comment.