12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ import os
16
+ from unittest import mock
17
+
15
18
import click
16
19
from click .testing import CliRunner
17
20
@@ -32,6 +35,7 @@ def tearDown(self):
32
35
super ().tearDown ()
33
36
ClickInstrumentor ().uninstrument ()
34
37
38
+ @mock .patch ("sys.argv" , ["command.py" ])
35
39
def test_cli_command_wrapping (self ):
36
40
@click .command ()
37
41
def command ():
@@ -45,7 +49,44 @@ def command():
45
49
self .assertEqual (span .status .status_code , StatusCode .UNSET )
46
50
self .assertEqual (span .kind , SpanKind .INTERNAL )
47
51
self .assertEqual (span .name , "command" )
52
+ self .assertEqual (
53
+ dict (span .attributes ),
54
+ {
55
+ "process.executable.name" : "command.py" ,
56
+ "process.command_args" : ("command.py" ,),
57
+ "process.exit.code" : 0 ,
58
+ "process.pid" : os .getpid (),
59
+ },
60
+ )
61
+
62
+ @mock .patch ("sys.argv" , ["flask" , "command" ])
63
+ def test_flask_run_command_wrapping (self ):
64
+ @click .command ()
65
+ def command ():
66
+ pass
48
67
68
+ runner = CliRunner ()
69
+ result = runner .invoke (command )
70
+ self .assertEqual (result .exit_code , 0 )
71
+
72
+ (span ,) = self .memory_exporter .get_finished_spans ()
73
+ self .assertEqual (span .status .status_code , StatusCode .UNSET )
74
+ self .assertEqual (span .kind , SpanKind .INTERNAL )
75
+ self .assertEqual (span .name , "command" )
76
+ self .assertEqual (
77
+ dict (span .attributes ),
78
+ {
79
+ "process.executable.name" : "flask" ,
80
+ "process.command_args" : (
81
+ "flask" ,
82
+ "command" ,
83
+ ),
84
+ "process.exit.code" : 0 ,
85
+ "process.pid" : os .getpid (),
86
+ },
87
+ )
88
+
89
+ @mock .patch ("sys.argv" , ["command.py" ])
49
90
def test_cli_command_wrapping_with_name (self ):
50
91
@click .command ("mycommand" )
51
92
def renamedcommand ():
@@ -59,7 +100,44 @@ def renamedcommand():
59
100
self .assertEqual (span .status .status_code , StatusCode .UNSET )
60
101
self .assertEqual (span .kind , SpanKind .INTERNAL )
61
102
self .assertEqual (span .name , "mycommand" )
103
+ self .assertEqual (
104
+ dict (span .attributes ),
105
+ {
106
+ "process.executable.name" : "command.py" ,
107
+ "process.command_args" : ("command.py" ,),
108
+ "process.exit.code" : 0 ,
109
+ "process.pid" : os .getpid (),
110
+ },
111
+ )
112
+
113
+ @mock .patch ("sys.argv" , ["command.py" , "--opt" , "argument" ])
114
+ def test_cli_command_wrapping_with_options (self ):
115
+ @click .command ()
116
+ @click .argument ("argument" )
117
+ @click .option ("--opt/--no-opt" , default = False )
118
+ def command (argument , opt ):
119
+ pass
120
+
121
+ argv = ["command.py" , "--opt" , "argument" ]
122
+ runner = CliRunner ()
123
+ result = runner .invoke (command , argv [1 :])
124
+ self .assertEqual (result .exit_code , 0 )
62
125
126
+ (span ,) = self .memory_exporter .get_finished_spans ()
127
+ self .assertEqual (span .status .status_code , StatusCode .UNSET )
128
+ self .assertEqual (span .kind , SpanKind .INTERNAL )
129
+ self .assertEqual (span .name , "command" )
130
+ self .assertEqual (
131
+ dict (span .attributes ),
132
+ {
133
+ "process.executable.name" : "command.py" ,
134
+ "process.command_args" : tuple (argv ),
135
+ "process.exit.code" : 0 ,
136
+ "process.pid" : os .getpid (),
137
+ },
138
+ )
139
+
140
+ @mock .patch ("sys.argv" , ["command-raises.py" ])
63
141
def test_cli_command_raises_error (self ):
64
142
@click .command ()
65
143
def command_raises ():
@@ -73,6 +151,16 @@ def command_raises():
73
151
self .assertEqual (span .status .status_code , StatusCode .ERROR )
74
152
self .assertEqual (span .kind , SpanKind .INTERNAL )
75
153
self .assertEqual (span .name , "command-raises" )
154
+ self .assertEqual (
155
+ dict (span .attributes ),
156
+ {
157
+ "process.executable.name" : "command-raises.py" ,
158
+ "process.command_args" : ("command-raises.py" ,),
159
+ "process.exit.code" : 1 ,
160
+ "process.pid" : os .getpid (),
161
+ "error.type" : "ValueError" ,
162
+ },
163
+ )
76
164
77
165
def test_uninstrument (self ):
78
166
ClickInstrumentor ().uninstrument ()
0 commit comments