@@ -49,7 +49,7 @@ def __create_origin(self):
4949 with Path (f"{ repo_dir } /README.md" ).open ("w" ) as readme :
5050 readme .write ("xyz branch readme" )
5151 repo .git .add ("--all" )
52- repo .git .commit ("-m" , "xyz brach commit" , "--author" , f"{ git_user } <{ git_email } >" )
52+ repo .git .commit ("-m" , "initial xyz branch commit" , "--author" , f"{ git_user } <{ git_email } >" )
5353
5454 repo .git .checkout ("master" ) # master = default branch
5555 repo .git .config ("receive.denyCurrentBranch" , "ignore" )
@@ -313,6 +313,102 @@ def test_commit_nothing_to_commit(self, logging_mock):
313313 self .assertEqual ("initial commit\n " , commits [0 ].message )
314314 logging_mock .assert_not_called ()
315315
316+ @patch ("gitopscli.git_api.git_repo.logging" )
317+ def test_pull_rebase_master_single_commit (self , logging_mock ):
318+ origin_repo = self .__origin
319+ with GitRepo (self .__mock_repo_api ) as testee :
320+ testee .clone ()
321+
322+ # local commit
323+ with Path (testee .get_full_file_path ("local.md" )).open ("w" ) as outfile :
324+ outfile .write ("local file" )
325+ local_repo = Repo (testee .get_full_file_path ("." ))
326+ local_repo .git .add ("--all" )
327+ local_repo .
git .
commit (
"-m" ,
"local commit" ,
"--author" ,
"local <[email protected] >" )
328+
329+ # origin commit
330+ with Path (f"{ origin_repo .working_dir } /origin.md" ).open ("w" ) as readme :
331+ readme .write ("origin file" )
332+ origin_repo .git .add ("--all" )
333+ origin_repo .
git .
commit (
"-m" ,
"origin commit" ,
"--author" ,
"origin <[email protected] >" )
334+
335+ # pull and rebase from remote
336+ logging_mock .reset_mock ()
337+
338+ testee .pull_rebase ()
339+
340+ logging_mock .info .assert_called_once_with ("Pull and rebase: %s" , "master" )
341+
342+ # then push should work
343+ testee .push ()
344+
345+ commits = list (self .__origin .iter_commits ("master" ))
346+ self .assertEqual (3 , len (commits ))
347+ self .assertEqual ("initial commit\n " , commits [2 ].message )
348+ self .assertEqual ("origin commit\n " , commits [1 ].message )
349+ self .assertEqual ("local commit\n " , commits [0 ].message )
350+
351+ @patch ("gitopscli.git_api.git_repo.logging" )
352+ def test_pull_rebase_remote_branch_single_commit (self , logging_mock ):
353+ origin_repo = self .__origin
354+ origin_repo .git .checkout ("xyz" )
355+ with GitRepo (self .__mock_repo_api ) as testee :
356+ testee .clone (branch = "xyz" )
357+
358+ # local commit
359+ with Path (testee .get_full_file_path ("local.md" )).open ("w" ) as outfile :
360+ outfile .write ("local file" )
361+ local_repo = Repo (testee .get_full_file_path ("." ))
362+ local_repo .git .add ("--all" )
363+ local_repo .
git .
commit (
"-m" ,
"local branch commit" ,
"--author" ,
"local <[email protected] >" )
364+
365+ # origin commit
366+ with Path (f"{ origin_repo .working_dir } /origin.md" ).open ("w" ) as readme :
367+ readme .write ("origin file" )
368+ origin_repo .git .add ("--all" )
369+ origin_repo .
git .
commit (
"-m" ,
"origin branch commit" ,
"--author" ,
"origin <[email protected] >" )
370+
371+ # pull and rebase from remote
372+ logging_mock .reset_mock ()
373+
374+ testee .pull_rebase ()
375+
376+ logging_mock .info .assert_called_once_with ("Pull and rebase: %s" , "xyz" )
377+
378+ # then push should work
379+ testee .push ()
380+
381+ commits = list (self .__origin .iter_commits ("xyz" ))
382+ self .assertEqual (4 , len (commits ))
383+ self .assertEqual ("local branch commit\n " , commits [0 ].message )
384+ self .assertEqual ("origin branch commit\n " , commits [1 ].message )
385+ self .assertEqual ("initial xyz branch commit\n " , commits [2 ].message )
386+
387+ @patch ("gitopscli.git_api.git_repo.logging" )
388+ def test_pull_rebase_without_new_commits (self , logging_mock ):
389+ with GitRepo (self .__mock_repo_api ) as testee :
390+ testee .clone ()
391+
392+ # pull and rebase from remote
393+ logging_mock .reset_mock ()
394+
395+ testee .pull_rebase ()
396+
397+ logging_mock .info .assert_called_once_with ("Pull and rebase: %s" , "master" )
398+
399+ @patch ("gitopscli.git_api.git_repo.logging" )
400+ def test_pull_rebase_if_no_remote_branch_is_noop (self , logging_mock ):
401+ with GitRepo (self .__mock_repo_api ) as testee :
402+ testee .clone ()
403+ testee .new_branch ("new-branch-only-local" )
404+
405+ # pull and rebase from remote
406+ logging_mock .reset_mock ()
407+
408+ testee .pull_rebase ()
409+
410+ logging_mock .assert_not_called ()
411+
316412 @patch ("gitopscli.git_api.git_repo.logging" )
317413 def test_push (self , logging_mock ):
318414 with GitRepo (self .__mock_repo_api ) as testee :
0 commit comments