Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions cycode/cli/zip_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def __init__(self):

def append(self, filename, unique_id, content):
# Write the file to the in-memory zip
filename = filename if unique_id is None else concat_unique_id(filename, unique_id)
if unique_id:
filename = concat_unique_id(filename, unique_id)

self.zip.writestr(filename, content)

def close(self):
Expand All @@ -24,4 +26,8 @@ def read(self) -> bytes:


def concat_unique_id(filename: str, unique_id: str) -> str:
return f'{unique_id}{filename}' if filename.startswith(os.sep) else os.path.join(unique_id, filename)
if filename.startswith(os.sep):
# remove leading slash to join path correctly
filename = filename[len(os.sep):]

return os.path.join(unique_id, filename)
21 changes: 17 additions & 4 deletions tests/test_zip_file.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import os

from cycode.cli import zip_file


def test_concat_unique_id_to_file_starting_with_seperator():
assert zip_file.concat_unique_id('/path/to/file', 'unique_id') == 'unique_id/path/to/file'
def test_concat_unique_id_to_file_with_leading_slash():
filename = os.path.join('path', 'to', 'file') # we should care about slash characters in tests
unique_id = 'unique_id'

expected_path = os.path.join(unique_id, filename)

filename = os.sep + filename
assert zip_file.concat_unique_id(filename, unique_id) == expected_path


def test_concat_unique_id_to_file_without_leading_slash():
filename = os.path.join('path', 'to', 'file') # we should care about slash characters in tests
unique_id = 'unique_id'

expected_path = os.path.join(unique_id, *filename.split('/'))

def test_concat_unique_id_to_file_starting_without_seperator():
assert zip_file.concat_unique_id('path/to/file', 'unique_id') == 'unique_id/path/to/file'
assert zip_file.concat_unique_id(filename, unique_id) == expected_path