Skip to content

Commit

Permalink
Add doctests, coverage now 89%
Browse files Browse the repository at this point in the history
  • Loading branch information
yves-chevallier committed Oct 9, 2024
1 parent f47ea09 commit 499b858
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- '4 failed, 0 passed (0.0%% ok).' remove the duplicated pecentage sign
- Fix output by adding quotes and `(empty)` for empty strings
- More tests (89% coverage), enabled doctests

### Deprecated

Expand Down
3 changes: 2 additions & 1 deletion baygon/executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ class Executable:
For example:
>>> e = Executable('echo')
>>> e
Executable<echo>
>>> e('-n', 'Hello World')
Outputs(exit_status=0, stdout='Hello World', stderr='')
>>> e('-n', 'Hello World').stdout
'Hello World!'
'Hello World'
"""

def __new__(cls, filename):
Expand Down
39 changes: 39 additions & 0 deletions baygon/helpers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,49 @@
import re
import shlex


def escape_argument(arg):
"""Escape a command line argument.
>>> print(escape_argument("hello"))
hello
>>> print(escape_argument("hello world"))
'hello world'
>>> print(escape_argument("hello'world'"))
'hello'"'"'world'"'"''
"""
return shlex.quote(arg)


def create_command_line(args):
"""Create a command line from a list of arguments.
>>> print(create_command_line(["echo", "hello world"]))
echo 'hello world'
"""
escaped_args = [escape_argument(str(arg)) for arg in args]
return " ".join(escaped_args)


class GreppableString(str):
"""A string that can be parsed with regular expressions."""

def grep(self, pattern: str, *args) -> bool:
"""Return True if the pattern is found in the string.
>>> GreppableString("hello world").grep("w.{3}d")
['world']
>>> GreppableString("hello world").grep(r"\b[a-z]{4}\b")
[]
"""
return re.findall(pattern, self, *args)

def contains(self, value: str) -> bool:
"""Return True if the value is found in the string.
>>> GreppableString("hello world").contains("world")
True
>>> GreppableString("hello world").contains("earth")
False
"""
return value in self
13 changes: 0 additions & 13 deletions baygon/str.py

This file was deleted.

33 changes: 26 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,21 @@ classifiers = [
"Topic :: Software Development :: Testing",
"Topic :: Software Development :: Libraries",
]
exclude = [".editorconfig", "baygon/version.py", ".gitignore",
".eggs", ".env", ".tox", ".vscode", ".vscode/settings.json",
"*.pyc", "*~", ".DS_Store", "__pycache__", "*.pyo", "node_modules"
exclude = [
".editorconfig",
"baygon/version.py",
".gitignore",
".eggs",
".env",
".tox",
".vscode",
".vscode/settings.json",
"*.pyc",
"*~",
".DS_Store",
"__pycache__",
"*.pyo",
"node_modules",
]

[tool.poetry.urls]
Expand All @@ -60,7 +72,7 @@ black = "^24.10.0"
tox = "^4.21.2"

[tool.check-manifest]
ignore = [ "node_modules/*",]
ignore = ["node_modules/*"]

[tool.poetry.scripts]
baygon = "baygon.__main__:cli"
Expand All @@ -76,8 +88,15 @@ select = ["E", "F", "W", "C", "I"]
line-length = 88

[tool.pytest.ini_options]
testpaths = "tests"
testpaths = "baygon tests"
timeout = 2
python_files = "test_*.py"
python_classes = "Test*"
norecursedirs = [ ".git", "_build",]
addopts = "--cov=baygon --cov-report term --cov-report xml --cov-fail-under=85"
norecursedirs = [".git", "_build"]
addopts = '''
--doctest-modules
--ignore-glob='*.exe.py'
--cov=baygon --cov-report
term --cov-report
xml --cov-fail-under=85
'''
File renamed without changes.
8 changes: 8 additions & 0 deletions tests/click/foo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
failures: 2
points:
earned: 4
total: 10
skipped: 0
successes: 2
time: 0.03
total: 4
2 changes: 1 addition & 1 deletion tests/click/ignorespaces.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ filters:
tests:
- name: Stdout is the spaces are removed
args: [4, 5]
executable: add.py
executable: add.exe.py
stdout:
- contains: "a+b=4+5"
File renamed without changes.
21 changes: 21 additions & 0 deletions tests/click/points.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: 1
executable: ./main.exe.py
filters:
trim: true
tests:
- name: Foo
args: [4, 0]
stdout: [ equals: 4 ]
points: 1
- name: Bar
args: [8, 0]
stdout: [ equals: 2 ]
points: 2
- name: Baz
args: [15, 0]
stdout: [ equals: 15 ]
points: 3
- name: Qux
args: [16, 0]
stdout: [ equals: 42 ]
points: 4
2 changes: 1 addition & 1 deletion tests/click/test_click.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def directory(self):

@property
def executable(self):
return str(self.directory.joinpath("main.py"))
return str(self.directory.joinpath("main.exe.py"))

def get_config(self, name):
return self.directory.joinpath(name)
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions tests/executable/test_executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from baygon import Executable
from baygon.error import InvalidExecutableError
from baygon.str import GreppableString
from baygon.helpers import GreppableString

dir_path = Path(__file__).resolve(strict=True).parent

Expand All @@ -21,13 +21,13 @@ def test_arg(self):
self.assertEqual(output.stdout, test_string)

def test_stdout(self):
e = Executable(dir_path.joinpath("dummy.py"))
e = Executable(dir_path.joinpath("dummy.exe.py"))
output = e.run()
print(output)
self.assertEqual(output.stdout, "an apple\n")

def test_stderr(self):
e = Executable(dir_path.joinpath("dummy.py"))
e = Executable(dir_path.joinpath("dummy.exe.py"))
output = e.run()
print(output)
self.assertEqual(output.stderr, "an orange\n")
Expand All @@ -40,13 +40,13 @@ def test_stdin(self):
self.assertEqual(output.stdout, test_string)

def test_exit_status(self):
e = Executable(dir_path.joinpath("dummy.py"))
e = Executable(dir_path.joinpath("dummy.exe.py"))
output = e.run()
print(output)
self.assertEqual(output.exit_status, 42)

def test_args(self):
e = Executable(dir_path.joinpath("args.py"))
e = Executable(dir_path.joinpath("args.exe.py"))
test_string = "foobar"
output = e.run(2, test_string)
print(output)
Expand Down
2 changes: 1 addition & 1 deletion tests/full/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class TestDemo(TestCase):
def setUp(self):
self.exe = baygon.Executable(dir_path.joinpath("main.py"))
self.exe = baygon.Executable(dir_path.joinpath("main.exe.py"))

def test_minimal(self):
print(dir_path)
Expand Down
File renamed without changes.

0 comments on commit 499b858

Please sign in to comment.