@@ -98,9 +98,8 @@ def test_checkout_invalid_branch(xtl_clone, git2cpp_path, tmp_path):
9898 assert "error: could not resolve pathspec 'nonexistent'" in p_checkout .stderr
9999
100100
101- # @pytest.mark.skip(reason="Waiting for print_tobecommited and print_tracking_info implementations")
102101def test_checkout_with_unstaged_changes (xtl_clone , git2cpp_path , tmp_path ):
103- """Test that checkout warns about unstaged changes"""
102+ """Test that checkout shows unstaged changes when switching branches """
104103 assert (tmp_path / "xtl" ).exists ()
105104 xtl_path = tmp_path / "xtl"
106105
@@ -113,52 +112,82 @@ def test_checkout_with_unstaged_changes(xtl_clone, git2cpp_path, tmp_path):
113112 readme_path = xtl_path / "README.md"
114113 readme_path .write_text ("Modified content" )
115114
116- # Try to checkout - should warn about unstaged changes
115+ # Checkout - should succeed and show the modified file status
117116 checkout_cmd = [git2cpp_path , "checkout" , "newbranch" ]
118117 p_checkout = subprocess .run (
119118 checkout_cmd , capture_output = True , cwd = xtl_path , text = True
120119 )
121- print (p_checkout .stdout )
122- # Should show warning message (once implemented)
123- # assert (
124- # "Your local changes to the following files would be overwritten by checkout:"
125- # in p_checkout.stdout
126- # )
127- # assert "README.md" in p_checkout.stdout
128- # assert (
129- # "Please commit your changes or stash them before you switch branches"
130- # in p_checkout.stdout
131- # )
132-
133- # Verify we stayed on master branch
134- branch_cmd = [git2cpp_path , "branch" ]
135- p_branch = subprocess .run (branch_cmd , capture_output = True , cwd = xtl_path , text = True )
136- assert p_branch .returncode == 0
137- # print(p_branch.stdout)
138- # assert "* master" in p_branch.stdout
139-
140-
141- # def test_checkout_force_with_unstaged_changes(xtl_clone, git2cpp_path, tmp_path):
142- # """Test that checkout -f forces checkout even with unstaged changes"""
143- # assert (tmp_path / "xtl").exists()
144- # xtl_path = tmp_path / "xtl"
145-
146- # # Create a new branch
147- # create_cmd = [git2cpp_path, 'branch', 'forcebranch']
148- # p_create = subprocess.run(create_cmd, capture_output=True, cwd=xtl_path, text=True)
149- # assert p_create.returncode == 0
150-
151- # # Modify a file (unstaged change)
152- # readme_path = xtl_path / "README.md"
153- # readme_path.write_text("Modified content that will be lost")
154-
155- # # Force checkout - should succeed and discard changes
156- # checkout_cmd = [git2cpp_path, 'checkout', '-f', 'forcebranch']
157- # p_checkout = subprocess.run(checkout_cmd, capture_output=True, cwd=xtl_path, text=True)
158- # assert p_checkout.returncode == 0
159- # assert "Switched to branch 'forcebranch'" in p_checkout.stdout
160-
161- # # Verify we're on the branch
162- # branch_cmd = [git2cpp_path, 'branch']
163- # p_branch = subprocess.run(branch_cmd, capture_output=True, cwd=xtl_path, text=True)
164- # assert '* forcebranch' in p_branch.stdout
120+
121+ # Should succeed and show status
122+ assert p_checkout .returncode == 0
123+ assert " M README.md" in p_checkout .stdout
124+ assert "Switched to branch 'newbranch'" in p_checkout .stdout
125+
126+
127+ @pytest .mark .parametrize ("force_flag" , ["" , "-f" , "--force" ])
128+ def test_checkout_refuses_overwrite (xtl_clone , git2cpp_path , tmp_path , force_flag ):
129+ """Test that checkout refuses to switch when local changes would be overwritten, and switches when using --force"""
130+ assert (tmp_path / "xtl" ).exists ()
131+ xtl_path = tmp_path / "xtl"
132+
133+ # Create a new branch and switch to it
134+ create_cmd = [git2cpp_path , "checkout" , "-b" , "newbranch" ]
135+ p_create = subprocess .run (create_cmd , capture_output = True , cwd = xtl_path , text = True )
136+ assert p_create .returncode == 0
137+
138+ # Modify README.md and commit it on newbranch
139+ readme_path = xtl_path / "README.md"
140+ readme_path .write_text ("Content on newbranch" )
141+
142+ add_cmd = [git2cpp_path , "add" , "README.md" ]
143+ subprocess .run (add_cmd , cwd = xtl_path , text = True )
144+
145+ commit_cmd = [git2cpp_path , "commit" , "-m" , "Change on newbranch" ]
146+ subprocess .run (commit_cmd , cwd = xtl_path , text = True )
147+
148+ # Switch back to master
149+ checkout_master_cmd = [git2cpp_path , "checkout" , "master" ]
150+ p_master = subprocess .run (
151+ checkout_master_cmd , capture_output = True , cwd = xtl_path , text = True
152+ )
153+ assert p_master .returncode == 0
154+
155+ # Now modify README.md locally (unstaged) on master
156+ readme_path .write_text ("Local modification on master" )
157+
158+ # Try to checkout newbranch
159+ checkout_cmd = [git2cpp_path , "checkout" ]
160+ if force_flag != "" :
161+ checkout_cmd .append (force_flag )
162+ checkout_cmd .append ("newbranch" )
163+ p_checkout = subprocess .run (
164+ checkout_cmd , capture_output = True , cwd = xtl_path , text = True
165+ )
166+
167+ if force_flag == "" :
168+ assert p_checkout .returncode == 0
169+ assert (
170+ "Your local changes to the following files would be overwritten by checkout:"
171+ in p_checkout .stdout
172+ )
173+ assert "README.md" in p_checkout .stdout
174+ assert (
175+ "Please commit your changes or stash them before you switch branches"
176+ in p_checkout .stdout
177+ )
178+
179+ # Verify we're still on master (didn't switch)
180+ branch_cmd = [git2cpp_path , "branch" ]
181+ p_branch = subprocess .run (
182+ branch_cmd , capture_output = True , cwd = xtl_path , text = True
183+ )
184+ assert "* master" in p_branch .stdout
185+ else :
186+ assert "Switched to branch 'newbranch'" in p_checkout .stdout
187+
188+ # Verify we switched to newbranch
189+ branch_cmd = [git2cpp_path , "branch" ]
190+ p_branch = subprocess .run (
191+ branch_cmd , capture_output = True , cwd = xtl_path , text = True
192+ )
193+ assert "* newbranch" in p_branch .stdout
0 commit comments