Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
Test as a Service Client
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
INS Courses
AutPy
Test as a Service Client
Commits
455b2d40
Commit
455b2d40
authored
2 years ago
by
Florian Bruhin
Browse files
Options
Downloads
Patches
Plain Diff
tests: Refactor to use custom ExpectedConsole
parent
11626d41
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/test_cli.py
+57
-50
57 additions, 50 deletions
tests/test_cli.py
with
57 additions
and
50 deletions
tests/test_cli.py
+
57
−
50
View file @
455b2d40
import
os
import
io
import
textwrap
import
pathlib
import
datetime
from
typing
import
cast
import
rich.console
import
rich.panel
import
pytest
import
typer.testing
import
click.testing
from
pytest_httpx
import
HTTPXMock
from
pytest_golden.plugin
import
GoldenTestFixture
from
typer.testing
import
CliRunner
from
time_machine
import
TimeMachineFixture
from
taas_client
import
cli
runner
=
CliRunner
()
runner
=
typer
.
testing
.
CliRunner
()
FROZEN_TIME
=
datetime
.
datetime
(
2022
,
10
,
12
,
13
,
37
).
astimezone
()
class
CaptureConsole
(
rich
.
console
.
Console
):
class
ExpectedConsole
:
"""
Object to store expected prints.
This is used to make testing rich output easier.
"""
def
__init__
(
self
):
super
().
__init__
(
file
=
io
.
StringIO
())
self
.
_file
=
io
.
StringIO
()
self
.
_console
=
rich
.
console
.
Console
(
file
=
self
.
_file
)
self
.
exit_code
:
int
|
None
=
None
def
getvalue
(
self
)
->
str
:
f
=
cast
(
io
.
StringIO
,
self
.
file
)
return
f
.
getvalue
()
def
check
(
self
,
result
:
click
.
testing
.
Result
)
->
None
:
print
(
result
.
output
)
assert
result
.
exit_code
==
self
.
exit_code
assert
result
.
output
==
self
.
_file
.
getvalue
()
def
error_panel
(
self
,
text
:
str
)
->
rich
.
panel
.
Pa
ne
l
:
return
rich
.
panel
.
Panel
(
text
,
title
=
"
Error
"
,
title_align
=
"
left
"
)
def
error_panel
(
self
,
text
:
str
)
->
No
ne
:
self
.
_console
.
print
(
rich
.
panel
.
Panel
(
text
,
title
=
"
Error
"
,
title_align
=
"
left
"
)
)
def
usage_lines
(
self
)
->
str
:
return
(
"
Usage: upload-file [OPTIONS] NOTEBOOK [URL]
\n
"
"
Try
'
upload-file --help
'
for help.
"
)
def
line
(
self
,
text
:
str
)
->
None
:
self
.
_console
.
print
(
text
)
def
usage_lines
(
self
)
->
None
:
self
.
_console
.
print
(
"
Usage: upload-file [OPTIONS] NOTEBOOK [URL]
"
)
self
.
_console
.
print
(
"
Try
'
upload-file --help
'
for help.
"
)
@pytest.fixture
def
console
()
->
Capture
Console
:
return
Capture
Console
()
def
expected
()
->
Expected
Console
:
return
Expected
Console
()
class
TestArgumentParsing
:
def
test_no_args
(
self
,
console
:
Capture
Console
)
->
None
:
def
test_no_args
(
self
,
expected
:
Expected
Console
)
->
None
:
result
=
runner
.
invoke
(
cli
.
app
,
[])
print
(
result
.
stdout
)
console
.
print
(
console
.
usage_lines
())
console
.
print
(
console
.
error_panel
(
"
Missing argument
'
NOTEBOOK
'
.
"
))
expected
.
usage_lines
()
expected
.
error_panel
(
"
Missing argument
'
NOTEBOOK
'
.
"
)
expected
.
exit_code
=
2
assert
result
.
exit_code
==
2
assert
result
.
stdout
==
console
.
getvalue
()
expected
.
check
(
result
)
def
test_inexistent_file
(
self
,
tmp_path
:
pathlib
.
Path
,
console
:
CaptureConsole
)
->
None
:
def
test_inexistent_file
(
self
,
tmp_path
:
pathlib
.
Path
,
expected
:
ExpectedConsole
)
->
None
:
nb_path
=
tmp_path
/
"
nb.ipynb
"
result
=
runner
.
invoke
(
cli
.
app
,
[
str
(
nb_path
)])
print
(
result
.
stdout
)
msg
=
f
"
Invalid value for
'
NOTEBOOK
'
: File
'
{
nb_path
}
'
does not exist.
"
console
.
print
(
console
.
usage_lines
())
console
.
print
(
console
.
error_panel
(
msg
))
expected
.
usage_lines
()
expected
.
error_panel
(
msg
)
expected
.
exit_code
=
2
assert
result
.
exit_code
==
2
assert
result
.
stdout
==
console
.
getvalue
()
expected
.
check
(
result
)
def
test_dir
(
self
,
tmp_path
:
pathlib
.
Path
,
console
:
Capture
Console
)
->
None
:
def
test_dir
(
self
,
tmp_path
:
pathlib
.
Path
,
expected
:
Expected
Console
)
->
None
:
nb_path
=
tmp_path
/
"
nb.ipynb
"
nb_path
.
mkdir
()
result
=
runner
.
invoke
(
cli
.
app
,
[
str
(
nb_path
)])
print
(
result
.
stdout
)
msg
=
f
"
Invalid value for
'
NOTEBOOK
'
: File
'
{
nb_path
}
'
is a directory.
"
console
.
print
(
console
.
usage_lines
())
console
.
print
(
console
.
error_panel
(
msg
))
expected
.
usage_lines
()
expected
.
error_panel
(
msg
)
expected
.
exit_code
=
2
assert
result
.
exit_code
==
2
assert
result
.
stdout
==
console
.
getvalue
()
expected
.
check
(
result
)
def
test_unreadable
(
self
,
tmp_path
:
pathlib
.
Path
,
console
:
CaptureConsole
)
->
None
:
def
test_unreadable
(
self
,
tmp_path
:
pathlib
.
Path
,
expected
:
ExpectedConsole
)
->
None
:
nb_path
=
tmp_path
/
"
nb.ipynb
"
nb_path
.
touch
()
nb_path
.
chmod
(
0
)
...
...
@@ -90,30 +99,28 @@ class TestArgumentParsing:
pytest
.
skip
(
"
failed to make file unreadable
"
)
# pragma: no cover
result
=
runner
.
invoke
(
cli
.
app
,
[
str
(
nb_path
)])
print
(
result
.
stdout
)
msg
=
f
"
Invalid value for
'
NOTEBOOK
'
: File
'
{
nb_path
}
'
is not readable.
"
console
.
print
(
console
.
usage_lines
())
console
.
print
(
console
.
error_panel
(
msg
))
expected
.
usage_lines
()
expected
.
error_panel
(
msg
)
expected
.
exit_code
=
2
assert
result
.
exit_code
==
2
assert
result
.
stdout
==
console
.
getvalue
()
expected
.
check
(
result
)
def
test_wrong_location
(
tmp_path
:
pathlib
.
Path
)
->
None
:
def
test_wrong_location
(
tmp_path
:
pathlib
.
Path
,
expected
:
ExpectedConsole
)
->
None
:
nb_path
=
tmp_path
/
"
nb.ipynb
"
nb_path
.
touch
()
result
=
runner
.
invoke
(
cli
.
app
,
[
str
(
nb_path
)])
print
(
result
.
stdout
)
expected
=
f
"""
Refusing to submit file from unexpected location:
{
nb_path
}
Make sure you
'
re not accidentally working in the
'
Originale
'
folder!
"""
expected
.
line
(
"
Refusing to submit file from unexpected location:
"
)
expected
.
line
(
str
(
nb_path
))
expected
.
line
(
"
Make sure you
'
re not accidentally working in the
'
Originale
'
folder!
"
)
expected
.
exit_code
=
1
assert
result
.
exit_code
==
1
assert
result
.
stdout
==
textwrap
.
dedent
(
expected
).
lstrip
(
"
\n
"
)
expected
.
check
(
result
)
@pytest.fixture
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment