Skip to content

Commit

Permalink
Move ls and cat tests into test.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Eeems committed May 26, 2024
1 parent 6300b74 commit 70c25af
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 18 deletions.
23 changes: 6 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,19 @@ test: $(VENV_BIN_ACTIVATE) .venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed
@echo "[info] Running test"
@set -e; \
. $(VENV_BIN_ACTIVATE); \
python test.py
if [ -d .venv/mnt ] && mountpoint -q .venv/mnt; then \
umount -ql .venv/mnt; \
fi
mkdir -p .venv/mnt
@set -e; \
. $(VENV_BIN_ACTIVATE); \
python test.py; \
if [[ "linux" == "$$(python -c 'import sys;print(sys.platform)')" ]]; then \
if [ -d .venv/mnt ] && mountpoint -q .venv/mnt; then \
umount -ql .venv/mnt; \
fi; \
mkdir -p .venv/mnt; \
python -m codexctl mount --out .venv/mnt ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed"; \
mountpoint .venv/mnt; \
umount -ql .venv/mnt; \
fi; \
python -m codexctl extract --out ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.img" ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed"; \
echo "${IMG_SHA} .venv/${FW_VERSION}_reMarkable2-${FW_DATA}.img" | sha256sum --check; \
rm -f ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.img"; \
set -o pipefail; \
if ! diff --color <(python -m codexctl ls ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed" /) <(echo ${LS_DATA}) | cat -te; then \
echo "codexctl ls failed test"; \
exit 1; \
fi; \
if ! diff --color <(python -m codexctl cat ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed" /etc/version) <(echo ${CAT_DATA}) | cat -te; then \
echo "codexctl cat failed test"; \
exit 1; \
fi
rm -f ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.img"

test-executable: .venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed
@set -e; \
Expand Down
89 changes: 88 additions & 1 deletion test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
import os
import sys
import difflib
import contextlib
import codexctl

from collections import namedtuple
from io import StringIO
from io import BytesIO

FAILED = False
UPDATE_FILE_PATH = ".venv/2.15.1.1189_reMarkable2-wVbHkgKisg-.signed"

assert os.path.exists(UPDATE_FILE_PATH), "Update image missing"


class BufferWriter:
def __init__(self, buffer):
self._buffer = buffer

def write(self, data):
self._buffer.write(data)


class BufferBytesIO(BytesIO):
@property
def buffer(self):
return BufferWriter(self)


def test_set_server_config(original, expected):
Expand All @@ -15,7 +38,60 @@ def test_set_server_config(original, expected):

FAILED = True
print("fail")
for diff in difflib.ndiff(expected.splitlines(), result.splitlines()):
for diff in difflib.ndiff(
expected.splitlines(keepends=True), result.splitlines(keepends=True)
):
print(f" {diff}")


def test_ls(path, expected):
global FAILED
global UPDATE_FILE_PATH
print(f"Testing ls {path}: ", end="")
with contextlib.redirect_stdout(StringIO()) as f:
Args = namedtuple("Args", "file target_path")
try:
codexctl.do_ls(Args(file=UPDATE_FILE_PATH, target_path=path))

except SystemExit:
pass

result = f.getvalue()
if result == expected:
print("pass")
return

FAILED = True
print("fail")
for diff in difflib.ndiff(
expected.splitlines(keepends=True), result.splitlines(keepends=True)
):
print(f" {diff}")


def test_cat(path, expected):
global FAILED
global UPDATE_FILE_PATH
print(f"Testing ls {path}: ", end="")
with contextlib.redirect_stdout(BufferBytesIO()) as f:
Args = namedtuple("Args", "file target_path")
try:
codexctl.do_cat(Args(file=UPDATE_FILE_PATH, target_path=path))

except SystemExit:
pass

result = f.getvalue()
if result == expected:
print("pass")
return

FAILED = True
print("fail")
for diff in difflib.ndiff(
expected.decode("utf-8").splitlines(keepends=True),
result.decode("utf-8").splitlines(keepends=True),
):
print(f" {diff}")


Expand Down Expand Up @@ -62,5 +138,16 @@ def test_set_server_config(original, expected):
""",
)

test_ls(
"/",
". .. lost+found bin boot dev etc home lib media mnt postinst proc run sbin sys tmp uboot-postinst uboot-version usr var\n",
)
test_ls(
"/mnt",
". ..\n",
)

test_cat("/etc/version", b"20221026104022\n")

if FAILED:
sys.exit(1)

0 comments on commit 70c25af

Please sign in to comment.