1+ #!/usr/bin/env python3
2+ """
3+ CLI unpack command tests - ensures CLI unpack functionality works correctly.
4+ This test would have caught the binary file mode bug in GitHub issue #113.
5+ """
6+
7+ import os
8+ import tempfile
9+ import shutil
10+ from click .testing import CliRunner
11+ from RAMP import ramp
12+
13+
14+ def test_cli_unpack_binary_files ():
15+ """Test CLI unpack command correctly extracts binary files."""
16+
17+ # Use the existing test bundle
18+ bundle_path = os .path .join (os .getcwd (), "test_assets" ,
19+ "redisgears_python.Linux-ubuntu18.04-x86_64.1.2.5.zip" )
20+
21+ if not os .path .exists (bundle_path ):
22+ print (f"⚠️ Test bundle not found: { bundle_path } " )
23+ print (" Skipping CLI unpack test" )
24+ return True # Skip test if bundle not available
25+
26+ # Create temporary directory for extraction
27+ with tempfile .TemporaryDirectory () as temp_dir :
28+ original_cwd = os .getcwd ()
29+
30+ try :
31+ # Change to temp directory for extraction
32+ os .chdir (temp_dir )
33+
34+ # Test CLI unpack command
35+ runner = CliRunner ()
36+ result = runner .invoke (ramp .unpack , [bundle_path ])
37+
38+ # Verify command succeeded
39+ assert result .exit_code == 0 , (
40+ f"CLI unpack failed with exit code { result .exit_code } \n "
41+ f"Output: { result .output } \n "
42+ f"Exception: { result .exception } "
43+ )
44+
45+ # Check that files were extracted
46+ extracted_files = os .listdir ('.' )
47+ json_files = [f for f in extracted_files if f .endswith ('.json' )]
48+ so_files = [f for f in extracted_files if f .endswith ('.so' )]
49+
50+ assert len (json_files ) > 0 , f"No JSON metadata file extracted. Files: { extracted_files } "
51+ assert len (so_files ) > 0 , f"No .so binary file extracted. Files: { extracted_files } "
52+
53+ # Verify binary file is valid and not corrupted
54+ so_file = so_files [0 ]
55+ file_size = os .path .getsize (so_file )
56+ assert file_size > 0 , f"Binary file { so_file } is empty"
57+ assert file_size > 1000 , f"Binary file { so_file } too small ({ file_size } bytes), likely corrupted"
58+
59+ # Check that it's actually a valid binary file (ELF format for Linux modules)
60+ with open (so_file , 'rb' ) as f :
61+ header = f .read (4 )
62+ assert header == b'\x7f ELF' , (
63+ f"Binary file { so_file } corrupted. Expected ELF header (7f454c46), "
64+ f"got: { header .hex () if header else 'empty' } "
65+ )
66+
67+ # Verify JSON metadata file is valid
68+ json_file = json_files [0 ]
69+ json_size = os .path .getsize (json_file )
70+ assert json_size > 0 , f"JSON file { json_file } is empty"
71+
72+ # Verify JSON is parseable
73+ import json
74+ with open (json_file , 'r' ) as f :
75+ metadata = json .load (f )
76+ assert isinstance (metadata , dict ), "Metadata is not a valid JSON object"
77+ assert 'module_name' in metadata , "Metadata missing module_name"
78+ assert 'module_file' in metadata , "Metadata missing module_file"
79+
80+ print ("✅ CLI unpack test passed!" )
81+ return True
82+
83+ except Exception as e :
84+ print (f"❌ CLI unpack test failed: { e } " )
85+ raise
86+ finally :
87+ os .chdir (original_cwd )
88+
89+
90+ def test_cli_unpack_nonexistent_file ():
91+ """Test CLI unpack command handles missing files gracefully."""
92+
93+ runner = CliRunner ()
94+ result = runner .invoke (ramp .unpack , ['nonexistent_bundle.zip' ])
95+
96+ # Should fail gracefully, not crash
97+ assert result .exit_code != 0 , "Expected failure for nonexistent file"
98+ assert result .exception is not None or "not found" in result .output .lower ()
99+
100+ print ("✅ CLI unpack error handling test passed!" )
101+ return True
102+
103+
104+ if __name__ == '__main__' :
105+ print ("🧪 Running CLI Unpack Tests" )
106+ print ("=" * 35 )
107+
108+ try :
109+ test_cli_unpack_binary_files ()
110+ test_cli_unpack_nonexistent_file ()
111+ print ("\n 🎉 All CLI unpack tests passed!" )
112+ except Exception as e :
113+ print (f"\n ❌ CLI unpack tests failed: { e } " )
114+ raise
0 commit comments