|
| 1 | +#!/usr/bin/env python |
| 2 | +"""Test that the built package can be installed and works correctly.""" |
| 3 | + |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +# Find the dist directory |
| 9 | +dist_dir = Path(__file__).parent.parent.parent / "dist" |
| 10 | +if not dist_dir.exists(): |
| 11 | + print(f"Error: dist directory not found at {dist_dir}") |
| 12 | + sys.exit(1) |
| 13 | + |
| 14 | +# Find wheel or source distribution files |
| 15 | +dist_files = list(dist_dir.glob("*.whl")) + list(dist_dir.glob("*.tar.gz")) |
| 16 | +if not dist_files: |
| 17 | + print(f"Error: No distribution files found in {dist_dir}") |
| 18 | + sys.exit(1) |
| 19 | + |
| 20 | +# Install the package |
| 21 | +dist_file = dist_files[0] |
| 22 | +print(f"Installing {dist_file.name}...") |
| 23 | +result = subprocess.run( |
| 24 | + [sys.executable, "-m", "pip", "install", "--force-reinstall", str(dist_file)], |
| 25 | + capture_output=True, |
| 26 | + text=True, |
| 27 | +) |
| 28 | +if result.returncode != 0: |
| 29 | + print(f"Error installing package: {result.stderr}") |
| 30 | + sys.exit(1) |
| 31 | + |
| 32 | +# Test that the package can be imported and basic functionality works |
| 33 | +print("Testing package import and basic functionality...") |
| 34 | +try: |
| 35 | + import multibase |
| 36 | + |
| 37 | + # Test that version is accessible |
| 38 | + assert hasattr(multibase, "__version__"), "Package should have __version__" |
| 39 | + assert multibase.__version__, "Version should not be empty" |
| 40 | + print(f"✓ Version accessible: {multibase.__version__}") |
| 41 | + |
| 42 | + # Test that main functions are available |
| 43 | + assert callable(multibase.encode), "encode should be callable" |
| 44 | + assert callable(multibase.decode), "decode should be callable" |
| 45 | + assert callable(multibase.is_encoded), "is_encoded should be callable" |
| 46 | + assert callable(multibase.list_encodings), "list_encodings should be callable" |
| 47 | + assert callable(multibase.is_encoding_supported), "is_encoding_supported should be callable" |
| 48 | + assert callable(multibase.get_encoding_info), "get_encoding_info should be callable" |
| 49 | + print("✓ Main functions are callable") |
| 50 | + |
| 51 | + # Test that exceptions are importable |
| 52 | + assert hasattr(multibase, "UnsupportedEncodingError"), "UnsupportedEncodingError should be available" |
| 53 | + assert hasattr(multibase, "InvalidMultibaseStringError"), "InvalidMultibaseStringError should be available" |
| 54 | + assert hasattr(multibase, "DecodingError"), "DecodingError should be available" |
| 55 | + print("✓ Exceptions are importable") |
| 56 | + |
| 57 | + # Test that classes are importable |
| 58 | + assert hasattr(multibase, "Encoder"), "Encoder class should be available" |
| 59 | + assert hasattr(multibase, "Decoder"), "Decoder class should be available" |
| 60 | + print("✓ Classes are importable") |
| 61 | + |
| 62 | + # Test basic encode/decode with multiple encodings |
| 63 | + test_data = "hello world" |
| 64 | + |
| 65 | + # Test base64 |
| 66 | + encoded = multibase.encode("base64", test_data) |
| 67 | + assert encoded.startswith(b"m"), "Base64 encoding should start with 'm'" |
| 68 | + decoded = multibase.decode(encoded) |
| 69 | + assert decoded == b"hello world", "Base64 decoded data should match original" |
| 70 | + |
| 71 | + # Test base16 |
| 72 | + encoded = multibase.encode("base16", test_data) |
| 73 | + assert encoded.startswith(b"f"), "Base16 encoding should start with 'f'" |
| 74 | + decoded = multibase.decode(encoded) |
| 75 | + assert decoded == b"hello world", "Base16 decoded data should match original" |
| 76 | + |
| 77 | + # Test base32 |
| 78 | + encoded = multibase.encode("base32", test_data) |
| 79 | + assert encoded.startswith(b"b"), "Base32 encoding should start with 'b'" |
| 80 | + decoded = multibase.decode(encoded) |
| 81 | + assert decoded == b"hello world", "Base32 decoded data should match original" |
| 82 | + print("✓ Multiple encoding types work (base64, base16, base32)") |
| 83 | + |
| 84 | + # Test is_encoded |
| 85 | + assert multibase.is_encoded(encoded), "is_encoded should return True for valid multibase string" |
| 86 | + assert not multibase.is_encoded("invalid"), "is_encoded should return False for invalid string" |
| 87 | + print("✓ is_encoded function works") |
| 88 | + |
| 89 | + # Test is_encoding_supported |
| 90 | + assert multibase.is_encoding_supported("base64"), "base64 should be supported" |
| 91 | + assert multibase.is_encoding_supported("base16"), "base16 should be supported" |
| 92 | + assert not multibase.is_encoding_supported("base999"), "base999 should not be supported" |
| 93 | + print("✓ is_encoding_supported function works") |
| 94 | + |
| 95 | + # Test list_encodings |
| 96 | + encodings = multibase.list_encodings() |
| 97 | + assert isinstance(encodings, list), "list_encodings should return a list" |
| 98 | + assert "base64" in encodings, "base64 should be in encodings list" |
| 99 | + assert "base16" in encodings, "base16 should be in encodings list" |
| 100 | + assert "base32" in encodings, "base32 should be in encodings list" |
| 101 | + assert len(encodings) >= 20, "Should have at least 20 encodings" |
| 102 | + print(f"✓ list_encodings works (found {len(encodings)} encodings)") |
| 103 | + |
| 104 | + # Test get_encoding_info |
| 105 | + info = multibase.get_encoding_info("base64") |
| 106 | + assert info.encoding == "base64", "get_encoding_info should return correct encoding name" |
| 107 | + assert info.code == b"m", "base64 code should be 'm'" |
| 108 | + assert info.converter is not None, "converter should not be None" |
| 109 | + print("✓ get_encoding_info function works") |
| 110 | + |
| 111 | + # Test decode with return_encoding |
| 112 | + test_encoded = multibase.encode("base16", "test") |
| 113 | + encoding, decoded = multibase.decode(test_encoded, return_encoding=True) |
| 114 | + assert encoding == "base16", "return_encoding should return correct encoding" |
| 115 | + assert decoded == b"test", "decoded data should match" |
| 116 | + print("✓ decode with return_encoding works") |
| 117 | + |
| 118 | + # Test Encoder class |
| 119 | + encoder = multibase.Encoder("base64") |
| 120 | + assert encoder.encoding == "base64", "Encoder should have correct encoding" |
| 121 | + encoded = encoder.encode("test") |
| 122 | + assert encoded.startswith(b"m"), "Encoder.encode should work" |
| 123 | + print("✓ Encoder class works") |
| 124 | + |
| 125 | + # Test Decoder class |
| 126 | + decoder = multibase.Decoder() |
| 127 | + test_encoded = multibase.encode("base64", "test") |
| 128 | + decoded = decoder.decode(test_encoded) |
| 129 | + assert decoded == b"test", "Decoder.decode should work" |
| 130 | + encoding, decoded = decoder.decode(test_encoded, return_encoding=True) |
| 131 | + assert encoding == "base64", "Decoder.decode with return_encoding should work" |
| 132 | + print("✓ Decoder class works") |
| 133 | + |
| 134 | + # Test error handling |
| 135 | + try: |
| 136 | + multibase.encode("base999", "test") |
| 137 | + assert False, "Should raise UnsupportedEncodingError" |
| 138 | + except multibase.UnsupportedEncodingError: |
| 139 | + pass |
| 140 | + print("✓ Error handling works (UnsupportedEncodingError)") |
| 141 | + |
| 142 | + try: |
| 143 | + multibase.decode("invalid") |
| 144 | + assert False, "Should raise InvalidMultibaseStringError" |
| 145 | + except multibase.InvalidMultibaseStringError: |
| 146 | + pass |
| 147 | + print("✓ Error handling works (InvalidMultibaseStringError)") |
| 148 | + |
| 149 | + print(f"\n✓ Package installed successfully (version {multibase.__version__})") |
| 150 | + print("✓ All functionality tests passed") |
| 151 | + print("Package is ready for release!") |
| 152 | + |
| 153 | +except ImportError as e: |
| 154 | + print(f"Error importing package: {e}") |
| 155 | + import traceback |
| 156 | + |
| 157 | + traceback.print_exc() |
| 158 | + sys.exit(1) |
| 159 | +except AssertionError as e: |
| 160 | + print(f"Test failed: {e}") |
| 161 | + import traceback |
| 162 | + |
| 163 | + traceback.print_exc() |
| 164 | + sys.exit(1) |
| 165 | +except Exception as e: |
| 166 | + print(f"Unexpected error: {e}") |
| 167 | + import traceback |
| 168 | + |
| 169 | + traceback.print_exc() |
| 170 | + sys.exit(1) |
0 commit comments