1+ import io
12import os
23
34from django .core .management .base import CommandError
@@ -17,3 +18,171 @@ def test_handle_no_args(settings):
1718
1819 with pytest .raises (CommandError ):
1920 Command ().handle ()
21+
22+
23+ def test_handle_new_app (settings , tmp_path , monkeypatch , capsys ):
24+ settings .BASE_DIR = tmp_path
25+
26+ # Reply "y" then "n"
27+ monkeypatch .setattr ("sys.stdin" , io .StringIO ("y\n n\n " ))
28+
29+ app_name = "test123"
30+ component_names = ["hello-world" ]
31+ Command ().handle (app_name = app_name , component_names = component_names )
32+
33+ assert (tmp_path / f"{ app_name } /components/__init__.py" ).exists ()
34+ assert (tmp_path / f"{ app_name } /components/hello_world.py" ).exists ()
35+ assert (tmp_path / f"{ app_name } /templates/unicorn/hello-world.html" ).exists ()
36+
37+ captured = capsys .readouterr ()
38+ assert "Starring the GitHub repo " in captured .out
39+ assert f'Make sure to add `"{ app_name } ",` to your' in captured .out
40+
41+
42+ def test_handle_existing_app (settings , tmp_path , monkeypatch , capsys ):
43+ settings .BASE_DIR = tmp_path
44+
45+ app_name = "test123"
46+ (tmp_path / app_name ).mkdir ()
47+
48+ # Reply "y" then "n"
49+ monkeypatch .setattr ("sys.stdin" , io .StringIO ("y\n n\n " ))
50+
51+ component_names = ["hello-world" ]
52+ Command ().handle (app_name = app_name , component_names = component_names )
53+
54+ assert (tmp_path / f"{ app_name } /components/__init__.py" ).exists ()
55+ assert (tmp_path / f"{ app_name } /components/hello_world.py" ).exists ()
56+ assert (tmp_path / f"{ app_name } /templates/unicorn/hello-world.html" ).exists ()
57+
58+ captured = capsys .readouterr ()
59+ assert "Starring the GitHub repo " in captured .out
60+ assert "That's a bummer" in captured .out
61+ assert "Make sure to add " not in captured .out
62+
63+
64+ def test_handle_existing_component (settings , tmp_path , monkeypatch , capsys ):
65+ settings .BASE_DIR = tmp_path
66+
67+ app_name = "test123"
68+ (tmp_path / app_name ).mkdir ()
69+ (tmp_path / app_name / "components" ).mkdir ()
70+
71+ # Reply "y"
72+ monkeypatch .setattr ("sys.stdin" , io .StringIO ("y\n " ))
73+
74+ component_names = ["hello-world" ]
75+ Command ().handle (app_name = app_name , component_names = component_names )
76+
77+ assert (tmp_path / f"{ app_name } /components/__init__.py" ).exists ()
78+ assert (tmp_path / f"{ app_name } /components/hello_world.py" ).exists ()
79+ assert (tmp_path / f"{ app_name } /templates/unicorn/hello-world.html" ).exists ()
80+
81+ captured = capsys .readouterr ()
82+ assert "Starring the GitHub repo " not in captured .out
83+ assert "Make sure to add " not in captured .out
84+
85+
86+ def test_handle_existing_templates (settings , tmp_path , monkeypatch , capsys ):
87+ settings .BASE_DIR = tmp_path
88+
89+ app_name = "test123"
90+ (tmp_path / app_name ).mkdir ()
91+ (tmp_path / app_name / "components" ).mkdir ()
92+ (tmp_path / app_name / "templates" ).mkdir ()
93+
94+ # Reply "y"
95+ monkeypatch .setattr ("sys.stdin" , io .StringIO ("y\n " ))
96+
97+ component_names = ["hello-world" ]
98+ Command ().handle (app_name = app_name , component_names = component_names )
99+
100+ assert (tmp_path / f"{ app_name } /components/__init__.py" ).exists ()
101+ assert (tmp_path / f"{ app_name } /components/hello_world.py" ).exists ()
102+ assert (tmp_path / f"{ app_name } /templates/unicorn/hello-world.html" ).exists ()
103+
104+ captured = capsys .readouterr ()
105+ assert "Starring the GitHub repo " not in captured .out
106+ assert "Make sure to add " not in captured .out
107+
108+
109+ def test_handle_existing_unicorn_templates (settings , tmp_path , monkeypatch , capsys ):
110+ settings .BASE_DIR = tmp_path
111+
112+ app_name = "test123"
113+ (tmp_path / app_name ).mkdir ()
114+ (tmp_path / app_name / "components" ).mkdir ()
115+ (tmp_path / app_name / "templates" ).mkdir ()
116+ (tmp_path / app_name / "templates" / "unicorn" ).mkdir ()
117+
118+ # Reply "y"
119+ monkeypatch .setattr ("sys.stdin" , io .StringIO ("y\n " ))
120+
121+ component_names = ["hello-world" ]
122+ Command ().handle (app_name = app_name , component_names = component_names )
123+
124+ assert (tmp_path / f"{ app_name } /components/__init__.py" ).exists ()
125+ assert (tmp_path / f"{ app_name } /components/hello_world.py" ).exists ()
126+ assert (tmp_path / f"{ app_name } /templates/unicorn/hello-world.html" ).exists ()
127+
128+ captured = capsys .readouterr ()
129+ assert "Starring the GitHub repo " not in captured .out
130+ assert "Make sure to add " not in captured .out
131+
132+
133+ def test_handle_reply_no (settings , tmp_path , monkeypatch , capsys ):
134+ settings .BASE_DIR = tmp_path
135+
136+ # Reply "n"
137+ monkeypatch .setattr ("sys.stdin" , io .StringIO ("n\n " ))
138+
139+ app_name = "test123"
140+ component_names = ["hello-world" ]
141+ Command ().handle (app_name = app_name , component_names = component_names )
142+
143+ assert not (tmp_path / f"{ app_name } /components/__init__.py" ).exists ()
144+ assert not (tmp_path / f"{ app_name } /components/hello_world.py" ).exists ()
145+ assert not (tmp_path / f"{ app_name } /templates/unicorn/hello-world.html" ).exists ()
146+
147+ captured = capsys .readouterr ()
148+ assert "Make sure to add " not in captured .out
149+
150+ # Reply "no"
151+ monkeypatch .setattr ("sys.stdin" , io .StringIO ("no\n " ))
152+
153+ Command ().handle (app_name = app_name , component_names = component_names )
154+
155+ assert not (tmp_path / f"{ app_name } /components/__init__.py" ).exists ()
156+ assert not (tmp_path / f"{ app_name } /components/hello_world.py" ).exists ()
157+ assert not (tmp_path / f"{ app_name } /templates/unicorn/hello-world.html" ).exists ()
158+
159+ captured = capsys .readouterr ()
160+ assert "Make sure to add " not in captured .out
161+
162+
163+ def test_handle_reply_yes_star (settings , tmp_path , monkeypatch , capsys ):
164+ settings .BASE_DIR = tmp_path
165+
166+ # Reply "y" then "y"
167+ monkeypatch .setattr ("sys.stdin" , io .StringIO ("y\n y\n " ))
168+
169+ # Patch opening a webbrowser
170+ def webbrowser_open (url , ** kwargs ):
171+ assert url == "https://github.com/adamghill/django-unicorn"
172+
173+ monkeypatch .setattr ("webbrowser.open" , webbrowser_open )
174+
175+ app_name = "test123"
176+ component_names = ["hello-world" ]
177+
178+ Command ().handle (app_name = app_name , component_names = component_names )
179+
180+ assert (tmp_path / f"{ app_name } /components/__init__.py" ).exists ()
181+ assert (tmp_path / f"{ app_name } /components/hello_world.py" ).exists ()
182+ assert (tmp_path / f"{ app_name } /templates/unicorn/hello-world.html" ).exists ()
183+
184+ captured = capsys .readouterr ()
185+ assert "Starring the GitHub repo " in captured .out
186+ assert "That's a bummer" not in captured .out
187+ assert "Thank you for helping spread the" in captured .out
188+ assert f'Make sure to add `"{ app_name } ",` to your' in captured .out
0 commit comments