mirror of
https://github.com/KnugiHK/WhatsApp-Chat-Exporter.git
synced 2026-04-27 00:11:33 +00:00
Updated to also use shutil for JSON file copying
This commit is contained in:
@@ -284,8 +284,7 @@ def incremental_merge(source_dir: str, target_dir: str, media_dir: str, pretty_p
|
|||||||
if not os.path.exists(target_path):
|
if not os.path.exists(target_path):
|
||||||
print(f"Copying '{json_file}' to target directory...")
|
print(f"Copying '{json_file}' to target directory...")
|
||||||
os.makedirs(target_dir, exist_ok=True)
|
os.makedirs(target_dir, exist_ok=True)
|
||||||
with open(source_path, 'rb') as src, open(target_path, 'wb') as dst:
|
shutil.copy2(source_path, target_path)
|
||||||
dst.write(src.read())
|
|
||||||
else:
|
else:
|
||||||
print(
|
print(
|
||||||
f"Merging '{json_file}' with existing file in target directory...")
|
f"Merging '{json_file}' with existing file in target directory...")
|
||||||
|
|||||||
@@ -177,31 +177,20 @@ def test_incremental_merge_new_file(mock_filesystem):
|
|||||||
source_dir = "/source"
|
source_dir = "/source"
|
||||||
target_dir = "/target"
|
target_dir = "/target"
|
||||||
media_dir = "media"
|
media_dir = "media"
|
||||||
|
|
||||||
# Setup mock filesystem
|
# Setup mock filesystem
|
||||||
mock_filesystem["exists"].side_effect = lambda x: x == "/source"
|
mock_filesystem["exists"].side_effect = lambda x: x == "/source"
|
||||||
mock_filesystem["listdir"].return_value = ["chat.json"]
|
mock_filesystem["listdir"].return_value = ["chat.json"]
|
||||||
|
|
||||||
# Mock file operations
|
# Run the function
|
||||||
mock_file_content = {
|
incremental_merge(source_dir, target_dir, media_dir, 2, True)
|
||||||
"/source/chat.json": json.dumps(chat_data_1),
|
|
||||||
}
|
# Verify the operations
|
||||||
|
mock_filesystem["makedirs"].assert_called_once_with(target_dir, exist_ok=True)
|
||||||
with patch("builtins.open", mock_open()) as mock_file:
|
mock_filesystem["copy2"].assert_called_once_with(
|
||||||
|
os.path.join(source_dir, "chat.json"),
|
||||||
def mock_file_read(filename, mode="r"):
|
os.path.join(target_dir, "chat.json")
|
||||||
content = mock_file_content.get(filename)
|
)
|
||||||
file_mock = mock_open(read_data=content).return_value
|
|
||||||
return file_mock
|
|
||||||
|
|
||||||
mock_file.side_effect = mock_file_read
|
|
||||||
|
|
||||||
# Run the function
|
|
||||||
incremental_merge(source_dir, target_dir, media_dir, 2, True)
|
|
||||||
|
|
||||||
# Verify the operations
|
|
||||||
mock_file.assert_any_call("/source/chat.json", "rb")
|
|
||||||
mock_file.assert_any_call("/target/chat.json", "wb")
|
|
||||||
|
|
||||||
|
|
||||||
def test_incremental_merge_existing_file_with_changes(mock_filesystem):
|
def test_incremental_merge_existing_file_with_changes(mock_filesystem):
|
||||||
|
|||||||
Reference in New Issue
Block a user