<commit_before>package remote

import (
	"github.com/gruntwork-io/terragrunt/options"
	"github.com/stretchr/testify/assert"
	"strings"
	"testing"
)

func TestToTerraformRemoteConfigArgs(t *testing.T) {
	t.Parallel()

	remoteState := RemoteState{
		Backend: "s3",
		Config: map[string]string{
			"encrypt": "true",
			"bucket":  "my-bucket",
			"key":     "terraform.tfstate",
			"region":  "us-east-1",
		},
	}
	args := remoteState.ToTerraformInitArgs()

	assertRemoteConfigArgsEqual(t, args, "init -backend-config=encrypt=true -backend-config=bucket=my-bucket -backend-config=key=terraform.tfstate -backend-config=region=us-east-1")
}

func TestToTerraformRemoteConfigArgsNoBackendConfigs(t *testing.T) {
	t.Parallel()

	remoteState := RemoteState{Backend: "s3"}
	args := remoteState.ToTerraformInitArgs()

	assertRemoteConfigArgsEqual(t, args, "init")
}

func TestShouldOverrideExistingRemoteState(t *testing.T) {
	t.Parallel()

	terragruntOptions := options.NewTerragruntOptionsForTest("remote_state_test")

	testCases := []struct {
		existingState   TerraformStateRemote
		stateFromConfig RemoteState
		shouldOverride  bool
	}{
		{TerraformStateRemote{}, RemoteState{}, false},
		{TerraformStateRemote{Type: "s3"}, RemoteState{Backend: "s3"}, false},
		{TerraformStateRemote{Type: "s3"}, RemoteState{Backend: "atlas"}, true},
		{
			TerraformStateRemote{
				Type:   "s3",
				Config: map[string]string{"bucket": "foo", "key": "bar", "region": "us-east-1"},
			},
			RemoteState{
				Backend: "s3",
				Config:  map[string]string{"bucket": "foo", "key": "bar", "region": "us-east-1"},
			},
			false,
		}, {
			TerraformStateRemote{
				Type:   "s3",
				Config: map[string]string{"bucket": "foo", "key": "bar", "region": "us-east-1"},
			},
			RemoteState{
				Backend: "s3",
				Config:  map[string]string{"bucket": "different", "key": "bar", "region": "us-east-1"},
			},
			true,
		}, {
			TerraformStateRemote{
				Type:   "s3",
				Config: map[string]string{"bucket": "foo", "key": "bar", "region": "us-east-1"},
			},
			RemoteState{
				Backend: "s3",
				Config:  map[string]string{"bucket": "foo", "key": "different", "region": "us-east-1"},
			},
			true,
		}, {
			TerraformStateRemote{
				Type:   "s3",
				Config: map[string]string{"bucket": "foo", "key": "bar", "region": "us-east-1"},
			},
			RemoteState{
				Backend: "s3",
				Config:  map[string]string{"bucket": "foo", "key": "bar", "region": "different"},
			},
			true,
		},
	}

	for _, testCase := range testCases {
		shouldOverride, err := shouldOverrideExistingRemoteState(&testCase.existingState, testCase.stateFromConfig, terragruntOptions)
		assert.Nil(t, err, "Unexpected error: %v", err)
		assert.Equal(t, testCase.shouldOverride, shouldOverride, "Expect shouldOverrideExistingRemoteState to return %t but got %t for existingRemoteState %v and remoteStateFromTerragruntConfig %v", testCase.shouldOverride, shouldOverride, testCase.existingState, testCase.stateFromConfig)
	}
}

func assertRemoteConfigArgsEqual(t *testing.T, actualArgs []string, expectedArgs string) {
	expected := strings.Split(expectedArgs, " ")
	assert.Len(t, actualArgs, len(expected))

	for _, expectedArg := range expected {
		assert.Contains(t, actualArgs, expectedArg)
	}
}<commit_msg>Update test cases for init command<commit_after>package remote

import (
	"github.com/gruntwork-io/terragrunt/options"
	"github.com/stretchr/testify/assert"
	"strings"
	"testing"
)

func TestToTerraformInitArgs(t *testing.T) {
	t.Parallel()

	remoteState := RemoteState{
		Backend: "s3",
		Config: map[string]string{
			"encrypt": "true",
			"bucket":  "my-bucket",
			"key":     "terraform.tfstate",
			"region":  "us-east-1",
		},
	}
	args := remoteState.ToTerraformInitArgs()

	assertTerraformInitArgsEqual(t, args, "-backend-config=encrypt=true -backend-config=bucket=my-bucket -backend-config=key=terraform.tfstate -backend-config=region=us-east-1")
}

func TestToTerraformInitArgsNoBackendConfigs(t *testing.T) {
	t.Parallel()

	remoteState := RemoteState{Backend: "s3"}
	args := remoteState.ToTerraformInitArgs()

	assertTerraformInitArgsEqual(t, args, "")
}

func TestShouldOverrideExistingRemoteState(t *testing.T) {
	t.Parallel()

	terragruntOptions := options.NewTerragruntOptionsForTest("remote_state_test")

	testCases := []struct {
		existingState   TerraformStateRemote
		stateFromConfig RemoteState
		shouldOverride  bool
	}{
		{TerraformStateRemote{}, RemoteState{}, false},
		{TerraformStateRemote{Type: "s3"}, RemoteState{Backend: "s3"}, false},
		{TerraformStateRemote{Type: "s3"}, RemoteState{Backend: "atlas"}, true},
		{
			TerraformStateRemote{
				Type:   "s3",
				Config: map[string]string{"bucket": "foo", "key": "bar", "region": "us-east-1"},
			},
			RemoteState{
				Backend: "s3",
				Config:  map[string]string{"bucket": "foo", "key": "bar", "region": "us-east-1"},
			},
			false,
		}, {
			TerraformStateRemote{
				Type:   "s3",
				Config: map[string]string{"bucket": "foo", "key": "bar", "region": "us-east-1"},
			},
			RemoteState{
				Backend: "s3",
				Config:  map[string]string{"bucket": "different", "key": "bar", "region": "us-east-1"},
			},
			true,
		}, {
			TerraformStateRemote{
				Type:   "s3",
				Config: map[string]string{"bucket": "foo", "key": "bar", "region": "us-east-1"},
			},
			RemoteState{
				Backend: "s3",
				Config:  map[string]string{"bucket": "foo", "key": "different", "region": "us-east-1"},
			},
			true,
		}, {
			TerraformStateRemote{
				Type:   "s3",
				Config: map[string]string{"bucket": "foo", "key": "bar", "region": "us-east-1"},
			},
			RemoteState{
				Backend: "s3",
				Config:  map[string]string{"bucket": "foo", "key": "bar", "region": "different"},
			},
			true,
		},
	}

	for _, testCase := range testCases {
		shouldOverride, err := shouldOverrideExistingRemoteState(&testCase.existingState, testCase.stateFromConfig, terragruntOptions)
		assert.Nil(t, err, "Unexpected error: %v", err)
		assert.Equal(t, testCase.shouldOverride, shouldOverride, "Expect shouldOverrideExistingRemoteState to return %t but got %t for existingRemoteState %v and remoteStateFromTerragruntConfig %v", testCase.shouldOverride, shouldOverride, testCase.existingState, testCase.stateFromConfig)
	}
}

func assertTerraformInitArgsEqual(t *testing.T, actualArgs []string, expectedArgs string) {
	expected := strings.Split(expectedArgs, " ")
	assert.Len(t, actualArgs, len(expected))

	for _, expectedArg := range expected {
		assert.Contains(t, actualArgs, expectedArg)
	}
}
{'fraction_non_alphanumeric': 0.16761670004621784, 'fraction_numerical': 0.006778616545986751, 'mean_word_length': 4.875113122171946, 'pattern_counts': {'":': 56, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2199851', 'n_tokens_mistral': 2484, 'n_tokens_neox': 2268, 'n_words': 427}