-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1614db3
commit 8a9c70e
Showing
9 changed files
with
194 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/function" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
var _ function.Function = &DirectoryExistsFunction{} | ||
|
||
type DirectoryExistsFunction struct{} | ||
|
||
func NewDirectoryExistsFunction() function.Function { | ||
return &DirectoryExistsFunction{} | ||
} | ||
|
||
func (f *DirectoryExistsFunction) Metadata(ctx context.Context, req function.MetadataRequest, resp *function.MetadataResponse) { | ||
resp.Name = "direxists" | ||
} | ||
|
||
func (f *DirectoryExistsFunction) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) { | ||
resp.Definition = function.Definition{ | ||
Summary: "Determines whether a directory exists at a given path.", | ||
Description: "Given a path string, will return true if the directory exists. " + | ||
"This function works only with directories. If used with a regular file, FIFO, or other " + | ||
"special mode, it will return an error.", | ||
|
||
Parameters: []function.Parameter{ | ||
function.StringParameter{ | ||
Name: "path", | ||
Description: "Relative or absolute path to check for the existence of a directory", | ||
}, | ||
}, | ||
Return: function.BoolReturn{}, | ||
} | ||
} | ||
|
||
func (f *DirectoryExistsFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { | ||
var directoryPath string | ||
|
||
resp.Diagnostics.Append(req.Arguments.Get(ctx, &directoryPath)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
fi, err := os.Stat(directoryPath) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
resp.Diagnostics.Append(resp.Result.Set(ctx, types.BoolValue(false))...) | ||
return | ||
} else { | ||
resp.Diagnostics.AddArgumentError(0, "Error checking for directory", err.Error()) | ||
return | ||
} | ||
} | ||
|
||
if fi.IsDir() { | ||
resp.Diagnostics.Append(resp.Result.Set(ctx, types.BoolValue(true))...) | ||
return | ||
} | ||
|
||
// TODO: Should we mimic the terraform core logic for printing a nice message of what the underlying file mode is? | ||
// https://github.com/hashicorp/terraform/blob/8da1c006f9d7f381e644a5f0e2c25094ea29571d/internal/lang/funcs/filesystem.go#L234-L257 | ||
resp.Diagnostics.AddArgumentError(0, "Invalid file mode detected", fmt.Sprintf("%q was found, but is not a directory", directoryPath)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package provider | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/config" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/knownvalue" | ||
"github.com/hashicorp/terraform-plugin-testing/plancheck" | ||
) | ||
|
||
func TestDirectoryExists_basic(t *testing.T) { | ||
resource.UnitTest(t, resource.TestCase{ | ||
// TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
// tfversion.RequireAbove(tfversion.Version1_8_0), | ||
// }, | ||
ProtoV5ProviderFactories: protoV5ProviderFactories(), | ||
Steps: []resource.TestStep{ | ||
{ | ||
ConfigDirectory: config.TestNameDirectory(), | ||
ConfigPlanChecks: resource.ConfigPlanChecks{ | ||
PreApply: []plancheck.PlanCheck{ | ||
plancheck.ExpectKnownOutputValue("test_dir_exists", knownvalue.BoolExact(true)), | ||
plancheck.ExpectKnownOutputValue("test_dir_doesnt_exist", knownvalue.BoolExact(false)), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestDirectoryExists_invalid(t *testing.T) { | ||
// TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
// tfversion.RequireAbove(tfversion.Version1_8_0), | ||
// }, | ||
resource.UnitTest(t, resource.TestCase{ | ||
ProtoV5ProviderFactories: protoV5ProviderFactories(), | ||
Steps: []resource.TestStep{ | ||
{ | ||
ConfigDirectory: config.TestNameDirectory(), | ||
ExpectError: regexp.MustCompile("\"./testdata/TestDirectoryExists_invalid/not_a_dir\" was found, but\nis not a directory."), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
internal/provider/testdata/TestDirectoryExists_basic/main.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
terraform { | ||
required_providers { | ||
local = { | ||
source = "hashicorp/local" | ||
} | ||
} | ||
} | ||
|
||
output "test_dir_exists" { | ||
# Known issue where relative path is based on where the test working directory is located: | ||
# https://github.com/hashicorp/terraform-plugin-testing/issues/277 | ||
value = provider::local::direxists("${path.module}/testdata/TestDirectoryExists_basic/test_dir") | ||
} | ||
|
||
|
||
output "test_dir_doesnt_exist" { | ||
# Known issue where relative path is based on where the test working directory is located: | ||
# https://github.com/hashicorp/terraform-plugin-testing/issues/277 | ||
value = provider::local::direxists("${path.module}/testdata/TestDirectoryExists_basic/nothing_here") | ||
} |
Empty file.
13 changes: 13 additions & 0 deletions
13
internal/provider/testdata/TestDirectoryExists_invalid/main.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
terraform { | ||
required_providers { | ||
local = { | ||
source = "hashicorp/local" | ||
} | ||
} | ||
} | ||
|
||
output "test_not_a_dir" { | ||
# Known issue where relative path is based on where the test working directory is located: | ||
# https://github.com/hashicorp/terraform-plugin-testing/issues/277 | ||
value = provider::local::direxists("${path.module}/testdata/TestDirectoryExists_invalid/not_a_dir") | ||
} |
1 change: 1 addition & 0 deletions
1
internal/provider/testdata/TestDirectoryExists_invalid/not_a_dir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is a text file |