@@ -122,6 +122,24 @@ def test_custom_github_url_unix_uses_sh(self) -> None:
122122 parsed = urlparse (url )
123123 assert parsed .path .endswith ("install.sh" )
124124
125+ def test_custom_github_url_uses_supplied_script_ref (self ) -> None :
126+ """Supplying a script ref points installer downloads to that tag path."""
127+ from urllib .parse import urlparse
128+
129+ env = self ._clean_env ()
130+ env ["GITHUB_URL" ] = "https://gh.corp.com"
131+ env ["APM_REPO" ] = "corp/apm"
132+ with patch .dict ("os.environ" , env , clear = True ):
133+ with patch (
134+ "apm_cli.commands.self_update._is_windows_platform" ,
135+ return_value = False ,
136+ ):
137+ from apm_cli .commands .self_update import _get_update_installer_url
138+
139+ url = _get_update_installer_url ("v1.2.3" )
140+ parsed = urlparse (url )
141+ assert parsed .path == "/corp/apm/raw/v1.2.3/install.sh"
142+
125143 def test_github_url_with_trailing_slash_is_normalised (self ) -> None :
126144 """GITHUB_URL with a trailing slash must not produce double-slash in the URL."""
127145 env = self ._clean_env ()
@@ -357,3 +375,65 @@ def fake_get(url: str, **_kwargs: object) -> Mock:
357375 "/apm/installers/install.sh" ,
358376 ]
359377 mock_run .assert_called_once ()
378+
379+ def test_self_update_pins_installer_ref_to_latest_version (self ) -> None :
380+ """Self-update downloads installer from the exact latest release ref."""
381+ env = self ._clean_env ()
382+ env .update (
383+ {
384+ "APM_NO_DIRECT_FALLBACK" : "1" ,
385+ "APM_RELEASE_METADATA_URL" : "https://mirror.corp.example/apm/latest.json" ,
386+ "GITHUB_URL" : "https://gh.corp.com" ,
387+ "APM_TEMP_DIR" : str (self .scratch ),
388+ "APM_REPO" : "corp/apm" ,
389+ "APM_E2E_TESTS" : "1" ,
390+ }
391+ )
392+ requested_urls : list [str ] = []
393+ target_version = "1.1.0"
394+ expected_installer_path = f"/corp/apm/raw/v{ target_version } /install.sh"
395+
396+ def fake_get (url : str , ** _kwargs : object ) -> Mock :
397+ requested_urls .append (url )
398+ parsed = urlparse (url )
399+ if parsed .hostname == "mirror.corp.example" :
400+ if parsed .path == "/apm/latest.json" :
401+ response = Mock ()
402+ response .status_code = 200
403+ response .raise_for_status .return_value = None
404+ response .json .return_value = {"tag_name" : f"v{ target_version } " }
405+ return response
406+ raise AssertionError (f"unexpected mirror request: { url } " )
407+
408+ if parsed .hostname == "gh.corp.com" :
409+ if parsed .path == expected_installer_path :
410+ response = Mock ()
411+ response .status_code = 200
412+ response .raise_for_status .return_value = None
413+ response .text = "echo install"
414+ return response
415+ raise AssertionError (f"unexpected github request: { url } " )
416+
417+ raise AssertionError (f"unexpected request: { url } " )
418+
419+ with (
420+ patch .dict ("os.environ" , env , clear = True ),
421+ patch ("requests.get" , side_effect = fake_get ),
422+ patch ("subprocess.run" , return_value = Mock (returncode = 0 )) as mock_run ,
423+ patch ("apm_cli.commands.self_update.get_version" , return_value = "1.0.0" ),
424+ patch ("apm_cli.commands.self_update.os.chmod" ),
425+ patch .object (update_module .sys , "platform" , "linux" ),
426+ patch ("apm_cli.commands.self_update.os.path.exists" , return_value = True ),
427+ ):
428+ result = self .runner .invoke (cli , ["self-update" ])
429+
430+ assert result .exit_code == 0
431+ assert "Successfully updated to version 1.1.0" in result .output
432+ parsed_paths = [urlparse (url ).path for url in requested_urls ]
433+ assert parsed_paths == [
434+ "/apm/latest.json" ,
435+ expected_installer_path ,
436+ ]
437+ assert "/main/" not in "" .join (parsed_paths )
438+ assert f"/raw/v{ target_version } /" in "" .join (parsed_paths )
439+ mock_run .assert_called_once ()
0 commit comments