From 510f43216bbe8970fc91296e166025afdfd380b7 Mon Sep 17 00:00:00 2001 From: Nathaniel van Diepen Date: Thu, 14 Mar 2024 14:21:20 -0600 Subject: [PATCH 1/4] Start adding unit tests --- Makefile | 2 ++ test.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 test.py diff --git a/Makefile b/Makefile index 2d07df0..7b9dd90 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,8 @@ $(VENV_BIN_ACTIVATE): requirements.remote.txt requirements.txt test: $(VENV_BIN_ACTIVATE) .venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed @echo "[info] Running test" + . $(VENV_BIN_ACTIVATE); \ + python test.py if [ -d .venv/mnt ] && mountpoint -q .venv/mnt; then \ umount -ql .venv/mnt; \ fi diff --git a/test.py b/test.py new file mode 100644 index 0000000..4c4d286 --- /dev/null +++ b/test.py @@ -0,0 +1,37 @@ +import sys +import codexctl + +FAILED = False + +print("Testing set_server_config: ", end="") +result = codexctl.set_server_config( + """ +[General] +#REMARKABLE_RELEASE_APPID={98DA7DF2-4E3E-4744-9DE6-EC931886ABAB} +#SERVER=https://updates.cloud.remarkable.engineering/service/update2 +#GROUP=Prod +#PLATFORM=reMarkable2 +REMARKABLE_RELEASE_VERSION=3.9.5.2026 +""", + "test", +) +if ( + result + == """ +[General] +SERVER=test +#REMARKABLE_RELEASE_APPID={98DA7DF2-4E3E-4744-9DE6-EC931886ABAB} +#SERVER=https://updates.cloud.remarkable.engineering/service/update2 +#GROUP=Prod +#PLATFORM=reMarkable2 +REMARKABLE_RELEASE_VERSION=3.9.5.2026 +""" +): + FAILED = True + print("fail") + +else: + print("pass") + +if FAILED: + sys.exit(1) From 86556169449b11fea2756b8c4ed73d3f7d525abd Mon Sep 17 00:00:00 2001 From: Nathaniel van Diepen Date: Thu, 14 Mar 2024 14:35:02 -0600 Subject: [PATCH 2/4] Flush out tests and fix issue with set_server_config --- Makefile | 16 ++++++------ codexctl/__init__.py | 8 +++--- test.py | 61 ++++++++++++++++++++++++++++++++------------ 3 files changed, 57 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index 7b9dd90..c77334a 100644 --- a/Makefile +++ b/Makefile @@ -29,14 +29,14 @@ test: $(VENV_BIN_ACTIVATE) .venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed @echo "[info] Running test" . $(VENV_BIN_ACTIVATE); \ python test.py - if [ -d .venv/mnt ] && mountpoint -q .venv/mnt; then \ - umount -ql .venv/mnt; \ - fi - mkdir -p .venv/mnt - . $(VENV_BIN_ACTIVATE); \ - python -m codexctl mount --out .venv/mnt ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed" - mountpoint .venv/mnt - umount -ql .venv/mnt +# if [ -d .venv/mnt ] && mountpoint -q .venv/mnt; then \ +# umount -ql .venv/mnt; \ +# fi +# mkdir -p .venv/mnt +# . $(VENV_BIN_ACTIVATE); \ +# python -m codexctl mount --out .venv/mnt ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed" +# mountpoint .venv/mnt +# umount -ql .venv/mnt clean: @echo "[info] Cleaning" diff --git a/codexctl/__init__.py b/codexctl/__init__.py index a45fe8c..a4edbf6 100644 --- a/codexctl/__init__.py +++ b/codexctl/__init__.py @@ -142,11 +142,11 @@ def set_server_config(contents, server_host_name): data_attributes = contents.split("\n") line = 0 - logger.debug(f"Contents are: {contents}") + logger.debug(f"Contents are:\n{contents}") for i in range(0, len(data_attributes)): - if data_attributes[i].startswith("[GENERAL]"): - logger.debug("Found GENERAL= line") + if data_attributes[i].startswith("[General]"): + logger.debug("Found [General] line") line = i + 1 if not data_attributes[i].startswith("SERVER="): continue @@ -157,7 +157,7 @@ def set_server_config(contents, server_host_name): data_attributes.insert(line, f"SERVER={server_host_name}") converted = "\n".join(data_attributes) - logger.debug("Converted contents are: {converted}") + logger.debug(f"Converted contents are:\n{converted}") return converted diff --git a/test.py b/test.py index 4c4d286..224a55d 100644 --- a/test.py +++ b/test.py @@ -1,37 +1,66 @@ import sys +import difflib import codexctl FAILED = False -print("Testing set_server_config: ", end="") -result = codexctl.set_server_config( - """ -[General] + +def test_set_server_config(original, expected): + global FAILED + print("Testing set_server_config: ", end="") + result = codexctl.set_server_config(original, "test") + if result == expected: + print("pass") + return + + FAILED = True + print("fail") + for diff in difflib.ndiff(expected.splitlines(), result.splitlines()): + print(f" {diff}") + + +test_set_server_config( + "", + "SERVER=test\n", +) + +test_set_server_config( + """[General] #REMARKABLE_RELEASE_APPID={98DA7DF2-4E3E-4744-9DE6-EC931886ABAB} #SERVER=https://updates.cloud.remarkable.engineering/service/update2 #GROUP=Prod #PLATFORM=reMarkable2 REMARKABLE_RELEASE_VERSION=3.9.5.2026 """, - "test", -) -if ( - result - == """ -[General] + """[General] SERVER=test #REMARKABLE_RELEASE_APPID={98DA7DF2-4E3E-4744-9DE6-EC931886ABAB} #SERVER=https://updates.cloud.remarkable.engineering/service/update2 #GROUP=Prod #PLATFORM=reMarkable2 REMARKABLE_RELEASE_VERSION=3.9.5.2026 -""" -): - FAILED = True - print("fail") +""", +) -else: - print("pass") +test_set_server_config( + """[General] +SERVER=testing +#REMARKABLE_RELEASE_APPID={98DA7DF2-4E3E-4744-9DE6-EC931886ABAB} +#SERVER=https://updates.cloud.remarkable.engineering/service/update2 +#GROUP=Prod +#PLATFORM=reMarkable2 +REMARKABLE_RELEASE_VERSION=3.9.5.2026 +""", + """[General] +SERVER=test +#SERVER=testing +#REMARKABLE_RELEASE_APPID={98DA7DF2-4E3E-4744-9DE6-EC931886ABAB} +#SERVER=https://updates.cloud.remarkable.engineering/service/update2 +#GROUP=Prod +#PLATFORM=reMarkable2 +REMARKABLE_RELEASE_VERSION=3.9.5.2026 +""", +) if FAILED: sys.exit(1) From b1b2c1e1e3131032de592ad4bb173f179aef9ca1 Mon Sep 17 00:00:00 2001 From: Nathaniel van Diepen Date: Thu, 14 Mar 2024 14:38:56 -0600 Subject: [PATCH 3/4] uncomment existing test --- Makefile | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index c77334a..7b9dd90 100644 --- a/Makefile +++ b/Makefile @@ -29,14 +29,14 @@ test: $(VENV_BIN_ACTIVATE) .venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed @echo "[info] Running test" . $(VENV_BIN_ACTIVATE); \ python test.py -# if [ -d .venv/mnt ] && mountpoint -q .venv/mnt; then \ -# umount -ql .venv/mnt; \ -# fi -# mkdir -p .venv/mnt -# . $(VENV_BIN_ACTIVATE); \ -# python -m codexctl mount --out .venv/mnt ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed" -# mountpoint .venv/mnt -# umount -ql .venv/mnt + if [ -d .venv/mnt ] && mountpoint -q .venv/mnt; then \ + umount -ql .venv/mnt; \ + fi + mkdir -p .venv/mnt + . $(VENV_BIN_ACTIVATE); \ + python -m codexctl mount --out .venv/mnt ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed" + mountpoint .venv/mnt + umount -ql .venv/mnt clean: @echo "[info] Cleaning" From 642549ff5ec72b1fca0d5823c997f201dd06ff98 Mon Sep 17 00:00:00 2001 From: Nathaniel van Diepen Date: Thu, 14 Mar 2024 14:50:19 -0600 Subject: [PATCH 4/4] Don't output progress bar when not outputting to a terminal --- codexctl/updates.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/codexctl/updates.py b/codexctl/updates.py index 3c5198e..693ca82 100644 --- a/codexctl/updates.py +++ b/codexctl/updates.py @@ -255,13 +255,15 @@ def download_file( for data in response.iter_content(chunk_size=4096): dl += len(data) f.write(data) - done = int(50 * dl / total_length) - sys.stdout.write("\r[%s%s]" % ("=" * done, " " * (50 - done))) - sys.stdout.flush() + if sys.stdout.isatty(): + done = int(50 * dl / total_length) + sys.stdout.write("\r[%s%s]" % ("=" * done, " " * (50 - done))) + sys.stdout.flush() - print(end="\r\n") + if sys.stdout.isatty(): + print(end="\r\n") - self.logger.debug(f"Downloaded filename") + self.logger.debug("Downloaded filename") if os.path.getsize(filename) != total_length: os.remove(filename)