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.argv" , ["command.py" ])
3539 def test_cli_command_wrapping (self ):
3640 @click .command ()
3741 def command ():
@@ -45,7 +49,44 @@ def command():
4549 self .assertEqual (span .status .status_code , StatusCode .UNSET )
4650 self .assertEqual (span .kind , SpanKind .INTERNAL )
4751 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
4867
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" ])
4990 def test_cli_command_wrapping_with_name (self ):
5091 @click .command ("mycommand" )
5192 def renamedcommand ():
@@ -59,7 +100,44 @@ def renamedcommand():
59100 self .assertEqual (span .status .status_code , StatusCode .UNSET )
60101 self .assertEqual (span .kind , SpanKind .INTERNAL )
61102 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 )
62125
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" ])
63141 def test_cli_command_raises_error (self ):
64142 @click .command ()
65143 def command_raises ():
@@ -73,6 +151,16 @@ def command_raises():
73151 self .assertEqual (span .status .status_code , StatusCode .ERROR )
74152 self .assertEqual (span .kind , SpanKind .INTERNAL )
75153 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+ )
76164
77165 def test_uninstrument (self ):
78166 ClickInstrumentor ().uninstrument ()
0 commit comments