Overhaul in printing using rich

This commit is contained in:
pedrocarlo
2025-04-03 23:07:07 -03:00
parent b34e7e011e
commit d71029cda7
6 changed files with 58 additions and 23 deletions

View File

@@ -3,6 +3,7 @@ from cli_tests.test_limbo_cli import TestLimboShell
from pathlib import Path from pathlib import Path
import time import time
import os import os
from cli_tests import console
def test_basic_queries(): def test_basic_queries():
@@ -301,7 +302,7 @@ def test_insert_default_values():
def main(): def main():
print("Running all Limbo CLI tests...") console.info("Running all Limbo CLI tests...")
test_basic_queries() test_basic_queries()
test_schema_operations() test_schema_operations()
test_file_operations() test_file_operations()
@@ -319,7 +320,7 @@ def main():
test_table_patterns() test_table_patterns()
test_update_with_limit() test_update_with_limit()
test_update_with_limit_and_offset() test_update_with_limit_and_offset()
print("All tests have passed") console.info("All tests have passed")
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -4,7 +4,7 @@ from rich.theme import Theme
from rich.style import Style from rich.style import Style
custom_theme = Theme({"info": "blue", "error": "bold red"}) custom_theme = Theme({"info": "bold blue", "error": "bold red", "debug": "bold blue"})
console = Console(theme=custom_theme) console = Console(theme=custom_theme)
@@ -31,7 +31,7 @@ def info(
markup=markup, markup=markup,
highlight=highlight, highlight=highlight,
log_locals=log_locals, log_locals=log_locals,
_stack_offset=_stack_offset, _stack_offset=_stack_offset + 1,
) )
@@ -58,5 +58,32 @@ def error(
markup=markup, markup=markup,
highlight=highlight, highlight=highlight,
log_locals=log_locals, log_locals=log_locals,
_stack_offset=_stack_offset, _stack_offset=_stack_offset + 1,
)
def debug(
*objects: Any,
sep: str = " ",
end: str = "\n",
style: Optional[Union[str, Style]] = None,
justify: Optional[JustifyMethod] = None,
emoji: Optional[bool] = None,
markup: Optional[bool] = None,
highlight: Optional[bool] = None,
log_locals: bool = False,
_stack_offset: int = 1,
):
console.log(
"[debug]DEBUG[/debug]",
*objects,
sep=sep,
end=end,
style=style,
justify=justify,
emoji=emoji,
markup=markup,
highlight=highlight,
log_locals=log_locals,
_stack_offset=_stack_offset + 1,
) )

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os import os
from cli_tests.test_limbo_cli import TestLimboShell from cli_tests.test_limbo_cli import TestLimboShell
from cli_tests import console
sqlite_exec = "./scripts/limbo-sqlite3" sqlite_exec = "./scripts/limbo-sqlite3"
sqlite_flags = os.getenv("SQLITE_FLAGS", "-q").split(" ") sqlite_flags = os.getenv("SQLITE_FLAGS", "-q").split(" ")
@@ -81,7 +82,7 @@ def test_regexp():
lambda res: "Parse error: no such function" in res, lambda res: "Parse error: no such function" in res,
) )
limbo.run_test_fn(f".load {extension_path}", null) limbo.run_test_fn(f".load {extension_path}", null)
print(f"Extension {extension_path} loaded successfully.") console.info(f"Extension {extension_path} loaded successfully.")
limbo.run_test_fn("SELECT regexp('a.c', 'abc');", true) limbo.run_test_fn("SELECT regexp('a.c', 'abc');", true)
limbo.run_test_fn("SELECT regexp('a.c', 'ac');", false) limbo.run_test_fn("SELECT regexp('a.c', 'ac');", false)
limbo.run_test_fn("SELECT regexp('[0-9]+', 'the year is 2021');", true) limbo.run_test_fn("SELECT regexp('[0-9]+', 'the year is 2021');", true)
@@ -522,7 +523,7 @@ def test_vfs():
lambda res: res == "50", lambda res: res == "50",
"Tested large write to testfs", "Tested large write to testfs",
) )
print("Tested large write to testfs") console.info("Tested large write to testfs")
limbo.quit() limbo.quit()
@@ -601,11 +602,11 @@ def main():
test_kv() test_kv()
test_drop_virtual_table() test_drop_virtual_table()
except Exception as e: except Exception as e:
print(f"Test FAILED: {e}") console.error(f"Test FAILED: {e}")
cleanup() cleanup()
exit(1) exit(1)
cleanup() cleanup()
print("All tests passed successfully.") console.info("All tests passed successfully.")
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -5,6 +5,7 @@ from time import sleep
import subprocess import subprocess
from pathlib import Path from pathlib import Path
from typing import Callable, List, Optional from typing import Callable, List, Optional
from cli_tests import console
PIPE_BUF = 4096 PIPE_BUF = 4096
@@ -77,7 +78,7 @@ class LimboShell:
error_output = self.pipe.stderr.read(PIPE_BUF) error_output = self.pipe.stderr.read(PIPE_BUF)
if error_output == b"": if error_output == b"":
return True return True
print(error_output.decode(), end="") console.error(error_output.decode(), end="")
return False return False
@staticmethod @staticmethod
@@ -128,7 +129,7 @@ INSERT INTO t VALUES (zeroblob(1024 - 1), zeroblob(1024 - 2), zeroblob(1024 - 3)
self.shell.quit() self.shell.quit()
def run_test(self, name: str, sql: str, expected: str) -> None: def run_test(self, name: str, sql: str, expected: str) -> None:
print(f"Running test: {name}") console.info(f"Running test: {name}")
actual = self.shell.execute(sql) actual = self.shell.execute(sql)
assert actual == expected, ( assert actual == expected, (
f"Test failed: {name}\n" f"Test failed: {name}\n"
@@ -138,9 +139,9 @@ INSERT INTO t VALUES (zeroblob(1024 - 1), zeroblob(1024 - 2), zeroblob(1024 - 3)
) )
def debug_print(self, sql: str): def debug_print(self, sql: str):
print(f"debugging: {sql}") console.debug(f"debugging: {sql}")
actual = self.shell.execute(sql) actual = self.shell.execute(sql)
print(f"OUTPUT:\n{repr(actual)}") console.debug(f"OUTPUT:\n{repr(actual)}")
def run_test_fn( def run_test_fn(
self, sql: str, validate: Callable[[str], bool], desc: str = "" self, sql: str, validate: Callable[[str], bool], desc: str = ""
@@ -148,7 +149,7 @@ INSERT INTO t VALUES (zeroblob(1024 - 1), zeroblob(1024 - 2), zeroblob(1024 - 3)
# Print the test that is executing before executing the sql command # Print the test that is executing before executing the sql command
# Printing later confuses the user of the code what test has actually failed # Printing later confuses the user of the code what test has actually failed
if desc: if desc:
print(f"Testing: {desc}") console.info(f"Testing: {desc}")
actual = self.shell.execute(sql) actual = self.shell.execute(sql)
assert validate(actual), f"Test failed\nSQL: {sql}\nActual:\n{repr(actual)}" assert validate(actual), f"Test failed\nSQL: {sql}\nActual:\n{repr(actual)}"

View File

@@ -2,6 +2,7 @@
import os import os
from cli_tests.test_limbo_cli import TestLimboShell from cli_tests.test_limbo_cli import TestLimboShell
from pydantic import BaseModel from pydantic import BaseModel
from cli_tests import console
sqlite_flags = os.getenv("SQLITE_FLAGS", "-q").split(" ") sqlite_flags = os.getenv("SQLITE_FLAGS", "-q").split(" ")
@@ -61,7 +62,7 @@ class UpdateTest(BaseModel):
limbo.run_test(self.name, stmt, "") limbo.run_test(self.name, stmt, "")
def test_compat(self): def test_compat(self):
print("Testing in SQLite\n") console.info("Testing in SQLite\n")
with TestLimboShell( with TestLimboShell(
init_commands="", init_commands="",
@@ -99,7 +100,7 @@ class UpdateTest(BaseModel):
"".join(stmt), "".join(stmt),
"\n".join(expected), "\n".join(expected),
) )
print() console.info()
def cleanup(db_fullpath: str): def cleanup(db_fullpath: str):
@@ -113,6 +114,8 @@ def cleanup(db_fullpath: str):
def main(): def main():
test = UpdateTest(name="Update 1 column", vals=1) test = UpdateTest(name="Update 1 column", vals=1)
console.info(test)
db_path = test.db_path db_path = test.db_path
try: try:
test.init_db() test.init_db()
@@ -123,12 +126,12 @@ def main():
test.test_compat() test.test_compat()
except Exception as e: except Exception as e:
print(f"Test FAILED: {e}") console.error(f"Test FAILED: {e}")
cleanup(db_path) cleanup(db_path)
exit(1) exit(1)
# delete db after every compat test so we we have fresh db for next test # delete db after every compat test so we we have fresh db for next test
cleanup(db_path) cleanup(db_path)
print("All tests passed successfully.") console.info("All tests passed successfully.")
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -2,6 +2,7 @@
import os import os
from cli_tests.test_limbo_cli import TestLimboShell from cli_tests.test_limbo_cli import TestLimboShell
from pydantic import BaseModel from pydantic import BaseModel
from cli_tests import console
sqlite_flags = os.getenv("SQLITE_FLAGS", "-q").split(" ") sqlite_flags = os.getenv("SQLITE_FLAGS", "-q").split(" ")
@@ -44,7 +45,7 @@ class InsertTest(BaseModel):
) )
def test_compat(self): def test_compat(self):
print("Testing in SQLite\n") console.info("Testing in SQLite\n")
with TestLimboShell( with TestLimboShell(
init_commands="", init_commands="",
@@ -66,7 +67,7 @@ class InsertTest(BaseModel):
lambda res: res == str(self.vals * 2), lambda res: res == str(self.vals * 2),
"Counting total rows inserted", "Counting total rows inserted",
) )
print() console.info()
def validate_with_expected(result: str, expected: str): def validate_with_expected(result: str, expected: str):
@@ -133,6 +134,7 @@ def cleanup(db_fullpath: str):
def main(): def main():
tests = blob_tests() tests = blob_tests()
for test in tests: for test in tests:
console.info(test)
db_path = test.db_path db_path = test.db_path
try: try:
# Use with syntax to automatically close shell on error # Use with syntax to automatically close shell on error
@@ -143,12 +145,12 @@ def main():
test.test_compat() test.test_compat()
except Exception as e: except Exception as e:
print(f"Test FAILED: {e}") console.error(f"Test FAILED: {e}")
cleanup(db_path) cleanup(db_path)
exit(1) exit(1)
# delete db after every compat test so we we have fresh db for next test # delete db after every compat test so we we have fresh db for next test
cleanup(db_path) cleanup(db_path)
print("All tests passed successfully.") console.info("All tests passed successfully.")
if __name__ == "__main__": if __name__ == "__main__":