Skip to content

Commit 021bdd8

Browse files
committed
migrate scaffold command
1 parent ef0e0c3 commit 021bdd8

File tree

2 files changed

+92
-25
lines changed

2 files changed

+92
-25
lines changed

gearbox/commands/scaffold.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from gearbox.command import Command
77
from gearbox.template import GearBoxTemplate
8-
from gearbox.utils.plugins import find_egg_info_dir
98

109

1110
class ScaffoldCommand(Command):
@@ -61,6 +60,7 @@ def get_parser(self, prog_name):
6160
"-np",
6261
"--no-package",
6362
dest="nopackage",
63+
action="store_true",
6464
help="When using subdir option do not create python "
6565
"packages, but plain directories.",
6666
)
@@ -77,27 +77,6 @@ def _lookup(self, template, where):
7777
break
7878
return template_filename
7979

80-
def _find_toplevel_packages(self):
81-
egg_info_dir = find_egg_info_dir(os.getcwd())
82-
if egg_info_dir is None:
83-
print(
84-
"Unable to find distribution egg-info, did you run setup.py egg_info or develop?"
85-
)
86-
return []
87-
88-
modules = os.path.join(egg_info_dir, "top_level.txt")
89-
try:
90-
modules = open(modules).readlines()
91-
except Exception:
92-
print("Unable to detect distribution top level packages.")
93-
return []
94-
95-
modules = map(lambda x: x.strip().replace(os.sep, "."), modules)
96-
modules = filter(
97-
lambda x: not x.startswith("test"), modules
98-
) # Remove test suite modules
99-
return list(modules)
100-
10180
def take_action(self, opts):
10281
for template in opts.scaffold_name:
10382
template_filename = None
@@ -136,7 +115,7 @@ def take_action(self, opts):
136115

137116
if not os.path.exists(output_dir):
138117
os.makedirs(output_dir)
139-
if opts.subdir:
118+
if opts.subdir and not opts.nopackage:
140119
package_init = os.path.join(output_dir, "__init__.py")
141120
if not os.path.exists(package_init):
142121
with open(package_init, "w") as pif:
@@ -157,7 +136,6 @@ def take_action(self, opts):
157136
"subdir": opts.subdir,
158137
"subpackage": subdir_as_package,
159138
"dotted_subpackage": "." + subdir_as_package,
160-
"packages": self._find_toplevel_packages(),
161139
},
162140
)
163141
except NameError as e:

tests/test_commands.py

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
from gearbox.commandmanager import CommandManager
1010
from gearbox.commands.help import HelpAction
11-
from gearbox.commands.scaffold import ScaffoldCommand
1211
from gearbox.commands.serve import ServeCommand
1312
from gearbox.commands.setup_app import SetupAppCommand
1413
from gearbox.main import GearBox, main
@@ -193,6 +192,96 @@ def test_scaffold(tmp_path):
193192
assert "class Testmodel" in content
194193

195194

195+
def test_scaffold_derives_output_extension_from_template_name(tmp_path):
196+
lookup_dir = tmp_path / "scaffolds"
197+
lookup_dir.mkdir()
198+
template_file = lookup_dir / "controller.py.template"
199+
template_file.write_text("class {{target.capitalize()}}:\n pass\n")
200+
project_dir = tmp_path / "project"
201+
project_dir.mkdir()
202+
203+
with patch.object(
204+
sys,
205+
"argv",
206+
[
207+
"gearbox",
208+
"scaffold",
209+
"controller",
210+
"Demo",
211+
"-l",
212+
str(lookup_dir),
213+
"-p",
214+
str(project_dir),
215+
],
216+
):
217+
main()
218+
219+
output_file = project_dir / "Demo.py"
220+
assert output_file.is_file()
221+
assert "class Demo" in output_file.read_text()
222+
223+
224+
def test_scaffold_no_package_prevents_subdir_init_file(tmp_path):
225+
lookup_dir = tmp_path / "scaffolds"
226+
lookup_dir.mkdir()
227+
template_file = lookup_dir / "controller.py.template"
228+
template_file.write_text("class {{target.capitalize()}}:\n pass\n")
229+
project_dir = tmp_path / "project"
230+
project_dir.mkdir()
231+
232+
with patch.object(
233+
sys,
234+
"argv",
235+
[
236+
"gearbox",
237+
"scaffold",
238+
"controller",
239+
"Demo",
240+
"-l",
241+
str(lookup_dir),
242+
"-p",
243+
str(project_dir),
244+
"-s",
245+
"admin",
246+
"--no-package",
247+
],
248+
):
249+
main()
250+
251+
assert (project_dir / "admin" / "Demo.py").is_file()
252+
assert not (project_dir / "admin" / "__init__.py").exists()
253+
254+
255+
def test_scaffold_subdir_creates_package_init_by_default(tmp_path):
256+
lookup_dir = tmp_path / "scaffolds"
257+
lookup_dir.mkdir()
258+
template_file = lookup_dir / "controller.py.template"
259+
template_file.write_text("class {{target.capitalize()}}:\n pass\n")
260+
project_dir = tmp_path / "project"
261+
project_dir.mkdir()
262+
263+
with patch.object(
264+
sys,
265+
"argv",
266+
[
267+
"gearbox",
268+
"scaffold",
269+
"controller",
270+
"Demo",
271+
"-l",
272+
str(lookup_dir),
273+
"-p",
274+
str(project_dir),
275+
"-s",
276+
"admin",
277+
],
278+
):
279+
main()
280+
281+
assert (project_dir / "admin" / "Demo.py").is_file()
282+
assert (project_dir / "admin" / "__init__.py").is_file()
283+
284+
196285
# --- Test for patch command ---
197286
def test_patch(tmp_path):
198287
test_file = tmp_path / "test.txt"

0 commit comments

Comments
 (0)