1+ from pathlib import Path
2+
3+ import pytest
14from create_awesome_python_app .cli import app
25from typer .testing import CliRunner
36
47runner = CliRunner ()
8+ _CPA_ENV_VARS = ("CPA_REFRESH" , "CPA_NO_CATALOG_CACHE" , "CPA_CACHE_DIR" )
9+
10+
11+ @pytest .fixture (autouse = True )
12+ def _clean_cpa_env (monkeypatch ):
13+ for name in _CPA_ENV_VARS :
14+ monkeypatch .delenv (name , raising = False )
15+ yield
16+ for name in _CPA_ENV_VARS :
17+ monkeypatch .delenv (name , raising = False )
518
619
720def test_version () -> None :
@@ -15,3 +28,143 @@ def test_help() -> None:
1528 result = runner .invoke (app , ["--help" ])
1629 assert result .exit_code == 0
1730 assert "Scaffold" in result .stdout or "create" in result .stdout .lower ()
31+
32+
33+ def test_refresh_flag_is_forwarded (tmp_path : Path , monkeypatch ) -> None :
34+ tpl = tmp_path / "tpl"
35+ tpl .mkdir ()
36+ captured : dict [str , object ] = {}
37+
38+ async def fake_check_for_latest_version (_package_name ):
39+ return None
40+
41+ async def fake_create_python_app (project_directory , options , * _args , ** _kwargs ):
42+ captured ["project_directory" ] = project_directory
43+ captured ["options" ] = options
44+
45+ monkeypatch .setattr (
46+ "create_awesome_python_app.cli.check_for_latest_version" ,
47+ fake_check_for_latest_version ,
48+ )
49+ monkeypatch .setattr (
50+ "create_awesome_python_app.cli.create_python_app" ,
51+ fake_create_python_app ,
52+ )
53+
54+ result = runner .invoke (
55+ app ,
56+ [
57+ "--template" ,
58+ f"file://{ tpl } " ,
59+ "--refresh" ,
60+ "always" ,
61+ "--no-install" ,
62+ "--no-interactive" ,
63+ "api" ,
64+ ],
65+ )
66+
67+ assert result .exit_code == 0 , result .stdout + result .stderr
68+ options = captured ["options" ]
69+ assert isinstance (options , dict )
70+ assert options ["refresh" ] == "always"
71+
72+
73+ def test_invalid_refresh_mode_fails (tmp_path : Path ) -> None :
74+ tpl = tmp_path / "tpl"
75+ tpl .mkdir ()
76+
77+ result = runner .invoke (
78+ app ,
79+ [
80+ "--template" ,
81+ f"file://{ tpl } " ,
82+ "--refresh" ,
83+ "bogus" ,
84+ "--no-install" ,
85+ "--no-interactive" ,
86+ "api" ,
87+ ],
88+ )
89+
90+ assert result .exit_code == 2
91+ assert "Invalid --refresh mode" in result .stdout + result .stderr
92+
93+
94+ def test_no_cache_sets_explicit_refresh (tmp_path : Path , monkeypatch ) -> None :
95+ tpl = tmp_path / "tpl"
96+ tpl .mkdir ()
97+ captured : dict [str , object ] = {}
98+
99+ async def fake_check_for_latest_version (_package_name ):
100+ return None
101+
102+ async def fake_create_python_app (project_directory , options , * _args , ** _kwargs ):
103+ captured ["project_directory" ] = project_directory
104+ captured ["options" ] = options
105+
106+ monkeypatch .setattr (
107+ "create_awesome_python_app.cli.check_for_latest_version" ,
108+ fake_check_for_latest_version ,
109+ )
110+ monkeypatch .setattr (
111+ "create_awesome_python_app.cli.create_python_app" ,
112+ fake_create_python_app ,
113+ )
114+
115+ result = runner .invoke (
116+ app ,
117+ [
118+ "--template" ,
119+ f"file://{ tpl } " ,
120+ "--no-cache" ,
121+ "--no-install" ,
122+ "--no-interactive" ,
123+ "api" ,
124+ ],
125+ )
126+
127+ assert result .exit_code == 0 , result .stdout + result .stderr
128+ options = captured ["options" ]
129+ assert isinstance (options , dict )
130+ assert options ["refresh" ] == "always"
131+
132+
133+ def test_pin_appends_ref_to_template_url (tmp_path : Path , monkeypatch ) -> None :
134+ tpl = tmp_path / "tpl"
135+ tpl .mkdir ()
136+ captured : dict [str , object ] = {}
137+
138+ async def fake_check_for_latest_version (_package_name ):
139+ return None
140+
141+ async def fake_create_python_app (project_directory , options , * _args , ** _kwargs ):
142+ captured ["project_directory" ] = project_directory
143+ captured ["options" ] = options
144+
145+ monkeypatch .setattr (
146+ "create_awesome_python_app.cli.check_for_latest_version" ,
147+ fake_check_for_latest_version ,
148+ )
149+ monkeypatch .setattr (
150+ "create_awesome_python_app.cli.create_python_app" ,
151+ fake_create_python_app ,
152+ )
153+
154+ result = runner .invoke (
155+ app ,
156+ [
157+ "--template" ,
158+ f"file://{ tpl } " ,
159+ "--pin" ,
160+ "abc123" ,
161+ "--no-install" ,
162+ "--no-interactive" ,
163+ "api" ,
164+ ],
165+ )
166+
167+ assert result .exit_code == 0 , result .stdout + result .stderr
168+ options = captured ["options" ]
169+ assert isinstance (options , dict )
170+ assert options ["template" ] == f"file://{ tpl } ?ref=abc123"
0 commit comments