Skip to content
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

Modify regex to detect other legitimate forms of provider block declaration #265

Merged
merged 4 commits into from
Jan 9, 2024
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
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20240108-154019.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'helper/resource: Fix detection of provider block declaration in `Config`,
`ConfigDirectory`, and `ConfigFile`'
time: 2024-01-08T15:40:19.089652Z
custom:
Issue: "265"
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20240108-170824.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'helper/resource: Fix detection of terraform block declaration in `Config`,
`ConfigDirectory`, and `ConfigFile`'
time: 2024-01-08T17:08:24.219063Z
custom:
Issue: "265"
13 changes: 11 additions & 2 deletions internal/teststep/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@ const (
)

var (
providerConfigBlockRegex = regexp.MustCompile(`provider "?[a-zA-Z0-9_-]+"? {`)
terraformConfigBlockRegex = regexp.MustCompile(`terraform {`)
// Expected to match:
// provider "example" {
// provider "example"{
// provider example {
// provider example{
// provider"example"{
providerConfigBlockRegex = regexp.MustCompile(`provider(\s*"[a-zA-Z0-9_-]+"\s*|\s+[a-zA-Z0-9_-]+\s*){`)
// Expected to match:
// terraform {
// terraform{
terraformConfigBlockRegex = regexp.MustCompile(`terraform\s*{`)
)

// Config defines an interface implemented by all types
Expand Down
60 changes: 60 additions & 0 deletions internal/teststep/directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,48 @@ func TestConfigurationDirectory_HasProviderBlock(t *testing.T) {
},
expected: true,
},
"provider-block-quoted-with-attributes-no-spaces": {
configDirectory: configurationDirectory{
directory: "testdata/provider_block_quoted_with_attributes_no_spaces",
},
expected: true,
},
"provider-block-unquoted-with-attributes": {
configDirectory: configurationDirectory{
directory: "testdata/provider_block_unquoted_with_attributes",
},
expected: true,
},
"provider-block-unquoted-with-attributes_no-trailing-space": {
configDirectory: configurationDirectory{
directory: "testdata/provider_block_unquoted_with_attributes_no_trailing_space",
},
expected: true,
},
"provider-block-quoted-without-attributes": {
configDirectory: configurationDirectory{
directory: "testdata/provider_block_quoted_without_attributes",
},
expected: true,
},
"provider-block-quoted-without-attributes-no-spaces": {
configDirectory: configurationDirectory{
directory: "testdata/provider_block_quoted_without_attributes_no_spaces",
},
expected: true,
},
"provider-block-unquoted-without-attributes": {
configDirectory: configurationDirectory{
directory: "testdata/provider_block_unquoted_without_attributes",
},
expected: true,
},
"provider-block-unquoted-without-attributes-no-trailing-space": {
configDirectory: configurationDirectory{
directory: "testdata/provider_block_unquoted_without_attributes_no_trailing_space",
},
expected: true,
},
}

for name, testCase := range testCases {
Expand Down Expand Up @@ -150,24 +174,48 @@ func TestConfigurationDirectory_HasProviderBlock_AbsolutePath(t *testing.T) {
},
expected: true,
},
"provider-block-quoted-with-attributes-no-spaces": {
configDirectory: configurationDirectory{
directory: "testdata/provider_block_quoted_with_attributes_no_spaces",
},
expected: true,
},
"provider-block-unquoted-with-attributes": {
configDirectory: configurationDirectory{
directory: "testdata/provider_block_unquoted_with_attributes",
},
expected: true,
},
"provider-block-unquoted-with-attributes_no-trailing-space": {
configDirectory: configurationDirectory{
directory: "testdata/provider_block_unquoted_with_attributes_no_trailing_space",
},
expected: true,
},
"provider-block-quoted-without-attributes": {
configDirectory: configurationDirectory{
directory: "testdata/provider_block_quoted_without_attributes",
},
expected: true,
},
"provider-block-quoted-without-attributes-no-spaces": {
configDirectory: configurationDirectory{
directory: "testdata/provider_block_quoted_without_attributes_no_spaces",
},
expected: true,
},
"provider-block-unquoted-without-attributes": {
configDirectory: configurationDirectory{
directory: "testdata/provider_block_unquoted_without_attributes",
},
expected: true,
},
"provider-block-unquoted-without-attributes-no-trailing-space": {
configDirectory: configurationDirectory{
directory: "testdata/provider_block_unquoted_without_attributes_no_trailing_space",
},
expected: true,
},
}

for name, testCase := range testCases {
Expand Down Expand Up @@ -251,6 +299,12 @@ func TestConfigurationDirectory_HasTerraformBlock(t *testing.T) {
},
expected: true,
},
"terraform-block-no-space": {
configDirectory: configurationDirectory{
directory: "testdata/terraform_block_no_space",
},
expected: true,
},
}

for name, testCase := range testCases {
Expand Down Expand Up @@ -326,6 +380,12 @@ func TestConfigurationDirectory_HasTerraformBlock_AbsolutePath(t *testing.T) {
},
expected: true,
},
"terraform-block-no-space": {
configDirectory: configurationDirectory{
directory: "testdata/terraform_block_no_space",
},
expected: true,
},
}

for name, testCase := range testCases {
Expand Down
60 changes: 60 additions & 0 deletions internal/teststep/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,48 @@ func TestConfigurationFile_HasProviderBlock(t *testing.T) {
},
expected: true,
},
"provider-block-quoted-with-attributes-no-spaces": {
configFile: configurationFile{
file: "testdata/provider_block_quoted_with_attributes_no_spaces/main.tf",
},
expected: true,
},
"provider-block-unquoted-with-attributes": {
configFile: configurationFile{
file: "testdata/provider_block_unquoted_with_attributes/main.tf",
},
expected: true,
},
"provider-block-unquoted-with-attributes-no-trailing-space": {
configFile: configurationFile{
file: "testdata/provider_block_unquoted_with_attributes_no_trailing_space/main.tf",
},
expected: true,
},
"provider-block-quoted-without-attributes": {
configFile: configurationFile{
file: "testdata/provider_block_quoted_without_attributes/main.tf",
},
expected: true,
},
"provider-block-quoted-without-attributes-no-spaces": {
configFile: configurationFile{
file: "testdata/provider_block_quoted_without_attributes_no_spaces/main.tf",
},
expected: true,
},
"provider-block-unquoted-without-attributes": {
configFile: configurationFile{
file: "testdata/provider_block_unquoted_without_attributes/main.tf",
},
expected: true,
},
"provider-block-unquoted-without-attributes-no-trailing-space": {
configFile: configurationFile{
file: "testdata/provider_block_unquoted_without_attributes_no_trailing_space/main.tf",
},
expected: true,
},
}

for name, testCase := range testCases {
Expand Down Expand Up @@ -150,24 +174,48 @@ func TestConfigurationFile_HasProviderBlock_AbsolutePath(t *testing.T) {
},
expected: true,
},
"provider-block-quoted-with-attributes-no-spaces": {
configFile: configurationFile{
file: "testdata/provider_block_quoted_with_attributes_no_spaces/main.tf",
},
expected: true,
},
"provider-block-unquoted-with-attributes": {
configFile: configurationFile{
file: "testdata/provider_block_unquoted_with_attributes/main.tf",
},
expected: true,
},
"provider-block-unquoted-with-attributes-no-trailing-space": {
configFile: configurationFile{
file: "testdata/provider_block_unquoted_with_attributes_no_trailing_space/main.tf",
},
expected: true,
},
"provider-block-quoted-without-attributes": {
configFile: configurationFile{
file: "testdata/provider_block_quoted_without_attributes/main.tf",
},
expected: true,
},
"provider-block-quoted-without-attributes-no-spaces": {
configFile: configurationFile{
file: "testdata/provider_block_quoted_without_attributes_no_spaces/main.tf",
},
expected: true,
},
"provider-block-unquoted-without-attributes": {
configFile: configurationFile{
file: "testdata/provider_block_unquoted_without_attributes/main.tf",
},
expected: true,
},
"provider-block-unquoted-without-attributes-no-trailing-space": {
configFile: configurationFile{
file: "testdata/provider_block_unquoted_without_attributes_no_trailing_space/main.tf",
},
expected: true,
},
}

for name, testCase := range testCases {
Expand Down Expand Up @@ -251,6 +299,12 @@ func TestConfigurationFile_HasTerraformBlock(t *testing.T) {
},
expected: true,
},
"terraform-block-no-space": {
configFile: configurationFile{
file: "testdata/terraform_block_no_space/main.tf",
},
expected: true,
},
}

for name, testCase := range testCases {
Expand Down Expand Up @@ -326,6 +380,12 @@ func TestConfigurationFile_HasTerraformBlock_AbsolutePath(t *testing.T) {
},
expected: true,
},
"terraform-block-no-space": {
configFile: configurationFile{
file: "testdata/terraform_block_no_space/main.tf",
},
expected: true,
},
}

for name, testCase := range testCases {
Expand Down
56 changes: 56 additions & 0 deletions internal/teststep/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ provider "test" {
test = true
}

resource "test_test" "test" {}
`,
},
expected: true,
},
"provider-block-quoted-with-attributes-no-spaces": {
configRaw: configurationString{
raw: `
provider"test"{
test = true
}

resource "test_test" "test" {}
`,
},
Expand All @@ -77,6 +89,18 @@ provider test {
test = true
}

resource "test_test" "test" {}
`,
},
expected: true,
},
"provider-block-unquoted-with-attributes-no-trailing-space": {
configRaw: configurationString{
raw: `
provider test{
test = true
}

resource "test_test" "test" {}
`,
},
Expand All @@ -87,6 +111,16 @@ resource "test_test" "test" {}
raw: `
provider "test" {}

resource "test_test" "test" {}
`,
},
expected: true,
},
"provider-block-quoted-without-attributes-no-spaces": {
configRaw: configurationString{
raw: `
provider"test"{}

resource "test_test" "test" {}
`,
},
Expand All @@ -97,6 +131,16 @@ resource "test_test" "test" {}
raw: `
provider test {}

resource "test_test" "test" {}
`,
},
expected: true,
},
"provider-block-unquoted-without-attributes-no-trailing-space": {
configRaw: configurationString{
raw: `
provider test{}

resource "test_test" "test" {}
`,
},
Expand Down Expand Up @@ -176,6 +220,18 @@ terraform {
test = true
}

resource "test_test" "test" {}
`,
},
expected: true,
},
"terraform-block-no-space": {
configRaw: configurationString{
raw: `
terraform{
test = true
}

resource "test_test" "test" {}
`,
},
Expand Down
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
Loading