1
1
import io
2
2
import os
3
+ import uuid
3
4
4
5
from django .core .management .base import CommandError
5
6
@@ -23,16 +24,24 @@ def test_handle_no_args(settings):
23
24
def test_handle_new_app (settings , tmp_path , monkeypatch , capsys ):
24
25
settings .BASE_DIR = tmp_path
25
26
26
- # Reply "y" then "n"
27
+ # Reply "y" to create new app then "n" to star the repo
27
28
monkeypatch .setattr ("sys.stdin" , io .StringIO ("y\n n\n " ))
28
29
29
- app_name = "test123"
30
- component_names = ["hello-world" ]
30
+ # Prevent the `startapp` command from actually creating a new app
31
+ monkeypatch .setattr (
32
+ "django_unicorn.management.commands.startunicorn.call_command" ,
33
+ lambda * args , ** kwargs : None ,
34
+ )
35
+
36
+ app_name = f"test-{ uuid .uuid4 ()} " .replace ("-" , "_" )
37
+ component_names = [
38
+ "hello-world" ,
39
+ ]
31
40
Command ().handle (app_name = app_name , component_names = component_names )
32
41
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 ()
42
+ assert (tmp_path / app_name / " components/__init__.py" ).exists ()
43
+ assert (tmp_path / app_name / " components/hello_world.py" ).exists ()
44
+ assert (tmp_path / app_name / " templates/unicorn/hello-world.html" ).exists ()
36
45
37
46
captured = capsys .readouterr ()
38
47
assert "Starring the GitHub repo " in captured .out
@@ -41,19 +50,24 @@ def test_handle_new_app(settings, tmp_path, monkeypatch, capsys):
41
50
42
51
def test_handle_existing_app (settings , tmp_path , monkeypatch , capsys ):
43
52
settings .BASE_DIR = tmp_path
53
+ app_name = f"test-{ uuid .uuid4 ()} " .replace ("-" , "_" )
44
54
45
- app_name = "test123"
46
- (tmp_path / app_name ).mkdir ()
55
+ monkeypatch .setattr (
56
+ "django_unicorn.management.commands.startunicorn.get_app_path" ,
57
+ lambda a : tmp_path / app_name ,
58
+ )
47
59
48
- # Reply "y" then "n"
49
- monkeypatch .setattr ("sys.stdin" , io .StringIO ("y \n n \n " ))
60
+ # Reply "n" to starring question
61
+ monkeypatch .setattr ("sys.stdin" , io .StringIO ("n \n " ))
50
62
51
- component_names = ["hello-world" ]
63
+ component_names = [
64
+ "hello-world" ,
65
+ ]
52
66
Command ().handle (app_name = app_name , component_names = component_names )
53
67
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 ()
68
+ assert (tmp_path / app_name / " components/__init__.py" ).exists ()
69
+ assert (tmp_path / app_name / " components/hello_world.py" ).exists ()
70
+ assert (tmp_path / app_name / " templates/unicorn/hello-world.html" ).exists ()
57
71
58
72
captured = capsys .readouterr ()
59
73
assert "Starring the GitHub repo " in captured .out
@@ -63,20 +77,24 @@ def test_handle_existing_app(settings, tmp_path, monkeypatch, capsys):
63
77
64
78
def test_handle_existing_component (settings , tmp_path , monkeypatch , capsys ):
65
79
settings .BASE_DIR = tmp_path
80
+ app_name = f"test-{ uuid .uuid4 ()} " .replace ("-" , "_" )
81
+
82
+ monkeypatch .setattr (
83
+ "django_unicorn.management.commands.startunicorn.get_app_path" ,
84
+ lambda a : tmp_path / app_name ,
85
+ )
66
86
67
- app_name = "test123"
68
87
(tmp_path / app_name ).mkdir ()
69
88
(tmp_path / app_name / "components" ).mkdir ()
70
89
71
- # Reply "y"
72
- monkeypatch .setattr ("sys.stdin" , io .StringIO ("y\n " ))
73
-
74
- component_names = ["hello-world" ]
90
+ component_names = [
91
+ "hello-world" ,
92
+ ]
75
93
Command ().handle (app_name = app_name , component_names = component_names )
76
94
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 ()
95
+ assert (tmp_path / app_name / " components/__init__.py" ).exists ()
96
+ assert (tmp_path / app_name / " components/hello_world.py" ).exists ()
97
+ assert (tmp_path / app_name / " templates/unicorn/hello-world.html" ).exists ()
80
98
81
99
captured = capsys .readouterr ()
82
100
assert "Starring the GitHub repo " not in captured .out
@@ -85,21 +103,25 @@ def test_handle_existing_component(settings, tmp_path, monkeypatch, capsys):
85
103
86
104
def test_handle_existing_templates (settings , tmp_path , monkeypatch , capsys ):
87
105
settings .BASE_DIR = tmp_path
106
+ app_name = f"test-{ uuid .uuid4 ()} " .replace ("-" , "_" )
107
+
108
+ monkeypatch .setattr (
109
+ "django_unicorn.management.commands.startunicorn.get_app_path" ,
110
+ lambda a : tmp_path / app_name ,
111
+ )
88
112
89
- app_name = "test123"
90
113
(tmp_path / app_name ).mkdir ()
91
114
(tmp_path / app_name / "components" ).mkdir ()
92
115
(tmp_path / app_name / "templates" ).mkdir ()
93
116
94
- # Reply "y"
95
- monkeypatch .setattr ("sys.stdin" , io .StringIO ("y\n " ))
96
-
97
- component_names = ["hello-world" ]
117
+ component_names = [
118
+ "hello-world" ,
119
+ ]
98
120
Command ().handle (app_name = app_name , component_names = component_names )
99
121
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 ()
122
+ assert (tmp_path / app_name / " components/__init__.py" ).exists ()
123
+ assert (tmp_path / app_name / " components/hello_world.py" ).exists ()
124
+ assert (tmp_path / app_name / " templates/unicorn/hello-world.html" ).exists ()
103
125
104
126
captured = capsys .readouterr ()
105
127
assert "Starring the GitHub repo " not in captured .out
@@ -108,17 +130,21 @@ def test_handle_existing_templates(settings, tmp_path, monkeypatch, capsys):
108
130
109
131
def test_handle_existing_unicorn_templates (settings , tmp_path , monkeypatch , capsys ):
110
132
settings .BASE_DIR = tmp_path
133
+ app_name = f"test-{ uuid .uuid4 ()} " .replace ("-" , "_" )
134
+
135
+ monkeypatch .setattr (
136
+ "django_unicorn.management.commands.startunicorn.get_app_path" ,
137
+ lambda a : tmp_path / app_name ,
138
+ )
111
139
112
- app_name = "test123"
113
140
(tmp_path / app_name ).mkdir ()
114
141
(tmp_path / app_name / "components" ).mkdir ()
115
142
(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 " ))
143
+ (tmp_path / app_name / "templates/unicorn" ).mkdir ()
120
144
121
- component_names = ["hello-world" ]
145
+ component_names = [
146
+ "hello-world" ,
147
+ ]
122
148
Command ().handle (app_name = app_name , component_names = component_names )
123
149
124
150
assert (tmp_path / f"{ app_name } /components/__init__.py" ).exists ()
@@ -130,33 +156,49 @@ def test_handle_existing_unicorn_templates(settings, tmp_path, monkeypatch, caps
130
156
assert "Make sure to add " not in captured .out
131
157
132
158
133
- def test_handle_reply_no (settings , tmp_path , monkeypatch , capsys ):
159
+ def test_handle_reply_n (settings , tmp_path , monkeypatch , capsys ):
134
160
settings .BASE_DIR = tmp_path
135
161
136
162
# Reply "n"
137
163
monkeypatch .setattr ("sys.stdin" , io .StringIO ("n\n " ))
138
164
139
- app_name = "test123"
140
- component_names = ["hello-world" ]
141
- Command ().handle (app_name = app_name , component_names = component_names )
165
+ app_name = f"test-{ uuid .uuid4 ()} " .replace ("-" , "_" )
166
+ component_names = [
167
+ "hello-world" ,
168
+ ]
169
+
170
+ with pytest .raises (CommandError ):
171
+ Command ().handle (app_name = app_name , component_names = component_names )
142
172
143
173
assert not (tmp_path / f"{ app_name } /components/__init__.py" ).exists ()
144
174
assert not (tmp_path / f"{ app_name } /components/hello_world.py" ).exists ()
145
175
assert not (tmp_path / f"{ app_name } /templates/unicorn/hello-world.html" ).exists ()
146
176
147
177
captured = capsys .readouterr ()
178
+ assert "cannot be found." in captured .out
148
179
assert "Make sure to add " not in captured .out
149
180
181
+
182
+ def test_handle_reply_no (settings , tmp_path , monkeypatch , capsys ):
183
+ settings .BASE_DIR = tmp_path
184
+
150
185
# Reply "no"
151
186
monkeypatch .setattr ("sys.stdin" , io .StringIO ("no\n " ))
152
187
153
- Command ().handle (app_name = app_name , component_names = component_names )
188
+ app_name = f"test-{ uuid .uuid4 ()} " .replace ("-" , "_" )
189
+ component_names = [
190
+ "hello-world" ,
191
+ ]
192
+
193
+ with pytest .raises (CommandError ):
194
+ Command ().handle (app_name = app_name , component_names = component_names )
154
195
155
196
assert not (tmp_path / f"{ app_name } /components/__init__.py" ).exists ()
156
197
assert not (tmp_path / f"{ app_name } /components/hello_world.py" ).exists ()
157
198
assert not (tmp_path / f"{ app_name } /templates/unicorn/hello-world.html" ).exists ()
158
199
159
200
captured = capsys .readouterr ()
201
+ assert "cannot be found." in captured .out
160
202
assert "Make sure to add " not in captured .out
161
203
162
204
@@ -172,14 +214,22 @@ def webbrowser_open(url, **kwargs):
172
214
173
215
monkeypatch .setattr ("webbrowser.open" , webbrowser_open )
174
216
175
- app_name = "test123"
176
- component_names = ["hello-world" ]
217
+ # Prevent the `startapp` command from actually creating a new app
218
+ monkeypatch .setattr (
219
+ "django_unicorn.management.commands.startunicorn.call_command" ,
220
+ lambda * args , ** kwargs : None ,
221
+ )
222
+
223
+ app_name = f"test-{ uuid .uuid4 ()} " .replace ("-" , "_" )
224
+ component_names = [
225
+ "hello-world" ,
226
+ ]
177
227
178
228
Command ().handle (app_name = app_name , component_names = component_names )
179
229
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 ()
230
+ assert (tmp_path / app_name / " components/__init__.py" ).exists ()
231
+ assert (tmp_path / app_name / " components/hello_world.py" ).exists ()
232
+ assert (tmp_path / app_name / " templates/unicorn/hello-world.html" ).exists ()
183
233
184
234
captured = capsys .readouterr ()
185
235
assert "Starring the GitHub repo " in captured .out
0 commit comments