@@ -274,119 +274,84 @@ var _ = Describe("tarballCompressor", func() {
274274 })
275275
276276 Describe ("IsNonCompressedTarball" , func () {
277- It ("returns true for non-compressed tarball files" , func () {
278- cmdRunner := fakesys .NewFakeCmdRunner ()
279- compressor := NewTarballCompressor (cmdRunner , fs )
280-
281- // Mock the file command output for a non-compressed tarball
282- cmdRunner .AddCmdResult ("file /test/file.tar" , fakesys.FakeCmdResult {
283- Stdout : "/test/file.tar: POSIX tar archive\n " ,
284- Stderr : "" ,
285- ExitStatus : 0 ,
286- })
277+ It ("returns true for non-compressed tarball created with NoCompression=true" , func () {
278+ tgzName , err := compressor .CompressFilesInDir (testAssetsFixtureDir , CompressorOptions {NoCompression : true })
279+ Expect (err ).ToNot (HaveOccurred ())
280+ defer os .Remove (tgzName )
287281
288- result , err := compressor .IsNonCompressedTarball ("/test/file.tar" )
282+ result , err := compressor .IsNonCompressedTarball (tgzName )
289283 Expect (err ).ToNot (HaveOccurred ())
290284 Expect (result ).To (BeTrue ())
291-
292- Expect (1 ).To (Equal (len (cmdRunner .RunCommands )))
293- Expect (cmdRunner .RunCommands [0 ]).To (Equal ([]string {"file" , "/test/file.tar" }))
294285 })
295286
296- It ("returns false for compressed tarball files" , func () {
297- cmdRunner := fakesys .NewFakeCmdRunner ()
298- compressor := NewTarballCompressor (cmdRunner , fs )
299-
300- // Mock the file command output for a compressed tarball
301- cmdRunner .AddCmdResult ("file /test/file.tgz" , fakesys.FakeCmdResult {
302- Stdout : "/test/file.tgz: gzip compressed data, from Unix, original size modulo 2^32 1024\n " ,
303- Stderr : "" ,
304- ExitStatus : 0 ,
305- })
287+ It ("returns false for compressed tarball created with NoCompression=false" , func () {
288+ tgzName , err := compressor .CompressFilesInDir (testAssetsFixtureDir , CompressorOptions {NoCompression : false })
289+ Expect (err ).ToNot (HaveOccurred ())
290+ defer os .Remove (tgzName )
306291
307- result , err := compressor .IsNonCompressedTarball ("/test/file.tgz" )
292+ result , err := compressor .IsNonCompressedTarball (tgzName )
308293 Expect (err ).ToNot (HaveOccurred ())
309294 Expect (result ).To (BeFalse ())
310-
311- Expect (1 ).To (Equal (len (cmdRunner .RunCommands )))
312- Expect (cmdRunner .RunCommands [0 ]).To (Equal ([]string {"file" , "/test/file.tgz" }))
313295 })
314296
315- It ("returns false for non-tarball files" , func () {
316- cmdRunner := fakesys .NewFakeCmdRunner ()
317- compressor := NewTarballCompressor (cmdRunner , fs )
318-
319- // Mock the file command output for a regular text file
320- cmdRunner .AddCmdResult ("file /test/file.txt" , fakesys.FakeCmdResult {
321- Stdout : "/test/file.txt: ASCII text\n " ,
322- Stderr : "" ,
323- ExitStatus : 0 ,
324- })
297+ It ("returns false for compressed tarball created with default options" , func () {
298+ tgzName , err := compressor .CompressFilesInDir (testAssetsFixtureDir , CompressorOptions {})
299+ Expect (err ).ToNot (HaveOccurred ())
300+ defer os .Remove (tgzName )
325301
326- result , err := compressor .IsNonCompressedTarball ("/test/file.txt" )
302+ result , err := compressor .IsNonCompressedTarball (tgzName )
327303 Expect (err ).ToNot (HaveOccurred ())
328304 Expect (result ).To (BeFalse ())
305+ })
329306
330- Expect (1 ).To (Equal (len (cmdRunner .RunCommands )))
331- Expect (cmdRunner .RunCommands [0 ]).To (Equal ([]string {"file" , "/test/file.txt" }))
307+ It ("returns error for non-existent file" , func () {
308+ result , err := compressor .IsNonCompressedTarball ("/nonexistent/file.tar" )
309+ Expect (err ).To (HaveOccurred ())
310+ Expect (result ).To (BeFalse ())
332311 })
333312
334- It ("returns error when file command fails" , func () {
335- cmdRunner := fakesys .NewFakeCmdRunner ()
336- compressor := NewTarballCompressor (cmdRunner , fs )
313+ It ("returns error for non-tarball file" , func () {
314+ tempFile , err := fs .TempFile ("test-non-tarball" )
315+ Expect (err ).ToNot (HaveOccurred ())
316+ defer os .Remove (tempFile .Name ())
337317
338- // Mock the file command to return an error
339- cmdRunner .AddCmdResult ("file /test/nonexistent.tar" , fakesys.FakeCmdResult {
340- Stdout : "" ,
341- Stderr : "file: cannot open `/test/nonexistent.tar' (No such file or directory)\n " ,
342- ExitStatus : 1 ,
343- })
318+ err = fs .WriteFileString (tempFile .Name (), "This is not a tar file" )
319+ Expect (err ).ToNot (HaveOccurred ())
344320
345- result , _ := compressor .IsNonCompressedTarball ("/test/nonexistent.tar" )
321+ result , err := compressor .IsNonCompressedTarball (tempFile .Name ())
322+ Expect (err ).ToNot (HaveOccurred ())
346323 Expect (result ).To (BeFalse ())
347-
348- Expect (1 ).To (Equal (len (cmdRunner .RunCommands )))
349- Expect (cmdRunner .RunCommands [0 ]).To (Equal ([]string {"file" , "/test/nonexistent.tar" }))
350324 })
351325
352- It ("returns error when file command execution fails" , func () {
353- cmdRunner := fakesys .NewFakeCmdRunner ()
354- compressor := NewTarballCompressor (cmdRunner , fs )
355-
356- // Mock the file command to return an execution error
357- cmdRunner .AddCmdResult ("file /test/file.tar" , fakesys.FakeCmdResult {
358- Stdout : "" ,
359- Stderr : "" ,
360- ExitStatus : 0 ,
361- Error : errors .New ("command execution failed" ),
362- })
326+ It ("returns error for empty file" , func () {
327+ tempFile , err := fs .TempFile ("test-empty-file" )
328+ Expect (err ).ToNot (HaveOccurred ())
329+ defer os .Remove (tempFile .Name ())
330+ tempFile .Close ()
363331
364- result , err := compressor .IsNonCompressedTarball ("/test/file.tar" )
365- Expect (err ).To (HaveOccurred ())
366- Expect (err .Error ()).To (ContainSubstring ("command execution failed" ))
332+ result , err := compressor .IsNonCompressedTarball (tempFile .Name ())
333+ Expect (err ).ToNot (HaveOccurred ())
367334 Expect (result ).To (BeFalse ())
368-
369- Expect (1 ).To (Equal (len (cmdRunner .RunCommands )))
370- Expect (cmdRunner .RunCommands [0 ]).To (Equal ([]string {"file" , "/test/file.tar" }))
371335 })
372336
373- It ("handles file command output with extra whitespace" , func () {
374- cmdRunner := fakesys .NewFakeCmdRunner ()
375- compressor := NewTarballCompressor (cmdRunner , fs )
337+ It ("correctly identifies tarballs created with CompressSpecificFilesInDir" , func () {
338+ files := []string {"app.stdout.log" , "app.stderr.log" }
376339
377- // Mock the file command output with extra whitespace
378- cmdRunner .AddCmdResult ("file /test/file.tar" , fakesys.FakeCmdResult {
379- Stdout : " /test/file.tar: POSIX tar archive \n " ,
380- Stderr : "" ,
381- ExitStatus : 0 ,
382- })
340+ tgzName , err := compressor .CompressSpecificFilesInDir (testAssetsFixtureDir , files , CompressorOptions {NoCompression : true })
341+ Expect (err ).ToNot (HaveOccurred ())
342+ defer os .Remove (tgzName )
383343
384- result , err := compressor .IsNonCompressedTarball ("/test/file.tar" )
344+ result , err := compressor .IsNonCompressedTarball (tgzName )
385345 Expect (err ).ToNot (HaveOccurred ())
386346 Expect (result ).To (BeTrue ())
387347
388- Expect (1 ).To (Equal (len (cmdRunner .RunCommands )))
389- Expect (cmdRunner .RunCommands [0 ]).To (Equal ([]string {"file" , "/test/file.tar" }))
348+ tgzName2 , err := compressor .CompressSpecificFilesInDir (testAssetsFixtureDir , files , CompressorOptions {NoCompression : false })
349+ Expect (err ).ToNot (HaveOccurred ())
350+ defer os .Remove (tgzName2 )
351+
352+ result2 , err := compressor .IsNonCompressedTarball (tgzName2 )
353+ Expect (err ).ToNot (HaveOccurred ())
354+ Expect (result2 ).To (BeFalse ())
390355 })
391356 })
392357
0 commit comments