-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
test_json__subcommands.py
145 lines (114 loc) · 4.99 KB
/
test_json__subcommands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#-----------------------------------------------------------------------------
# Copyright (c) 2012 - 2024, Anaconda, Inc., and Bokeh Contributors.
# All rights reserved.
#
# The full license is in the file LICENSE.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Boilerplate
#-----------------------------------------------------------------------------
from __future__ import annotations # isort:skip
import pytest ; pytest
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# Standard library imports
import argparse
import os
# Bokeh imports
from bokeh.command.bootstrap import main
from bokeh.command.subcommand import Argument
from tests.support.util.filesystem import TmpDir, WorkingDir, with_directory_contents
from tests.support.util.types import Capture
from _util_subcommands import basic_scatter_script
# Module under test
import bokeh.command.subcommands.json as scjson # isort:skip
#-----------------------------------------------------------------------------
# Setup
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# General API
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Dev API
#-----------------------------------------------------------------------------
def test_create() -> None:
import argparse
from bokeh.command.subcommand import Subcommand
obj = scjson.JSON(parser=argparse.ArgumentParser())
assert isinstance(obj, Subcommand)
def test_name() -> None:
assert scjson.JSON.name == "json"
def test_help() -> None:
assert scjson.JSON.help == "Create JSON files for one or more applications"
def test_args() -> None:
assert scjson.JSON.args == (
('files', Argument(
metavar='DIRECTORY-OR-SCRIPT',
nargs='+',
help="The app directories or scripts to generate JSON for",
default=None,
)),
('--indent', Argument(
metavar='LEVEL',
type=int,
help="indentation to use when printing",
default=None,
)),
(('-o', '--output'), Argument(
metavar='FILENAME',
action='append',
type=str,
help="Name of the output file or - for standard output.",
)),
('--args', Argument(
metavar='COMMAND-LINE-ARGS',
nargs=argparse.REMAINDER,
help="Any command line arguments remaining are passed on to the application handler",
)),
)
def test_no_script(capsys: Capture) -> None:
with TmpDir(prefix="bokeh-json-no-script") as dirname:
with WorkingDir(dirname):
with pytest.raises(SystemExit):
main(["bokeh", "json"])
out, err = capsys.readouterr()
too_few = "the following arguments are required: DIRECTORY-OR-SCRIPT"
assert err == f"""usage: bokeh json [-h] [--indent LEVEL] [-o FILENAME] [--args ...]
DIRECTORY-OR-SCRIPT [DIRECTORY-OR-SCRIPT ...]
bokeh json: error: {too_few}
"""
assert out == ""
def test_basic_script(capsys: Capture) -> None:
def run(dirname: str) -> None:
with WorkingDir(dirname):
main(["bokeh", "json", "scatter.py"])
out, err = capsys.readouterr()
assert err == ""
assert out == ""
assert {"scatter.json", "scatter.py"} == set(os.listdir(dirname))
with_directory_contents({'scatter.py': basic_scatter_script}, run)
def test_basic_script_with_output_after(capsys: Capture) -> None:
def run(dirname: str) -> None:
with WorkingDir(dirname):
main(["bokeh", "json", "scatter.py", "--output", "foo.json"])
out, err = capsys.readouterr()
assert err == ""
assert out == ""
assert {"foo.json", "scatter.py"} == set(os.listdir(dirname))
with_directory_contents({'scatter.py': basic_scatter_script}, run)
def test_basic_script_with_output_before(capsys: Capture) -> None:
def run(dirname: str) -> None:
with WorkingDir(dirname):
main(["bokeh", "json", "--output", "foo.json", "scatter.py"])
out, err = capsys.readouterr()
assert err == ""
assert out == ""
assert {"foo.json", "scatter.py"} == set(os.listdir(dirname))
with_directory_contents({'scatter.py': basic_scatter_script}, run)
#-----------------------------------------------------------------------------
# Private API
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------