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.orig_argv" , ["command" ])
35
39
def test_cli_command_wrapping (self ):
36
40
@click .command ()
37
41
def command ():
@@ -45,7 +49,17 @@ 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" )
48
-
52
+ self .assertEqual (
53
+ dict (span .attributes ),
54
+ {
55
+ "process.executable.name" : "command" ,
56
+ "process.command_args" : ("command" ,),
57
+ "process.exit.code" : 0 ,
58
+ "process.pid" : os .getpid (),
59
+ },
60
+ )
61
+
62
+ @mock .patch ("sys.orig_argv" , ["mycommand" ])
49
63
def test_cli_command_wrapping_with_name (self ):
50
64
@click .command ("mycommand" )
51
65
def renamedcommand ():
@@ -59,7 +73,44 @@ def renamedcommand():
59
73
self .assertEqual (span .status .status_code , StatusCode .UNSET )
60
74
self .assertEqual (span .kind , SpanKind .INTERNAL )
61
75
self .assertEqual (span .name , "mycommand" )
76
+ self .assertEqual (
77
+ dict (span .attributes ),
78
+ {
79
+ "process.executable.name" : "mycommand" ,
80
+ "process.command_args" : ("mycommand" ,),
81
+ "process.exit.code" : 0 ,
82
+ "process.pid" : os .getpid (),
83
+ },
84
+ )
85
+
86
+ @mock .patch ("sys.orig_argv" , ["command" , "--opt" , "argument" ])
87
+ def test_cli_command_wrapping_with_options (self ):
88
+ @click .command ()
89
+ @click .argument ("argument" )
90
+ @click .option ("--opt/--no-opt" , default = False )
91
+ def command (argument , opt ):
92
+ pass
62
93
94
+ argv = ["command" , "--opt" , "argument" ]
95
+ runner = CliRunner ()
96
+ result = runner .invoke (command , argv [1 :])
97
+ self .assertEqual (result .exit_code , 0 )
98
+
99
+ (span ,) = self .memory_exporter .get_finished_spans ()
100
+ self .assertEqual (span .status .status_code , StatusCode .UNSET )
101
+ self .assertEqual (span .kind , SpanKind .INTERNAL )
102
+ self .assertEqual (span .name , "command" )
103
+ self .assertEqual (
104
+ dict (span .attributes ),
105
+ {
106
+ "process.executable.name" : "command" ,
107
+ "process.command_args" : tuple (argv ),
108
+ "process.exit.code" : 0 ,
109
+ "process.pid" : os .getpid (),
110
+ },
111
+ )
112
+
113
+ @mock .patch ("sys.orig_argv" , ["command-raises" ])
63
114
def test_cli_command_raises_error (self ):
64
115
@click .command ()
65
116
def command_raises ():
@@ -73,6 +124,16 @@ def command_raises():
73
124
self .assertEqual (span .status .status_code , StatusCode .ERROR )
74
125
self .assertEqual (span .kind , SpanKind .INTERNAL )
75
126
self .assertEqual (span .name , "command-raises" )
127
+ self .assertEqual (
128
+ dict (span .attributes ),
129
+ {
130
+ "process.executable.name" : "command-raises" ,
131
+ "process.command_args" : ("command-raises" ,),
132
+ "process.exit.code" : 1 ,
133
+ "process.pid" : os .getpid (),
134
+ "error.type" : "ValueError" ,
135
+ },
136
+ )
76
137
77
138
def test_uninstrument (self ):
78
139
ClickInstrumentor ().uninstrument ()
0 commit comments