22import os
33import subprocess
44import sys
5+ import textwrap
56from pathlib import Path
6- from typing import Iterable , Iterator
7+ from types import SimpleNamespace
8+ from typing import Any , Dict , Iterable , Iterator , Tuple
79from unittest .mock import patch
810
911import pytest
@@ -71,6 +73,26 @@ def new_cookie_with_lock(new_cookie: Path, temp_dir: str) -> Iterator[Path]:
7173 yield new_cookie
7274
7375
76+ @pytest .fixture
77+ def run_or_mock () -> Iterator [Dict [Tuple [str , ...], str ]]:
78+ real_run = subprocess .run
79+ mocked_commands : Dict [Tuple [str , ...], str ] = {}
80+
81+ def _run (* args : Any , ** kwargs : Any ) -> Any :
82+ cmd = args [0 ]
83+ cmd_tuple = tuple (cmd )
84+ while cmd_tuple :
85+ if cmd_tuple in mocked_commands :
86+ return SimpleNamespace (
87+ stdout = mocked_commands [cmd_tuple ].encode ()
88+ )
89+ cmd_tuple = cmd_tuple [:- 1 ]
90+ return real_run (* args , ** kwargs )
91+
92+ with patch .object (subprocess , "run" , _run ):
93+ yield mocked_commands
94+
95+
7496def _manage_cookie (argv : Iterable [str ]) -> None :
7597 with patch .object (sys , "argv" , argv ):
7698 manage_cookie_main ()
@@ -86,3 +108,26 @@ def test_manage_cookie_update(new_cookie_with_lock: str) -> None:
86108 _manage_cookie (
87109 ["manage-cookie" , "update" , str (new_cookie_with_lock ), "-p" ]
88110 )
111+
112+
113+ @pytest .mark .parametrize ("add_commit" , (True , False ))
114+ def test_manage_cookie_release (
115+ new_cookie : str , run_or_mock : Dict [Tuple [str , ...], str ], add_commit : bool
116+ ) -> None :
117+ run_or_mock [("gh" , "release" , "list" )] = textwrap .dedent (
118+ """
119+ TITLE TYPE TAG NAME PUBLISHED
120+ v1.1.38 Latest v1.1.38 in a galaxy far, far away
121+ v0.21.87 v0.21.87 about a long time ago
122+ v0.0.1 v0.0.1 about never ago
123+ """
124+ ).strip ()
125+ run_or_mock [("gh" ,)] = "" # Prevent any actual gh invocations
126+ subprocess .run (["git" , "tag" , "v1.1.38" , "@" ], cwd = new_cookie , check = True )
127+ if add_commit :
128+ subprocess .run (
129+ ["git" , "commit" , "--allow-empty" , "-m" , "create empty commit" ],
130+ cwd = new_cookie ,
131+ check = True ,
132+ )
133+ _manage_cookie (["manage-cookie" , "release" , str (new_cookie ), "-p" ])
0 commit comments