Skip empty example (#270)

* skip empty examples that contain no tf files

* refactor code
This commit is contained in:
lonegunmanb 2023-11-13 11:27:32 +08:00 committed by GitHub
parent 161494719e
commit 72c7e787ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,15 +1,17 @@
package e2e package e2e
import ( import (
"github.com/gruntwork-io/terratest/modules/files" "fmt"
"github.com/gruntwork-io/terratest/modules/packer"
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
"github.com/stretchr/testify/require"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
"github.com/gruntwork-io/terratest/modules/files"
"github.com/gruntwork-io/terratest/modules/packer"
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
"github.com/stretchr/testify/require"
helper "github.com/Azure/terraform-module-test-helper" helper "github.com/Azure/terraform-module-test-helper"
"github.com/gruntwork-io/terratest/modules/terraform" "github.com/gruntwork-io/terratest/modules/terraform"
) )
@ -35,14 +37,11 @@ func Test_Quickstarts(t *testing.T) {
folders = removeDuplicates(folders) folders = removeDuplicates(folders)
for _, f := range folders { for _, f := range folders {
f = strings.TrimSpace(f) f = strings.TrimSpace(f)
if filepath.Dir(f) != "quickstart" {
continue
}
rootPath := filepath.Join("..", "..") rootPath := filepath.Join("..", "..")
path := filepath.Join(rootPath, f) if skip(rootPath, f) {
if !files.IsExistingDir(path) {
continue continue
} }
test, ok := speicalTests[f] test, ok := speicalTests[f]
if !ok { if !ok {
test = func(t *testing.T) { test = func(t *testing.T) {
@ -63,14 +62,35 @@ func allExamples() ([]string, error) {
} }
var r []string var r []string
for _, f := range examples { for _, f := range examples {
if !f.IsDir() {
continue
}
r = append(r, filepath.Join("quickstart", f.Name())) r = append(r, filepath.Join("quickstart", f.Name()))
} }
return r, nil return r, nil
} }
func skip(rootPath string, f string) bool {
f = filepath.Join(rootPath, f)
if !files.IsExistingDir(f) {
return true
}
if !strings.HasSuffix(filepath.Dir(f), fmt.Sprintf("%squickstart", string(os.PathSeparator))) {
return true
}
return !containsTerraformFile(f)
}
func containsTerraformFile(f string) bool {
dir, err := os.ReadDir(f)
if err != nil {
return false
}
for _, entry := range dir {
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".tf") {
return true
}
}
return false
}
func test201VmssPackerJumpbox(t *testing.T) { func test201VmssPackerJumpbox(t *testing.T) {
examplePath := filepath.Join("..", "..", "quickstart", "201-vmss-packer-jumpbox") examplePath := filepath.Join("..", "..", "quickstart", "201-vmss-packer-jumpbox")
examplePath = test_structure.CopyTerraformFolderToTemp(t, examplePath, "") examplePath = test_structure.CopyTerraformFolderToTemp(t, examplePath, "")