f0a735b3f449ed7ea210bd185a0a7e587234cada
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-startup-opt.exp
1 # Copyright 2021 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 # Test the flags within GDB that can be used to control how Python is
17 # initialized.
18
19 gdb_start
20
21 # Skip all tests if Python scripting is not enabled.
22 if { [skip_python_tests] } { continue }
23
24 gdb_exit
25
26 # Return a list containing two directory paths for newly created home
27 # directories.
28 #
29 # The first directory is a HOME style home directory, it contains a
30 # .gdbearlyinit file containing CONTENT.
31 #
32 # The second directory is an XDG_CONFIG_HOME style home directory, it
33 # contains a sub-directory gdb/, inside which is a file gdbearlyinit
34 # that also contains CONTENT.
35 #
36 # The PREFIX is used in both directory names and should be unique for
37 # each call to this function.
38 proc setup_home_directories { prefix content } {
39 set home_dir [standard_output_file "${prefix}-home"]
40 set xdg_home_dir [standard_output_file "${prefix}-xdg"]
41
42 file mkdir $home_dir
43 file mkdir "$xdg_home_dir/gdb"
44
45 # Write the content into the HOME directory.
46 set fd [open "$home_dir/.gdbearlyinit" w]
47 puts $fd $content
48 close $fd
49
50 # Copy this from the HOME directory into the XDG_CONFIG_HOME
51 # directory.
52 file copy -force "$home_dir/.gdbearlyinit" "$xdg_home_dir/gdb/gdbearlyinit"
53
54 return [list $home_dir $xdg_home_dir]
55 }
56
57 # Start GDB and check the status of the Python system flags that we
58 # can control from within GDB.
59 proc test_python_settings { exp_state } {
60 gdb_start
61
62 gdb_test_no_output "python import sys"
63
64 foreach_with_prefix attr {ignore_environment dont_write_bytecode} {
65
66 # If we are checking 'dont_write_bytecode', and we are
67 # expecting this attribute to be 'off', then, if the user has
68 # PYTHONDONTWRITEBYTECODE set in their environment, the result
69 # will be 'on' instead of 'off', so override the expected
70 # result here.
71 #
72 # The reason for this is, 'set python dont-write-bytecode' by
73 # default is set to 'auto', which means, so long as 'set
74 # python ignore-environment' is 'off', GDB will check for the
75 # above environment variable.
76 #
77 # We could unset the environment variable, but until Python
78 # 3.8 there was no way to control where .pyc files are placed,
79 # and it feels bad to cause .pyc files to be created within
80 # the users filesystem when they clearly don't want them.
81 #
82 # And so, we adjust the expected results. Hopefully, between
83 # all GDB developers some will test GDB with this environment
84 # variable unset.
85 if { $attr == "dont_write_bytecode" \
86 && $exp_state == "off"
87 && [info exists ::env(PYTHONDONTWRITEBYTECODE)] } {
88 set answer "on"
89 } else {
90 set answer $exp_state
91 }
92
93 gdb_test_multiline "testname" \
94 "python" "" \
95 "if hasattr(sys, 'flags') and getattr(sys.flags, '${attr}', False):" "" \
96 " print (\"${attr} is on\")" "" \
97 "else:" "" \
98 " print (\"${attr} is off\")" "" \
99 "end" "${attr} is ${answer}"
100 }
101
102 gdb_exit
103 }
104
105 save_vars { env(TERM) } {
106 # We need an ANSI-capable terminal to get the output.
107 setenv TERM ansi
108
109 # Check the features are off by default.
110 test_python_settings "off"
111
112 # Create an empty directory we can use as HOME for some of the
113 # tests below. When we set XDG_CONFIG_HOME we still need to point
114 # HOME at something otherwise GDB complains that it doesn't know
115 # where to create the index cache.
116 set empty_home_dir [standard_output_file fake-empty-home]
117
118 # Create two directories to use for the style setting test.
119 set dirs [setup_home_directories "style" \
120 [multi_line_input \
121 "set python dont-write-bytecode on" \
122 "set python ignore-environment on"]]
123 set home_dir [lindex $dirs 0]
124 set xdg_home_dir [lindex $dirs 1]
125
126 # Now arrange to use the fake home directory early init file.
127 save_vars { INTERNAL_GDBFLAGS env(HOME) env(XDG_CONFIG_HOME) } {
128 set INTERNAL_GDBFLAGS [string map {"-nx" ""} $INTERNAL_GDBFLAGS]
129
130 with_test_prefix "using HOME config" {
131 # Now test GDB when using the HOME directory.
132 set env(HOME) $home_dir
133 unset -nocomplain env(XDG_CONFIG_HOME)
134 test_python_settings "on"
135 }
136
137 with_test_prefix "using XDG_CONFIG_HOME config" {
138 # Now test using the XDG_CONFIG_HOME folder. We still need to
139 # have a HOME directory set otherwise GDB will issue an error
140 # about not knowing where to place the index cache.
141 set env(XDG_CONFIG_HOME) $xdg_home_dir
142 set env(HOME) $empty_home_dir
143 test_python_settings "on"
144 }
145 }
146 }
This page took 0.033449 seconds and 3 git commands to generate.