|
1 | | -# EDIT: Import your main class or module here |
2 | | -# from your_package import YourMainClass |
| 1 | +import subprocess |
| 2 | +import sys |
| 3 | +import tempfile |
| 4 | +from pathlib import Path |
3 | 5 |
|
4 | 6 |
|
5 | 7 | def test_smoke(): |
6 | | - print("Smoke test starting...") |
7 | | - # EDIT: Add real functionality verification |
8 | | - # e.g., result = YourMainClass.method() |
9 | | - # assert result is not None |
| 8 | + """ |
| 9 | + Basic smoke test to ensure the CLI is functional and can be imported. |
| 10 | + """ |
| 11 | + print("Running smoke test...") |
| 12 | + |
| 13 | + # 1. Check help command |
| 14 | + # Using sys.executable -m to ensure we test the current environment |
| 15 | + result = subprocess.run( |
| 16 | + [sys.executable, "-m", "beancount_cli.cli", "--help"], |
| 17 | + capture_output=True, |
| 18 | + text=True, |
| 19 | + check=False, |
| 20 | + ) |
| 21 | + assert result.returncode == 0 |
| 22 | + assert "Beancount CLI tool" in result.stdout |
| 23 | + |
| 24 | + # 2. Check version command |
| 25 | + result = subprocess.run( |
| 26 | + [sys.executable, "-m", "beancount_cli.cli", "--version"], |
| 27 | + capture_output=True, |
| 28 | + text=True, |
| 29 | + check=False, |
| 30 | + ) |
| 31 | + assert result.returncode == 0 |
| 32 | + assert "beancount-cli" in result.stdout |
| 33 | + |
| 34 | + # 3. Check transaction schema command |
| 35 | + result = subprocess.run( |
| 36 | + [sys.executable, "-m", "beancount_cli.cli", "transaction", "schema"], |
| 37 | + capture_output=True, |
| 38 | + text=True, |
| 39 | + check=False, |
| 40 | + ) |
| 41 | + assert result.returncode == 0 |
| 42 | + assert "TransactionModel" in result.stdout |
| 43 | + |
| 44 | + # 4. Check with a minimal ledger file |
| 45 | + with tempfile.NamedTemporaryFile(mode="w", suffix=".beancount", delete=False) as f: |
| 46 | + f.write('2023-01-01 open Assets:Cash USD\n') |
| 47 | + temp_path = Path(f.name) |
| 48 | + |
| 49 | + try: |
| 50 | + result = subprocess.run( |
| 51 | + [sys.executable, "-m", "beancount_cli.cli", "check", str(temp_path)], |
| 52 | + capture_output=True, |
| 53 | + text=True, |
| 54 | + check=False, |
| 55 | + ) |
| 56 | + assert result.returncode == 0 |
| 57 | + assert "No errors found" in result.stdout |
| 58 | + finally: |
| 59 | + if temp_path.exists(): |
| 60 | + temp_path.unlink() |
| 61 | + |
10 | 62 | print("Smoke test passed.") |
11 | 63 |
|
12 | 64 |
|
13 | 65 | if __name__ == "__main__": |
14 | | - test_smoke() |
| 66 | + try: |
| 67 | + test_smoke() |
| 68 | + except AssertionError as e: |
| 69 | + print(f"Smoke test failed: {e}") |
| 70 | + sys.exit(1) |
| 71 | + except Exception as e: |
| 72 | + print(f"An error occurred: {e}") |
| 73 | + import traceback |
| 74 | + |
| 75 | + traceback.print_exc() |
| 76 | + sys.exit(1) |
0 commit comments