4
4
5
5
import hashlib
6
6
import io
7
- import os
8
- import shutil
9
7
import stat
10
8
import tarfile
11
- import tempfile
12
- import unittest
13
9
14
10
import pytest
15
11
@@ -36,7 +32,6 @@ def file_hash(path):
36
32
37
33
@pytest .fixture
38
34
def create_files (tmp_path ):
39
-
40
35
def inner ():
41
36
files = {}
42
37
for i in range (10 ):
@@ -69,9 +64,7 @@ def verify_basic_tarfile(tf):
69
64
assert ti .mtime == DEFAULT_MTIME
70
65
71
66
72
- @pytest .mark .xfail (
73
- reason = "ValueError is not thrown despite being provided directory."
74
- )
67
+ @pytest .mark .xfail (reason = "ValueError is not thrown despite being provided directory." )
75
68
def test_dirs_refused (tmp_path ):
76
69
tp = tmp_path / "test.tar"
77
70
with open (tp , "wb" ) as fh :
@@ -110,45 +103,79 @@ def test_create_tar_basic(tmp_path, create_files):
110
103
verify_basic_tarfile (tf )
111
104
112
105
113
- @pytest .mark .xfail (reason = "hash mismatch" )
114
- def test_executable_preserved ():
115
- p = os .path .join (d , "exec" )
116
- with open (p , "wb" ) as fh :
117
- fh .write (b"#!/bin/bash\n " )
118
- os .chmod (p , MODE_STANDARD | stat .S_IXUSR )
106
+ def test_executable_preserved (tmp_path ):
107
+ p = tmp_path / "exec"
108
+ p .write_bytes (b"#!/bin/bash\n " )
109
+ p .chmod (MODE_STANDARD | stat .S_IXUSR )
119
110
120
- tp = os . path . join ( d , "test.tar" )
111
+ tp = tmp_path / "test.tar"
121
112
with open (tp , "wb" ) as fh :
122
- create_tar_from_files (fh , {"exec" : p })
113
+ create_tar_from_files (fh , {"exec" : str ( p ) })
123
114
124
- assert file_hash (tp ) == "357e1b81c0b6cfdfa5d2d118d420025c3c76ee93"
115
+ # Test determinism by creating the same file again
116
+ tp2 = tmp_path / "test2.tar"
117
+ with open (tp2 , "wb" ) as fh :
118
+ create_tar_from_files (fh , {"exec" : str (p )})
125
119
120
+ assert file_hash (str (tp )) == file_hash (str (tp2 ))
121
+
122
+ # Verify executable permissions are preserved in tar
126
123
with tarfile .open (tp , "r" ) as tf :
127
124
m = tf .getmember ("exec" )
128
125
assert m .mode == MODE_STANDARD | stat .S_IXUSR
129
126
127
+ # Verify file content is correct
128
+ extracted_content = tf .extractfile (m ).read ()
129
+ assert extracted_content == b"#!/bin/bash\n "
130
130
131
- def test_create_tar_gz_basic (tmp_path , create_files ):
132
- files = create_files ()
133
131
132
+ def test_create_tar_gz_basic (tmp_path , create_files ):
134
133
gp = tmp_path / "test.tar.gz"
135
134
with open (gp , "wb" ) as fh :
136
- create_tar_gz_from_files (fh , files )
135
+ create_tar_gz_from_files (fh , create_files () )
137
136
138
- assert file_hash (gp ) == "7c4da5adc5088cdf00911d5daf9a67b15de714b7"
137
+ # Test determinism by creating the same file again with fresh BytesIO objects
138
+ gp2 = tmp_path / "test2.tar.gz"
139
+ with open (gp2 , "wb" ) as fh :
140
+ create_tar_gz_from_files (fh , create_files ())
139
141
142
+ assert file_hash (str (gp )) == file_hash (str (gp2 ))
143
+
144
+ # Create uncompressed version for size comparison
145
+ tp = tmp_path / "test.tar"
146
+ with open (tp , "wb" ) as fh :
147
+ create_tar_from_files (fh , create_files ())
148
+ uncompressed_size = tp .stat ().st_size
149
+ compressed_size = gp .stat ().st_size
150
+
151
+ # Compressed should be smaller than uncompressed
152
+ assert compressed_size < uncompressed_size
153
+
154
+ # Verify the contents are correct
140
155
with tarfile .open (gp , "r:gz" ) as tf :
141
156
verify_basic_tarfile (tf )
142
157
143
158
144
159
def test_tar_gz_name (tmp_path , create_files ):
145
- files = create_files ()
146
-
147
160
gp = tmp_path / "test.tar.gz"
148
161
with open (gp , "wb" ) as fh :
149
- create_tar_gz_from_files (fh , files , filename = "foobar" )
162
+ create_tar_gz_from_files (fh , create_files (), filename = "foobar" )
163
+
164
+ # Test determinism by creating the same file again with fresh BytesIO objects
165
+ gp2 = tmp_path / "test2.tar.gz"
166
+ with open (gp2 , "wb" ) as fh :
167
+ create_tar_gz_from_files (fh , create_files (), filename = "foobar" )
168
+
169
+ assert file_hash (str (gp )) == file_hash (str (gp2 ))
170
+
171
+ # Create version without filename for comparison
172
+ gp_no_name = tmp_path / "test_no_name.tar.gz"
173
+ with open (gp_no_name , "wb" ) as fh :
174
+ create_tar_gz_from_files (fh , create_files ())
150
175
151
- assert file_hash (gp ) == "721e00083c17d16df2edbddf40136298c06d0c49"
176
+ # Files should be different (different filename in gzip header)
177
+ assert file_hash (str (gp )) != file_hash (str (gp_no_name ))
152
178
179
+ # Verify the contents are correct
153
180
with tarfile .open (gp , "r:gz" ) as tf :
154
181
verify_basic_tarfile (tf )
0 commit comments