Skip to content

Commit

Permalink
Adding test coverage for error conditions and configurationDirectory,…
Browse files Browse the repository at this point in the history
… configurationFile and configurationString Write methods (#150)
  • Loading branch information
bendbennett committed Jul 28, 2023
1 parent 28af0cc commit 6fbfd10
Show file tree
Hide file tree
Showing 7 changed files with 627 additions and 42 deletions.
311 changes: 296 additions & 15 deletions internal/teststep/directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (
"context"
"os"
"path/filepath"
"regexp"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestConfigurationDirectory_HasProviderBlock(t *testing.T) {
Expand All @@ -16,7 +19,14 @@ func TestConfigurationDirectory_HasProviderBlock(t *testing.T) {
testCases := map[string]struct {
configDirectory configurationDirectory
expected bool
expectedError *regexp.Regexp
}{
"not-directory": {
configDirectory: configurationDirectory{
directory: "testdata/empty_file/main.tf",
},
expectedError: regexp.MustCompile(`.*not a directory`),
},
"no-config": {
configDirectory: configurationDirectory{
directory: "testdata/empty_dir",
Expand Down Expand Up @@ -75,12 +85,22 @@ func TestConfigurationDirectory_HasProviderBlock(t *testing.T) {

got, err := testCase.configDirectory.HasProviderBlock(context.Background())

if err != nil {
t.Errorf("unexpected error: %s", err)
if testCase.expectedError == nil && err != nil {
t.Errorf("unexpected error %s", err)
}

if testCase.expectedError != nil && err == nil {
t.Errorf("expected error but got none")
}

if testCase.expectedError != nil && err != nil {
if !testCase.expectedError.MatchString(err.Error()) {
t.Errorf("expected error %s, got error %s", testCase.expectedError.String(), err)
}
}

if testCase.expected != got {
t.Errorf("expected %t, got %t", testCase.expected, got)
if diff := cmp.Diff(testCase.expected, got); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
Expand All @@ -92,7 +112,14 @@ func TestConfigurationDirectory_HasProviderBlock_AbsolutePath(t *testing.T) {
testCases := map[string]struct {
configDirectory configurationDirectory
expected bool
expectedError *regexp.Regexp
}{
"not-directory": {
configDirectory: configurationDirectory{
directory: "testdata/empty_file/main.tf",
},
expectedError: regexp.MustCompile(`.*not a directory`),
},
"no-config": {
configDirectory: configurationDirectory{
directory: "testdata/empty_dir",
Expand Down Expand Up @@ -159,12 +186,22 @@ func TestConfigurationDirectory_HasProviderBlock_AbsolutePath(t *testing.T) {

got, err := testCase.configDirectory.HasProviderBlock(context.Background())

if err != nil {
t.Errorf("unexpected error: %s", err)
if testCase.expectedError == nil && err != nil {
t.Errorf("unexpected error %s", err)
}

if testCase.expected != got {
t.Errorf("expected %t, got %t", testCase.expected, got)
if testCase.expectedError != nil && err == nil {
t.Errorf("expected error but got none")
}

if testCase.expectedError != nil && err != nil {
if !testCase.expectedError.MatchString(err.Error()) {
t.Errorf("expected error %s, got error %s", testCase.expectedError.String(), err)
}
}

if diff := cmp.Diff(testCase.expected, got); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
Expand All @@ -176,7 +213,14 @@ func TestConfigurationDirectory_HasTerraformBlock(t *testing.T) {
testCases := map[string]struct {
configDirectory configurationDirectory
expected bool
expectedError *regexp.Regexp
}{
"not-directory": {
configDirectory: configurationDirectory{
directory: "testdata/empty_file/main.tf",
},
expectedError: regexp.MustCompile(`.*not a directory`),
},
"no-config": {
configDirectory: configurationDirectory{
directory: "testdata/empty_dir",
Expand Down Expand Up @@ -217,12 +261,22 @@ func TestConfigurationDirectory_HasTerraformBlock(t *testing.T) {

got, err := testCase.configDirectory.HasTerraformBlock(context.Background())

if err != nil {
t.Errorf("unexpected error: %s", err)
if testCase.expectedError == nil && err != nil {
t.Errorf("unexpected error %s", err)
}

if testCase.expectedError != nil && err == nil {
t.Errorf("expected error but got none")
}

if testCase.expected != got {
t.Errorf("expected %t, got %t", testCase.expected, got)
if testCase.expectedError != nil && err != nil {
if !testCase.expectedError.MatchString(err.Error()) {
t.Errorf("expected error %s, got error %s", testCase.expectedError.String(), err)
}
}

if diff := cmp.Diff(testCase.expected, got); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
Expand All @@ -234,7 +288,14 @@ func TestConfigurationDirectory_HasTerraformBlock_AbsolutePath(t *testing.T) {
testCases := map[string]struct {
configDirectory configurationDirectory
expected bool
expectedError *regexp.Regexp
}{
"not-directory": {
configDirectory: configurationDirectory{
directory: "testdata/empty_file/main.tf",
},
expectedError: regexp.MustCompile(`.*not a directory`),
},
"no-config": {
configDirectory: configurationDirectory{
directory: "testdata/empty_dir",
Expand Down Expand Up @@ -283,13 +344,233 @@ func TestConfigurationDirectory_HasTerraformBlock_AbsolutePath(t *testing.T) {

got, err := testCase.configDirectory.HasTerraformBlock(context.Background())

if testCase.expectedError == nil && err != nil {
t.Errorf("unexpected error %s", err)
}

if testCase.expectedError != nil && err == nil {
t.Errorf("expected error but got none")
}

if testCase.expectedError != nil && err != nil {
if !testCase.expectedError.MatchString(err.Error()) {
t.Errorf("expected error %s, got error %s", testCase.expectedError.String(), err)
}
}

if diff := cmp.Diff(testCase.expected, got); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}

func TestConfigurationDirectory_Write(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
configDirectory configurationDirectory
expectedError *regexp.Regexp
}{
"not-directory": {
configDirectory: configurationDirectory{
directory: "testdata/empty_file/main.tf",
},
expectedError: regexp.MustCompile(`.*not a directory`),
},
"no-config": {
configDirectory: configurationDirectory{
"testdata/empty_dir",
},
},
"dir-single-file": {
configDirectory: configurationDirectory{
"testdata/random",
},
},
"dir-multiple-files": {
configDirectory: configurationDirectory{
"testdata/random_multiple_files",
},
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

tempDir := t.TempDir()

err := testCase.configDirectory.Write(context.Background(), tempDir)

if testCase.expectedError == nil && err != nil {
t.Errorf("unexpected error %s", err)
}

if testCase.expectedError != nil && err == nil {
t.Errorf("expected error but got none")
}

if testCase.expectedError != nil && err != nil {
if !testCase.expectedError.MatchString(err.Error()) {
t.Errorf("expected error %s, got error %s", testCase.expectedError.String(), err)
}
}

if err == nil {
dirEntries, err := os.ReadDir(testCase.configDirectory.directory)

if err != nil {
t.Errorf("error reading directory: %s", err)
}

tempDirEntries, err := os.ReadDir(tempDir)

if err != nil {
t.Errorf("error reading temp directory: %s", err)
}

if len(dirEntries) != len(tempDirEntries) {
t.Errorf("expected %d dir entries, got %d dir entries", dirEntries, tempDirEntries)
}

for k, v := range dirEntries {
dirEntryInfo, err := v.Info()

if err != nil {
t.Errorf("error getting dir entry info: %s", err)
}

tempDirEntryInfo, err := tempDirEntries[k].Info()

if err != nil {
t.Errorf("error getting temp dir entry info: %s", err)
}

if diff := cmp.Diff(tempDirEntryInfo, dirEntryInfo, fileInfoComparer); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
}
}
})
}
}

func TestConfigurationDirectory_Write_AbsolutePath(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
configDirectory configurationDirectory
expectedError *regexp.Regexp
}{
"not-directory": {
configDirectory: configurationDirectory{
directory: "testdata/empty_file/main.tf",
},
expectedError: regexp.MustCompile(`.*not a directory`),
},
"no-config": {
configDirectory: configurationDirectory{
"testdata/empty_dir",
},
},
"dir-single-file": {
configDirectory: configurationDirectory{
"testdata/random",
},
},
"dir-multiple-files": {
configDirectory: configurationDirectory{
"testdata/random_multiple_files",
},
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

tempDir := t.TempDir()

pwd, err := os.Getwd()

if err != nil {
t.Errorf("unexpected error: %s", err)
t.Errorf("error getting wd: %s", err)
}

if testCase.expected != got {
t.Errorf("expected %t, got %t", testCase.expected, got)
testCase.configDirectory.directory = filepath.Join(pwd, testCase.configDirectory.directory)

err = testCase.configDirectory.Write(context.Background(), tempDir)

if testCase.expectedError == nil && err != nil {
t.Errorf("unexpected error %s", err)
}

if testCase.expectedError != nil && err == nil {
t.Errorf("expected error but got none")
}

if testCase.expectedError != nil && err != nil {
if !testCase.expectedError.MatchString(err.Error()) {
t.Errorf("expected error %s, got error %s", testCase.expectedError.String(), err)
}
}

if err == nil {
dirEntries, err := os.ReadDir(testCase.configDirectory.directory)

if err != nil {
t.Errorf("error reading directory: %s", err)
}

tempDirEntries, err := os.ReadDir(tempDir)

if err != nil {
t.Errorf("error reading temp directory: %s", err)
}

if len(dirEntries) != len(tempDirEntries) {
t.Errorf("expected %d dir entries, got %d dir entries", dirEntries, tempDirEntries)
}

for k, v := range dirEntries {
dirEntryInfo, err := v.Info()

if err != nil {
t.Errorf("error getting dir entry info: %s", err)
}

tempDirEntryInfo, err := tempDirEntries[k].Info()

if err != nil {
t.Errorf("error getting temp dir entry info: %s", err)
}

if diff := cmp.Diff(tempDirEntryInfo, dirEntryInfo, fileInfoComparer); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
}
}
})
}
}

var fileInfoComparer = cmp.Comparer(func(x, y os.FileInfo) bool {
if x.Name() != y.Name() {
return false
}

if x.Mode() != y.Mode() {
return false
}

if x.Size() != y.Size() {
return false
}

return true
})
Loading

0 comments on commit 6fbfd10

Please sign in to comment.