config: IsEmptyDir is true if dir doesn't exist

This commit is contained in:
Mitchell Hashimoto 2014-09-26 16:28:18 -07:00
parent 9689a34b28
commit 6cbadf14df
2 changed files with 14 additions and 0 deletions

View File

@ -103,6 +103,10 @@ func LoadDir(root string) (*Config, error) {
// IsEmptyDir returns true if the directory given has no Terraform // IsEmptyDir returns true if the directory given has no Terraform
// configuration files. // configuration files.
func IsEmptyDir(root string) (bool, error) { func IsEmptyDir(root string) (bool, error) {
if _, err := os.Stat(root); err != nil && os.IsNotExist(err) {
return true, nil
}
fs, os, err := dirFiles(root) fs, os, err := dirFiles(root)
if err != nil { if err != nil {
return false, err return false, err

View File

@ -16,6 +16,16 @@ func TestIsEmptyDir(t *testing.T) {
} }
} }
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) { func TestIsEmptyDir_noConfigs(t *testing.T) {
val, err := IsEmptyDir(filepath.Join(fixtureDir, "dir-empty")) val, err := IsEmptyDir(filepath.Join(fixtureDir, "dir-empty"))
if err != nil { if err != nil {