Skip to content

Commit 1723b86

Browse files
author
Chris McDonnell
committed
Make commit prefixes migration only return true if it enters if statement
1 parent b2fd612 commit 1723b86

File tree

2 files changed

+50
-21
lines changed

2 files changed

+50
-21
lines changed

pkg/config/app_config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ func changeElementToSequence(changedContent []byte, path []string) ([]byte, erro
306306

307307
func changeCommitPrefixesMap(changedContent []byte) ([]byte, error) {
308308
return yaml_utils.TransformNode(changedContent, []string{"git", "commitPrefixes"}, func(prefixesNode *yaml.Node) (bool, error) {
309+
changedAnyNodes := false
309310
if prefixesNode.Kind == yaml.MappingNode {
310311
for _, contentNode := range prefixesNode.Content {
311312
if contentNode.Kind == yaml.MappingNode {
@@ -317,12 +318,11 @@ func changeCommitPrefixesMap(changedContent []byte) ([]byte, error) {
317318
Kind: yaml.MappingNode,
318319
Content: nodeContentCopy,
319320
}}
320-
321+
changedAnyNodes = true
321322
}
322323
}
323-
return true, nil
324324
}
325-
return false, nil
325+
return changedAnyNodes, nil
326326
})
327327
}
328328

pkg/config/app_config_test.go

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,30 @@ import (
99

1010
func TestCommitPrefixMigrations(t *testing.T) {
1111
scenarios := []struct {
12-
name string
13-
input string
14-
expected string
12+
name string
13+
input string
14+
expected string
15+
assertAsString bool
1516
}{
1617
{
17-
"Empty String",
18-
"",
19-
"",
18+
name: "Empty String",
19+
input: "",
20+
expected: "",
2021
}, {
21-
"Single CommitPrefix Rename",
22-
`
22+
name: "Single CommitPrefix Rename",
23+
input: `
2324
git:
2425
commitPrefix:
2526
pattern: "^\\w+-\\w+.*"
2627
replace: '[JIRA $0] '`,
27-
`
28+
expected: `
2829
git:
2930
commitPrefix:
3031
- pattern: "^\\w+-\\w+.*"
3132
replace: '[JIRA $0] '`,
3233
}, {
33-
"Complicated CommitPrefixes Rename",
34-
`
34+
name: "Complicated CommitPrefixes Rename",
35+
input: `
3536
git:
3637
commitPrefixes:
3738
foo:
@@ -40,7 +41,7 @@ git:
4041
CrazyName!@#$^*&)_-)[[}{f{[]:
4142
pattern: "^foo.bar*"
4243
replace: '[FUN $0] '`,
43-
`
44+
expected: `
4445
git:
4546
commitPrefixes:
4647
foo:
@@ -50,9 +51,30 @@ git:
5051
- pattern: "^foo.bar*"
5152
replace: '[FUN $0] '`,
5253
}, {
53-
"Incomplete Configuration",
54-
"git:",
55-
"git:",
54+
name: "Incomplete Configuration",
55+
input: "git:",
56+
expected: "git:",
57+
}, {
58+
name: "No changes made when already migrated",
59+
input: `
60+
git:
61+
commitPrefix:
62+
- pattern: "Hello World"
63+
replace: "Goodbye"
64+
commitPrefixes:
65+
foo:
66+
- pattern: "^\\w+-\\w+.*"
67+
replace: '[JIRA $0] '`,
68+
expected: `
69+
git:
70+
commitPrefix:
71+
- pattern: "Hello World"
72+
replace: "Goodbye"
73+
commitPrefixes:
74+
foo:
75+
- pattern: "^\\w+-\\w+.*"
76+
replace: '[JIRA $0] '`,
77+
assertAsString: true,
5678
},
5779
}
5880

@@ -63,16 +85,23 @@ git:
6385
if err != nil {
6486
t.Error(err)
6587
}
66-
actual, err := computeMigratedConfig("path doesn't matter", []byte(s.input))
88+
actualBytes, err := computeMigratedConfig("path doesn't matter", []byte(s.input))
6789
if err != nil {
6890
t.Error(err)
6991
}
7092
actualConfig := GetDefaultConfig()
71-
err = yaml.Unmarshal(actual, actualConfig)
93+
err = yaml.Unmarshal(actualBytes, actualConfig)
7294
if err != nil {
7395
t.Error(err)
7496
}
75-
assert.Equal(t, expectedConfig, actualConfig)
97+
98+
// In most cases, yaml assertion is more robust,
99+
// but in some we are checking for differences in whitespace
100+
if s.assertAsString {
101+
assert.Equal(t, s.expected, string(actualBytes))
102+
} else {
103+
assert.Equal(t, expectedConfig, actualConfig)
104+
}
76105
})
77106
}
78107
}

0 commit comments

Comments
 (0)