@@ -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,106 @@ 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 .
config_writer ().
set_value (
"user" ,
"email" ,
"[email protected] " ).
release ()
328+ local_repo .git .commit ("-m" , "local commit" )
329+
330+ # origin commit
331+ with Path (f"{ origin_repo .working_dir } /origin.md" ).open ("w" ) as readme :
332+ readme .write ("origin file" )
333+ origin_repo .git .add ("--all" )
334+ origin_repo .
config_writer ().
set_value (
"user" ,
"email" ,
"[email protected] " ).
release ()
335+ origin_repo .git .commit ("-m" , "origin commit" )
336+
337+ # pull and rebase from remote
338+ logging_mock .reset_mock ()
339+
340+ testee .pull_rebase ()
341+
342+ logging_mock .info .assert_called_once_with ("Pull and rebase: %s" , "master" )
343+
344+ # then push should work
345+ testee .push ()
346+
347+ commits = list (self .__origin .iter_commits ("master" ))
348+ self .assertEqual (3 , len (commits ))
349+ self .assertEqual ("initial commit\n " , commits [2 ].message )
350+ self .assertEqual ("origin commit\n " , commits [1 ].message )
351+ self .assertEqual ("local commit\n " , commits [0 ].message )
352+
353+ @patch ("gitopscli.git_api.git_repo.logging" )
354+ def test_pull_rebase_remote_branch_single_commit (self , logging_mock ):
355+ origin_repo = self .__origin
356+ origin_repo .git .checkout ("xyz" )
357+ with GitRepo (self .__mock_repo_api ) as testee :
358+ testee .clone (branch = "xyz" )
359+
360+ # local commit
361+ with Path (testee .get_full_file_path ("local.md" )).open ("w" ) as outfile :
362+ outfile .write ("local file" )
363+ local_repo = Repo (testee .get_full_file_path ("." ))
364+ local_repo .git .add ("--all" )
365+ local_repo .
config_writer ().
set_value (
"user" ,
"email" ,
"[email protected] " ).
release ()
366+ local_repo .git .commit ("-m" , "local branch commit" )
367+
368+ # origin commit
369+ with Path (f"{ origin_repo .working_dir } /origin.md" ).open ("w" ) as readme :
370+ readme .write ("origin file" )
371+ origin_repo .git .add ("--all" )
372+ origin_repo .
config_writer ().
set_value (
"user" ,
"email" ,
"[email protected] " ).
release ()
373+ origin_repo .git .commit ("-m" , "origin branch commit" )
374+
375+ # pull and rebase from remote
376+ logging_mock .reset_mock ()
377+
378+ testee .pull_rebase ()
379+
380+ logging_mock .info .assert_called_once_with ("Pull and rebase: %s" , "xyz" )
381+
382+ # then push should work
383+ testee .push ()
384+
385+ commits = list (self .__origin .iter_commits ("xyz" ))
386+ self .assertEqual (4 , len (commits ))
387+ self .assertEqual ("local branch commit\n " , commits [0 ].message )
388+ self .assertEqual ("origin branch commit\n " , commits [1 ].message )
389+ self .assertEqual ("initial xyz branch commit\n " , commits [2 ].message )
390+
391+ @patch ("gitopscli.git_api.git_repo.logging" )
392+ def test_pull_rebase_without_new_commits (self , logging_mock ):
393+ with GitRepo (self .__mock_repo_api ) as testee :
394+ testee .clone ()
395+
396+ # pull and rebase from remote
397+ logging_mock .reset_mock ()
398+
399+ testee .pull_rebase ()
400+
401+ logging_mock .info .assert_called_once_with ("Pull and rebase: %s" , "master" )
402+
403+ @patch ("gitopscli.git_api.git_repo.logging" )
404+ def test_pull_rebase_if_no_remote_branch_is_noop (self , logging_mock ):
405+ with GitRepo (self .__mock_repo_api ) as testee :
406+ testee .clone ()
407+ testee .new_branch ("new-branch-only-local" )
408+
409+ # pull and rebase from remote
410+ logging_mock .reset_mock ()
411+
412+ testee .pull_rebase ()
413+
414+ logging_mock .assert_not_called ()
415+
316416 @patch ("gitopscli.git_api.git_repo.logging" )
317417 def test_push (self , logging_mock ):
318418 with GitRepo (self .__mock_repo_api ) as testee :
0 commit comments