@@ -32,8 +32,8 @@ def test_create_invalid_branch_name(ddev, name, mocker):
3232@pytest .mark .parametrize (
3333 'yaml_updated' ,
3434 [
35- pytest .param (True , id = 'agent_branch_exists ' ),
36- pytest .param (False , id = 'agent_branch_not_exists ' ),
35+ pytest .param (True , id = 'build_agent_yaml_updated ' ),
36+ pytest .param (False , id = 'build_agent_yaml_unchanged ' ),
3737 ],
3838)
3939def test_create_branch (ddev , mocker , yaml_updated ):
@@ -58,7 +58,7 @@ def test_create_branch(ddev, mocker, yaml_updated):
5858 run_mock .assert_any_call ('checkout' , '-B' , '5.5.x' )
5959 run_mock .assert_any_call ('push' , 'origin' , '5.5.x' )
6060
61- # yaml commit only happens when agent branch exists
61+ # yaml commit only happens when build_agent.yaml was updated
6262 yaml_commit = call ('add' , '.gitlab/build_agent.yaml' )
6363 assert (yaml_commit in run_mock .call_args_list ) is yaml_updated
6464
@@ -78,45 +78,73 @@ def test_create_branch(ddev, mocker, yaml_updated):
7878 assert run_mock .call_args_list [- 1 ] == call ('checkout' , 'master' )
7979
8080
81- @pytest .mark .parametrize (
82- 'ls_remote_output,expected_result,file_should_change' ,
83- [
84- pytest .param ('abc123\t refs/heads/7.99.x\n ' , True , True , id = 'branch_exists' ),
85- pytest .param ('' , False , False , id = 'branch_not_exists' ),
86- ],
87- )
88- def test_ensure_build_agent_yaml_updated (mocker , tmp_path , ls_remote_output , expected_result , file_should_change ):
89- """Test ensure_build_agent_yaml_updated with different branch existence scenarios."""
81+ def test_ensure_build_agent_yaml_updated (mocker , tmp_path ):
9082 build_agent_path = Path (tmp_path / '.gitlab' / 'build_agent.yaml' )
9183 build_agent_path .parent .ensure_dir_exists ()
9284 build_agent_path .write_text ('.build-agent-tpl:\n trigger:\n branch: main\n ' )
9385
9486 app_mock = mocker .MagicMock ()
95- app_mock .repo .git .capture .return_value = ls_remote_output
9687
9788 with Path (tmp_path ).as_cwd ():
9889 result = ensure_build_agent_yaml_updated (app_mock , '7.99.x' )
9990
100- assert result is expected_result
101- content = build_agent_path .read_text ()
102- if file_should_change :
103- assert 'branch: 7.99.x' in content
104- else :
105- assert 'branch: main' in content
91+ assert result is True
92+ assert 'branch: 7.99.x' in build_agent_path .read_text ()
93+ app_mock .repo .git .capture .assert_not_called ()
94+
95+
96+ def test_ensure_build_agent_yaml_updated_ignores_unrelated_main_branch (mocker , tmp_path ):
97+ build_agent_path = Path (tmp_path / '.gitlab' / 'build_agent.yaml' )
98+ build_agent_path .parent .ensure_dir_exists ()
99+ content = '.build-agent-tpl:\n trigger:\n branch: main\n unrelated-job:\n trigger:\n branch: main\n '
100+ build_agent_path .write_text (content )
101+
102+ app_mock = mocker .MagicMock ()
103+
104+ with Path (tmp_path ).as_cwd ():
105+ result = ensure_build_agent_yaml_updated (app_mock , '7.99.x' )
106+
107+ assert result is True
108+ assert build_agent_path .read_text () == (
109+ '.build-agent-tpl:\n trigger:\n branch: 7.99.x\n unrelated-job:\n trigger:\n branch: main\n '
110+ )
111+ app_mock .abort .assert_not_called ()
112+
113+
114+ def test_ensure_build_agent_yaml_updated_aborts_on_multiple_template_main_branches (mocker , tmp_path ):
115+ build_agent_path = Path (tmp_path / '.gitlab' / 'build_agent.yaml' )
116+ build_agent_path .parent .ensure_dir_exists ()
117+ content = '.build-agent-tpl:\n trigger:\n branch: main\n branch: main\n '
118+ build_agent_path .write_text (content )
119+
120+ app_mock = mocker .MagicMock ()
121+ app_mock .abort .side_effect = RuntimeError ('abort' )
122+
123+ with Path (tmp_path ).as_cwd (), pytest .raises (RuntimeError , match = 'abort' ):
124+ ensure_build_agent_yaml_updated (app_mock , '7.99.x' )
125+
126+ assert build_agent_path .read_text () == content
127+ app_mock .abort .assert_called_once_with (
128+ 'Expected exactly one `.build-agent-tpl` branch pointing to `main` in `.gitlab/build_agent.yaml`; found 2.'
129+ )
106130
107131
108132def test_ensure_build_agent_yaml_updated_already_on_release_branch (mocker , tmp_path ):
109133 """Test early return when file already points to a release branch."""
110134 build_agent_path = Path (tmp_path / '.gitlab' / 'build_agent.yaml' )
111135 build_agent_path .parent .ensure_dir_exists ()
112- build_agent_path .write_text ('.build-agent-tpl:\n trigger:\n branch: 7.98.x\n ' )
136+ build_agent_path .write_text (
137+ '.build-agent-tpl:\n trigger:\n branch: 7.98.x\n unrelated-job:\n trigger:\n branch: main\n '
138+ )
113139
114140 app_mock = mocker .MagicMock ()
115141
116142 with Path (tmp_path ).as_cwd ():
117143 result = ensure_build_agent_yaml_updated (app_mock , '7.99.x' )
118144
119145 assert result is False
146+ assert 'branch: 7.98.x' in build_agent_path .read_text ()
147+ assert 'branch: main' in build_agent_path .read_text ()
120148 app_mock .repo .git .capture .assert_not_called ()
121149
122150
0 commit comments