From 4cc44703fe7e9edec452e538676239595d26dd50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Ho=C3=9F?= Date: Wed, 13 Nov 2024 07:55:55 +0100 Subject: [PATCH] add example/test for #304 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sebastian Hoß --- internal/testutils/git_remote.go | 7 ++++++- .../data-sources/git_remote/single_remote/outputs.tf | 11 +++++++++++ terratest/tests/data_source_git_remote_test.go | 5 ++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/internal/testutils/git_remote.go b/internal/testutils/git_remote.go index dcccb18..bb1f912 100644 --- a/internal/testutils/git_remote.go +++ b/internal/testutils/git_remote.go @@ -8,13 +8,18 @@ package testutils import ( + "fmt" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/config" "testing" ) func CreateRemote(t *testing.T, repository *git.Repository, remote string) { - CreateRemoteWithUrls(t, repository, remote, []string{"https://example.com/metio/terraform-provider-git.git"}) + CreateRemoteWithName(t, repository, remote, "terraform-provider-git") +} + +func CreateRemoteWithName(t *testing.T, repository *git.Repository, remote string, name string) { + CreateRemoteWithUrls(t, repository, remote, []string{fmt.Sprintf("https://example.com/metio/%s.git", name)}) } func CreateRemoteWithUrls(t *testing.T, repository *git.Repository, remote string, urls []string) { diff --git a/terratest/data-sources/git_remote/single_remote/outputs.tf b/terratest/data-sources/git_remote/single_remote/outputs.tf index ee286fe..f81c106 100644 --- a/terratest/data-sources/git_remote/single_remote/outputs.tf +++ b/terratest/data-sources/git_remote/single_remote/outputs.tf @@ -16,3 +16,14 @@ output "name" { output "urls" { value = data.git_remote.single_remote.urls } + +locals { + selected_remote_url = data.git_remote.single_remote.urls[0] + splitted_url = split("/", local.selected_remote_url) + last_path_segment = local.splitted_url[length(local.splitted_url) - 1] + basename = trimsuffix(local.last_path_segment, ".git") +} + +output "upstream_repository_name" { + value = local.basename +} diff --git a/terratest/tests/data_source_git_remote_test.go b/terratest/tests/data_source_git_remote_test.go index 5e507ab..d363a8a 100644 --- a/terratest/tests/data_source_git_remote_test.go +++ b/terratest/tests/data_source_git_remote_test.go @@ -16,7 +16,8 @@ func TestDataSourceGitRemote_SingleRemote(t *testing.T) { t.Parallel() directory, repository := testutils.CreateRepository(t) name := "origin" - testutils.CreateRemote(t, repository, name) + repositoryName := "some-repository-name" + testutils.CreateRemoteWithName(t, repository, name, repositoryName) terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ TerraformDir: "../data-sources/git_remote/single_remote", @@ -34,9 +35,11 @@ func TestDataSourceGitRemote_SingleRemote(t *testing.T) { actualId := terraform.Output(t, terraformOptions, "id") actualName := terraform.Output(t, terraformOptions, "name") actualUrls := terraform.OutputList(t, terraformOptions, "urls") + actualUpstreamRepositoryName := terraform.Output(t, terraformOptions, "upstream_repository_name") assert.Equal(t, directory, actualDirectory, "actualDirectory") assert.Equal(t, name, actualId, "actualId") assert.Equal(t, name, actualName, "actualName") assert.Equal(t, 1, len(actualUrls), "actualUrls") + assert.Equal(t, repositoryName, actualUpstreamRepositoryName, "actualUpstreamRepositoryName") }