Skip to content

Commit

Permalink
add example/test for #304
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Hoß <seb@xn--ho-hia.de>
  • Loading branch information
sebhoss committed Nov 13, 2024
1 parent 180981d commit 4cc4470
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
7 changes: 6 additions & 1 deletion internal/testutils/git_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions terratest/data-sources/git_remote/single_remote/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 4 additions & 1 deletion terratest/tests/data_source_git_remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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")
}

0 comments on commit 4cc4470

Please sign in to comment.