1919import os
2020import sys
2121from errno import ENOENT
22- from pathlib import Path
22+ from pathlib import Path , PurePosixPath
2323from types import SimpleNamespace
2424from unittest .mock import MagicMock , patch
2525
@@ -459,7 +459,10 @@ def test_github_tree_url_resolves_a_checked_out_subdirectory(tmp_path: Path) ->
459459 handler = InputHandler ()
460460 clone = tmp_path / "repo"
461461 (clone / "skills" / "biome-gritql" ).mkdir (parents = True )
462- with patch .object (handler , "_clone_git" , return_value = clone ) as clone_git :
462+ with (
463+ patch .object (handler , "_clone_git" , return_value = clone ) as clone_git ,
464+ patch .object (handler , "_list_remote_refs" , return_value = {"main" }),
465+ ):
463466 resolved , source_type = handler .resolve (
464467 "https://github.com/somtougeh/somto-dev-toolkit/tree/main/skills/biome-gritql"
465468 )
@@ -470,6 +473,55 @@ def test_github_tree_url_resolves_a_checked_out_subdirectory(tmp_path: Path) ->
470473 )
471474
472475
476+ def test_github_tree_url_resolves_slash_containing_ref (tmp_path : Path ) -> None :
477+ """A branch name containing / must not be split into ref + subdirectory."""
478+ handler = InputHandler ()
479+ clone = tmp_path / "repo"
480+ (clone / "skills" / "demo" ).mkdir (parents = True )
481+ with (
482+ patch .object (handler , "_clone_git" , return_value = clone ) as clone_git ,
483+ patch .object (handler , "_list_remote_refs" , return_value = {"main" , "feature" , "feature/foo" }),
484+ ):
485+ resolved , source_type = handler .resolve (
486+ "https://github.com/example/repo/tree/feature/foo/skills/demo"
487+ )
488+ assert resolved == clone / "skills" / "demo"
489+ assert source_type == "git"
490+ clone_git .assert_called_once_with ("https://github.com/example/repo.git" , branch = "feature/foo" )
491+
492+
493+ def test_github_tree_url_prefers_shorter_ref_when_longest_absent () -> None :
494+ """The longest *advertised* ref wins, not the longest URL prefix."""
495+ handler = InputHandler ()
496+ with patch .object (handler , "_list_remote_refs" , return_value = {"feature" }):
497+ repository_url , ref , subdirectory = handler ._github_tree_target (
498+ "https://github.com/example/repo/tree/feature/sub"
499+ )
500+ assert repository_url == "https://github.com/example/repo.git"
501+ assert ref == "feature"
502+ assert subdirectory == PurePosixPath ("sub" )
503+
504+
505+ def test_github_tree_url_rejects_unknown_ref () -> None :
506+ handler = InputHandler ()
507+ with (
508+ patch .object (handler , "_list_remote_refs" , return_value = {"main" }),
509+ pytest .raises (ValueError , match = "does not name a known branch or tag" ),
510+ ):
511+ handler ._github_tree_target ("https://github.com/example/repo/tree/nope/sub" )
512+
513+
514+ def test_github_tree_url_supports_ref_without_subdirectory () -> None :
515+ handler = InputHandler ()
516+ with patch .object (handler , "_list_remote_refs" , return_value = {"main" }):
517+ repository_url , ref , subdirectory = handler ._github_tree_target (
518+ "https://github.com/example/repo/tree/main"
519+ )
520+ assert repository_url == "https://github.com/example/repo.git"
521+ assert ref == "main"
522+ assert subdirectory == PurePosixPath ("." )
523+
524+
473525@pytest .mark .parametrize ("segment" , ["%2Fetc" , "%2E%2E%2Frepo" , "%5Coutside" ])
474526def test_github_tree_url_rejects_encoded_path_escapes (segment : str ) -> None :
475527 with pytest .raises (ValueError , match = "stay within the repository" ):
@@ -487,7 +539,10 @@ def test_github_tree_url_selection_failure_cleans_owned_clone(tmp_path: Path, ta
487539 if target == "SKILL.md" :
488540 (clone / target ).write_text ("# skill\n " )
489541 handler ._temp_dir = tmp_path
490- with patch .object (handler , "_clone_git" , return_value = clone ):
542+ with (
543+ patch .object (handler , "_clone_git" , return_value = clone ),
544+ patch .object (handler , "_list_remote_refs" , return_value = {"main" }),
545+ ):
491546 with pytest .raises (ValueError ):
492547 handler .resolve (f"https://github.com/example/repo/tree/main/{ target } " )
493548 assert not tmp_path .exists ()
0 commit comments