Skip to content

Commit 85d1b43

Browse files
author
Stavros Ntentos
committed
If smartless is invoked with a single [-h|-\?|--help|-v|-V|--version], act accordingly
Signed-off-by: Stavros Ntentos <[email protected]>
1 parent d2c402f commit 85d1b43

File tree

6 files changed

+180
-0
lines changed

6 files changed

+180
-0
lines changed

Diff for: .github/workflows/test.yml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- 'dev/**'
8+
tags:
9+
- '*'
10+
pull_request:
11+
branches:
12+
- master
13+
14+
permissions:
15+
contents: read
16+
17+
env:
18+
PYTHON_VERSION: 3.9
19+
20+
jobs:
21+
tests:
22+
name: Core Test
23+
if: "!contains(github.event.head_commit.message, '[CI SKIP]')"
24+
runs-on: ubuntu-latest
25+
steps:
26+
- uses: actions/checkout@v3
27+
- name: Set up Python ${{ env.PYTHON_VERSION }}
28+
uses: actions/setup-python@v4
29+
with:
30+
python-version: '${{ env.PYTHON_VERSION }}'
31+
- name: Install dependencies
32+
run: |
33+
python -m pip install --upgrade --requirement requirements-dev.txt
34+
- name: Run tests
35+
run: |
36+
pytest

Diff for: smartless

+25
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,31 @@ for var ; do
119119
fi
120120
done
121121

122+
if [ "${#args[@]}" -eq 1 ] && [ "${SMARTLESS_PAGER}" == 'less' ] ; then
123+
case "${args[0]}" in
124+
-h|-\?|--help)
125+
head -7 "${BASH_SOURCE[0]}"
126+
if [ "${args[0]}" == '-h' ] ; then
127+
echo
128+
echo "less (v551) has no '-h' argument; but invoking help"
129+
fi
130+
less -?
131+
exit $?
132+
;;
133+
-v|-V|--version)
134+
head -7 "${BASH_SOURCE[0]}"
135+
echo
136+
if [ "${args[0]}" == '-v' ] ; then
137+
echo "less (v551) has no '-v' argument; but invoking version"
138+
echo
139+
fi
140+
less -V
141+
exit $?
142+
;;
143+
*);;
144+
esac
145+
fi
146+
122147
# if more than one file provided on cmdline, revert to original less ...
123148
if (( isfile > 1 )); then
124149
exec "${SMARTLESS_PAGER}" "${SMARTLESS_PAGER_ARGUMENTS}" "$@"

Diff for: tests/__init__.py

Whitespace-only changes.

Diff for: tests/conftest.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
import pty
5+
import sys
6+
from pathlib import Path
7+
8+
import pytest
9+
10+
from tests.structures import FixtureFD
11+
12+
SMARTLESS_OPT = "--smartless"
13+
14+
15+
@pytest.fixture
16+
def smartless(request):
17+
smartless_argv = [str(request.config.getoption(SMARTLESS_OPT))]
18+
19+
debug = getattr(request.config, "use_pdb", False)
20+
debug |= getattr(request.config, "use_pudb", False)
21+
debug |= sys.modules.get("_jb_runner_tools") is not None
22+
23+
if debug:
24+
smartless_argv = ["bash", "-x"] + smartless_argv
25+
26+
yield smartless_argv
27+
28+
29+
@pytest.fixture
30+
def pty_factory():
31+
master_fd, slave_fd = pty.openpty()
32+
33+
yield FixtureFD(parent=master_fd, child=slave_fd)
34+
35+
os.close(master_fd)
36+
os.close(slave_fd)
37+
38+
39+
pty_stdin = pty_factory
40+
pty_stdout = pty_factory
41+
pty_stderr = pty_factory
42+
43+
44+
def pytest_addoption(parser):
45+
parser.addoption(
46+
SMARTLESS_OPT,
47+
action="store",
48+
default=Path(__file__).parent.parent / "smartless",
49+
help="smartless path",
50+
)

Diff for: tests/structures.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING, NamedTuple
4+
5+
if TYPE_CHECKING:
6+
from _typeshed import FileDescriptorLike
7+
8+
9+
class Dimensions(NamedTuple):
10+
rows: int
11+
columns: int
12+
13+
14+
class FixtureFD(NamedTuple):
15+
parent: FileDescriptorLike
16+
child: FileDescriptorLike

Diff for: tests/test_smartless.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
import re
5+
import subprocess
6+
7+
import pytest
8+
9+
10+
@pytest.mark.parametrize("help_flag", ["-h", "--help", "-?"])
11+
def test_help(smartless, help_flag):
12+
with subprocess.Popen(
13+
[*smartless, help_flag],
14+
text=True,
15+
stdout=subprocess.PIPE,
16+
) as process:
17+
stdout, _ = process.communicate(None)
18+
19+
with open("smartless", encoding="utf-8") as f:
20+
smartless_head = "".join([next(f) for _ in range(7)])
21+
22+
assert smartless_head in stdout
23+
assert process.wait(5) == os.EX_OK
24+
25+
if help_flag == "-h":
26+
assert "less (v551) has no '-h' argument; but invoking help" in stdout
27+
28+
assert "Commands marked with * may be preceded by a number," in stdout
29+
30+
31+
@pytest.mark.parametrize("version_flag", ["-v", "-V", "--version"])
32+
def test_version(smartless, version_flag):
33+
with subprocess.Popen(
34+
[*smartless, "-v"], text=True, stdout=subprocess.PIPE
35+
) as process:
36+
stdout, _ = process.communicate()
37+
38+
with open("smartless", encoding="utf-8") as f:
39+
smartless_head = "".join([next(f) for _ in range(7)])
40+
41+
assert smartless_head in stdout
42+
assert process.wait(5) == os.EX_OK
43+
44+
if version_flag == "-v":
45+
assert "less (v551) has no '-v' argument; but invoking version" in stdout
46+
47+
assert re.search(r"less \d+ \(GNU regular expressions\)", stdout)
48+
49+
50+
if __name__ == "__main__":
51+
import sys
52+
53+
sys.exit(pytest.main())

0 commit comments

Comments
 (0)