-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add test for iso extraction * Move test file creation and `safeCloser` functions into `util_test.go` * Enable running tests for `tar.z` archive on Windows
- Loading branch information
Showing
3 changed files
with
185 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package xtractr_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/kdomanski/iso9660" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"golift.io/xtractr" | ||
) | ||
|
||
func TestIso(t *testing.T) { | ||
t.Parallel() | ||
|
||
testFilesInfo := createTestFiles(t) | ||
|
||
writer, err := iso9660.NewWriter() | ||
require.NoError(t, err, "failed to create writer") | ||
defer func() { | ||
err = writer.Cleanup() | ||
require.NoError(t, err, "failed to cleanup writer") | ||
}() | ||
|
||
walkErr := filepath.Walk(testFilesInfo.srcFilesDir, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return fmt.Errorf("unexpected error: %w", err) | ||
} | ||
if info.IsDir() { | ||
return nil | ||
} | ||
|
||
fileToAdd, err := os.Open(path) | ||
if err != nil { | ||
return fmt.Errorf("failed to open file: %w", err) | ||
} | ||
defer fileToAdd.Close() | ||
|
||
err = writer.AddFile(fileToAdd, strings.TrimLeft(fileToAdd.Name(), testFilesInfo.srcFilesDir)) | ||
if err != nil { | ||
return fmt.Errorf("failed to add file: %w", err) | ||
} | ||
return nil | ||
}) | ||
require.NoError(t, walkErr, "failed to walk files") | ||
|
||
isoFileName := filepath.Join(testFilesInfo.dstFilesDir, "archive.iso") | ||
isoFile, err := os.Create(isoFileName) | ||
defer safeCloser(t, isoFile) | ||
require.NoError(t, err, "failed to create ISO file") | ||
|
||
err = writer.WriteTo(isoFile, "test") | ||
require.NoError(t, err, "failed to write ISO") | ||
|
||
size, files, archives, err := xtractr.ExtractFile(&xtractr.XFile{ | ||
FilePath: isoFileName, | ||
OutputDir: filepath.Clean(testFilesInfo.dstFilesDir), | ||
FileMode: 0o600, | ||
DirMode: 0o700, | ||
}) | ||
require.NoError(t, err) | ||
assert.Equal(t, int64(testFilesInfo.dataSize), size) | ||
assert.Len(t, files, testFilesInfo.fileCount) | ||
assert.Len(t, archives, testFilesInfo.archiveCount) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package xtractr_test | ||
|
||
// Shared utility functions/structs used in testing only. | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type testFilesInfo struct { | ||
srcFilesDir string | ||
dstFilesDir string | ||
dataSize int | ||
fileCount int | ||
archiveCount int | ||
} | ||
|
||
// Create test files for the tests and returns information | ||
// about the created files and directories. | ||
func createTestFiles(t *testing.T) *testFilesInfo { | ||
t.Helper() | ||
const ( | ||
loremIpsum = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, | ||
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. | ||
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip | ||
ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit | ||
esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non | ||
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.` | ||
testDataSize = 1544 | ||
testFileCount = 5 | ||
testArchiveCount = 1 | ||
) | ||
|
||
randomDigits := []uint8{ | ||
157, 242, 143, 106, 163, 159, 194, 141, 32, 22, 249, 78, | ||
225, 206, 190, 199, 99, 146, 53, 149, 239, 179, 72, 2, 197, 196, 91, 81, 192, | ||
241, 69, 166, 213, 172, 111, 117, 210, 51, 136, 185, 130, 109, 139, 57, 150, 63, | ||
85, 86, 204, 10, 26, 1, 186, 234, 96, 187, 205, 138, 224, 77, 114, 226, 16, 222, | ||
151, 175, 200, 116, 36, 198, 173, 168, 230, 4, 18, 245, 31, 214, 158, 105, 171, | ||
123, 195, 137, 40, 93, 83, 215, 6, 118, 161, 223, 43, 167, 7, 3, 113, 148, 201, | ||
125, | ||
} | ||
|
||
testFiles := []string{ | ||
"README.txt", | ||
"level1/", | ||
"level1/level1.txt", | ||
"level1/level1.bin", | ||
"level1/level2/", | ||
"level1/level2/level2.txt", | ||
"level1/level2/level2.bin", | ||
} | ||
|
||
testDataDir, err := os.MkdirTemp(".", "xtractr_test_*_data") | ||
require.NoError(t, err, "creating temp directory failed") | ||
t.Cleanup(func() { | ||
os.RemoveAll(testDataDir) | ||
}) | ||
|
||
srcFilesDir := filepath.Join(testDataDir, "sources") | ||
err = os.MkdirAll(srcFilesDir, 0o700) | ||
require.NoError(t, err) | ||
|
||
var destFilesDir string | ||
for _, file := range testFiles { | ||
fullPath := filepath.Join(srcFilesDir, file) | ||
var err error | ||
switch { | ||
case file[len(file)-1] == '/': | ||
err = os.MkdirAll(fullPath, 0o700) | ||
case filepath.Ext(file) == ".txt": | ||
err = os.WriteFile(fullPath, []byte(loremIpsum), 0o600) | ||
default: | ||
err = os.WriteFile(fullPath, randomDigits, 0o600) | ||
} | ||
require.NoError(t, err) | ||
|
||
destFilesDir = filepath.Join(testDataDir, "out") | ||
err = os.MkdirAll(destFilesDir, 0o700) | ||
require.NoError(t, err) | ||
} | ||
|
||
return &testFilesInfo{ | ||
srcFilesDir: srcFilesDir, | ||
dstFilesDir: destFilesDir, | ||
dataSize: testDataSize, | ||
fileCount: testFileCount, | ||
archiveCount: testArchiveCount, | ||
} | ||
} | ||
|
||
func safeCloser(t *testing.T, c io.Closer) { | ||
t.Helper() | ||
err := c.Close() | ||
require.NoError(t, err) | ||
} |