1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import os
16+ from unittest import mock
17+
1518import click
1619from click .testing import CliRunner
1720
@@ -32,6 +35,7 @@ def tearDown(self):
3235 super ().tearDown ()
3336 ClickInstrumentor ().uninstrument ()
3437
38+ @mock .patch ("sys.orig_argv" , ["command" ])
3539 def test_cli_command_wrapping (self ):
3640 @click .command ()
3741 def command ():
@@ -45,7 +49,17 @@ def command():
4549 self .assertEqual (span .status .status_code , StatusCode .UNSET )
4650 self .assertEqual (span .kind , SpanKind .INTERNAL )
4751 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" ])
4963 def test_cli_command_wrapping_with_name (self ):
5064 @click .command ("mycommand" )
5165 def renamedcommand ():
@@ -59,7 +73,44 @@ def renamedcommand():
5973 self .assertEqual (span .status .status_code , StatusCode .UNSET )
6074 self .assertEqual (span .kind , SpanKind .INTERNAL )
6175 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
6293
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" ])
63114 def test_cli_command_raises_error (self ):
64115 @click .command ()
65116 def command_raises ():
@@ -73,6 +124,16 @@ def command_raises():
73124 self .assertEqual (span .status .status_code , StatusCode .ERROR )
74125 self .assertEqual (span .kind , SpanKind .INTERNAL )
75126 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+ )
76137
77138 def test_uninstrument (self ):
78139 ClickInstrumentor ().uninstrument ()
0 commit comments