11import os
22import shutil
33import subprocess
4- import tempfile
54
65import pytest
76
@@ -56,6 +55,59 @@ def test_synchronize():
5655 cleanup_remote_repo (owner , repository , result )
5756
5857
58+ def test_synchronize_to_existing_pr ():
59+ """Test synchronization tool (when PR already exists)."""
60+
61+ # Define your test data here
62+ owner = "ansys"
63+ repository = "ansys-tools-repo-sync"
64+ from_dir = os .path .join (ASSETS_DIRECTORY , "ansys" )
65+ to_dir = "src/ansys"
66+ manifest = os .path .join (ASSETS_DIRECTORY , "manifest.txt" )
67+
68+ # Call the function
69+ result = None
70+ try :
71+ result = synchronize (
72+ owner = owner ,
73+ repository = repository ,
74+ token = TOKEN ,
75+ from_dir = from_dir ,
76+ to_dir = to_dir ,
77+ include_manifest = manifest ,
78+ skip_ci = True ,
79+ )
80+
81+ # Assertions or validations
82+ assert f"https://github.com/ansys/ansys-tools-repo-sync/pull/" in result
83+
84+ # Call the function again - and check that the PR already exists.
85+ result_pr_already_exists = synchronize (
86+ owner = owner ,
87+ repository = repository ,
88+ token = TOKEN ,
89+ from_dir = from_dir ,
90+ to_dir = to_dir ,
91+ include_manifest = manifest ,
92+ skip_ci = True ,
93+ )
94+
95+ # Verify the PR is the same
96+ assert result_pr_already_exists == result
97+
98+ # Check that the proper modified files have been added
99+ list_of_files = ["src/ansys/api/test/v0/hello_world.py" , "src/ansys/api/test/v0/test.proto" ]
100+ assert check_files_in_pr (owner , repository , result , list_of_files )
101+
102+ except Exception as err :
103+ raise err
104+ finally :
105+ if result :
106+ cleanup_remote_repo (owner , repository , result )
107+ if result_pr_already_exists and result != result_pr_already_exists :
108+ cleanup_remote_repo (owner , result_pr_already_exists , result )
109+
110+
59111def test_synchronize_with_only_proto_manifest ():
60112 """Test synchronization tool (with manifest)."""
61113
@@ -94,6 +146,40 @@ def test_synchronize_with_only_proto_manifest():
94146 cleanup_remote_repo (owner , repository , result )
95147
96148
149+ def test_synchronize_no_sync_needed ():
150+ """Test synchronization tool (with manifest referring to non-existing files)."""
151+
152+ # Define your test data here
153+ owner = "ansys"
154+ repository = "ansys-tools-repo-sync"
155+ from_dir = os .path .join (ASSETS_DIRECTORY , "ansys" )
156+ to_dir = "src/ansys"
157+ manifest = os .path .join (ASSETS_DIRECTORY , "manifest_no_files.txt" )
158+
159+ # Call the function
160+ result = None
161+ try :
162+ result = synchronize (
163+ owner = owner ,
164+ repository = repository ,
165+ token = TOKEN ,
166+ from_dir = from_dir ,
167+ to_dir = to_dir ,
168+ include_manifest = manifest ,
169+ skip_ci = True ,
170+ random_branch_name = True ,
171+ )
172+
173+ # Assertions or validations
174+ assert result is None
175+
176+ except Exception as err :
177+ raise err
178+ finally :
179+ if result :
180+ cleanup_remote_repo (owner , repository , result )
181+
182+
97183def test_synchronize_with_cleanup_and_dry_run (capsys ):
98184 """
99185 Test synchronization tool (with --clean-to-dir flag).
@@ -140,14 +226,13 @@ def test_synchronize_with_cleanup_and_dry_run(capsys):
140226
141227
142228@pytest .mark .skipif (SKIP_LOCALLY , reason = "Only runs on workflow" )
143- def test_synchronize_from_cli ():
229+ def test_synchronize_from_cli (tmpdir ):
144230 """Test synchronization tool (without manifest) from CLI."""
145231
146232 # Define a temp directory and copy assets in it
147- temp_dir = tempfile .TemporaryDirectory (prefix = "repo_clone_cli_" )
148233 shutil .copytree (
149234 ASSETS_DIRECTORY ,
150- temp_dir . name ,
235+ tmpdir ,
151236 dirs_exist_ok = True ,
152237 )
153238
@@ -184,7 +269,7 @@ def test_synchronize_from_cli():
184269 ],
185270 stdout = subprocess .PIPE ,
186271 stderr = subprocess .PIPE ,
187- cwd = temp_dir . name ,
272+ cwd = tmpdir ,
188273 )
189274
190275 # Check output info
@@ -204,14 +289,13 @@ def test_synchronize_from_cli():
204289
205290
206291@pytest .mark .skipif (SKIP_LOCALLY , reason = "Only runs on workflow" )
207- def test_synchronize_with_only_proto_manifest_from_cli ():
292+ def test_synchronize_with_only_proto_manifest_from_cli (tmpdir ):
208293 """Test synchronization tool (with manifest) from CLI."""
209294
210295 # Define a temp directory and copy assets in it
211- temp_dir = tempfile .TemporaryDirectory (prefix = "repo_clone_cli_" )
212296 shutil .copytree (
213297 ASSETS_DIRECTORY ,
214- temp_dir . name ,
298+ tmpdir ,
215299 dirs_exist_ok = True ,
216300 )
217301
@@ -248,7 +332,7 @@ def test_synchronize_with_only_proto_manifest_from_cli():
248332 ],
249333 stdout = subprocess .PIPE ,
250334 stderr = subprocess .PIPE ,
251- cwd = temp_dir . name ,
335+ cwd = tmpdir ,
252336 )
253337
254338 # Check output info
@@ -268,7 +352,7 @@ def test_synchronize_with_only_proto_manifest_from_cli():
268352
269353
270354@pytest .mark .skipif (SKIP_LOCALLY , reason = "Only runs on workflow" )
271- def test_synchronize_with_cleanup_cli ():
355+ def test_synchronize_with_cleanup_cli (tmpdir ):
272356 """
273357 Test synchronization tool (with --clean-to-dir flag) from CLI.
274358
@@ -279,10 +363,9 @@ def test_synchronize_with_cleanup_cli():
279363 """
280364
281365 # Define a temp directory and copy assets in it
282- temp_dir = tempfile .TemporaryDirectory (prefix = "repo_clone_cli_" )
283366 shutil .copytree (
284367 ASSETS_DIRECTORY ,
285- temp_dir . name ,
368+ tmpdir ,
286369 dirs_exist_ok = True ,
287370 )
288371
@@ -321,7 +404,7 @@ def test_synchronize_with_cleanup_cli():
321404 ],
322405 stdout = subprocess .PIPE ,
323406 stderr = subprocess .PIPE ,
324- cwd = temp_dir . name ,
407+ cwd = tmpdir ,
325408 )
326409
327410 # Check output info
@@ -338,3 +421,62 @@ def test_synchronize_with_cleanup_cli():
338421 assert "src/ansys/tools/repo_sync/__init__.py" in captured
339422 assert "src/ansys/tools/repo_sync/__main__.py" in captured
340423 assert "src/ansys/tools/repo_sync/repo_sync.py" in captured
424+
425+
426+ @pytest .mark .skipif (SKIP_LOCALLY , reason = "Only runs on workflow" )
427+ def test_synchronize_with_no_sync_cli (tmpdir ):
428+ """Test synchronization tool (with no files needed to be synced) from CLI."""
429+
430+ # Define a temp directory and copy assets in it
431+ shutil .copytree (
432+ ASSETS_DIRECTORY ,
433+ tmpdir ,
434+ dirs_exist_ok = True ,
435+ )
436+
437+ # Requires installing project
438+ subprocess .run (
439+ [
440+ "pip" ,
441+ "install" ,
442+ "." ,
443+ ],
444+ stdout = subprocess .PIPE ,
445+ stderr = subprocess .PIPE ,
446+ cwd = ROOT_PATH ,
447+ )
448+
449+ # Call CLI tool
450+ completed_process = subprocess .run (
451+ [
452+ "repo-sync" ,
453+ "--token" ,
454+ TOKEN ,
455+ "--owner" ,
456+ "ansys" ,
457+ "--repository" ,
458+ "ansys-tools-repo-sync" ,
459+ "--from-dir" ,
460+ "ansys" ,
461+ "--to-dir" ,
462+ "src/ansys" ,
463+ "--include-manifest" ,
464+ "manifest_no_files.txt" ,
465+ "--skip-ci" ,
466+ "--random-branch-name" ,
467+ ],
468+ stdout = subprocess .PIPE ,
469+ stderr = subprocess .PIPE ,
470+ cwd = tmpdir ,
471+ )
472+
473+ # Check output info
474+ print (completed_process .returncode )
475+ print (completed_process .stdout )
476+ print (completed_process .stderr )
477+
478+ # Check stdout
479+ captured = completed_process .stdout .decode ()
480+
481+ # Search for the modified files
482+ assert ">>> No files to sync... Ignoring PR request." in captured
0 commit comments