From 7452f8e329fc54a61c0499313f5470635bcff7c8 Mon Sep 17 00:00:00 2001 From: Jeremy Muriel Date: Wed, 6 Jan 2021 19:29:28 +0100 Subject: [PATCH] Add support for return `...` blocks in acctest (#39) --- README.md | 1 + lib/blocks/blockreader.go | 4 ++++ lib/blocks/testdata/test1.go | 8 ++++++++ lib/blocks/testdata/test1_results.yaml | 4 ++++ 4 files changed, 17 insertions(+) diff --git a/README.md b/README.md index 7aab3e86..08ff87e4 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ The tool currently supports blocks with the following start and end lines: |```terraform |`, | |return fmt.Sprintf(`|`, | |return fmt.Sprintf(`|`) | +|return ` |` | ### Extract Terraform Blocks diff --git a/lib/blocks/blockreader.go b/lib/blocks/blockreader.go index d0220dc9..7822acbd 100644 --- a/lib/blocks/blockreader.go +++ b/lib/blocks/blockreader.go @@ -64,6 +64,8 @@ func ReaderIgnore(br *Reader, number int, line string) error { func IsStartLine(line string) bool { if strings.HasSuffix(line, "return fmt.Sprintf(`\n") { // acctest return true + } else if strings.HasSuffix(line, "return `\n") { // acctest + return true } else if strings.HasPrefix(line, "```hcl") { // documentation return true } else if strings.HasPrefix(line, "```terraform") { // documentation @@ -78,6 +80,8 @@ func IsStartLine(line string) bool { func IsFinishLine(line string) bool { if accTestFinishLineWithLeadingSpacesMatcher.MatchString(line) { // acctest return true + } else if strings.HasSuffix(line, "`\n") { // acctest + return true } else if strings.HasPrefix(line, "```") { // documentation return true } diff --git a/lib/blocks/testdata/test1.go b/lib/blocks/testdata/test1.go index b0512b04..145164c9 100644 --- a/lib/blocks/testdata/test1.go +++ b/lib/blocks/testdata/test1.go @@ -12,6 +12,14 @@ resource "aws_s3_bucket" "simple" { `) } +func testReturnStringSimple() string { + return ` +resource "aws_s3_bucket" "simple2" { + bucket = "tf-test-bucket-simple2" +} +` +} + func testReturnSprintfWithParameters(randInt int) string { return fmt.Sprintf(` resource "aws_s3_bucket" "with-parameters" { diff --git a/lib/blocks/testdata/test1_results.yaml b/lib/blocks/testdata/test1_results.yaml index 4c38f2e6..e7dc3ebf 100644 --- a/lib/blocks/testdata/test1_results.yaml +++ b/lib/blocks/testdata/test1_results.yaml @@ -3,6 +3,10 @@ expected_results: resource "aws_s3_bucket" "simple" { bucket = "tf-test-bucket-simple" } + - | + resource "aws_s3_bucket" "simple2" { + bucket = "tf-test-bucket-simple2" + } - | resource "aws_s3_bucket" "with-parameters" { bucket = "tf-test-bucket-with-parameters-%d"