behave-gen 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- behave_gen/__init__.py +7 -0
- behave_gen/__main__.py +13 -0
- behave_gen/cli/__init__.py +1 -0
- behave_gen/cli/app.py +363 -0
- behave_gen/commands/__init__.py +1 -0
- behave_gen/commands/add.py +135 -0
- behave_gen/commands/check.py +177 -0
- behave_gen/commands/environment.py +251 -0
- behave_gen/commands/format.py +48 -0
- behave_gen/commands/from_openapi.py +62 -0
- behave_gen/commands/from_postman.py +55 -0
- behave_gen/commands/from_swagger.py +78 -0
- behave_gen/commands/init.py +130 -0
- behave_gen/commands/lint.py +48 -0
- behave_gen/commands/migrate.py +56 -0
- behave_gen/commands/preview.py +131 -0
- behave_gen/commands/stats.py +122 -0
- behave_gen/commands/steps.py +111 -0
- behave_gen/commands/update.py +138 -0
- behave_gen/config.py +138 -0
- behave_gen/diagnostics.py +60 -0
- behave_gen/generators/__init__.py +11 -0
- behave_gen/generators/base.py +48 -0
- behave_gen/generators/openapi.py +74 -0
- behave_gen/generators/postman.py +80 -0
- behave_gen/paths.py +52 -0
- behave_gen/plugins/__init__.py +1 -0
- behave_gen/plugins/cucumber/__init__.py +22 -0
- behave_gen/plugins/cucumber/migrator.py +122 -0
- behave_gen/plugins/openapi/__init__.py +25 -0
- behave_gen/plugins/openapi/feature_builder.py +105 -0
- behave_gen/plugins/openapi/parser.py +147 -0
- behave_gen/plugins/openapi/step_builder.py +35 -0
- behave_gen/plugins/postman/__init__.py +22 -0
- behave_gen/plugins/postman/feature_builder.py +77 -0
- behave_gen/plugins/postman/parser.py +128 -0
- behave_gen/plugins/swagger/__init__.py +87 -0
- behave_gen/project.py +88 -0
- behave_gen/step_libraries/__init__.py +7 -0
- behave_gen/step_libraries/auth_steps.py.tpl +85 -0
- behave_gen/step_libraries/http_steps.py.tpl +130 -0
- behave_gen/templates/__init__.py +1 -0
- behave_gen/templates/default/README.md +29 -0
- behave_gen/templates/default/behave.toml +6 -0
- behave_gen/templates/default/environment.py +31 -0
- behave_gen/templates/default/environment_with_data.py +34 -0
- behave_gen/templates/default/environment_with_kit.py +34 -0
- behave_gen/templates/default/environment_with_kit_data.py +36 -0
- behave_gen/templates/default/features/.gitkeep +0 -0
- behave_gen/templates/default/features/sample.feature +3 -0
- behave_gen/templates/default/features/steps/.gitkeep +0 -0
- behave_gen/templates/default/pyproject.toml +14 -0
- behave_gen/templates/discovery.py +92 -0
- behave_gen/templates/engine.py +125 -0
- behave_gen/templates/features/crud.feature +18 -0
- behave_gen/templates/features/default.feature +7 -0
- behave_gen/templates/registry.py +65 -0
- behave_gen-0.1.0.dist-info/METADATA +233 -0
- behave_gen-0.1.0.dist-info/RECORD +62 -0
- behave_gen-0.1.0.dist-info/WHEEL +4 -0
- behave_gen-0.1.0.dist-info/entry_points.txt +2 -0
- behave_gen-0.1.0.dist-info/licenses/LICENSE +21 -0
behave_gen/__init__.py
ADDED
behave_gen/__main__.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""Entry point for ``python -m behave_gen``.
|
|
2
|
+
|
|
3
|
+
Delegates to the Typer CLI defined in :mod:`behave_gen.cli.app`.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import sys
|
|
9
|
+
|
|
10
|
+
from behave_gen.cli.app import run
|
|
11
|
+
|
|
12
|
+
if __name__ == "__main__":
|
|
13
|
+
sys.exit(run(sys.argv[1:]))
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""CLI package for behave-gen."""
|
behave_gen/cli/app.py
ADDED
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
"""Typer CLI application for behave-gen.
|
|
2
|
+
|
|
3
|
+
Phase 2 registers every command as a no-op placeholder that prints
|
|
4
|
+
``"not implemented yet"`` and exits with code ``1``. Real logic is wired in
|
|
5
|
+
later phases by delegating to ``behave_gen.commands.*`` modules.
|
|
6
|
+
|
|
7
|
+
The command set and global options match the conventions used by
|
|
8
|
+
``behave-doctor``.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import sys
|
|
14
|
+
from collections.abc import Sequence
|
|
15
|
+
from typing import Annotated
|
|
16
|
+
|
|
17
|
+
import typer
|
|
18
|
+
|
|
19
|
+
from behave_gen.commands.add import AddFeatureOptions, run_add_feature
|
|
20
|
+
from behave_gen.commands.check import run_check
|
|
21
|
+
from behave_gen.commands.environment import (
|
|
22
|
+
AddEnvironmentOptions,
|
|
23
|
+
run_add_config,
|
|
24
|
+
run_add_environment,
|
|
25
|
+
)
|
|
26
|
+
from behave_gen.commands.format import run_format
|
|
27
|
+
from behave_gen.commands.from_openapi import FromOpenApiOptions, run_from_openapi
|
|
28
|
+
from behave_gen.commands.from_postman import FromPostmanOptions, run_from_postman
|
|
29
|
+
from behave_gen.commands.from_swagger import FromSwaggerOptions, run_from_swagger
|
|
30
|
+
from behave_gen.commands.init import InitOptions, run_init
|
|
31
|
+
from behave_gen.commands.lint import run_lint
|
|
32
|
+
from behave_gen.commands.migrate import MigrateOptions, run_migrate
|
|
33
|
+
from behave_gen.commands.preview import run_preview
|
|
34
|
+
from behave_gen.commands.stats import run_stats
|
|
35
|
+
from behave_gen.commands.steps import AddStepsOptions, run_add_steps
|
|
36
|
+
from behave_gen.commands.update import UpdateOptions, run_update
|
|
37
|
+
|
|
38
|
+
app = typer.Typer(
|
|
39
|
+
name="behave-gen",
|
|
40
|
+
help="Scaffold and evolve Behave BDD projects.",
|
|
41
|
+
no_args_is_help=True,
|
|
42
|
+
add_completion=False,
|
|
43
|
+
rich_markup_mode="rich",
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
# Global option state, populated by the root callback and read by commands.
|
|
47
|
+
_PROJECT_OPTION: Annotated[str | None, typer.Option] = typer.Option(
|
|
48
|
+
None, "--project", help="Project root directory (default: auto-detect)."
|
|
49
|
+
)
|
|
50
|
+
_CONFIG_OPTION: Annotated[str | None, typer.Option] = typer.Option(
|
|
51
|
+
None, "--config", help="Path to an explicit pyproject.toml config file."
|
|
52
|
+
)
|
|
53
|
+
_VERBOSE_OPTION: Annotated[bool, typer.Option] = typer.Option(
|
|
54
|
+
False, "--verbose", "-v", help="Enable verbose (DEBUG) output."
|
|
55
|
+
)
|
|
56
|
+
_DRY_RUN_OPTION: Annotated[bool, typer.Option] = typer.Option(
|
|
57
|
+
False, "--dry-run", help="Show what would happen without writing files."
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class _GlobalState:
|
|
62
|
+
"""Mutable container for global CLI options."""
|
|
63
|
+
|
|
64
|
+
project: str | None = None
|
|
65
|
+
config: str | None = None
|
|
66
|
+
verbose: bool = False
|
|
67
|
+
dry_run: bool = False
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
state = _GlobalState()
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@app.callback()
|
|
74
|
+
def main_callback(
|
|
75
|
+
project: Annotated[str | None, typer.Option] = _PROJECT_OPTION,
|
|
76
|
+
config: Annotated[str | None, typer.Option] = _CONFIG_OPTION,
|
|
77
|
+
verbose: Annotated[bool, typer.Option] = _VERBOSE_OPTION,
|
|
78
|
+
dry_run: Annotated[bool, typer.Option] = _DRY_RUN_OPTION,
|
|
79
|
+
) -> None:
|
|
80
|
+
"""Configure global options shared by all subcommands."""
|
|
81
|
+
state.project = project
|
|
82
|
+
state.config = config
|
|
83
|
+
state.verbose = verbose
|
|
84
|
+
state.dry_run = dry_run
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
# --- Project lifecycle -----------------------------------------------------
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@app.command("init")
|
|
91
|
+
def init_cmd(
|
|
92
|
+
name: Annotated[str, typer.Argument(help="New project directory name.")],
|
|
93
|
+
template: Annotated[str, typer.Option] = typer.Option(
|
|
94
|
+
"default", "--template", help="Template set to use."
|
|
95
|
+
),
|
|
96
|
+
kit: Annotated[bool, typer.Option] = typer.Option(
|
|
97
|
+
False, "--kit", help="Pre-wire behave-kit in environment.py."
|
|
98
|
+
),
|
|
99
|
+
data: Annotated[bool, typer.Option] = typer.Option(
|
|
100
|
+
False, "--data", help="Pre-wire behave-data hooks."
|
|
101
|
+
),
|
|
102
|
+
force: Annotated[bool, typer.Option] = typer.Option(
|
|
103
|
+
False, "--force", help="Overwrite an existing directory."
|
|
104
|
+
),
|
|
105
|
+
) -> None:
|
|
106
|
+
"""Create a new Behave project from a template."""
|
|
107
|
+
options = InitOptions(name=name, template=template, kit=kit, data=data, force=force)
|
|
108
|
+
code = run_init(options)
|
|
109
|
+
raise typer.Exit(code=code)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
add_app = typer.Typer(help="Add features, steps, environment, or config to a project.")
|
|
113
|
+
app.add_typer(add_app, name="add")
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
@add_app.command("feature")
|
|
117
|
+
def add_feature_cmd(
|
|
118
|
+
name: Annotated[str, typer.Argument(help="Feature name (without extension).")],
|
|
119
|
+
tags: Annotated[str | None, typer.Option] = typer.Option(
|
|
120
|
+
None, "--tags", help="Comma or space separated tags."
|
|
121
|
+
),
|
|
122
|
+
template: Annotated[str, typer.Option] = typer.Option(
|
|
123
|
+
"default", "--template", help="Feature template to use."
|
|
124
|
+
),
|
|
125
|
+
) -> None:
|
|
126
|
+
"""Add a .feature file to the project."""
|
|
127
|
+
options = AddFeatureOptions(name=name, tags=tags, template=template)
|
|
128
|
+
code = run_add_feature(options)
|
|
129
|
+
raise typer.Exit(code=code)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
@add_app.command("steps")
|
|
133
|
+
def add_steps_cmd(
|
|
134
|
+
lib: Annotated[str, typer.Option] = typer.Option(
|
|
135
|
+
..., "--lib", help="Step library name (e.g. http, auth)."
|
|
136
|
+
),
|
|
137
|
+
from_openapi: Annotated[str | None, typer.Option] = typer.Option(
|
|
138
|
+
None, "--from-openapi", help="Generate steps from an OpenAPI spec."
|
|
139
|
+
),
|
|
140
|
+
) -> None:
|
|
141
|
+
"""Add real step definitions from a step library."""
|
|
142
|
+
options = AddStepsOptions(lib=lib, from_openapi=from_openapi)
|
|
143
|
+
code = run_add_steps(options)
|
|
144
|
+
raise typer.Exit(code=code)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
@add_app.command("environment")
|
|
148
|
+
def add_environment_cmd(
|
|
149
|
+
kit: Annotated[bool, typer.Option] = typer.Option(
|
|
150
|
+
False, "--kit", help="Add behave-kit fixtures."
|
|
151
|
+
),
|
|
152
|
+
data: Annotated[bool, typer.Option] = typer.Option(
|
|
153
|
+
False, "--data", help="Add behave-data hooks."
|
|
154
|
+
),
|
|
155
|
+
) -> None:
|
|
156
|
+
"""Add or update environment.py with ecosystem hooks."""
|
|
157
|
+
options = AddEnvironmentOptions(kit=kit, data=data)
|
|
158
|
+
code = run_add_environment(options)
|
|
159
|
+
raise typer.Exit(code=code)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
@add_app.command("config")
|
|
163
|
+
def add_config_cmd(
|
|
164
|
+
name: Annotated[str, typer.Argument(help="Config to add (behave-kit, behave-data).")],
|
|
165
|
+
) -> None:
|
|
166
|
+
"""Add an ecosystem config to pyproject.toml."""
|
|
167
|
+
code = run_add_config(name)
|
|
168
|
+
raise typer.Exit(code=code)
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
# --- Codegen ---------------------------------------------------------------
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
@app.command("from-openapi")
|
|
175
|
+
def from_openapi_cmd(
|
|
176
|
+
spec: Annotated[str, typer.Argument(help="Path to an OpenAPI 3.x spec.")],
|
|
177
|
+
out_dir: Annotated[str, typer.Option] = typer.Option(
|
|
178
|
+
"features", "--out-dir", help="Output directory for generated files."
|
|
179
|
+
),
|
|
180
|
+
step_lib: Annotated[str | None, typer.Option] = typer.Option(
|
|
181
|
+
None, "--step-lib", help="Step library to bind (e.g. http)."
|
|
182
|
+
),
|
|
183
|
+
tag: Annotated[str | None, typer.Option] = typer.Option(
|
|
184
|
+
None, "--tag", help="Tag generated scenarios."
|
|
185
|
+
),
|
|
186
|
+
include_path: Annotated[list[str], typer.Option] = typer.Option(
|
|
187
|
+
[], "--include-path", help="Restrict to these paths (repeatable)."
|
|
188
|
+
),
|
|
189
|
+
include_method: Annotated[list[str], typer.Option] = typer.Option(
|
|
190
|
+
[], "--include-method", help="Restrict to these HTTP methods (repeatable)."
|
|
191
|
+
),
|
|
192
|
+
) -> None:
|
|
193
|
+
"""Generate features and steps from an OpenAPI spec."""
|
|
194
|
+
options = FromOpenApiOptions(
|
|
195
|
+
spec=spec,
|
|
196
|
+
out_dir=out_dir,
|
|
197
|
+
step_lib=step_lib,
|
|
198
|
+
tag=tag,
|
|
199
|
+
include_paths=tuple(include_path),
|
|
200
|
+
include_methods=tuple(include_method),
|
|
201
|
+
)
|
|
202
|
+
code = run_from_openapi(options)
|
|
203
|
+
raise typer.Exit(code=code)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
@app.command("from-postman")
|
|
207
|
+
def from_postman_cmd(
|
|
208
|
+
collection: Annotated[str, typer.Argument(help="Path to a Postman collection JSON.")],
|
|
209
|
+
out_dir: Annotated[str, typer.Option] = typer.Option(
|
|
210
|
+
"features", "--out-dir", help="Output directory for generated files."
|
|
211
|
+
),
|
|
212
|
+
step_lib: Annotated[str | None, typer.Option] = typer.Option(
|
|
213
|
+
None, "--step-lib", help="Step library to bind (e.g. http)."
|
|
214
|
+
),
|
|
215
|
+
) -> None:
|
|
216
|
+
"""Generate features and steps from a Postman collection."""
|
|
217
|
+
options = FromPostmanOptions(collection=collection, out_dir=out_dir, step_lib=step_lib)
|
|
218
|
+
code = run_from_postman(options)
|
|
219
|
+
raise typer.Exit(code=code)
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
@app.command("from-swagger")
|
|
223
|
+
def from_swagger_cmd(
|
|
224
|
+
spec: Annotated[str, typer.Argument(help="Path to a Swagger 2.0 spec.")],
|
|
225
|
+
out_dir: Annotated[str, typer.Option] = typer.Option(
|
|
226
|
+
"features", "--out-dir", help="Output directory for generated files."
|
|
227
|
+
),
|
|
228
|
+
step_lib: Annotated[str | None, typer.Option] = typer.Option(
|
|
229
|
+
None, "--step-lib", help="Step library to bind (e.g. http)."
|
|
230
|
+
),
|
|
231
|
+
tag: Annotated[str | None, typer.Option] = typer.Option(
|
|
232
|
+
None, "--tag", help="Tag generated scenarios."
|
|
233
|
+
),
|
|
234
|
+
) -> None:
|
|
235
|
+
"""Generate features and steps from a Swagger 2.0 spec."""
|
|
236
|
+
options = FromSwaggerOptions(spec=spec, out_dir=out_dir, step_lib=step_lib, tag=tag)
|
|
237
|
+
code = run_from_swagger(options)
|
|
238
|
+
raise typer.Exit(code=code)
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
@app.command("migrate")
|
|
242
|
+
def migrate_cmd(
|
|
243
|
+
source_dir: Annotated[str, typer.Argument(help="Cucumber project to migrate.")],
|
|
244
|
+
out_dir: Annotated[str, typer.Option] = typer.Option(
|
|
245
|
+
".", "--out-dir", help="Output directory for the Behave project."
|
|
246
|
+
),
|
|
247
|
+
from_lang: Annotated[str, typer.Option] = typer.Option(
|
|
248
|
+
"java", "--from", help="Source language (java, ruby)."
|
|
249
|
+
),
|
|
250
|
+
) -> None:
|
|
251
|
+
"""Migrate a Cucumber project to Behave."""
|
|
252
|
+
options = MigrateOptions(source=source_dir, out_dir=out_dir)
|
|
253
|
+
code = run_migrate(options)
|
|
254
|
+
raise typer.Exit(code=code)
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
# --- Health and formatting -------------------------------------------------
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
@app.command("doctor")
|
|
261
|
+
def doctor_cmd() -> None:
|
|
262
|
+
"""Run behave-doctor diagnostics (alias for check)."""
|
|
263
|
+
code = run_check()
|
|
264
|
+
raise typer.Exit(code=code)
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
@app.command("lint")
|
|
268
|
+
def lint_cmd(
|
|
269
|
+
fix: Annotated[bool, typer.Option] = typer.Option(
|
|
270
|
+
False, "--fix", help="Apply fixes where possible."
|
|
271
|
+
),
|
|
272
|
+
) -> None:
|
|
273
|
+
"""Lint .feature files via behave-lint."""
|
|
274
|
+
code = run_lint(fix=fix)
|
|
275
|
+
raise typer.Exit(code=code)
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
@app.command("format")
|
|
279
|
+
def format_cmd(
|
|
280
|
+
check: Annotated[bool, typer.Option] = typer.Option(
|
|
281
|
+
False, "--check", help="Check formatting without writing."
|
|
282
|
+
),
|
|
283
|
+
) -> None:
|
|
284
|
+
"""Format .feature files via behave-format."""
|
|
285
|
+
code = run_format(check=check)
|
|
286
|
+
raise typer.Exit(code=code)
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
@app.command("check")
|
|
290
|
+
def check_cmd(
|
|
291
|
+
fmt: Annotated[str, typer.Option] = typer.Option(
|
|
292
|
+
"text", "--format", help="Output format: text or json."
|
|
293
|
+
),
|
|
294
|
+
) -> None:
|
|
295
|
+
"""Check project health via behave-doctor."""
|
|
296
|
+
code = run_check(fmt=fmt)
|
|
297
|
+
raise typer.Exit(code=code)
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
# --- Inspection -------------------------------------------------------------
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
@app.command("preview")
|
|
304
|
+
def preview_cmd(
|
|
305
|
+
feature: Annotated[str, typer.Argument(help="Path to a .feature file.")],
|
|
306
|
+
fmt: Annotated[str, typer.Option] = typer.Option(
|
|
307
|
+
"text", "--format", help="Output format: text or json."
|
|
308
|
+
),
|
|
309
|
+
) -> None:
|
|
310
|
+
"""Preview a feature file with resolved examples and tables."""
|
|
311
|
+
code = run_preview(feature)
|
|
312
|
+
raise typer.Exit(code=code)
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
@app.command("stats")
|
|
316
|
+
def stats_cmd(
|
|
317
|
+
fmt: Annotated[str, typer.Option] = typer.Option(
|
|
318
|
+
"text", "--format", help="Output format: text or json."
|
|
319
|
+
),
|
|
320
|
+
by_tag: Annotated[bool, typer.Option] = typer.Option(
|
|
321
|
+
False, "--by-tag", help="Break down stats by tag."
|
|
322
|
+
),
|
|
323
|
+
) -> None:
|
|
324
|
+
"""Report project statistics."""
|
|
325
|
+
_ = by_tag # reserved for future per-tag breakdown
|
|
326
|
+
code = run_stats(fmt=fmt)
|
|
327
|
+
raise typer.Exit(code=code)
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
# --- Update -----------------------------------------------------------------
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
@app.command("update")
|
|
334
|
+
def update_cmd(
|
|
335
|
+
from_openapi: Annotated[str | None, typer.Option] = typer.Option(
|
|
336
|
+
None, "--from-openapi", help="Re-apply an OpenAPI generator."
|
|
337
|
+
),
|
|
338
|
+
only_missing: Annotated[bool, typer.Option] = typer.Option(
|
|
339
|
+
False, "--only-missing", help="Add only missing features/steps."
|
|
340
|
+
),
|
|
341
|
+
force: Annotated[bool, typer.Option] = typer.Option(
|
|
342
|
+
False, "--force", help="Regenerate and back up changed files."
|
|
343
|
+
),
|
|
344
|
+
) -> None:
|
|
345
|
+
"""Re-apply generators to an existing project."""
|
|
346
|
+
options = UpdateOptions(force=force)
|
|
347
|
+
code = run_update(options)
|
|
348
|
+
raise typer.Exit(code=code)
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def run(argv: Sequence[str] | None = None) -> int:
|
|
352
|
+
"""Programmatic entry point used by tests and ``python -m behave_gen``."""
|
|
353
|
+
try:
|
|
354
|
+
app(args=list(argv) if argv is not None else None, standalone_mode=False)
|
|
355
|
+
except typer.Exit as exc:
|
|
356
|
+
return int(getattr(exc, "code", 1))
|
|
357
|
+
except SystemExit as exc: # pragma: no cover - defensive.
|
|
358
|
+
return int(exc.code) if isinstance(exc.code, int) else 1
|
|
359
|
+
return 0
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
if __name__ == "__main__":
|
|
363
|
+
sys.exit(run(sys.argv[1:]))
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Command implementations for behave-gen."""
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"""``behave-gen add`` command implementations.
|
|
2
|
+
|
|
3
|
+
Phase 5 implements ``add feature``. Other ``add`` subcommands are wired in
|
|
4
|
+
later phases.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import string
|
|
10
|
+
import sys
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
from importlib import resources
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
from behave_model import ParseError, parse_feature
|
|
16
|
+
|
|
17
|
+
_FEATURE_TEMPLATE_ROOT = "behave_gen.templates.features"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class AddError(Exception):
|
|
21
|
+
"""User-facing error raised by add commands."""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _normalize_tags(tags: str | None) -> str:
|
|
25
|
+
"""Normalize a tag string into a ``@tag1 @tag2\\n`` prefix line.
|
|
26
|
+
|
|
27
|
+
Accepts comma- or space-separated tags. Returns an empty string when no
|
|
28
|
+
tags are provided, so the template collapses to ``Feature: ...``.
|
|
29
|
+
"""
|
|
30
|
+
if not tags:
|
|
31
|
+
return ""
|
|
32
|
+
cleaned = tags.replace(",", " ")
|
|
33
|
+
parts = [p for p in cleaned.split() if p]
|
|
34
|
+
if not parts:
|
|
35
|
+
return ""
|
|
36
|
+
normalized = [p if p.startswith("@") else f"@{p}" for p in parts]
|
|
37
|
+
return " ".join(normalized) + "\n"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _feature_template_path(template: str) -> Path:
|
|
41
|
+
"""Return the on-disk path to a feature template."""
|
|
42
|
+
with resources.as_file(
|
|
43
|
+
resources.files(_FEATURE_TEMPLATE_ROOT).joinpath(f"{template}.feature")
|
|
44
|
+
) as p:
|
|
45
|
+
path = Path(p)
|
|
46
|
+
if not path.is_file():
|
|
47
|
+
raise AddError(
|
|
48
|
+
f"Unknown feature template {template!r}. Available templates: default, crud."
|
|
49
|
+
)
|
|
50
|
+
return path
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass(frozen=True, slots=True)
|
|
54
|
+
class AddFeatureOptions:
|
|
55
|
+
"""Options for ``add feature``."""
|
|
56
|
+
|
|
57
|
+
name: str
|
|
58
|
+
tags: str | None = None
|
|
59
|
+
template: str = "default"
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _humanize(name: str) -> str:
|
|
63
|
+
"""Turn a slug like ``user_login`` into ``User login``."""
|
|
64
|
+
return name.replace("_", " ").replace("-", " ").capitalize()
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def add_feature(
|
|
68
|
+
project_root: str | Path,
|
|
69
|
+
options: AddFeatureOptions,
|
|
70
|
+
*,
|
|
71
|
+
features_dir: str = "features",
|
|
72
|
+
) -> Path:
|
|
73
|
+
"""Generate a ``.feature`` file inside ``project_root``.
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
project_root: Root of the Behave project.
|
|
77
|
+
options: Add-feature options.
|
|
78
|
+
features_dir: Features directory relative to ``project_root``.
|
|
79
|
+
|
|
80
|
+
Returns:
|
|
81
|
+
The path to the generated feature file.
|
|
82
|
+
|
|
83
|
+
Raises:
|
|
84
|
+
AddError: If the project/features dir is missing, the template is
|
|
85
|
+
unknown, the name is invalid, or the generated file fails to parse.
|
|
86
|
+
"""
|
|
87
|
+
name = options.name.strip()
|
|
88
|
+
if not name or any(ch in name for ch in ("\\", ":", "*", "?", '"', "<", ">", "|", "/")):
|
|
89
|
+
raise AddError(f"Invalid feature name: {name!r}.")
|
|
90
|
+
|
|
91
|
+
root = Path(project_root).resolve()
|
|
92
|
+
if not root.is_dir():
|
|
93
|
+
raise AddError(f"Project root not found: {root}")
|
|
94
|
+
|
|
95
|
+
features = root / features_dir
|
|
96
|
+
features.mkdir(parents=True, exist_ok=True)
|
|
97
|
+
target = features / f"{name}.feature"
|
|
98
|
+
if target.exists():
|
|
99
|
+
raise AddError(f"Feature file already exists: {target}. Use a different name or remove it.")
|
|
100
|
+
|
|
101
|
+
template_path = _feature_template_path(options.template)
|
|
102
|
+
raw = template_path.read_text(encoding="utf-8")
|
|
103
|
+
context = {
|
|
104
|
+
"feature_name": _humanize(name),
|
|
105
|
+
"name": name,
|
|
106
|
+
"tags": _normalize_tags(options.tags),
|
|
107
|
+
}
|
|
108
|
+
try:
|
|
109
|
+
rendered = string.Template(raw).substitute(context)
|
|
110
|
+
except KeyError as exc:
|
|
111
|
+
raise AddError(f"Missing template variable ${exc.args[0]}.") from exc
|
|
112
|
+
|
|
113
|
+
# Validate the generated feature parses cleanly with behave-model.
|
|
114
|
+
try:
|
|
115
|
+
parse_feature(rendered, filename=str(target))
|
|
116
|
+
except ParseError as exc:
|
|
117
|
+
raise AddError(f"Generated feature failed to parse: {exc}") from exc
|
|
118
|
+
|
|
119
|
+
target.write_text(rendered, encoding="utf-8")
|
|
120
|
+
return target
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def run_add_feature(
|
|
124
|
+
options: AddFeatureOptions,
|
|
125
|
+
project_root: str | Path | None = None,
|
|
126
|
+
) -> int:
|
|
127
|
+
"""CLI entry point for ``behave-gen add feature``."""
|
|
128
|
+
root = Path(project_root) if project_root is not None else Path.cwd()
|
|
129
|
+
try:
|
|
130
|
+
path = add_feature(root, options)
|
|
131
|
+
except AddError as exc:
|
|
132
|
+
print(f"add feature: {exc}", file=sys.stderr)
|
|
133
|
+
return 1
|
|
134
|
+
print(f"Created feature at {path}")
|
|
135
|
+
return 0
|