11# frozen_string_literal: true
22
3+ require "open3"
4+
35# Test assertions and helpers for working with tarballs.
46#
57# Includes Minitar in-memory and on-disk operations and GNU tar helpers.
@@ -143,14 +145,7 @@ def prepare_workspace(with_files:)
143145 def gnu_tar_create_in_workspace
144146 missing_workspace!
145147
146- if Minitar . windows?
147- p @workspace . tarball . to_s , @workspace . source . to_s
148- end
149-
150- system (
151- GNU_TAR , "-cf" , @workspace . tarball . to_s , "-C" , @workspace . source . to_s , "." ,
152- exception : true
153- )
148+ __gnu_tar ( :create )
154149
155150 assert @workspace . tarball . file? , "Workspace tarball not created by GNU tar"
156151 assert @workspace . tarball . size > 0 , "Workspace tarball should not be empty"
@@ -162,10 +157,7 @@ def gnu_tar_extract_in_workspace
162157 assert @workspace . tarball . file? , "Workspace tarball not present for extraction"
163158 assert @workspace . tarball . size > 0 , "Workspace tarball should not be empty"
164159
165- system (
166- GNU_TAR , "-xf" , @workspace . tarball . to_s , "-C" , @workspace . target . to_s ,
167- exception : true
168- )
160+ __gnu_tar ( :extract )
169161 end
170162
171163 def gnu_tar_list_in_workspace
@@ -174,7 +166,7 @@ def gnu_tar_list_in_workspace
174166 assert @workspace . tarball . file? , "Workspace tarball not present for extraction"
175167 assert @workspace . tarball . size > 0 , "Workspace tarball should not be empty"
176168
177- ` #{ GNU_TAR } -tf " #{ @workspace . tarball } " 2>/dev/null` . strip . split ( $/)
169+ __gnu_tar ( :list ) . strip . split ( $/)
178170 end
179171
180172 def minitar_pack_in_workspace
@@ -285,5 +277,47 @@ def missing_workspace!
285277 raise "Missing workspace" unless defined? ( @workspace )
286278 end
287279
280+ def __gnu_tar ( action )
281+ missing_workspace!
282+
283+ cmd = [ GNU_TAR ]
284+ cmd . push ( "--force-local" ) if Minitar . windows?
285+
286+ case action
287+ when :create
288+ cmd . push ( "-cf" , @workspace . tarball . to_s , "-C" , @workspace . source . to_s , "." )
289+ when :extract
290+ cmd . push ( "-xf" , @workspace . tarball . to_s , "-C" , @workspace . target . to_s )
291+ when :list
292+ cmd . push ( "-tf" , @workspace . tarball . to_s )
293+ end
294+
295+ stdout_str = ""
296+ stderr_str = ""
297+ status = nil
298+
299+ Open3 . popen3 ( *cmd ) do |stdin , stdout , stderr , wait_thr |
300+ stdin . close
301+ out_t = Thread . new { stdout . read }
302+ err_t = Thread . new { stderr . read }
303+ stdout_str = out_t . value . to_s
304+ stderr_str = err_t . value . to_s
305+ status = wait_thr . value
306+ end
307+
308+ unless status . success?
309+ warn stdout_str unless stdout_str . empty?
310+ warn stderr_str unless stderr_str . empty?
311+
312+ if status . exited?
313+ raise "command #{ cmd . join ( " " ) } failed (exit status: #{ status . exitstatus } )"
314+ else
315+ raise "command #{ cmd . join ( " " ) } failed (status: #{ status . inspect } )"
316+ end
317+ end
318+
319+ stdout_str
320+ end
321+
288322 Minitest ::Test . send ( :include , self )
289323end
0 commit comments