Пример #1
0
func (input InputModel) convert() (envmanModels.EnvironmentItemModel, error) {
	environment := envmanModels.EnvironmentItemModel{
		input.MappedTo:          input.Value,
		envmanModels.OptionsKey: input.getOptions(),
	}
	if err := environment.FillMissingDefaults(); err != nil {
		return envmanModels.EnvironmentItemModel{}, err
	}
	return environment, nil
}
Пример #2
0
func TestUpdateOrAddToEnvlist(t *testing.T) {
	env1 := models.EnvironmentItemModel{
		"test_key1": "test_value1",
	}
	require.Equal(t, nil, env1.FillMissingDefaults())

	env2 := models.EnvironmentItemModel{
		"test_key2": "test_value2",
	}
	require.Equal(t, nil, env2.FillMissingDefaults())

	// Should add to list, but not override
	oldEnvSlice := []models.EnvironmentItemModel{env1, env2}
	newEnvSlice, err := UpdateOrAddToEnvlist(oldEnvSlice, env1, false)
	require.Equal(t, nil, err)

	env1Cnt, err := countOfEnvKeyInEnvSlice(env1, newEnvSlice)
	require.Equal(t, nil, err)
	require.Equal(t, 2, env1Cnt)

	env2Cnt, err := countOfEnvKeyInEnvSlice(env2, newEnvSlice)
	require.Equal(t, nil, err)
	require.Equal(t, 1, env2Cnt)

	// Should update list
	oldEnvSlice = []models.EnvironmentItemModel{env1, env2}
	newEnvSlice, err = UpdateOrAddToEnvlist(oldEnvSlice, env1, true)
	require.Equal(t, nil, err)

	env1Cnt, err = countOfEnvKeyInEnvSlice(env1, newEnvSlice)
	require.Equal(t, nil, err)
	require.Equal(t, 1, env1Cnt)

	env2Cnt, err = countOfEnvKeyInEnvSlice(env2, newEnvSlice)
	require.Equal(t, nil, err)
	require.Equal(t, 1, env2Cnt)
}
Пример #3
0
func TestUpdateOrAddToEnvlist(t *testing.T) {
	env1 := models.EnvironmentItemModel{
		"test_key1": "test_value1",
	}
	err := env1.FillMissingDefaults()
	if err != nil {
		t.Fatal(err)
	}

	env2 := models.EnvironmentItemModel{
		"test_key2": "test_value2",
	}
	err = env2.FillMissingDefaults()
	if err != nil {
		t.Fatal(err)
	}

	// Should add to list, but not override
	oldEnvSlice := []models.EnvironmentItemModel{env1, env2}
	newEnvSlice, err := UpdateOrAddToEnvlist(oldEnvSlice, env1, false)
	if err != nil {
		t.Fatal(err)
	}

	env1Cnt, err := countOfEnvKeyInEnvSlice(env1, newEnvSlice)
	if err != nil {
		t.Fatal(err)
	}
	if env1Cnt != 2 {
		t.Fatalf("Failed to proper add env, %d x (test_key1)", env1Cnt)
	}

	env2Cnt, err := countOfEnvKeyInEnvSlice(env2, newEnvSlice)
	if err != nil {
		t.Fatal(err)
	}
	if env2Cnt != 1 {
		t.Fatalf("Failed to proper add env, %d x (test_key2)", env2Cnt)
	}

	// Should update list
	oldEnvSlice = []models.EnvironmentItemModel{env1, env2}
	newEnvSlice, err = UpdateOrAddToEnvlist(oldEnvSlice, env1, true)
	if err != nil {
		t.Fatal(err)
	}

	env1Cnt, err = countOfEnvKeyInEnvSlice(env1, newEnvSlice)
	if err != nil {
		t.Fatal(err)
	}
	if env1Cnt != 1 {
		t.Fatalf("Failed to proper add env, %d x (test_key1)", env1Cnt)
	}

	env2Cnt, err = countOfEnvKeyInEnvSlice(env2, newEnvSlice)
	if err != nil {
		t.Fatal(err)
	}
	if env2Cnt != 1 {
		t.Fatalf("Failed to proper add env, %d x (test_key2)", env2Cnt)
	}
}
Пример #4
0
func TestCommandEnvs(t *testing.T) {
	t.Log("commandEnvs test")
	{
		env1 := models.EnvironmentItemModel{
			"test_key1": "test_value1",
		}
		require.Equal(t, nil, env1.FillMissingDefaults())

		env2 := models.EnvironmentItemModel{
			"test_key2": "test_value2",
		}
		require.Equal(t, nil, env2.FillMissingDefaults())

		envs := []models.EnvironmentItemModel{env1, env2}

		sessionEnvs, err := commandEnvs(envs)
		require.Equal(t, nil, err)

		env1Found := false
		env2Found := false
		for _, envString := range sessionEnvs {
			comp := strings.Split(envString, "=")
			key := comp[0]
			value := comp[1]

			envKey1, envValue1, err := env1.GetKeyValuePair()
			require.Equal(t, nil, err)

			envKey2, envValue2, err := env2.GetKeyValuePair()
			require.Equal(t, nil, err)

			if key == envKey1 && value == envValue1 {
				env1Found = true
			}
			if key == envKey2 && value == envValue2 {
				env2Found = true
			}
		}
		require.Equal(t, true, env1Found)
		require.Equal(t, true, env2Found)
	}

	// Test skip_if_empty
	t.Log("skip_if_empty=false && value=empty => should add")
	{
		env1 := models.EnvironmentItemModel{
			"test_key3": "",
		}
		require.Equal(t, nil, env1.FillMissingDefaults())

		env2 := models.EnvironmentItemModel{
			"test_key4": "test_value4",
		}
		require.Equal(t, nil, env2.FillMissingDefaults())

		envs := []models.EnvironmentItemModel{env1, env2}

		sessionEnvs, err := commandEnvs(envs)
		require.Equal(t, nil, err)

		env1Found := false
		env2Found := false
		for _, envString := range sessionEnvs {
			comp := strings.Split(envString, "=")
			key := comp[0]
			value := comp[1]

			envKey1, envValue1, err := env1.GetKeyValuePair()
			require.Equal(t, nil, err)

			envKey2, envValue2, err := env2.GetKeyValuePair()
			require.Equal(t, nil, err)

			if key == envKey1 && value == envValue1 {
				env1Found = true
			}
			if key == envKey2 && value == envValue2 {
				env2Found = true
			}
		}
		require.Equal(t, true, env1Found)
		require.Equal(t, true, env2Found)
	}

	t.Log("skip_if_empty=true && value=empty => should NOT add")
	{
		env1 := models.EnvironmentItemModel{
			"test_key5": "",
			"opts": models.EnvironmentItemOptionsModel{
				SkipIfEmpty: pointers.NewBoolPtr(true),
			},
		}
		require.Equal(t, nil, env1.FillMissingDefaults())

		env2 := models.EnvironmentItemModel{
			"test_key6": "test_value6",
		}
		require.Equal(t, nil, env2.FillMissingDefaults())

		envs := []models.EnvironmentItemModel{env1, env2}

		sessionEnvs, err := commandEnvs(envs)
		require.Equal(t, nil, err)

		env1Found := false
		env2Found := false
		for _, envString := range sessionEnvs {
			comp := strings.Split(envString, "=")
			key := comp[0]
			value := comp[1]

			envKey1, envValue1, err := env1.GetKeyValuePair()
			require.Equal(t, nil, err)

			envKey2, envValue2, err := env2.GetKeyValuePair()
			require.Equal(t, nil, err)

			if key == envKey1 && value == envValue1 {
				env1Found = true
			}
			if key == envKey2 && value == envValue2 {
				env2Found = true
			}
		}
		require.Equal(t, false, env1Found)
		require.Equal(t, true, env2Found)
	}

	t.Log("skip_if_empty=true && value=NOT_empty => should add")
	{
		env1 := models.EnvironmentItemModel{
			"test_key7": "test_value7",
			"opts": models.EnvironmentItemOptionsModel{
				SkipIfEmpty: pointers.NewBoolPtr(true),
			},
		}
		require.Equal(t, nil, env1.FillMissingDefaults())

		env2 := models.EnvironmentItemModel{
			"test_key8": "test_value8",
		}
		require.Equal(t, nil, env2.FillMissingDefaults())

		envs := []models.EnvironmentItemModel{env1, env2}

		sessionEnvs, err := commandEnvs(envs)
		require.Equal(t, nil, err)

		env1Found := false
		env2Found := false
		for _, envString := range sessionEnvs {
			comp := strings.Split(envString, "=")
			key := comp[0]
			value := comp[1]

			envKey1, envValue1, err := env1.GetKeyValuePair()
			require.Equal(t, nil, err)

			envKey2, envValue2, err := env2.GetKeyValuePair()
			require.Equal(t, nil, err)

			if key == envKey1 && value == envValue1 {
				env1Found = true
			}
			if key == envKey2 && value == envValue2 {
				env2Found = true
			}
		}
		require.Equal(t, true, env1Found)
		require.Equal(t, true, env2Found)
	}

	t.Log("expand envs test")
	{
		env1 := models.EnvironmentItemModel{
			"env1": "Hello",
		}
		require.Equal(t, nil, env1.FillMissingDefaults())

		env2 := models.EnvironmentItemModel{
			"env2": "${env1} world",
		}
		require.Equal(t, nil, env2.FillMissingDefaults())

		env3 := models.EnvironmentItemModel{
			"env3": "${env2} !",
		}
		require.Equal(t, nil, env3.FillMissingDefaults())

		envs := []models.EnvironmentItemModel{env1, env2, env3}

		sessionEnvs, err := commandEnvs(envs)
		require.Equal(t, nil, err)

		env3Found := false
		for _, envString := range sessionEnvs {
			comp := strings.Split(envString, "=")
			key := comp[0]
			value := comp[1]

			envKey3, _, err := env3.GetKeyValuePair()
			require.Equal(t, nil, err)

			if key == envKey3 {
				require.Equal(t, "Hello world !", value)
				env3Found = true
			}
		}
		require.Equal(t, true, env3Found)
	}
}