コード例 #1
0
ファイル: util.go プロジェクト: bazscsa/envman
func removeDefaults(env *models.EnvironmentItemModel) error {
	opts, err := env.GetOptions()
	if err != nil {
		return err
	}
	if opts.Title != nil && *opts.Title == "" {
		opts.Title = nil
	}
	if opts.Description != nil && *opts.Description == "" {
		opts.Description = nil
	}
	if opts.Summary != nil && *opts.Summary == "" {
		opts.Summary = nil
	}
	if opts.IsRequired != nil && *opts.IsRequired == models.DefaultIsRequired {
		opts.IsRequired = nil
	}
	if opts.IsExpand != nil && *opts.IsExpand == models.DefaultIsExpand {
		opts.IsExpand = nil
	}
	if opts.IsDontChangeValue != nil && *opts.IsDontChangeValue == models.DefaultIsDontChangeValue {
		opts.IsDontChangeValue = nil
	}
	(*env)[models.OptionsKey] = opts
	return nil
}
コード例 #2
0
func removeEnvironmentRedundantFields(env *envmanModels.EnvironmentItemModel) error {
	options, err := env.GetOptions()
	if err != nil {
		return err
	}

	hasOptions := false

	if options.Title != nil {
		if *options.Title == "" {
			options.Title = nil
		} else {
			hasOptions = true
		}
	}
	if options.Description != nil {
		if *options.Description == "" {
			options.Description = nil
		} else {
			hasOptions = true
		}
	}
	if options.Summary != nil {
		if *options.Summary == "" {
			options.Summary = nil
		} else {
			hasOptions = true
		}
	}
	if options.IsRequired != nil {
		if *options.IsRequired == envmanModels.DefaultIsRequired {
			options.IsRequired = nil
		} else {
			hasOptions = true
		}
	}
	if options.IsExpand != nil {
		if *options.IsExpand == envmanModels.DefaultIsExpand {
			options.IsExpand = nil
		} else {
			hasOptions = true
		}
	}
	if options.IsDontChangeValue != nil {
		if *options.IsDontChangeValue == envmanModels.DefaultIsDontChangeValue {
			options.IsDontChangeValue = nil
		} else {
			hasOptions = true
		}
	}

	if hasOptions {
		(*env)[envmanModels.OptionsKey] = options
	} else {
		delete(*env, envmanModels.OptionsKey)
	}

	return nil
}
コード例 #3
0
ファイル: models_methods.go プロジェクト: birmacher/bitrise
// MergeEnvironmentWith ...
func MergeEnvironmentWith(env *envmanModels.EnvironmentItemModel, otherEnv envmanModels.EnvironmentItemModel) error {
	// merge key-value
	key, _, err := env.GetKeyValuePair()
	if err != nil {
		return err
	}

	otherKey, otherValue, err := otherEnv.GetKeyValuePair()
	if err != nil {
		return err
	}

	if otherKey != key {
		return errors.New("Env keys are diferent")
	}

	(*env)[key] = otherValue

	//merge options
	options, err := env.GetOptions()
	if err != nil {
		return err
	}

	otherOptions, err := otherEnv.GetOptions()
	if err != nil {
		return err
	}
	if otherOptions.Title != nil {
		options.Title = pointers.NewStringPtr(*otherOptions.Title)
	}
	if otherOptions.Description != nil {
		options.Description = pointers.NewStringPtr(*otherOptions.Description)
	}
	if otherOptions.Summary != nil {
		options.Summary = pointers.NewStringPtr(*otherOptions.Summary)
	}
	if len(otherOptions.ValueOptions) > 0 {
		options.ValueOptions = otherOptions.ValueOptions
	}
	if otherOptions.IsRequired != nil {
		options.IsRequired = pointers.NewBoolPtr(*otherOptions.IsRequired)
	}
	if otherOptions.IsExpand != nil {
		options.IsExpand = pointers.NewBoolPtr(*otherOptions.IsExpand)
	}
	if otherOptions.IsDontChangeValue != nil {
		options.IsDontChangeValue = pointers.NewBoolPtr(*otherOptions.IsDontChangeValue)
	}
	if otherOptions.IsTemplate != nil {
		options.IsTemplate = pointers.NewBoolPtr(*otherOptions.IsTemplate)
	}
	(*env)[envmanModels.OptionsKey] = options
	return nil
}
コード例 #4
0
func TestMergeEnvironmentWith(t *testing.T) {
	diffEnv := envmanModels.EnvironmentItemModel{
		"test_key": "test_value",
		envmanModels.OptionsKey: envmanModels.EnvironmentItemOptionsModel{
			Title:             pointers.NewStringPtr("test_title"),
			Description:       pointers.NewStringPtr("test_description"),
			Summary:           pointers.NewStringPtr("test_summary"),
			ValueOptions:      []string{"test_valu_options1", "test_valu_options2"},
			IsRequired:        pointers.NewBoolPtr(true),
			IsExpand:          pointers.NewBoolPtr(false),
			IsDontChangeValue: pointers.NewBoolPtr(true),
			IsTemplate:        pointers.NewBoolPtr(true),
		},
	}

	t.Log("Different keys")
	{
		env := envmanModels.EnvironmentItemModel{
			"test_key1": "test_value",
		}
		require.Error(t, MergeEnvironmentWith(&env, diffEnv))
	}

	t.Log("Normal merge")
	{
		env := envmanModels.EnvironmentItemModel{
			"test_key": "test_value",
			envmanModels.OptionsKey: envmanModels.EnvironmentItemOptionsModel{
				SkipIfEmpty: pointers.NewBoolPtr(true),
				Category:    pointers.NewStringPtr("test"),
			},
		}
		require.NoError(t, MergeEnvironmentWith(&env, diffEnv))

		options, err := env.GetOptions()
		require.NoError(t, err)

		diffOptions, err := diffEnv.GetOptions()
		require.NoError(t, err)

		require.Equal(t, *diffOptions.Title, *options.Title)
		require.Equal(t, *diffOptions.Description, *options.Description)
		require.Equal(t, *diffOptions.Summary, *options.Summary)
		require.Equal(t, len(diffOptions.ValueOptions), len(options.ValueOptions))
		require.Equal(t, *diffOptions.IsRequired, *options.IsRequired)
		require.Equal(t, *diffOptions.IsExpand, *options.IsExpand)
		require.Equal(t, *diffOptions.IsDontChangeValue, *options.IsDontChangeValue)
		require.Equal(t, *diffOptions.IsTemplate, *options.IsTemplate)

		require.Equal(t, true, *options.SkipIfEmpty)
		require.Equal(t, "test", *options.Category)
	}
}
コード例 #5
0
// ValidateStepInputOutputModel ...
func ValidateStepInputOutputModel(env envmanModels.EnvironmentItemModel, checkRequiredFields bool) error {
	if err := env.Validate(); err != nil {
		return err
	}

	if checkRequiredFields {
		options, err := env.GetOptions()
		if err != nil {
			return err
		}

		if options.Title == nil || *options.Title == "" {
			return errors.New("Invalid environment: missing or empty title")
		}
	}

	return nil
}
コード例 #6
0
ファイル: util_test.go プロジェクト: bazscsa/envman
func TestRemoveDefaults(t *testing.T) {
	defaultIsRequired := models.DefaultIsRequired
	defaultIsExpand := models.DefaultIsExpand
	defaultIsDontChangeValue := models.DefaultIsDontChangeValue

	// Filled env
	env := models.EnvironmentItemModel{
		testKey: testValue,
		models.OptionsKey: models.EnvironmentItemOptionsModel{
			Title:             pointers.NewStringPtr(testTitle),
			Description:       pointers.NewStringPtr(testEmptyString),
			ValueOptions:      []string{},
			IsRequired:        pointers.NewBoolPtr(defaultIsRequired),
			IsExpand:          pointers.NewBoolPtr(defaultIsExpand),
			IsDontChangeValue: pointers.NewBoolPtr(defaultIsDontChangeValue),
		},
	}

	err := removeDefaults(&env)
	if err != nil {
		t.Fatal(err)
	}

	opts, err := env.GetOptions()
	if err != nil {
		t.Fatal(err)
	}
	if opts.Title == nil {
		t.Fatal("Removed Title")
	}
	if opts.Description != nil {
		t.Fatal("Failed to remove default Description")
	}
	if opts.IsRequired != nil {
		t.Fatal("Failed to remove default IsRequired")
	}
	if opts.IsExpand != nil {
		t.Fatal("Failed to remove default IsExpand")
	}
	if opts.IsDontChangeValue != nil {
		t.Fatal("Failed to remove default IsDontChangeValue")
	}
}
コード例 #7
0
// ValidateStepInputOutputModel ...
func ValidateStepInputOutputModel(env envmanModels.EnvironmentItemModel) error {
	key, _, err := env.GetKeyValuePair()
	if err != nil {
		return err
	}
	if key == "" {
		return errors.New("Invalid environment: empty env_key")
	}

	options, err := env.GetOptions()
	if err != nil {
		return err
	}

	if options.Title == nil || *options.Title == "" {
		return errors.New("Invalid environment: missing or empty title")
	}

	return nil
}
コード例 #8
0
ファイル: models_methods.go プロジェクト: birmacher/bitrise
// ValidateStepInputOutputModel ...
func ValidateStepInputOutputModel(env envmanModels.EnvironmentItemModel, checkRequiredFields bool) error {
	key, _, err := env.GetKeyValuePair()
	if err != nil {
		return fmt.Errorf("Invalid environment (%s), err: %s", key, err)
	}

	if err := env.Validate(); err != nil {
		return fmt.Errorf("Invalid environment (%s), err: %s", key, err)
	}

	if checkRequiredFields {
		options, err := env.GetOptions()
		if err != nil {
			return fmt.Errorf("Invalid environment (%s), err: %s", key, err)
		}

		if options.Title == nil || *options.Title == "" {
			return fmt.Errorf("Invalid environment (%s), err: missing or empty title", key)
		}
	}

	return nil
}
コード例 #9
0
ファイル: util_test.go プロジェクト: fbernardo/envman
func TestRemoveDefaults(t *testing.T) {
	// Filled env
	env := models.EnvironmentItemModel{
		"test_key": "test_value",
		models.OptionsKey: models.EnvironmentItemOptionsModel{
			Title:      pointers.NewStringPtr("test_title"),
			IsTemplate: pointers.NewBoolPtr(!models.DefaultIsTemplate),

			Description:       pointers.NewStringPtr(""),
			Summary:           pointers.NewStringPtr(""),
			ValueOptions:      []string{},
			IsRequired:        pointers.NewBoolPtr(models.DefaultIsRequired),
			IsDontChangeValue: pointers.NewBoolPtr(models.DefaultIsDontChangeValue),
			IsExpand:          pointers.NewBoolPtr(models.DefaultIsExpand),
			SkipIfEmpty:       pointers.NewBoolPtr(models.DefaultSkipIfEmpty),
		},
	}

	require.Equal(t, nil, removeDefaults(&env))

	opts, err := env.GetOptions()
	require.Equal(t, nil, err)

	require.NotEqual(t, (*string)(nil), opts.Title)
	require.Equal(t, "test_title", *opts.Title)
	require.NotEqual(t, (*bool)(nil), opts.IsTemplate)
	require.Equal(t, !models.DefaultIsTemplate, *opts.IsTemplate)

	require.Equal(t, (*string)(nil), opts.Description)
	require.Equal(t, (*string)(nil), opts.Summary)
	require.Equal(t, 0, len(opts.ValueOptions))
	require.Equal(t, (*bool)(nil), opts.IsRequired)
	require.Equal(t, (*bool)(nil), opts.IsDontChangeValue)
	require.Equal(t, (*bool)(nil), opts.IsExpand)
	require.Equal(t, (*bool)(nil), opts.SkipIfEmpty)

}
コード例 #10
0
func TestRemoveEnvironmentRedundantFields(t *testing.T) {
	t.Log("Trivial remove - all fields should be default value")
	{
		env := envmanModels.EnvironmentItemModel{
			"TEST_KEY": "test_value",
			envmanModels.OptionsKey: envmanModels.EnvironmentItemOptionsModel{
				Title:             pointers.NewStringPtr(""),
				Description:       pointers.NewStringPtr(""),
				Summary:           pointers.NewStringPtr(""),
				ValueOptions:      []string{},
				IsRequired:        pointers.NewBoolPtr(envmanModels.DefaultIsRequired),
				IsExpand:          pointers.NewBoolPtr(envmanModels.DefaultIsExpand),
				IsDontChangeValue: pointers.NewBoolPtr(envmanModels.DefaultIsDontChangeValue),
				IsTemplate:        pointers.NewBoolPtr(envmanModels.DefaultIsTemplate),
			},
		}
		require.NoError(t, removeEnvironmentRedundantFields(&env))

		options, err := env.GetOptions()
		require.NoError(t, err)

		require.Equal(t, (*string)(nil), options.Title)
		require.Equal(t, (*string)(nil), options.Description)
		require.Equal(t, (*string)(nil), options.Summary)
		require.Equal(t, 0, len(options.ValueOptions))
		require.Equal(t, (*bool)(nil), options.IsRequired)
		require.Equal(t, (*bool)(nil), options.IsExpand)
		require.Equal(t, (*bool)(nil), options.IsDontChangeValue)
		require.Equal(t, (*bool)(nil), options.IsTemplate)
	}

	t.Log("Trivial don't remove - no fields should be default value")
	{
		env := envmanModels.EnvironmentItemModel{
			"TEST_KEY": "test_value",
			envmanModels.OptionsKey: envmanModels.EnvironmentItemOptionsModel{
				Title:             pointers.NewStringPtr("t"),
				Description:       pointers.NewStringPtr("d"),
				Summary:           pointers.NewStringPtr("s"),
				ValueOptions:      []string{"i"},
				IsRequired:        pointers.NewBoolPtr(true),
				IsExpand:          pointers.NewBoolPtr(false),
				IsDontChangeValue: pointers.NewBoolPtr(true),
				IsTemplate:        pointers.NewBoolPtr(true),
			},
		}
		require.NoError(t, removeEnvironmentRedundantFields(&env))

		options, err := env.GetOptions()
		require.NoError(t, err)

		require.Equal(t, "t", *options.Title)
		require.Equal(t, "d", *options.Description)
		require.Equal(t, "s", *options.Summary)
		require.Equal(t, "i", options.ValueOptions[0])
		require.Equal(t, true, *options.IsRequired)
		require.Equal(t, false, *options.IsExpand)
		require.Equal(t, true, *options.IsDontChangeValue)
		require.Equal(t, true, *options.IsTemplate)
	}

	t.Log("No options - opts field shouldn't exist")
	{
		env := envmanModels.EnvironmentItemModel{
			"TEST_KEY": "test_value",
		}
		require.NoError(t, removeEnvironmentRedundantFields(&env))

		_, ok := env[envmanModels.OptionsKey]
		require.Equal(t, false, ok)
	}
}
コード例 #11
0
func TestMergeEnvironmentWith(t *testing.T) {
	// Different keys
	diffEnv := envmanModels.EnvironmentItemModel{
		testKey: testValue,
		envmanModels.OptionsKey: envmanModels.EnvironmentItemOptionsModel{
			Title:             pointers.NewStringPtr(testTitle),
			Description:       pointers.NewStringPtr(testDescription),
			Summary:           pointers.NewStringPtr(testSummary),
			ValueOptions:      testValueOptions,
			IsRequired:        pointers.NewBoolPtr(testTrue),
			IsExpand:          pointers.NewBoolPtr(testFalse),
			IsDontChangeValue: pointers.NewBoolPtr(testTrue),
		},
	}
	env := envmanModels.EnvironmentItemModel{
		testKey1: testValue,
	}

	err := MergeEnvironmentWith(&env, diffEnv)
	if err == nil {
		t.Fatal("Different keys, should case of error")
	}

	// Normal merge
	env = envmanModels.EnvironmentItemModel{
		testKey:                 testValue,
		envmanModels.OptionsKey: envmanModels.EnvironmentItemOptionsModel{},
	}

	err = MergeEnvironmentWith(&env, diffEnv)
	if err != nil {
		t.Fatal(err)
	}

	options, err := env.GetOptions()
	if err != nil {
		t.Fatal(err)
	}

	diffOptions, err := diffEnv.GetOptions()
	if err != nil {
		t.Fatal(err)
	}

	if *options.Title != *diffOptions.Title {
		t.Fatal("Failed to merge Title")
	}
	if *options.Description != *diffOptions.Description {
		t.Fatal("Failed to merge Description")
	}
	if *options.Summary != *diffOptions.Summary {
		t.Fatal("Failed to merge Summary")
	}
	if len(options.ValueOptions) != len(diffOptions.ValueOptions) {
		t.Fatal("Failed to merge ValueOptions")
	}
	if *options.IsRequired != *diffOptions.IsRequired {
		t.Fatal("Failed to merge IsRequired")
	}
	if *options.IsExpand != *diffOptions.IsExpand {
		t.Fatal("Failed to merge IsExpand")
	}
	if *options.IsDontChangeValue != *diffOptions.IsDontChangeValue {
		t.Fatal("Failed to merge IsDontChangeValue")
	}
}
コード例 #12
0
func TestRemoveEnvironmentRedundantFields(t *testing.T) {
	// Trivial remove - all fields should be default value
	env := envmanModels.EnvironmentItemModel{
		"TEST_KEY": "test_value",
		envmanModels.OptionsKey: envmanModels.EnvironmentItemOptionsModel{
			Title:             pointers.NewStringPtr(""),
			Description:       pointers.NewStringPtr(""),
			Summary:           pointers.NewStringPtr(""),
			ValueOptions:      []string{},
			IsRequired:        pointers.NewBoolPtr(envmanModels.DefaultIsRequired),
			IsExpand:          pointers.NewBoolPtr(envmanModels.DefaultIsExpand),
			IsDontChangeValue: pointers.NewBoolPtr(envmanModels.DefaultIsDontChangeValue),
		},
	}

	if err := removeEnvironmentRedundantFields(&env); err != nil {
		t.Fatal("Failed to remove redundant fields:", err)
	}

	options, err := env.GetOptions()
	if err != nil {
		t.Fatal("Failed to get env options:", err)
	}

	if options.Title != nil {
		t.Fatal("options.Title should be nil")
	}
	if options.Description != nil {
		t.Fatal("options.Description should be nil")
	}
	if options.Summary != nil {
		t.Fatal("options.Summary should be nil")
	}
	if len(options.ValueOptions) != 0 {
		t.Fatal("options.ValueOptions should be empty")
	}
	if options.IsRequired != nil {
		t.Fatal("options.IsRequired should be nil")
	}
	if options.IsExpand != nil {
		t.Fatal("options.IsExpand should be nil")
	}
	if options.IsDontChangeValue != nil {
		t.Fatal("options.IsDontChangeValue should be nil")
	}

	// Trivial don't remove - no fields should be default value
	env = envmanModels.EnvironmentItemModel{
		"TEST_KEY": "test_value",
		envmanModels.OptionsKey: envmanModels.EnvironmentItemOptionsModel{
			Title:             pointers.NewStringPtr("t"),
			Description:       pointers.NewStringPtr("d"),
			Summary:           pointers.NewStringPtr("s"),
			ValueOptions:      []string{"i"},
			IsRequired:        pointers.NewBoolPtr(true),
			IsExpand:          pointers.NewBoolPtr(false),
			IsDontChangeValue: pointers.NewBoolPtr(true),
		},
	}

	if err := removeEnvironmentRedundantFields(&env); err != nil {
		t.Fatal("Failed to remove redundant fields:", err)
	}

	options, err = env.GetOptions()
	if err != nil {
		t.Fatal("Failed to get env options:", err)
	}

	if *options.Title != "t" {
		t.Fatal("options.Title should be: t")
	}
	if *options.Description != "d" {
		t.Fatal("options.Description should be: d")
	}
	if *options.Summary != "s" {
		t.Fatal("options.Summary should be: s")
	}
	if options.ValueOptions[0] != "i" {
		t.Fatal("options.ValueOptions should be: {i}")
	}
	if *options.IsRequired != true {
		t.Fatal("options.IsRequired should be: false")
	}
	if *options.IsExpand != false {
		t.Fatal("options.IsExpand should be: false")
	}
	if *options.IsDontChangeValue != true {
		t.Fatal("options.IsDontChangeValue should be: true")
	}

	// No options - opts field shouldn't exist
	env = envmanModels.EnvironmentItemModel{
		"TEST_KEY": "test_value",
	}

	if err := removeEnvironmentRedundantFields(&env); err != nil {
		t.Fatal("Failed to remove redundant fields:", err)
	}

	_, ok := env[envmanModels.OptionsKey]
	if ok {
		t.Fatal("opts field shouldn't exist")
	}
}