Skip to content

Commit bbbd027

Browse files
authored
Check the extension of extracted file for backward matching (#65)
* Check the extension of extracted file for backward matching * Add unit test for extract-archive
1 parent 8d9caa3 commit bbbd027

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

jupyter_archive/handlers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ def make_reader(archive_path):
7474

7575
archive_format = "".join(archive_path.suffixes)[1:]
7676

77-
if archive_format == "zip":
77+
if archive_format.endswith("zip"):
7878
archive_file = zipfile.ZipFile(archive_path, mode="r")
79-
elif archive_format in ["tgz", "tar.gz"]:
79+
elif any([archive_format.endswith(ext) for ext in ["tgz", "tar.gz"]]):
8080
archive_file = tarfile.open(archive_path, mode="r|gz")
81-
elif archive_format in ["tbz", "tbz2", "tar.bz", "tar.bz2"]:
81+
elif any([archive_format.endswith(ext) for ext in ["tbz", "tbz2", "tar.bz", "tar.bz2"]]):
8282
archive_file = tarfile.open(archive_path, mode="r|bz2")
83-
elif archive_format in ["txz", "tar.xz"]:
83+
elif any([archive_format.endswith(ext) for ext in ["txz", "tar.xz"]]):
8484
archive_file = tarfile.open(archive_path, mode="r|xz")
8585
else:
8686
raise ValueError("'{}' is not a valid archive format.".format(archive_format))

jupyter_archive/tests/test_archive_handler.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ async def test_download(jp_fetch, jp_root_dir, followSymlinks, download_hidden,
102102
assert r.code == 200
103103
assert r.headers["content-type"] == "application/octet-stream"
104104
assert r.headers["cache-control"] == "no-cache"
105-
105+
106106
if format == "zip":
107107
with zipfile.ZipFile(r.buffer, mode=mode) as zf:
108108
assert set(zf.namelist()) == file_list
@@ -111,6 +111,14 @@ async def test_download(jp_fetch, jp_root_dir, followSymlinks, download_hidden,
111111
assert set(map(lambda m: m.name, tf.getmembers())) == file_list
112112

113113

114+
@pytest.mark.parametrize(
115+
"file_name",
116+
[
117+
('archive'),
118+
('archive.hello'),
119+
('archive.tar.gz'),
120+
],
121+
)
114122
@pytest.mark.parametrize(
115123
"format, mode",
116124
[
@@ -125,17 +133,17 @@ async def test_download(jp_fetch, jp_root_dir, followSymlinks, download_hidden,
125133
("tar.xz", "w|xz"),
126134
],
127135
)
128-
async def test_extract(jp_fetch, jp_root_dir, format, mode):
136+
async def test_extract(jp_fetch, jp_root_dir, file_name, format, mode):
129137
# Create a dummy directory.
130-
archive_dir_path = jp_root_dir / "extract-archive-dir"
138+
archive_dir_path = jp_root_dir / file_name
131139
archive_dir_path.mkdir(parents=True)
132140

133141
(archive_dir_path / "extract-test1.txt").write_text("hello1")
134142
(archive_dir_path / "extract-test2.txt").write_text("hello2")
135143
(archive_dir_path / "extract-test3.md").write_text("hello3")
136144

137145
# Make an archive
138-
archive_dir_path = jp_root_dir / "extract-archive-dir"
146+
archive_dir_path = jp_root_dir / file_name
139147
archive_path = archive_dir_path.with_suffix("." + format)
140148
if format == "zip":
141149
with zipfile.ZipFile(archive_path, mode=mode) as writer:

0 commit comments

Comments
 (0)