Skip to content

Commit

Permalink
Update TestUnzip_with_file_to_unzip to support windows
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewJohn committed Jun 5, 2024
1 parent 5093a5b commit f9d56fb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Unzip(src string, dest string, fileToUnzipSlice ...string) ([]string, error
}
var unzipWaitGroup sync.WaitGroup
for _, f := range reader.File {
// Only extract the "terraform" binary
// Only extract the main binary binary
// from the archive, ignoring LICENSE and other files
if f.Name != ConvertExecutableExt(fileToUnzip) {
continue
Expand Down
21 changes: 16 additions & 5 deletions lib/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,18 @@ func TestUnzip(t *testing.T) {
// TestUnzip_with_file_to_unzip : Test Unzip method with fileToUnzip argument
func TestUnzip_with_file_to_unzip(t *testing.T) {
installPath := "/.terraform.versions_test/"
absPath, _ := filepath.Abs("../test-data/test-data.zip")
var pathToTestFile string
var expectedFilename string
switch runtime.GOOS {
case "windows":
pathToTestFile = "../test-data/test-data_windows.zip"
expectedFilename = "another_file.exe"
default:
pathToTestFile = "../test-data/test-data.zip"
expectedFilename = "another_file"
}

absPath, _ := filepath.Abs(pathToTestFile)

t.Logf("Absolute Path: %q", absPath)

Expand All @@ -177,7 +188,7 @@ func TestUnzip_with_file_to_unzip(t *testing.T) {

createDirIfNotExist(installLocation)

files, errUnzip := Unzip(absPath, installLocation, "README")
files, errUnzip := Unzip(absPath, installLocation, "another_file")

if errUnzip != nil {
t.Errorf("Unable to unzip %q file: %v", absPath, errUnzip)
Expand All @@ -188,7 +199,7 @@ func TestUnzip_with_file_to_unzip(t *testing.T) {
}

// Ensure terraform file exists
expectedExtractFile := filepath.Join(installLocation, "README")
expectedExtractFile := filepath.Join(installLocation, expectedFilename)
if terraformFileExists := checkFileExist(expectedExtractFile); !terraformFileExists {
t.Errorf("File does not exist %v", expectedExtractFile)
}
Expand All @@ -198,12 +209,12 @@ func TestUnzip_with_file_to_unzip(t *testing.T) {
t.Error(err)
}
// Ensure terraform file contains test content from
if string(expectedFileContent[:]) != "This is a README\n" {
if !strings.Contains(string(expectedFileContent[:]), "This is another executable") {
t.Errorf("Extract test file (%q) content does not match expected value: %s", expectedExtractFile, string(expectedFileContent[:]))
}

// Ensure README and LICENSE files don't exist
nonExistentFiles := []string{"terraform", "LICENSE"}
nonExistentFiles := []string{"terraform", "terraform.exe", "LICENSE"}
for _, fileName := range nonExistentFiles {
filePath := filepath.Join(installLocation, fileName)
if fileExists := checkFileExist(filePath); fileExists {
Expand Down
Binary file modified test-data/test-data.zip
Binary file not shown.
Binary file modified test-data/test-data_windows.zip
Binary file not shown.

0 comments on commit f9d56fb

Please sign in to comment.