Update pycatfile.py
[PyCatFile.git] / pycatfile_test.py
blobc01aa81ab3b5ce72f8448a0da931ae124de3de4d
1 import unittest
2 import os
3 from io import BytesIO
4 import pycatfile # Ensure pycatfile.py is accessible
7 class TestPyCatFile(unittest.TestCase):
8 def setUp(self):
9 """Prepare environment for testing."""
10 # Create example files to pack
11 self.test_files = ['test_file1.txt', 'test_file2.txt']
12 for file_name in self.test_files:
13 with open(file_name, 'w') as f:
14 f.write(f'Contents of {file_name}\n')
16 # Name of the packed file for testing
17 self.packed_file = 'test_packed.cat'
19 def tearDown(self):
20 """Clean up after tests."""
21 # Remove created test files and packed file
22 for file_name in self.test_files + [self.packed_file]:
23 try:
24 os.remove(file_name)
25 except FileNotFoundError:
26 pass # File was not created or has been removed already
28 def test_pack_files(self):
29 """Test packing files into a single file."""
30 # Assuming a function PackCatFile exists for packing files
31 with open(self.packed_file, 'wb') as out_file:
32 pycatfile.PackCatFile(
33 self.test_files, out_file, compression="none", checksum="none", verbose=False)
35 # Check if the packed file has been created
36 self.assertTrue(os.path.exists(self.packed_file))
38 def test_list_packed_files(self):
39 """Test listing contents of a packed file."""
40 # First, pack files into a single file
41 with open(self.packed_file, 'wb') as out_file:
42 pycatfile.PackCatFile(
43 self.test_files, out_file, compression="none", checksum="none", verbose=False)
45 # Assuming a function CatFileListFiles exists for listing contents
46 with open(self.packed_file, 'rb') as in_file:
47 contents = pycatfile.CatFileListFiles(in_file, verbose=False)
49 # Check if the contents match the packed files
50 expected_contents = set(self.test_files)
51 self.assertEqual(set(contents), expected_contents)
54 if __name__ == '__main__':
55 unittest.main()