Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpiofile: Add unittest opening a symlink as fileobj #140

Conversation

bernhardkaindl
Copy link
Collaborator

@bernhardkaindl bernhardkaindl commented May 14, 2024

Add two more checks to tests/test_cpiofile.py:

Test extracting a symlink from a seekable .cpio archive to a fileobj:

    # Test extracting a symlink to a file object:
    if archive_mode.startswith("|"):  # Non-seekable streams raise StreamError
        with pytest.raises(StreamError):
            archive.extractfile("symlink")
    else:  # Expect a seekable fileobj for this test (not a stream) to work:
        fileobj = archive.extractfile("symlink")
        assert fileobj and fileobj.read() == binary_data

Run all tests not just using a passed cpio fileobj, but also an acutal cpio file archive:

    for comp in ["cpio", "gz", "bz2", "xz"]:
        for filetype in [":", "|"]:
            if comp == "xz" and filetype == ":":
                continue  # streaming xz is not implemented (supported only as file)
            check_archive_mode(filetype + comp, fs)
            if filetype == "|":
+               check_archive_mode(filetype + comp, fs, filename="archive." + comp)

Changes to tests/test_cpiofile.py

  • Refactor creating the cpio archive for the test into a reusable function
  • Improve it to also be able to create an actual file besides working with file objects.
  • Improve the tests to test both, cpio archive file objects and real cpio files.
  • Cover the changed lines in cpiofile.py which obsolete a blank # type: ignore comment

Changes to xcp/cpiofile.py

  • Replace the blank # type: ignore comment with an assertion and refactor it to simplify the code
    • Uses the fact that CpioFile.extract() accepts the member to extract as string
      • It does not have to be a member type:
        • Passing the string simplifies the code and obsoletes the need for the blank type: ignore

Signed-off-by: Bernhard Kaindl <bernhard.kaindl@cloud.com>
@coveralls
Copy link

Pull Request Test Coverage Report for Build 9072409400

Details

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+0.2%) to 83.67%

Files with Coverage Reduction New Missed Lines %
xcp/cpiofile.py 1 75.56%
Totals Coverage Status
Change from base Build 9072339625: 0.2%
Covered Lines: 2818
Relevant Lines: 3368

💛 - Coveralls

@bernhardkaindl bernhardkaindl merged commit 855cb9b into xenserver:master May 16, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants