terraform-provider-gitea/vendor/github.com/hashicorp/terraform-plugin-sdk/v2/internal/plugintest/config.go
dependabot[bot] 910ccdb092
Bump github.com/hashicorp/terraform-plugin-sdk/v2 from 2.26.1 to 2.27.0
Bumps [github.com/hashicorp/terraform-plugin-sdk/v2](https://github.com/hashicorp/terraform-plugin-sdk) from 2.26.1 to 2.27.0.
- [Release notes](https://github.com/hashicorp/terraform-plugin-sdk/releases)
- [Changelog](https://github.com/hashicorp/terraform-plugin-sdk/blob/main/CHANGELOG.md)
- [Commits](https://github.com/hashicorp/terraform-plugin-sdk/compare/v2.26.1...v2.27.0)

---
updated-dependencies:
- dependency-name: github.com/hashicorp/terraform-plugin-sdk/v2
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-07-03 20:21:30 +00:00

96 lines
3.0 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package plugintest
import (
"context"
"fmt"
"os"
"strings"
"github.com/hashicorp/go-version"
install "github.com/hashicorp/hc-install"
"github.com/hashicorp/hc-install/checkpoint"
"github.com/hashicorp/hc-install/fs"
"github.com/hashicorp/hc-install/product"
"github.com/hashicorp/hc-install/releases"
"github.com/hashicorp/hc-install/src"
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/logging"
)
// Config is used to configure the test helper. In most normal test programs
// the configuration is discovered automatically by an Init* function using
// DiscoverConfig, but this is exposed so that more complex scenarios can be
// implemented by direct configuration.
type Config struct {
SourceDir string
TerraformExec string
execTempDir string
PreviousPluginExec string
}
// DiscoverConfig uses environment variables and other means to automatically
// discover a reasonable test helper configuration.
func DiscoverConfig(ctx context.Context, sourceDir string) (*Config, error) {
tfVersion := strings.TrimPrefix(os.Getenv(EnvTfAccTerraformVersion), "v")
tfPath := os.Getenv(EnvTfAccTerraformPath)
tempDir := os.Getenv(EnvTfAccTempDir)
tfDir, err := os.MkdirTemp(tempDir, "plugintest-terraform")
if err != nil {
return nil, fmt.Errorf("failed to create temp dir: %w", err)
}
var sources []src.Source
switch {
case tfPath != "":
logging.HelperResourceTrace(ctx, fmt.Sprintf("Adding potential Terraform CLI source of exact path: %s", tfPath))
sources = append(sources, &fs.AnyVersion{
ExactBinPath: tfPath,
})
case tfVersion != "":
tfVersion, err := version.NewVersion(tfVersion)
if err != nil {
return nil, fmt.Errorf("invalid Terraform version: %w", err)
}
logging.HelperResourceTrace(ctx, fmt.Sprintf("Adding potential Terraform CLI source of releases.hashicorp.com exact version %q for installation in: %s", tfVersion, tfDir))
sources = append(sources, &releases.ExactVersion{
InstallDir: tfDir,
Product: product.Terraform,
Version: tfVersion,
})
default:
logging.HelperResourceTrace(ctx, "Adding potential Terraform CLI source of local filesystem PATH lookup")
logging.HelperResourceTrace(ctx, fmt.Sprintf("Adding potential Terraform CLI source of checkpoint.hashicorp.com latest version for installation in: %s", tfDir))
sources = append(sources, &fs.AnyVersion{
Product: &product.Terraform,
})
sources = append(sources, &checkpoint.LatestVersion{
InstallDir: tfDir,
Product: product.Terraform,
})
}
installer := install.NewInstaller()
tfExec, err := installer.Ensure(context.Background(), sources)
if err != nil {
return nil, fmt.Errorf("failed to find or install Terraform CLI from %+v: %w", sources, err)
}
ctx = logging.TestTerraformPathContext(ctx, tfExec)
logging.HelperResourceDebug(ctx, "Found Terraform CLI")
return &Config{
SourceDir: sourceDir,
TerraformExec: tfExec,
execTempDir: tfDir,
}, nil
}