Skip to content

Commit

Permalink
update unit tests for prepare
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Jan 15, 2025
1 parent b472536 commit 5081c87
Showing 1 changed file with 5 additions and 27 deletions.
32 changes: 5 additions & 27 deletions provisioner/testinfra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"os"
"slices"
"strings"
"testing"

"github.com/hashicorp/packer-plugin-sdk/packer"
Expand Down Expand Up @@ -114,11 +113,11 @@ func TestProvisionerPrepareMinimal(test *testing.T) {
test.Errorf("default setting for PytestPath is incorrect: %s", provisioner.config.PytestPath)
}

if len(provisioner.config.TestFiles) != 0 {
test.Errorf("default setting for TestFiles is incorrect: %+q", provisioner.config.TestFiles)
if len(provisioner.config.TestFiles) > 0 {
test.Errorf("default empty setting for TestFiles is incorrect: %+q", provisioner.config.TestFiles)
}

if len(provisioner.config.DestinationDir) != 0 {
if len(provisioner.config.DestinationDir) > 0 {
test.Errorf("default empty setting for DestinationDir is incorrect: %s", provisioner.config.DestinationDir)
}
}
Expand All @@ -138,25 +137,6 @@ func TestProvisionerPrepareNonExistChdir(test *testing.T) {
}
}

// test provisioner prepare defaults to empty slice for test files
func TestProvisionerPrepareEmptyTestFile(test *testing.T) {
var provisioner Provisioner
var emptyTestFileConfig = &Config{
PytestPath: "/usr/local/bin/py.test",
}

if !CI {
err := provisioner.Prepare(emptyTestFileConfig)
if err != nil {
test.Error("prepare function failed with no test_files minimal Testinfra Packer config")
}

if len(provisioner.config.TestFiles) > 0 {
test.Errorf("default setting for TestFiles is incorrect: %s", strings.Join(provisioner.config.TestFiles, ""))
}
}
}

// test provisioner prepare errors on nonexistent files
func TestProvisionerPrepareNonExistFiles(test *testing.T) {
var provisioner Provisioner
Expand All @@ -167,8 +147,7 @@ func TestProvisionerPrepareNonExistFiles(test *testing.T) {
TestFiles: []string{"../fixtures/test.py"},
}

err := provisioner.Prepare(noPytestConfig)
if !(errors.Is(err, os.ErrNotExist)) {
if err := provisioner.Prepare(noPytestConfig); err == nil || !(errors.Is(err, os.ErrNotExist)) {
test.Error("prepare function did not fail correctly on nonexistent pytest")
test.Error(err)
}
Expand All @@ -180,8 +159,7 @@ func TestProvisionerPrepareNonExistFiles(test *testing.T) {
}

if !CI {
err = provisioner.Prepare(noTestFileConfig)
if !(errors.Is(err, os.ErrNotExist)) {
if err := provisioner.Prepare(noTestFileConfig); err == nil || !(errors.Is(err, os.ErrNotExist)) {
test.Error("prepare function did not fail correctly on nonexistent testfile")
test.Error(err)
}
Expand Down

0 comments on commit 5081c87

Please sign in to comment.