Files
TheRock/build_tools/tests/archive_util_test.py
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

115 lines
3.7 KiB
Python

#!/usr/bin/env python
# Copyright Advanced Micro Devices, Inc.
# SPDX-License-Identifier: MIT
import os
import platform
import tempfile
import unittest
from pathlib import Path
from _therock_utils.archive_util import (
open_archive_for_read,
open_archive_for_write,
)
IS_WINDOWS = platform.system() == "Windows"
class ArchiveRoundtripTest(unittest.TestCase):
"""Test writing and reading archives for each compression type."""
def _roundtrip(self, suffix: str, compression_type: str):
with tempfile.TemporaryDirectory() as tmp:
tmp = Path(tmp)
# Create a source file to archive.
src = tmp / "hello.txt"
src.write_text("hello world")
# Write archive.
archive = tmp / f"test.tar.{suffix}"
with open_archive_for_write(archive, compression_type) as arc:
arc.add(str(src), arcname="hello.txt")
self.assertTrue(archive.exists())
# Read archive and verify contents.
with open_archive_for_read(archive) as arc:
members = arc.getnames()
self.assertIn("hello.txt", members)
def test_roundtrip_zstd(self):
self._roundtrip("zst", "zstd")
def test_roundtrip_xz(self):
self._roundtrip("xz", "xz")
def test_roundtrip_zstd_custom_level(self):
with tempfile.TemporaryDirectory() as tmp:
tmp = Path(tmp)
src = tmp / "hello.txt"
src.write_text("hello world")
archive = tmp / "test.tar.zst"
with open_archive_for_write(archive, "zstd", compression_level=1) as arc:
arc.add(str(src), arcname="hello.txt")
with open_archive_for_read(archive) as arc:
self.assertIn("hello.txt", arc.getnames())
class HandleLeakTest(unittest.TestCase):
"""Verify that closing a ZstdTarFile releases the OS file handle."""
@unittest.skipUnless(IS_WINDOWS, "Handle leak only blocks deletion on Windows")
def test_zstd_read_close_releases_handle(self):
"""After closing a zstd archive opened for read, the file can be deleted."""
with tempfile.TemporaryDirectory() as tmp:
tmp = Path(tmp)
src = tmp / "hello.txt"
src.write_text("hello world")
archive = tmp / "test.tar.zst"
with open_archive_for_write(archive, "zstd") as arc:
arc.add(str(src), arcname="hello.txt")
# Open for read, close, then delete.
tf = open_archive_for_read(archive)
tf.getnames()
tf.close()
# This would raise PermissionError if the handle leaked.
os.unlink(archive)
self.assertFalse(archive.exists())
@unittest.skipUnless(IS_WINDOWS, "Handle leak only blocks deletion on Windows")
def test_zstd_write_close_releases_handle(self):
"""After closing a zstd archive opened for write, the file can be deleted."""
with tempfile.TemporaryDirectory() as tmp:
tmp = Path(tmp)
src = tmp / "hello.txt"
src.write_text("hello world")
archive = tmp / "test.tar.zst"
tf = open_archive_for_write(archive, "zstd")
tf.add(str(src), arcname="hello.txt")
tf.close()
os.unlink(archive)
self.assertFalse(archive.exists())
class ErrorHandlingTest(unittest.TestCase):
def test_read_unknown_extension(self):
with self.assertRaises(ValueError):
open_archive_for_read(Path("test.tar.gz"))
def test_write_unknown_compression(self):
with self.assertRaises(ValueError):
open_archive_for_write(Path("test.tar.gz"), "gzip")
if __name__ == "__main__":
unittest.main()