Skip to content

Commit 6196e63

Browse files
radeksimkobflad
andauthoredNov 30, 2021
installer: Add top-level E2E test for fs.AnyVersion (#43)
* installer: Add top-level E2E test for fs.AnyVersion * Update installer_test.go Co-authored-by: Brian Flad <bflad417@gmail.com> * Update installer_test.go * make file executable Co-authored-by: Brian Flad <bflad417@gmail.com>
1 parent 6ecd9cb commit 6196e63

File tree

5 files changed

+75
-32
lines changed

5 files changed

+75
-32
lines changed
 

‎fs/fs_test.go

-24
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package fs
33
import (
44
"context"
55
"os"
6-
"path/filepath"
7-
"runtime"
86
"testing"
97

108
"github.com/hashicorp/go-version"
@@ -43,25 +41,3 @@ func TestExactVersion(t *testing.T) {
4341
t.Fatal(err)
4442
}
4543
}
46-
47-
func createTempFile(t *testing.T, content string) (string, string) {
48-
tmpDir := t.TempDir()
49-
fileName := t.Name()
50-
51-
if runtime.GOOS == "windows" {
52-
fileName += ".exe"
53-
}
54-
55-
filePath := filepath.Join(tmpDir, fileName)
56-
f, err := os.Create(filePath)
57-
if err != nil {
58-
t.Fatal(err)
59-
}
60-
defer f.Close()
61-
_, err = f.WriteString(content)
62-
if err != nil {
63-
t.Fatal(err)
64-
}
65-
66-
return tmpDir, fileName
67-
}

‎fs/fs_unix_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestAnyVersion_notExecutable(t *testing.T) {
2323
os.Setenv("PATH", originalPath)
2424
})
2525

26-
dirPath, fileName := createTempFile(t, "")
26+
dirPath, fileName := testutil.CreateTempFile(t, "")
2727
os.Setenv("PATH", dirPath)
2828

2929
av := &AnyVersion{
@@ -47,7 +47,7 @@ func TestAnyVersion_executable(t *testing.T) {
4747
os.Setenv("PATH", originalPath)
4848
})
4949

50-
dirPath, fileName := createTempFile(t, "")
50+
dirPath, fileName := testutil.CreateTempFile(t, "")
5151
os.Setenv("PATH", dirPath)
5252

5353
fullPath := filepath.Join(dirPath, fileName)
@@ -71,7 +71,7 @@ func TestAnyVersion_executable(t *testing.T) {
7171
func TestAnyVersion_exactBinPath(t *testing.T) {
7272
testutil.EndToEndTest(t)
7373

74-
dirPath, fileName := createTempFile(t, "")
74+
dirPath, fileName := testutil.CreateTempFile(t, "")
7575
fullPath := filepath.Join(dirPath, fileName)
7676
err := os.Chmod(fullPath, 0700)
7777
if err != nil {
@@ -91,7 +91,7 @@ func TestAnyVersion_exactBinPath(t *testing.T) {
9191
func TestAnyVersion_exactBinPath_notExecutable(t *testing.T) {
9292
testutil.EndToEndTest(t)
9393

94-
dirPath, fileName := createTempFile(t, "")
94+
dirPath, fileName := testutil.CreateTempFile(t, "")
9595
fullPath := filepath.Join(dirPath, fileName)
9696
err := os.Chmod(fullPath, 0600)
9797
if err != nil {

‎fs/fs_windows_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestAnyVersion_executable(t *testing.T) {
2020
os.Setenv("path", originalPath)
2121
})
2222

23-
dirPath, fileName := createTempFile(t, "")
23+
dirPath, fileName := testutil.CreateTempFile(t, "")
2424
os.Setenv("path", dirPath)
2525

2626
av := &AnyVersion{
@@ -38,7 +38,7 @@ func TestAnyVersion_executable(t *testing.T) {
3838
func TestAnyVersion_exactBinPath(t *testing.T) {
3939
testutil.EndToEndTest(t)
4040

41-
dirPath, fileName := createTempFile(t, "")
41+
dirPath, fileName := testutil.CreateTempFile(t, "")
4242
fullPath := filepath.Join(dirPath, fileName)
4343

4444
av := &AnyVersion{
@@ -54,7 +54,7 @@ func TestAnyVersion_exactBinPath(t *testing.T) {
5454
func TestAnyVersion_exactBinPath_notFound(t *testing.T) {
5555
testutil.EndToEndTest(t)
5656

57-
dirPath, fileName := createTempFile(t, "")
57+
dirPath, fileName := testutil.CreateTempFile(t, "")
5858
fullPath := filepath.Join(dirPath, fileName)
5959

6060
err := os.Remove(fullPath)

‎installer_test.go

+38-1
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ package install
22

33
import (
44
"context"
5+
"os"
6+
"path/filepath"
57
"testing"
68

9+
"github.com/hashicorp/hc-install/fs"
710
"github.com/hashicorp/hc-install/internal/testutil"
811
"github.com/hashicorp/hc-install/product"
912
"github.com/hashicorp/hc-install/releases"
1013
"github.com/hashicorp/hc-install/src"
1114
)
1215

13-
func TestInstaller_Ensure(t *testing.T) {
16+
func TestInstaller_Ensure_installable(t *testing.T) {
1417
testutil.EndToEndTest(t)
1518

1619
// most of this logic is already tested within individual packages
@@ -34,6 +37,40 @@ func TestInstaller_Ensure(t *testing.T) {
3437
}
3538
}
3639

40+
func TestInstaller_Ensure_findable(t *testing.T) {
41+
testutil.EndToEndTest(t)
42+
43+
dirPath, fileName := testutil.CreateTempFile(t, "")
44+
45+
fullPath := filepath.Join(dirPath, fileName)
46+
err := os.Chmod(fullPath, 0700)
47+
if err != nil {
48+
t.Fatal(err)
49+
}
50+
51+
t.Setenv("PATH", dirPath)
52+
53+
// most of this logic is already tested within individual packages
54+
// so this is just a simple E2E test to ensure the public API
55+
// also works and continues working
56+
57+
i := NewInstaller()
58+
i.SetLogger(testutil.TestLogger())
59+
ctx := context.Background()
60+
_, err = i.Ensure(ctx, []src.Source{
61+
&fs.AnyVersion{
62+
Product: &product.Product{
63+
BinaryName: func() string {
64+
return fileName
65+
},
66+
},
67+
},
68+
})
69+
if err != nil {
70+
t.Fatal(err)
71+
}
72+
}
73+
3774
func TestInstaller_Install(t *testing.T) {
3875
testutil.EndToEndTest(t)
3976

‎internal/testutil/temp_file.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package testutil
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"runtime"
7+
"testing"
8+
)
9+
10+
func CreateTempFile(t *testing.T, content string) (string, string) {
11+
tmpDir := t.TempDir()
12+
fileName := t.Name()
13+
14+
if runtime.GOOS == "windows" {
15+
fileName += ".exe"
16+
}
17+
18+
filePath := filepath.Join(tmpDir, fileName)
19+
f, err := os.Create(filePath)
20+
if err != nil {
21+
t.Fatal(err)
22+
}
23+
defer f.Close()
24+
_, err = f.WriteString(content)
25+
if err != nil {
26+
t.Fatal(err)
27+
}
28+
29+
return tmpDir, fileName
30+
}

0 commit comments

Comments
 (0)
Please sign in to comment.