Skip to content

Commit

Permalink
test: make tests platform-agnostic and faster
Browse files Browse the repository at this point in the history
Add a `-d`/`--dump` test.
  • Loading branch information
dbohdan committed Oct 14, 2024
1 parent f47411a commit 1a5edea
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions tests/test_memsparkline.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import annotations

import os
import re
import shlex
Expand All @@ -27,6 +29,7 @@
from pathlib import Path

PYTHON = sys.executable
PYTHON_SLEEP_COMMAND = [PYTHON, "-c", "import time; time.sleep(0.1)"]
TEST_PATH = Path(__file__).resolve().parent

COMMAND = shlex.split(os.environ.get("MEMSPARKLINE_COMMAND", ""))
Expand Down Expand Up @@ -56,46 +59,47 @@ def run(
return output


def sleep_command(duration: float = 0.1) -> list[str]:
return [PYTHON, "-c", f"import time; time.sleep({duration})"]


class TestMemsparkline(unittest.TestCase):
def test_usage(self) -> None:
assert re.search("^usage", run(check=False))

def test_version(self) -> None:
assert re.search("\\d+\\.\\d+\\.\\d+", run("-v", return_stdout=True))


@unittest.skipUnless(os.name == "posix", "requires a POSIX OS")
class TestMemsparklinePOSIX(unittest.TestCase):
def test_basic(self) -> None:
assert re.search("(?s).*avg:.*max:", run("sleep", "1"))
assert re.search("(?s).*avg:.*max:", run(*sleep_command()))

def test_length(self) -> None:
stderr = run("-l", "10", "-w", "10", "sleep", "1")
stderr = run("-l", "10", "-w", "10", *sleep_command(0.3))

assert re.search("(?m)\\r[^ ]{10} \\d+\\.\\d\\n avg", stderr)

def test_mem_format(self) -> None:
stderr = run("-l", "10", "-w", "10", "-m", "%0.2f", "sleep", "1")
stderr = run("-l", "10", "-w", "10", "-m", "%0.2f", *sleep_command(0.3))

assert re.search("(?m)\\r[^ ]{10} \\d+\\.\\d{2}\\n avg", stderr)

def test_time_format(self) -> None:
stderr = run("-l", "10", "-t", "%d:%05d:%06.3f", "sleep", "1")
stderr = run("-l", "10", "-t", "%d:%05d:%06.3f", *sleep_command())

assert re.search("(?m)time: \\d+\\:\\d{5}:\\d{2}\\.\\d{3}\\n", stderr)

def test_wait_1(self) -> None:
stderr = run("-w", "2000", "sleep", "1")
stderr = run("-w", "2000", *sleep_command())

assert len(stderr.split("\n")) == 5

def test_wait_2(self) -> None:
stderr = run("-n", "-w", "100", "sleep", "1")
stderr = run("-n", "-w", "50", *sleep_command(0.5))

assert len(stderr.split("\n")) in range(10, 15)

def test_quiet(self) -> None:
stderr = run("-q", "sleep", "1")
stderr = run("-q", *sleep_command())

assert re.search("^ avg", stderr)

Expand All @@ -113,28 +117,29 @@ def test_double_dash(self) -> None:
def test_two_double_dashes(self) -> None:
assert "\n" in run("--", *COMMAND, "--", "ls", "-l", return_stdout=True)

def test_dump(self) -> None:
dump_path = Path(TEST_PATH, "dump.log")
if dump_path.exists():
dump_path.unlink()

run("-q", "-w", "100", "-d", str(dump_path), *sleep_command(0.2))

lines = dump_path.read_text().splitlines()

assert len(lines) == 3
assert all(re.match(r"\d+", line) for line in lines)

def test_output(self) -> None:
output_path = Path(TEST_PATH, "output.log")
if output_path.exists():
output_path.unlink()

for _ in range(2):
run("-q", "-o", str(output_path), "sleep", "1")
run("-q", "-o", str(output_path), *sleep_command())

text = output_path.read_text()
assert len(text.split("\n")) == 7


@unittest.skipUnless(os.name == "nt", "requires Windows")
class TestMemsparklineWindows(unittest.TestCase):
def test_cmd_basic(self) -> None:
assert re.search("(?s).*avg:.*max:", run("cmd.exe", "/c", "dir"))

def test_python_sleep(self) -> None:
stderr = run("-w", "2000", PYTHON, "-c", "import time; time.sleep(1)")

assert len(stderr.split("\n")) == 5


if __name__ == "__main__":
unittest.main()

0 comments on commit 1a5edea

Please sign in to comment.