@@ -24,6 +24,7 @@ import (
24
24
"os/exec"
25
25
"path/filepath"
26
26
"runtime"
27
+ "strings"
27
28
28
29
. "github.com/onsi/ginkgo/v2"
29
30
. "github.com/onsi/gomega"
@@ -105,6 +106,51 @@ var _ = Describe("kubebuilder", func() {
105
106
By ("validating custom code preservation" )
106
107
validateCustomCodePreservation (mockProjectDir )
107
108
})
109
+
110
+ It ("should abort on merge conflicts when --force is not used" , func () {
111
+ By ("creating mock project with kubebuilder v4.5.2" )
112
+ createMockProject (mockProjectDir , binFromVersionPath )
113
+
114
+ By ("injecting conflicting code that will cause merge conflicts" )
115
+ injectConflictingCode (mockProjectDir )
116
+
117
+ By ("initializing git repository and committing mock project" )
118
+ initializeGitRepo (mockProjectDir )
119
+
120
+ By ("running alpha update without --force flag" )
121
+ cmd := exec .Command (kbc .BinaryName , "alpha" , "update" ,
122
+ "--from-version" , fromVersion , "--to-version" , toVersion , "--from-branch" , "main" )
123
+ cmd .Dir = mockProjectDir
124
+ output , err := cmd .CombinedOutput ()
125
+
126
+ By ("expecting the command to fail due to merge conflicts" )
127
+ Expect (err ).To (HaveOccurred ())
128
+ Expect (string (output )).To (ContainSubstring ("Merge conflicts detected" ))
129
+ Expect (string (output )).To (ContainSubstring ("merge aborted due to conflicts" ))
130
+ })
131
+
132
+ It ("should commit with conflict markers when --force is used" , func () {
133
+ By ("creating mock project with kubebuilder v4.5.2" )
134
+ createMockProject (mockProjectDir , binFromVersionPath )
135
+
136
+ By ("injecting conflicting code that will cause merge conflicts" )
137
+ injectConflictingCode (mockProjectDir )
138
+
139
+ By ("initializing git repository and committing mock project" )
140
+ initializeGitRepo (mockProjectDir )
141
+
142
+ By ("running alpha update with --force flag" )
143
+ cmd := exec .Command (kbc .BinaryName , "alpha" , "update" ,
144
+ "--from-version" , fromVersion , "--to-version" , toVersion , "--from-branch" , "main" , "--force" )
145
+ cmd .Dir = mockProjectDir
146
+ output , err := cmd .CombinedOutput ()
147
+
148
+ By ("expecting the command to succeed despite conflicts" )
149
+ Expect (err ).NotTo (HaveOccurred (), fmt .Sprintf ("Alpha update with --force failed: %s" , string (output )))
150
+
151
+ By ("validating that conflict markers are present in the merged files" )
152
+ validateConflictMarkers (mockProjectDir )
153
+ })
108
154
})
109
155
})
110
156
@@ -283,3 +329,27 @@ func validateCustomCodePreservation(projectDir string) {
283
329
Expect (string (content )).To (ContainSubstring ("log.Info(\" Reconciling TestOperator\" )" ))
284
330
Expect (string (content )).To (ContainSubstring ("log.Info(\" TestOperator size\" , \" size\" , testOperator.Spec.Size)" ))
285
331
}
332
+
333
+ func injectConflictingCode (projectDir string ) {
334
+ // Modify Makefile to create conflicts
335
+ makefilePath := filepath .Join (projectDir , "Makefile" )
336
+ content , err := os .ReadFile (makefilePath )
337
+ Expect (err ).NotTo (HaveOccurred ())
338
+
339
+ conflictingContent := string (content ) + "\n # Conflicting content\n conflict-target:\n \t @echo conflict\n "
340
+ err = os .WriteFile (makefilePath , []byte (conflictingContent ), 0644 )
341
+ Expect (err ).NotTo (HaveOccurred ())
342
+ }
343
+
344
+ func validateConflictMarkers (projectDir string ) {
345
+ makefilePath := filepath .Join (projectDir , "Makefile" )
346
+ content , err := os .ReadFile (makefilePath )
347
+ Expect (err ).NotTo (HaveOccurred ())
348
+
349
+ contentStr := string (content )
350
+ hasMarkers := strings .Contains (contentStr , "<<<<<<<" ) ||
351
+ strings .Contains (contentStr , "=======" ) ||
352
+ strings .Contains (contentStr , ">>>>>>>" )
353
+
354
+ Expect (hasMarkers ).To (BeTrue (), "Expected conflict markers in merged files" )
355
+ }
0 commit comments