Skip to content

installer: Add top-level E2E test for fs.AnyVersion #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions fs/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package fs
import (
"context"
"os"
"path/filepath"
"runtime"
"testing"

"github.com/hashicorp/go-version"
Expand Down Expand Up @@ -43,25 +41,3 @@ func TestExactVersion(t *testing.T) {
t.Fatal(err)
}
}

func createTempFile(t *testing.T, content string) (string, string) {
tmpDir := t.TempDir()
fileName := t.Name()

if runtime.GOOS == "windows" {
fileName += ".exe"
}

filePath := filepath.Join(tmpDir, fileName)
f, err := os.Create(filePath)
if err != nil {
t.Fatal(err)
}
defer f.Close()
_, err = f.WriteString(content)
if err != nil {
t.Fatal(err)
}

return tmpDir, fileName
}
8 changes: 4 additions & 4 deletions fs/fs_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestAnyVersion_notExecutable(t *testing.T) {
os.Setenv("PATH", originalPath)
})

dirPath, fileName := createTempFile(t, "")
dirPath, fileName := testutil.CreateTempFile(t, "")
os.Setenv("PATH", dirPath)

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

dirPath, fileName := createTempFile(t, "")
dirPath, fileName := testutil.CreateTempFile(t, "")
os.Setenv("PATH", dirPath)

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

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

dirPath, fileName := createTempFile(t, "")
dirPath, fileName := testutil.CreateTempFile(t, "")
fullPath := filepath.Join(dirPath, fileName)
err := os.Chmod(fullPath, 0600)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions fs/fs_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestAnyVersion_executable(t *testing.T) {
os.Setenv("path", originalPath)
})

dirPath, fileName := createTempFile(t, "")
dirPath, fileName := testutil.CreateTempFile(t, "")
os.Setenv("path", dirPath)

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

dirPath, fileName := createTempFile(t, "")
dirPath, fileName := testutil.CreateTempFile(t, "")
fullPath := filepath.Join(dirPath, fileName)

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

dirPath, fileName := createTempFile(t, "")
dirPath, fileName := testutil.CreateTempFile(t, "")
fullPath := filepath.Join(dirPath, fileName)

err := os.Remove(fullPath)
Expand Down
39 changes: 38 additions & 1 deletion installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package install

import (
"context"
"os"
"path/filepath"
"testing"

"github.com/hashicorp/hc-install/fs"
"github.com/hashicorp/hc-install/internal/testutil"
"github.com/hashicorp/hc-install/product"
"github.com/hashicorp/hc-install/releases"
"github.com/hashicorp/hc-install/src"
)

func TestInstaller_Ensure(t *testing.T) {
func TestInstaller_Ensure_installable(t *testing.T) {
testutil.EndToEndTest(t)

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

func TestInstaller_Ensure_findable(t *testing.T) {
testutil.EndToEndTest(t)

dirPath, fileName := testutil.CreateTempFile(t, "")

fullPath := filepath.Join(dirPath, fileName)
err := os.Chmod(fullPath, 0700)
if err != nil {
t.Fatal(err)
}

t.Setenv("PATH", dirPath)

// most of this logic is already tested within individual packages
// so this is just a simple E2E test to ensure the public API
// also works and continues working

i := NewInstaller()
i.SetLogger(testutil.TestLogger())
ctx := context.Background()
_, err = i.Ensure(ctx, []src.Source{
&fs.AnyVersion{
Product: &product.Product{
BinaryName: func() string {
return fileName
},
},
},
})
if err != nil {
t.Fatal(err)
}
}

func TestInstaller_Install(t *testing.T) {
testutil.EndToEndTest(t)

Expand Down
30 changes: 30 additions & 0 deletions internal/testutil/temp_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package testutil

import (
"os"
"path/filepath"
"runtime"
"testing"
)

func CreateTempFile(t *testing.T, content string) (string, string) {
tmpDir := t.TempDir()
fileName := t.Name()

if runtime.GOOS == "windows" {
fileName += ".exe"
}

filePath := filepath.Join(tmpDir, fileName)
f, err := os.Create(filePath)
if err != nil {
t.Fatal(err)
}
defer f.Close()
_, err = f.WriteString(content)
if err != nil {
t.Fatal(err)
}

return tmpDir, fileName
}