Skip to content

Don't rewrite config file unnecessarily when it contains commitPrefixes #4311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/config/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ func changeElementToSequence(changedContent []byte, path []string) ([]byte, erro

func changeCommitPrefixesMap(changedContent []byte) ([]byte, error) {
return yaml_utils.TransformNode(changedContent, []string{"git", "commitPrefixes"}, func(prefixesNode *yaml.Node) (bool, error) {
changedAnyNodes := false
if prefixesNode.Kind == yaml.MappingNode {
for _, contentNode := range prefixesNode.Content {
if contentNode.Kind == yaml.MappingNode {
Expand All @@ -317,12 +318,11 @@ func changeCommitPrefixesMap(changedContent []byte) ([]byte, error) {
Kind: yaml.MappingNode,
Content: nodeContentCopy,
}}

changedAnyNodes = true
}
}
return true, nil
}
return false, nil
return changedAnyNodes, nil
})
}

Expand Down
85 changes: 48 additions & 37 deletions pkg/config/app_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)

func TestCommitPrefixMigrations(t *testing.T) {
Expand All @@ -14,65 +13,77 @@ func TestCommitPrefixMigrations(t *testing.T) {
expected string
}{
{
"Empty String",
"",
"",
name: "Empty String",
input: "",
expected: "",
}, {
"Single CommitPrefix Rename",
`
git:
name: "Single CommitPrefix Rename",
input: `git:
commitPrefix:
pattern: "^\\w+-\\w+.*"
replace: '[JIRA $0] '`,
`
git:
replace: '[JIRA $0] '
`,
expected: `git:
commitPrefix:
- pattern: "^\\w+-\\w+.*"
replace: '[JIRA $0] '`,
replace: '[JIRA $0] '
`,
}, {
"Complicated CommitPrefixes Rename",
`
git:
name: "Complicated CommitPrefixes Rename",
input: `git:
commitPrefixes:
foo:
pattern: "^\\w+-\\w+.*"
replace: '[OTHER $0] '
CrazyName!@#$^*&)_-)[[}{f{[]:
pattern: "^foo.bar*"
replace: '[FUN $0] '`,
`
git:
replace: '[FUN $0] '
`,
expected: `git:
commitPrefixes:
foo:
- pattern: "^\\w+-\\w+.*"
replace: '[OTHER $0] '
CrazyName!@#$^*&)_-)[[}{f{[]:
- pattern: "^foo.bar*"
replace: '[FUN $0] '`,
foo:
- pattern: "^\\w+-\\w+.*"
replace: '[OTHER $0] '
CrazyName!@#$^*&)_-)[[}{f{[]:
- pattern: "^foo.bar*"
replace: '[FUN $0] '
`,
}, {
name: "Incomplete Configuration",
input: "git:",
expected: "git:",
}, {
"Incomplete Configuration",
"git:",
"git:",
// This test intentionally uses non-standard indentation to test that the migration
// does not change the input.
name: "No changes made when already migrated",
input: `
git:
commitPrefix:
- pattern: "Hello World"
replace: "Goodbye"
commitPrefixes:
foo:
- pattern: "^\\w+-\\w+.*"
replace: '[JIRA $0] '`,
expected: `
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is relying on the knowledge that our yaml rewriting will change the indentation to 4. If we change that as proposed in #4312, this test will no longer catch the error but succeed either way.

I think I would use some other indentation (e.g. 3 at top level, 1 at the second level) to make this more robust.

git:
commitPrefix:
- pattern: "Hello World"
replace: "Goodbye"
commitPrefixes:
foo:
- pattern: "^\\w+-\\w+.*"
replace: '[JIRA $0] '`,
},
}

for _, s := range scenarios {
t.Run(s.name, func(t *testing.T) {
expectedConfig := GetDefaultConfig()
err := yaml.Unmarshal([]byte(s.expected), expectedConfig)
if err != nil {
t.Error(err)
}
actual, err := computeMigratedConfig("path doesn't matter", []byte(s.input))
if err != nil {
t.Error(err)
}
actualConfig := GetDefaultConfig()
err = yaml.Unmarshal(actual, actualConfig)
if err != nil {
t.Error(err)
}
assert.Equal(t, expectedConfig, actualConfig)
assert.Equal(t, s.expected, string(actual))
})
}
}