move IsEmptyDir to configs package

This commit is contained in:
Alex Pilon 2019-07-18 13:07:10 -04:00
parent cc9797443b
commit 0450f487fa
No known key found for this signature in database
GPG Key ID: 95659F6AEFC48D7E
7 changed files with 55 additions and 50 deletions

View File

@ -15,7 +15,6 @@ import (
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/backend"
backendInit "github.com/hashicorp/terraform/backend/init"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/configs/configschema"
"github.com/hashicorp/terraform/configs/configupgrade"
@ -125,7 +124,7 @@ func (c *InitCommand) Run(args []string) int {
if flagFromModule != "" {
src := flagFromModule
empty, err := config.IsEmptyDir(path)
empty, err := configs.IsEmptyDir(path)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error validating destination directory: %s", err))
return 1
@ -157,7 +156,7 @@ func (c *InitCommand) Run(args []string) int {
// If our directory is empty, then we're done. We can't get or setup
// the backend with an empty directory.
empty, err := config.IsEmptyDir(path)
empty, err := configs.IsEmptyDir(path)
if err != nil {
diags = diags.Append(fmt.Errorf("Error checking configuration: %s", err))
return 1

View File

@ -5,7 +5,7 @@ import (
"path/filepath"
"sort"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/moduledeps"
"github.com/hashicorp/terraform/terraform"
"github.com/hashicorp/terraform/tfdiags"
@ -46,7 +46,7 @@ func (c *ProvidersCommand) Run(args []string) int {
var diags tfdiags.Diagnostics
empty, err := config.IsEmptyDir(configPath)
empty, err := configs.IsEmptyDir(configPath)
if err != nil {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,

View File

@ -135,21 +135,6 @@ func LoadDir(root string) (*Config, error) {
return result, nil
}
// IsEmptyDir returns true if the directory given has no Terraform
// configuration files.
func IsEmptyDir(root string) (bool, error) {
if _, err := os.Stat(root); err != nil && os.IsNotExist(err) {
return true, nil
}
fs, os, err := dirFiles(root)
if err != nil {
return false, err
}
return len(fs) == 0 && len(os) == 0, nil
}
// Ext returns the Terraform configuration extension of the given
// path, or a blank string if it is an invalid function.
func ext(path string) string {

View File

@ -12,36 +12,6 @@ func TestErrNoConfigsFound_impl(t *testing.T) {
var _ error = new(ErrNoConfigsFound)
}
func TestIsEmptyDir(t *testing.T) {
val, err := IsEmptyDir(fixtureDir)
if err != nil {
t.Fatalf("err: %s", err)
}
if val {
t.Fatal("should not be empty")
}
}
func TestIsEmptyDir_noExist(t *testing.T) {
val, err := IsEmptyDir(filepath.Join(fixtureDir, "nopenopenope"))
if err != nil {
t.Fatalf("err: %s", err)
}
if !val {
t.Fatal("should be empty")
}
}
func TestIsEmptyDir_noConfigs(t *testing.T) {
val, err := IsEmptyDir(filepath.Join(fixtureDir, "dir-empty"))
if err != nil {
t.Fatalf("err: %s", err)
}
if !val {
t.Fatal("should be empty")
}
}
func TestLoadFile_badType(t *testing.T) {
_, err := LoadFile(filepath.Join(fixtureDir, "bad_type.tf.nope"))
if err == nil {

View File

@ -2,6 +2,7 @@ package configs
import (
"fmt"
"os"
"path/filepath"
"strings"
@ -140,3 +141,23 @@ func IsIgnoredFile(name string) bool {
strings.HasSuffix(name, "~") || // vim
strings.HasPrefix(name, "#") && strings.HasSuffix(name, "#") // emacs
}
// IsEmptyDir returns true if the given filesystem path contains no Terraform
// configuration files.
//
// Unlike the methods of the Parser type, this function always consults the
// real filesystem, and thus it isn't appropriate to use when working with
// configuration loaded from a plan file.
func IsEmptyDir(path string) (bool, error) {
if _, err := os.Stat(path); err != nil && os.IsNotExist(err) {
return true, nil
}
p := NewParser(nil)
fs, os, err := p.dirFiles(path)
if err != nil {
return false, err
}
return len(fs) == 0 && len(os) == 0, nil
}

View File

@ -138,3 +138,33 @@ func TestParserLoadConfigDirFailure(t *testing.T) {
}
}
func TestIsEmptyDir(t *testing.T) {
val, err := IsEmptyDir(filepath.Join("testdata", "valid-files"))
if err != nil {
t.Fatalf("err: %s", err)
}
if val {
t.Fatal("should not be empty")
}
}
func TestIsEmptyDir_noExist(t *testing.T) {
val, err := IsEmptyDir(filepath.Join("testdata", "nopenopenope"))
if err != nil {
t.Fatalf("err: %s", err)
}
if !val {
t.Fatal("should be empty")
}
}
func TestIsEmptyDir_noConfigs(t *testing.T) {
val, err := IsEmptyDir(filepath.Join("testdata", "dir-empty"))
if err != nil {
t.Fatalf("err: %s", err)
}
if !val {
t.Fatal("should be empty")
}
}

0
configs/testdata/dir-empty/.gitkeep vendored Normal file
View File