@@ -9,15 +9,16 @@ import (
99 "path/filepath"
1010 "testing"
1111
12- lib "github.com/warrensbox/terraform-switcher/lib"
12+ "github.com/warrensbox/terraform-switcher/lib"
1313)
1414
1515// TestDownloadFromURL_FileNameMatch : Check expected filename exist when downloaded
1616func TestDownloadFromURL_FileNameMatch (t * testing.T ) {
1717
1818 hashiURL := "https://releases.hashicorp.com/terraform/"
1919 installVersion := "terraform_"
20- installPath := GetInstallLocation (".terraform.versions_test" )
20+ tempDir := t .TempDir ()
21+ installPath := fmt .Sprintf (tempDir + string (os .PathSeparator ) + ".terraform.versions_test" )
2122 macOS := "_darwin_amd64.zip"
2223
2324 // get current user
@@ -31,16 +32,16 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) {
3132
3233 // create /.terraform.versions_test/ directory to store code
3334 if _ , err := os .Stat (installLocation ); os .IsNotExist (err ) {
34- log . Printf ("Creating directory for terraform: %v" , installLocation )
35+ t . Logf ("Creating directory for terraform: %v" , installLocation )
3536 err = os .MkdirAll (installLocation , 0755 )
3637 if err != nil {
37- fmt . Printf ("Unable to create directory for terraform: %v" , installLocation )
38- panic ( err )
38+ t . Logf ("Unable to create directory for terraform: %v" , installLocation )
39+ t . Error ( "Test fail" )
3940 }
4041 }
4142
42- /* test download lowest terraform version */
43- lowestVersion := "0.1 .0"
43+ /* test download old terraform version */
44+ lowestVersion := "0.11 .0"
4445
4546 url := hashiURL + lowestVersion + "/" + installVersion + lowestVersion + macOS
4647 expectedFile := filepath .Join (usr .HomeDir , installPath , installVersion + lowestVersion + macOS )
@@ -61,156 +62,27 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) {
6162 t .Error ("Download file mismatches expected file (unexpected)" )
6263 }
6364
64- /* test download latest terraform version */
65- latestVersion := "0.11.7"
66-
67- url = hashiURL + latestVersion + "/" + installVersion + latestVersion + macOS
68- expectedFile = filepath .Join (usr .HomeDir , installPath , installVersion + latestVersion + macOS )
69- installedFile , errDownload = lib .DownloadFromURL (installLocation , url )
70-
71- if errDownload != nil {
72- t .Logf ("Expected file name %v to be downloaded" , expectedFile )
73- t .Error ("Download not possible (unexpected)" )
74- }
75-
76- if installedFile == expectedFile {
77- t .Logf ("Expected file name %v" , expectedFile )
78- t .Logf ("Downloaded file name %v" , installedFile )
79- t .Log ("Download file name matches expected file" )
80- } else {
81- t .Logf ("Expected file name %v" , expectedFile )
82- t .Logf ("Downloaded file name %v" , installedFile )
83- t .Error ("Dowload file name mismatches expected file (unexpected)" )
84- }
85-
86- cleanUp (installLocation )
87- }
88-
89- // TestDownloadFromURL_FileExist : Check expected file exist when downloaded
90- func TestDownloadFromURL_FileExist (t * testing.T ) {
91-
92- hashiURL := "https://releases.hashicorp.com/terraform/"
93- installFile := "terraform"
94- installVersion := "terraform_"
95- installPath := GetInstallLocation (".terraform.versions_test" )
96- macOS := "_darwin_amd64.zip"
97-
98- // get current user
99- usr , errCurr := user .Current ()
100- if errCurr != nil {
101- log .Fatal (errCurr )
102- }
103-
104- fmt .Printf ("Current user: %v \n " , usr .HomeDir )
105- installLocation := filepath .Join (usr .HomeDir , installPath )
106-
107- // create /.terraform.versions_test/ directory to store code
108- if _ , err := os .Stat (installLocation ); os .IsNotExist (err ) {
109- log .Printf ("Creating directory for terraform: %v" , installLocation )
110- err = os .MkdirAll (installLocation , 0755 )
111- if err != nil {
112- fmt .Printf ("Unable to create directory for terraform: %v" , installLocation )
113- panic (err )
114- }
115- }
116-
117- /* test download lowest terraform version */
118- lowestVersion := "0.1.0"
119-
120- url := hashiURL + lowestVersion + "/" + installVersion + lowestVersion + macOS
121- expectedFile := filepath .Join (usr .HomeDir , installPath , installVersion + lowestVersion + macOS )
122- installedFile , errDownload := lib .DownloadFromURL (installLocation , url )
123-
124- if errDownload != nil {
125- t .Logf ("Expected file name %v to be downloaded" , expectedFile )
126- t .Error ("Download not possible (unexpected)" )
127- }
128-
129- if checkFileExist (expectedFile ) {
130- t .Logf ("Expected file %v" , expectedFile )
131- t .Logf ("Downloaded file %v" , installedFile )
132- t .Log ("Download file matches expected file" )
133- } else {
134- t .Logf ("Expected file %v" , expectedFile )
135- t .Logf ("Downloaded file %v" , installedFile )
136- t .Error ("Download file mismatches expected file (unexpected)" )
137- }
138-
139- /* test download latest terraform version */
140- latestVersion := "0.11.7"
141-
142- url = hashiURL + latestVersion + "/" + installVersion + latestVersion + macOS
143- expectedFile = filepath .Join (usr .HomeDir , installPath , installVersion + latestVersion + macOS )
144- installFile , errDownload = lib .DownloadFromURL (installLocation , url )
145-
146- if errDownload != nil {
147- t .Logf ("Expected file name %v to be downloaded" , expectedFile )
148- t .Error ("Download not possible (unexpected)" )
149- }
150-
151- if checkFileExist (expectedFile ) {
152- t .Logf ("Expected file %v" , expectedFile )
153- t .Logf ("Downloaded file %v" , installFile )
154- t .Log ("Download file matches expected file" )
155- } else {
156- t .Logf ("Expected file %v" , expectedFile )
157- t .Logf ("Downloaded file %v" , installFile )
158- t .Error ("Download file mismatches expected file (unexpected)" )
159- }
160-
161- cleanUp (installLocation )
162- }
163-
164- // TestInvalidURL : Invalid url should throw an error
165- func TestInvalidURL (t * testing.T ) {
166-
167- hashiURL := "https://releases.hashicorp.com/terraform/"
168- installVersion := "terraform_"
169- installPath := GetInstallLocation (".terraform.versions_test" )
170- macOS := "_darwin_amd64.zip"
171- invalidVersion := "0.11.7-nonexistent"
172-
173- // get current user
174- usr , errCurr := user .Current ()
175- if errCurr != nil {
176- log .Fatal (errCurr )
177- }
178-
179- fmt .Printf ("Current user: %v \n " , usr .HomeDir )
180- installLocation := filepath .Join (usr .HomeDir , installPath )
181-
182- // create /.terraform.versions_test/ directory to store code
183- if _ , err := os .Stat (installLocation ); os .IsNotExist (err ) {
184- log .Printf ("Creating directory for terraform: %v\n " , installLocation )
185- err = os .MkdirAll (installLocation , 0755 )
186- if err != nil {
187- fmt .Printf ("Unable to create directory for terraform: %v\n " , installLocation )
188- panic (err )
189- }
190- }
191-
192- url := hashiURL + invalidVersion + "/" + installVersion + invalidVersion + macOS
193- //expectedFile :=filepath.Join(usr.HomeDir, installPath, installVersion + invalidVersion + macOS)
194- _ , errDownload := lib .DownloadFromURL (installLocation , url )
195-
196- if errDownload != nil {
197- t .Logf ("Unable to download from %s - invalid url or version (expected)\n " , url )
198- t .Logf ("Download not possible (expected)" )
65+ //check file name is what is expected
66+ _ , err := os .Stat (expectedFile )
67+ if err != nil {
68+ t .Logf ("Expected file does not exist %v" , expectedFile )
19969 }
20070
201- cleanUp (installLocation )
71+ t .Cleanup (func () {
72+ defer os .Remove (tempDir )
73+ fmt .Println ("Cleanup temporary directory" )
74+ })
20275}
20376
204- // TestDownloadFromURL_Valid : Test if https://releases.hashicorp.com/terraform/ is still valid
77+ // // TestDownloadFromURL_Valid : Test if https://releases.hashicorp.com/terraform/ is still valid
20578func TestDownloadFromURL_Valid (t * testing.T ) {
20679
20780 hashiURL := "https://releases.hashicorp.com/terraform/"
20881
20982 url , err := url .ParseRequestURI (hashiURL )
21083 if err != nil {
211- t .Errorf ("Valid URL provided: %v" , err )
212- t .Errorf ("Invalid URL %v" , err )
84+ t .Errorf ("Invalid URL %v [unexpected]" , err )
21385 } else {
214- t .Logf ("Valid URL from %v" , url )
86+ t .Logf ("Valid URL from %v [expected] " , url )
21587 }
21688}
0 commit comments