Skip to content

Commit

Permalink
- fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MatrixCrawler committed Apr 26, 2024
1 parent 770ea4c commit 003b092
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
8 changes: 5 additions & 3 deletions lib/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"os"
"path/filepath"
"runtime"
"sync"
"testing"

"github.com/mitchellh/go-homedir"
)

// TestDownloadFromURL_FileNameMatch : Check expected filename exist when downloaded
func TestDownloadFromURL_FileNameMatch(t *testing.T) {

logger = InitLogger("DEBUG")
hashiURL := "https://releases.hashicorp.com/terraform/"
installVersion := "terraform_"
tempDir := t.TempDir()
Expand Down Expand Up @@ -46,10 +47,11 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) {

/* test download old terraform version */
lowestVersion := "0.11.0"

var wg sync.WaitGroup
defer wg.Done()
urlToDownload := hashiURL + lowestVersion + "/" + installVersion + lowestVersion + macOS
expectedFile := filepath.Join(installLocation, installVersion+lowestVersion+macOS)
installedFile, errDownload := downloadFromURL(installLocation, urlToDownload, nil)
installedFile, errDownload := downloadFromURL(installLocation, urlToDownload, &wg)

if errDownload != nil {
t.Logf("Expected file name %v to be downloaded", expectedFile)
Expand Down
15 changes: 13 additions & 2 deletions lib/param_parsing/parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func TestGetParameters_params_are_overridden_by_toml_file(t *testing.T) {
if actual != expected {
t.Error("Version Param was not as expected. Actual: " + actual + ", Expected: " + expected)
}
t.Cleanup(func() {
getopt.CommandLine = getopt.New()
})
}

func TestGetParameters_toml_params_are_overridden_by_cli(t *testing.T) {
Expand All @@ -65,14 +68,16 @@ func TestGetParameters_toml_params_are_overridden_by_cli(t *testing.T) {
if actual != expected {
t.Error("Version Param was not as expected. Actual: " + actual + ", Expected: " + expected)
}
t.Cleanup(func() {
getopt.CommandLine = getopt.New()
})
}

func TestGetParameters_dry_run_wont_download_anything(t *testing.T) {
logger = lib.InitLogger("DEBUG")
installLocation := lib.GetInstallLocation()
expected := "../../test-data/integration-tests/test_versiontf"
//os.Args = []string{"cmd", "--chdir=" + expected, "--bin=/tmp", "--dry-run"}
os.Args = []string{"cmd", "--log-level=DEBUG", "--chdir=" + expected, "--bin=/tmp"}
os.Args = []string{"cmd", "--chdir=" + expected, "--bin=/tmp", "--dry-run"}
params := GetParameters()
installFileVersionPath := lib.ConvertExecutableExt(filepath.Join(installLocation, lib.VersionPrefix+params.Version))
// Make sure the file tfswitch WOULD download is absent
Expand All @@ -81,6 +86,9 @@ func TestGetParameters_dry_run_wont_download_anything(t *testing.T) {
if lib.FileExistsAndIsNotDir(installFileVersionPath) {
t.Error("Dry run should NOT download any files.")
}
t.Cleanup(func() {
getopt.CommandLine = getopt.New()
})
}

func TestGetParameters_check_config_precedence(t *testing.T) {
Expand All @@ -94,4 +102,7 @@ func TestGetParameters_check_config_precedence(t *testing.T) {
if parameters.Version != expected {
t.Error("Version Param was not as expected. Actual: " + parameters.Version + ", Expected: " + expected)
}
t.Cleanup(func() {
getopt.CommandLine = getopt.New()
})
}

0 comments on commit 003b092

Please sign in to comment.