Page Menu
Home
Software Heritage
Search
Configure Global Search
Log In
Files
F8395233
test_context.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Subscribers
None
test_context.py
View Options
# -*- coding: utf-8 -*-
import
click
def
test_ensure_context_objects
(
runner
):
class
Foo
(
object
):
def
__init__
(
self
):
self
.
title
=
'default'
pass_foo
=
click
.
make_pass_decorator
(
Foo
,
ensure
=
True
)
@click.group
()
@pass_foo
def
cli
(
foo
):
pass
@cli.command
()
@pass_foo
def
test
(
foo
):
click
.
echo
(
foo
.
title
)
result
=
runner
.
invoke
(
cli
,
[
'test'
])
assert
not
result
.
exception
assert
result
.
output
==
'default
\n
'
def
test_get_context_objects
(
runner
):
class
Foo
(
object
):
def
__init__
(
self
):
self
.
title
=
'default'
pass_foo
=
click
.
make_pass_decorator
(
Foo
,
ensure
=
True
)
@click.group
()
@click.pass_context
def
cli
(
ctx
):
ctx
.
obj
=
Foo
()
ctx
.
obj
.
title
=
'test'
@cli.command
()
@pass_foo
def
test
(
foo
):
click
.
echo
(
foo
.
title
)
result
=
runner
.
invoke
(
cli
,
[
'test'
])
assert
not
result
.
exception
assert
result
.
output
==
'test
\n
'
def
test_get_context_objects_no_ensuring
(
runner
):
class
Foo
(
object
):
def
__init__
(
self
):
self
.
title
=
'default'
pass_foo
=
click
.
make_pass_decorator
(
Foo
)
@click.group
()
@click.pass_context
def
cli
(
ctx
):
ctx
.
obj
=
Foo
()
ctx
.
obj
.
title
=
'test'
@cli.command
()
@pass_foo
def
test
(
foo
):
click
.
echo
(
foo
.
title
)
result
=
runner
.
invoke
(
cli
,
[
'test'
])
assert
not
result
.
exception
assert
result
.
output
==
'test
\n
'
def
test_get_context_objects_missing
(
runner
):
class
Foo
(
object
):
pass
pass_foo
=
click
.
make_pass_decorator
(
Foo
)
@click.group
()
@click.pass_context
def
cli
(
ctx
):
pass
@cli.command
()
@pass_foo
def
test
(
foo
):
click
.
echo
(
foo
.
title
)
result
=
runner
.
invoke
(
cli
,
[
'test'
])
assert
result
.
exception
is
not
None
assert
isinstance
(
result
.
exception
,
RuntimeError
)
assert
"Managed to invoke callback without a context object "
\
"of type 'Foo' existing"
in
str
(
result
.
exception
)
def
test_multi_enter
(
runner
):
called
=
[]
@click.command
()
@click.pass_context
def
cli
(
ctx
):
def
callback
():
called
.
append
(
True
)
ctx
.
call_on_close
(
callback
)
with
ctx
:
pass
assert
not
called
result
=
runner
.
invoke
(
cli
,
[])
assert
result
.
exception
is
None
assert
called
==
[
True
]
File Metadata
Details
Attached
Mime Type
text/x-python
Expires
Jun 4 2025, 7:36 PM (9 w, 6 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3373839
Attached To
R193 python3-click
Event Timeline
Log In to Comment