8000 Add support for --dry-run flag (#16) · jrmfg/functions-framework-python@2a30643 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2a30643

Browse files
authored
Add support for --dry-run flag (GoogleCloudPlatform#16)
* Test call to app.run * Add support for --dry-run flag
1 parent f1bf503 commit 2a30643

File tree

3 files changed

+50
-18
lines changed

3 files changed

+50
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- Add support for `--dry-run` flag ([#14])
10+
811
### Changed
912
- Make `--debug` a flag instead of a boolean option
1013

@@ -27,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2730
[1.0.1]: https://github.com/GoogleCloudPlatform/functions-framework-python/releases/tag/v1.0.1
2831
[1.0.0]: https://github.com/GoogleCloudPlatform/functions-framework-python/releases/tag/v1.0.0
2932

33+
[#14]: https://github.com/GoogleCloudPlatform/functions-framework-python/pull/14
3034
[#12]: https://github.com/GoogleCloudPlatform/functions-framework-python/pull/12
3135
[#8]: https://github.com/GoogleCloudPlatform/functions-framework-python/pull/8
3236
[#7]: https://github.com/GoogleCloudPlatform/functions-framework-python/pull/7

src/functions_framework/cli.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@
3030
)
3131
@click.option("--port", envvar="PORT", type=click.INT, default=8080)
3232
@click.option("--debug", envvar="DEBUG", is_flag=True)
33-
def cli(target, source, signature_type, port, debug):
33+
@click.option("--dry-run", envvar="DRY_RUN", is_flag=True)
34+
def cli(target, source, signature_type, port, debug, dry_run):
3435
host = "0.0.0.0"
3536
app = create_app(target, source, signature_type)
36-
app.run(host, port, debug)
37+
if dry_run:
38+
click.echo("Function: {}".format(target))
39+
click.echo("URL: http://{}:{}/".format(host, port))
40+
click.echo("Dry run successful, shutting down.")
41+
else:
42+
app.run(host, port, debug)

tests/test_cli.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323

2424

2525
@pytest.fixture
26-
def create_app(monkeypatch):
27-
create_app = pretend.call_recorder(
28-
lambda *a, **kw: pretend.stub(run=pretend.call_recorder(lambda *a, **kw: None))
29-
)
26+
def run():
27+
return pretend.call_recorder(lambda *a, **kw: None)
28+
29+
30+
@pytest.fixture
31+
def create_app(monkeypatch, run):
32+
create_app = pretend.call_recorder(lambda *a, **kw: pretend.stub(run=run))
3033
monkeypatch.setattr(functions_framework.cli, "create_app", create_app)
3134
return create_app
3235

@@ -40,38 +43,57 @@ def test_cli_no_arguments():
4043

4144

4245
@pytest.mark.parametrize(
43-
"args, env, call",
46+
"args, env, create_app_calls, run_calls",
4447
[
45-
(["--target", "foo"], {}, pretend.call("foo", None, "http")),
46-
([], {"FUNCTION_TARGET": "foo"}, pretend.call("foo", None, "http")),
48+
(
49+
["--target", "foo"],
50+
{},
51+
[pretend.call("foo", None, "http")],
52+
[pretend.call("0.0.0.0", 8080, False)],
53+
),
54+
(
55+
[],
56+
{"FUNCTION_TARGET": "foo"},
57+
[pretend.call("foo", None, "http")],
58+
[pretend.call("0.0.0.0", 8080, False 8000 )],
59+
),
4760
(
4861
["--target", "foo", "--source", "/path/to/source.py"],
4962
{},
50-
pretend.call("foo", "/path/to/source.py", "http"),
63+
[pretend.call("foo", "/path/to/source.py", "http")],
64+
[pretend.call("0.0.0.0", 8080, False)],
5165
),
5266
(
5367
[],
5468
{"FUNCTION_TARGET": "foo", "FUNCTION_SOURCE": "/path/to/source.py"},
55-
pretend.call("foo", "/path/to/source.py", "http"),
69+
[pretend.call("foo", "/path/to/source.py", "http")],
70+
[pretend.call("0.0.0.0", 8080, False)],
5671
),
5772
(
5873
["--target", "foo", "--signature-type", "event"],
5974
{},
60-
pretend.call("foo", None, "event"),
75+
[pretend.call("foo", None, "event")],
76+
[pretend.call("0.0.0.0", 8080, False)],
6177
),
6278
(
6379
[],
6480
{"FUNCTION_TARGET": "foo", "FUNCTION_SIGNATURE_TYPE": "event"},
65-
pretend.call("foo", None, "event"),
81+
[pretend.call("foo", None, "event")],
82+
[pretend.call("0.0.0.0", 8080, False)],
83+
),
84+
(["--target", "foo", "--dry-run"], {}, [pretend.call("foo", None, "http")], []),
85+
(
86+
[],
87+
{"FUNCTION_TARGET": "foo", "DRY_RUN": "True"},
88+
[pretend.call("foo", None, "http")],
89+
[],
6690
),
6791
],
6892
)
69-
def test_cli_arguments(create_app, args, env, call):
93+
def test_cli_arguments(create_app, run, args, env, create_app_calls, run_calls):
7094
runner = CliRunner(env=env)
7195
result = runner.invoke(cli, args)
7296

73-
if result.output:
74-
print(result.output)
75-
7697
assert result.exit_code == 0
77-
assert create_app.calls == [call]
98+
assert create_app.calls == create_app_calls
99+
assert run.calls == run_calls

0 commit comments

Comments
 (0)
0