Skip to content

Commit

Permalink
Adding checks for absolute filepath in configurationDirectory and con…
Browse files Browse the repository at this point in the history
…figurationFile HasProviderBlock, HasTerraformBlock and Write methods (#150)

  * Adding test coverage for configurationDirectory and configurationFile HasProviderBlock method
  • Loading branch information
bendbennett committed Jul 28, 2023
1 parent 0c4a1fe commit 36dd090
Show file tree
Hide file tree
Showing 13 changed files with 293 additions and 34 deletions.
49 changes: 32 additions & 17 deletions internal/teststep/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,27 @@ type configurationDirectory struct {
directory string
}

// HasConfigurationFiles is used during validation to ensure that
// ExternalProviders are not declared at the TestCase or TestStep
// level when using TestStep.ConfigDirectory.
func (c configurationDirectory) HasConfigurationFiles() bool {
return true
}

// HasProviderBlock returns true if the Config has declared a provider
// configuration block, e.g. provider "examplecloud" {...}
func (c configurationDirectory) HasProviderBlock(ctx context.Context) (bool, error) {
pwd, err := os.Getwd()
configDirectory := c.directory

if err != nil {
return false, err
}
if !filepath.IsAbs(configDirectory) {
pwd, err := os.Getwd()

if err != nil {
return false, err
}

configDirectory := filepath.Join(pwd, c.directory)
configDirectory = filepath.Join(pwd, configDirectory)
}

contains, err := filesContains(configDirectory, providerConfigBlockRegex)

Expand All @@ -42,13 +49,17 @@ func (c configurationDirectory) HasProviderBlock(ctx context.Context) (bool, err
// HasTerraformBlock returns true if the Config has declared a terraform
// configuration block, e.g. terraform {...}
func (c configurationDirectory) HasTerraformBlock(ctx context.Context) (bool, error) {
pwd, err := os.Getwd()
configDirectory := c.directory

if err != nil {
return false, err
}
if !filepath.IsAbs(configDirectory) {
pwd, err := os.Getwd()

configDirectory := filepath.Join(pwd, c.directory)
if err != nil {
return false, err
}

configDirectory = filepath.Join(pwd, configDirectory)
}

contains, err := filesContains(configDirectory, terraformConfigBlockRegex)

Expand All @@ -59,17 +70,21 @@ func (c configurationDirectory) HasTerraformBlock(ctx context.Context) (bool, er
return contains, nil
}

// Write copies all files from directory to destination.
func (c configurationDirectory) Write(ctx context.Context, dest string) error {
// Copy all files from c.directory to dest
pwd, err := os.Getwd()
configDirectory := c.directory

if err != nil {
return err
}
if !filepath.IsAbs(configDirectory) {
pwd, err := os.Getwd()

if err != nil {
return err
}

configDirectory := filepath.Join(pwd, c.directory)
configDirectory = filepath.Join(pwd, configDirectory)
}

err = copyFiles(configDirectory, dest)
err := copyFiles(configDirectory, dest)

if err != nil {
return err
Expand Down
85 changes: 85 additions & 0 deletions internal/teststep/directory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package teststep

import (
"context"
"testing"
)

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

testCases := map[string]struct {
configDirectory configurationDirectory
expected bool
}{
"no-config": {
configDirectory: configurationDirectory{
directory: "testdata/empty_dir",
},
expected: false,
},
"provider-meta-attribute": {
configDirectory: configurationDirectory{
directory: "testdata/provider_meta_attribute",
},
expected: false,
},
"provider-object-attribute": {
configDirectory: configurationDirectory{
directory: "testdata/provider_object_attribute",
},
expected: false,
},
"provider-string-attribute": {
configDirectory: configurationDirectory{
directory: "testdata/provider_string_attribute",
},
expected: false,
},
"provider-block-quoted-with-attributes": {
configDirectory: configurationDirectory{
directory: "testdata/provider_block_quoted_with_attributes",
},
expected: true,
},
"provider-block-unquoted-with-attributes": {
configDirectory: configurationDirectory{
directory: "testdata/provider_block_unquoted_with_attributes",
},
expected: true,
},
"provider-block-quoted-without-attributes": {
configDirectory: configurationDirectory{
directory: "testdata/provider_block_quoted_without_attributes",
},
expected: true,
},
"provider-block-unquoted-without-attributes": {
configDirectory: configurationDirectory{
directory: "testdata/provider_block_unquoted_without_attributes",
},
expected: true,
},
}

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

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

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

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

if testCase.expected != got {
t.Errorf("expected %t, got %t", testCase.expected, got)
}
})
}
}
49 changes: 32 additions & 17 deletions internal/teststep/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,27 @@ type configurationFile struct {
file string
}

// HasConfigurationFiles is used during validation to ensure that
// ExternalProviders are not declared at the TestCase or TestStep
// level when using TestStep.ConfigFile.
func (c configurationFile) HasConfigurationFiles() bool {
return true
}

// HasProviderBlock returns true if the Config has declared a provider
// configuration block, e.g. provider "examplecloud" {...}
func (c configurationFile) HasProviderBlock(ctx context.Context) (bool, error) {
pwd, err := os.Getwd()
configFile := c.file

if err != nil {
return false, err
}
if !filepath.IsAbs(configFile) {
pwd, err := os.Getwd()

if err != nil {
return false, err
}

configFile := filepath.Join(pwd, c.file)
configFile = filepath.Join(pwd, configFile)
}

contains, err := fileContains(configFile, providerConfigBlockRegex)

Expand All @@ -42,13 +49,17 @@ func (c configurationFile) HasProviderBlock(ctx context.Context) (bool, error) {
// HasTerraformBlock returns true if the Config has declared a terraform
// configuration block, e.g. terraform {...}
func (c configurationFile) HasTerraformBlock(ctx context.Context) (bool, error) {
pwd, err := os.Getwd()
configFile := c.file

if err != nil {
return false, err
}
if !filepath.IsAbs(configFile) {
pwd, err := os.Getwd()

configFile := filepath.Join(pwd, c.file)
if err != nil {
return false, err
}

configFile = filepath.Join(pwd, configFile)
}

contains, err := fileContains(configFile, terraformConfigBlockRegex)

Expand All @@ -59,17 +70,21 @@ func (c configurationFile) HasTerraformBlock(ctx context.Context) (bool, error)
return contains, nil
}

// Write copies file from c.file to destination.
func (c configurationFile) Write(ctx context.Context, dest string) error {
// Copy file from c.file to dest
pwd, err := os.Getwd()
configFile := c.file

if err != nil {
return err
}
if !filepath.IsAbs(configFile) {
pwd, err := os.Getwd()

if err != nil {
return err
}

configFile := filepath.Join(pwd, c.file)
configFile = filepath.Join(pwd, configFile)
}

err = copyFile(configFile, dest)
err := copyFile(configFile, dest)

if err != nil {
return err
Expand Down
85 changes: 85 additions & 0 deletions internal/teststep/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package teststep

import (
"context"
"testing"
)

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

testCases := map[string]struct {
configFile configurationFile
expected bool
}{
"no-config": {
configFile: configurationFile{
file: "testdata/empty_file/main.tf",
},
expected: false,
},
"provider-meta-attribute": {
configFile: configurationFile{
file: "testdata/provider_meta_attribute/main.tf",
},
expected: false,
},
"provider-object-attribute": {
configFile: configurationFile{
file: "testdata/provider_object_attribute/main.tf",
},
expected: false,
},
"provider-string-attribute": {
configFile: configurationFile{
file: "testdata/provider_string_attribute/main.tf",
},
expected: false,
},
"provider-block-quoted-with-attributes": {
configFile: configurationFile{
file: "testdata/provider_block_quoted_with_attributes/main.tf",
},
expected: true,
},
"provider-block-unquoted-with-attributes": {
configFile: configurationFile{
file: "testdata/provider_block_unquoted_with_attributes/main.tf",
},
expected: true,
},
"provider-block-quoted-without-attributes": {
configFile: configurationFile{
file: "testdata/provider_block_quoted_without_attributes/main.tf",
},
expected: true,
},
"provider-block-unquoted-without-attributes": {
configFile: configurationFile{
file: "testdata/provider_block_unquoted_without_attributes/main.tf",
},
expected: true,
},
}

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

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

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

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

if testCase.expected != got {
t.Errorf("expected %t, got %t", testCase.expected, got)
}
})
}
}
4 changes: 4 additions & 0 deletions internal/teststep/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type configurationString struct {
raw string
}

// HasConfigurationFiles is used during validation to allow declaration
// of ExternalProviders at the TestCase or TestStep level when using
// TestStep.Config.
func (c configurationString) HasConfigurationFiles() bool {
return false
}
Expand All @@ -33,6 +36,7 @@ func (c configurationString) HasTerraformBlock(ctx context.Context) (bool, error
return terraformConfigBlockRegex.MatchString(c.raw), nil
}

// Write creates a file and writes c.raw into it.
func (c configurationString) Write(ctx context.Context, dest string) error {
outFilename := filepath.Join(dest, rawConfigFileName)
rmFilename := filepath.Join(dest, rawConfigFileNameJSON)
Expand Down
3 changes: 3 additions & 0 deletions internal/teststep/testdata/empty_file/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

provider "test" {
test = true
}

resource "test_test" "test" {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

provider "test" {}

resource "test_test" "test" {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

provider test {
test = true
}

resource "test_test" "test" {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

provider test {}

resource "test_test" "test" {}
Loading

0 comments on commit 36dd090

Please sign in to comment.