Use save_vars to replace existing manipulation of globals in tests
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / gdbinit-history.exp
1 # Copyright 2015 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 # This file is part of the gdb testsuite.
17
18 # Test the setting of "history size" via $HOME/.gdbinit
19
20
21 # Check that the history size is properly set to SIZE when reading the .gdbinit
22 # file located in HOME with the environment variable GDBHISTSIZE optionally
23 # set to GDBHISTSIZE_VAL.
24
25 proc test_gdbinit_history_setting { home size { gdbhistsize_val "-" } } {
26 global env
27 global INTERNAL_GDBFLAGS
28 global srcdir
29 global subdir
30
31 save_vars { INTERNAL_GDBFLAGS env(GDBHISTSIZE) env(HOME) } {
32 set env(HOME) "$srcdir/$subdir/$home"
33
34 # The GDBHISTSIZE environment variable takes precedence over whatever
35 # history size is set in .gdbinit. Make sure the former is not
36 # set.
37 unset -nocomplain env(GDBHISTSIZE)
38
39 if { $gdbhistsize_val != "-" } {
40 set env(GDBHISTSIZE) $gdbhistsize_val
41 }
42
43 set INTERNAL_GDBFLAGS [string map {"-nx" ""} $INTERNAL_GDBFLAGS]
44
45 set prefix "home=$home"
46 if { $gdbhistsize_val != "-" } {
47 append prefix " gdbhistsize=$gdbhistsize_val"
48 }
49
50 with_test_prefix $prefix {
51 gdb_exit
52 gdb_start
53
54 gdb_test "show history size" "The size of the command history is $size."
55
56 if { $size == "0" } {
57 gdb_test_no_output "show commands"
58 } elseif { $size != "1" } {
59 gdb_test "show commands" " . show history size\r\n . show commands"
60 }
61 }
62 }
63 }
64
65 # Check that the history file does not get truncated to zero when a gdbinit
66 # file sets the history size to unlimited.
67
68 proc test_no_truncation_of_unlimited_history_file { } {
69 global env
70 global INTERNAL_GDBFLAGS
71
72 save_vars { INTERNAL_GDBFLAGS env(GDBHISTSIZE) } {
73 # The GDBHISTSIZE environment variable takes precedence over whatever
74 # history size is set in .gdbinit. Make sure the former is not
75 # set.
76 unset -nocomplain env(GDBHISTSIZE)
77
78 set temp_gdbinit [standard_output_file "gdbinit-history.gdbinit"]
79 set temp_histfile [standard_output_file "gdbinit-history.gdb_history"]
80 file delete $temp_gdbinit
81 file delete $temp_histfile
82
83 set fd [open $temp_gdbinit "w"]
84 puts $fd "set history size unlimited\n"
85 puts $fd "set history filename $temp_histfile\n"
86 puts $fd "set history save\n"
87 close $fd
88
89 append INTERNAL_GDBFLAGS " -x $temp_gdbinit"
90
91 # We have to start then exit GDB twice: the first time to test the creation
92 # of the initial history file, and the second time to test appending to it.
93 # In either case the initial "print 1" command should persist through the
94 # history file.
95 with_test_prefix "truncation" {
96 gdb_exit
97 gdb_start
98 gdb_test "print 1"
99
100 with_test_prefix "creating" {
101 gdb_exit
102 gdb_start
103 gdb_test "server show commands" " . print 1.*"
104 }
105
106 with_test_prefix "appending" {
107 gdb_exit
108 gdb_start
109 gdb_test "server show commands" " . print 1.*"
110 }
111 }
112 }
113 }
114
115 test_gdbinit_history_setting "gdbinit-history/unlimited" "unlimited"
116 test_gdbinit_history_setting "gdbinit-history/zero" "0"
117
118 test_no_truncation_of_unlimited_history_file
119
120 # A valid GDBHISTSIZE value overrides the setting inside the .gdbinit file; an
121 # invalid GDBHISTSIZE value is ignored, falling back on the setting inside the
122 # .gdbinit file.
123 test_gdbinit_history_setting "gdbinit-history/unlimited" "1000" "1000"
124 test_gdbinit_history_setting "gdbinit-history/unlimited" "unlimited" "foo"
This page took 0.035664 seconds and 5 git commands to generate.