blob: 68d21588765db06c06e10e6cdb4b4eba2d24cfa0 [file] [log] [blame]
Johnny Chen77377772010-09-07 17:06:13 +00001"""
2Test lldb settings command.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9
10class SettingsCommandTestCase(TestBase):
11
12 mydir = "settings"
13
Johnny Chen1a9f4dd2010-09-16 01:53:04 +000014 @classmethod
15 def classCleanup(cls):
Johnny Chena91b9472010-10-22 21:06:04 +000016 """Cleanup the test byproducts."""
Johnny Chena245f932010-12-14 23:43:29 +000017 system(["/bin/sh", "-c", "rm -f output1.txt"])
18 system(["/bin/sh", "-c", "rm -f output2.txt"])
Johnny Chen93b0c8b2011-01-25 17:39:43 +000019 system(["/bin/sh", "-c", "rm -f stderr.txt"])
Johnny Chen138532a2010-10-08 00:47:30 +000020 system(["/bin/sh", "-c", "rm -f stdout.txt"])
Johnny Chen1a9f4dd2010-09-16 01:53:04 +000021
Johnny Chen77377772010-09-07 17:06:13 +000022 def test_set_prompt(self):
23 """Test that 'set prompt' actually changes the prompt."""
Johnny Chen881c3712010-09-07 17:12:10 +000024
Johnny Chen23cb3712010-09-27 17:36:59 +000025 # Set prompt to 'lldb2'.
Johnny Chen622220b2010-12-20 21:29:34 +000026 self.runCmd("settings set prompt lldb2")
Johnny Chen881c3712010-09-07 17:12:10 +000027
28 # Immediately test the setting.
Johnny Chena913ea42010-10-19 19:39:20 +000029 self.expect("settings show prompt", SETTING_MSG("prompt"),
Johnny Chen77377772010-09-07 17:06:13 +000030 startstr = "prompt (string) = 'lldb2'")
Johnny Chen881c3712010-09-07 17:12:10 +000031
32 # The overall display should also reflect the new setting.
Johnny Chena913ea42010-10-19 19:39:20 +000033 self.expect("settings show", SETTING_MSG("prompt"),
Johnny Chen77377772010-09-07 17:06:13 +000034 substrs = ["prompt (string) = 'lldb2'"])
35
Johnny Chen23cb3712010-09-27 17:36:59 +000036 # Use '-r' option to reset to the original default prompt.
37 self.runCmd("settings set -r prompt")
38
Johnny Chena37764b2010-09-07 17:31:05 +000039 def test_set_term_width(self):
40 """Test that 'set term-width' actually changes the term-width."""
41
Johnny Chena37764b2010-09-07 17:31:05 +000042 self.runCmd("settings set term-width 70")
43
44 # Immediately test the setting.
Johnny Chena913ea42010-10-19 19:39:20 +000045 self.expect("settings show term-width", SETTING_MSG("term-width"),
Johnny Chena37764b2010-09-07 17:31:05 +000046 startstr = "term-width (int) = '70'")
47
48 # The overall display should also reflect the new setting.
Johnny Chena913ea42010-10-19 19:39:20 +000049 self.expect("settings show", SETTING_MSG("term-width"),
Johnny Chen138532a2010-10-08 00:47:30 +000050 substrs = ["term-width (int) = '70'"])
Johnny Chena37764b2010-09-07 17:31:05 +000051
Johnny Chen3e9c50c2010-10-18 17:51:45 +000052 def test_set_auto_confirm(self):
53 """Test that after 'set auto-confirm true', manual confirmation should not kick in."""
54 self.buildDefault()
55
56 exe = os.path.join(os.getcwd(), "a.out")
57 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
58
Johnny Chen3e9c50c2010-10-18 17:51:45 +000059 self.runCmd("settings set auto-confirm true")
60
61 # Immediately test the setting.
Johnny Chena913ea42010-10-19 19:39:20 +000062 self.expect("settings show auto-confirm", SETTING_MSG("auto-confirm"),
Johnny Chen3e9c50c2010-10-18 17:51:45 +000063 startstr = "auto-confirm (boolean) = 'true'")
64
65 # Now 'breakpoint delete' should just work fine without confirmation
66 # prompt from the command interpreter.
67 self.runCmd("breakpoint set -n main")
68 self.expect("breakpoint delete",
69 startstr = "All breakpoints removed")
70
71 # Restore the original setting of auto-confirm.
72 self.runCmd("settings set -r auto-confirm")
Johnny Chena913ea42010-10-19 19:39:20 +000073 self.expect("settings show auto-confirm", SETTING_MSG("auto-confirm"),
Johnny Chen3e9c50c2010-10-18 17:51:45 +000074 startstr = "auto-confirm (boolean) = 'false'")
75
Johnny Chend5e111c2010-09-15 22:27:29 +000076 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Johnny Chena245f932010-12-14 23:43:29 +000077 def test_run_args_and_env_vars_with_dsym(self):
Johnny Chend5e111c2010-09-15 22:27:29 +000078 """Test that run-args and env-vars are passed to the launched process."""
79 self.buildDsym()
80 self.pass_run_args_and_env_vars()
81
Johnny Chena245f932010-12-14 23:43:29 +000082 def test_run_args_and_env_vars_with_dwarf(self):
Johnny Chend5e111c2010-09-15 22:27:29 +000083 """Test that run-args and env-vars are passed to the launched process."""
84 self.buildDwarf()
85 self.pass_run_args_and_env_vars()
86
87 def pass_run_args_and_env_vars(self):
88 """Test that run-args and env-vars are passed to the launched process."""
89 exe = os.path.join(os.getcwd(), "a.out")
90 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
91
92 # Set the run-args and the env-vars.
Johnny Chen707d8222010-10-19 23:40:13 +000093 # And add hooks to restore the settings during tearDown().
Johnny Chenf48b6452010-12-14 23:47:55 +000094 self.runCmd('settings set target.process.run-args A B C')
Johnny Chen707d8222010-10-19 23:40:13 +000095 self.addTearDownHook(
96 lambda: self.runCmd("settings set -r target.process.run-args"))
Johnny Chenf48b6452010-12-14 23:47:55 +000097 self.runCmd('settings set target.process.env-vars ["MY_ENV_VAR"]=YES')
Johnny Chen707d8222010-10-19 23:40:13 +000098 self.addTearDownHook(
99 lambda: self.runCmd("settings set -r target.process.env-vars"))
Johnny Chend5e111c2010-09-15 22:27:29 +0000100
101 self.runCmd("run", RUN_SUCCEEDED)
102
103 # Read the output file produced by running the program.
Johnny Chena245f932010-12-14 23:43:29 +0000104 with open('output2.txt', 'r') as f:
Johnny Chen277c8f02010-10-08 22:10:42 +0000105 output = f.read()
Johnny Chend5e111c2010-09-15 22:27:29 +0000106
Johnny Chenbe7da212010-10-08 20:01:03 +0000107 self.expect(output, exe=False,
108 substrs = ["argv[1] matches",
109 "argv[2] matches",
110 "argv[3] matches",
111 "Environment variable 'MY_ENV_VAR' successfully passed."])
Johnny Chend5e111c2010-09-15 22:27:29 +0000112
Johnny Chen80554b82010-12-04 00:44:56 +0000113 def test_pass_host_env_vars(self):
114 """Test that the host env vars are passed to the launched process."""
115 self.buildDefault()
116
117 exe = os.path.join(os.getcwd(), "a.out")
118 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
119
120 # By default, inherit-env is 'true'.
121 self.expect('settings show target.process.inherit-env', "Default inherit-env is 'true'",
122 startstr = "target.process.inherit-env (boolean) = 'true'")
123
124 # Set some host environment variables now.
125 os.environ["MY_HOST_ENV_VAR1"] = "VAR1"
126 os.environ["MY_HOST_ENV_VAR2"] = "VAR2"
127
Johnny Chena245f932010-12-14 23:43:29 +0000128 # This is the function to unset the two env variables set above.
129 def unset_env_variables():
130 os.environ.pop("MY_HOST_ENV_VAR1")
131 os.environ.pop("MY_HOST_ENV_VAR2")
132
133 self.addTearDownHook(unset_env_variables)
Johnny Chen80554b82010-12-04 00:44:56 +0000134 self.runCmd("run", RUN_SUCCEEDED)
135
136 # Read the output file produced by running the program.
Johnny Chena245f932010-12-14 23:43:29 +0000137 with open('output1.txt', 'r') as f:
Johnny Chen80554b82010-12-04 00:44:56 +0000138 output = f.read()
139
140 self.expect(output, exe=False,
141 substrs = ["The host environment variable 'MY_HOST_ENV_VAR1' successfully passed.",
142 "The host environment variable 'MY_HOST_ENV_VAR2' successfully passed."])
143
Johnny Chen93b0c8b2011-01-25 17:39:43 +0000144 def test_set_error_output_path(self):
145 """Test that setting target.process.error/output-path for the launched process works."""
Johnny Chen2fcc0e52010-09-16 18:26:06 +0000146 self.buildDefault()
147
148 exe = os.path.join(os.getcwd(), "a.out")
149 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
150
Johnny Chen93b0c8b2011-01-25 17:39:43 +0000151 # Set the error-path and output-path and verify both are set.
152 self.runCmd("settings set target.process.error-path stderr.txt")
153 self.runCmd("settings set target.process.output-path stdout.txt")
154 # And add hooks to restore the original settings during tearDown().
Johnny Chen707d8222010-10-19 23:40:13 +0000155 self.addTearDownHook(
156 lambda: self.runCmd("settings set -r target.process.output-path"))
Johnny Chen93b0c8b2011-01-25 17:39:43 +0000157 self.addTearDownHook(
158 lambda: self.runCmd("settings set -r target.process.error-path"))
159
160 self.expect("settings show target.process.error-path",
161 SETTING_MSG("target.process.error-path"),
162 startstr = "target.process.error-path (string) = 'stderr.txt'")
Johnny Chen707d8222010-10-19 23:40:13 +0000163
Caroline Ticedaccaa92010-09-20 20:44:43 +0000164 self.expect("settings show target.process.output-path",
Johnny Chen3343f042010-10-19 19:11:38 +0000165 SETTING_MSG("target.process.output-path"),
Caroline Ticedaccaa92010-09-20 20:44:43 +0000166 startstr = "target.process.output-path (string) = 'stdout.txt'")
Johnny Chen2fcc0e52010-09-16 18:26:06 +0000167
168 self.runCmd("run", RUN_SUCCEEDED)
169
Johnny Chen93b0c8b2011-01-25 17:39:43 +0000170 # The 'stderr.txt' file should now exist.
171 self.assertTrue(os.path.isfile("stderr.txt"),
172 "'stderr.txt' exists due to target.process.error-path.")
173
174 # Read the output file produced by running the program.
175 with open('stderr.txt', 'r') as f:
176 output = f.read()
177
178 self.expect(output, exe=False,
179 startstr = "This message should go to standard error.")
180
Johnny Chen3e9c50c2010-10-18 17:51:45 +0000181 # The 'stdout.txt' file should now exist.
182 self.assertTrue(os.path.isfile("stdout.txt"),
183 "'stdout.txt' exists due to target.process.output-path.")
184
Johnny Chen2fcc0e52010-09-16 18:26:06 +0000185 # Read the output file produced by running the program.
Johnny Chenbe7da212010-10-08 20:01:03 +0000186 with open('stdout.txt', 'r') as f:
187 output = f.read()
Johnny Chen2fcc0e52010-09-16 18:26:06 +0000188
Johnny Chenbe7da212010-10-08 20:01:03 +0000189 self.expect(output, exe=False,
190 startstr = "This message should go to standard out.")
Johnny Chen2fcc0e52010-09-16 18:26:06 +0000191
Caroline Ticec9c235e2011-01-31 18:18:54 +0000192 def test_print_dictionary_setting(self):
193 self.runCmd ("settings set -r target.process.env-vars")
194 self.runCmd ("settings set target.process.env-vars [\"MY_VAR\"]=some-value")
195 self.expect ("settings show target.process.env-vars",
196 substrs = [ "'MY_VAR=some-value'" ])
197 self.runCmd ("settings set -r target.process.env-vars")
198
199 def test_print_array_setting(self):
200 self.runCmd ("settings set -r target.process.run-args")
201 self.runCmd ("settings set target.process.run-args gobbledy-gook")
202 self.expect ("settings show target.process.run-args",
203 substrs = [ "[0]: 'gobbledy-gook'" ])
204 self.runCmd ("settings set -r target.process.run-args")
205
206 def test_settings_with_quotes (self):
207 self.runCmd ("settings set -r target.process.run-args")
208 self.runCmd ("settings set target.process.run-args a b c")
209 self.expect ("settings show target.process.run-args",
210 substrs = [ "[0]: 'a'",
211 "[1]: 'b'",
212 "[2]: 'c'" ])
213 self.runCmd ("settings set target.process.run-args 'a b c'")
214 self.expect ("settings show target.process.run-args",
215 substrs = [ "[0]: 'a b c'" ])
216 self.runCmd ("settings set -r target.process.run-args")
217 self.runCmd ("settings set -r target.process.env-vars")
218 self.runCmd ("settings set target.process.env-vars [\"MY_FILE\"]='this is a file name with spaces.txt'")
219 self.expect ("settings show target.process.env-vars",
220 substrs = [ "'MY_FILE=this is a file name with spaces.txt'" ])
221 self.runCmd ("settings set -r target.process.env-vars")
222
Johnny Chen77377772010-09-07 17:06:13 +0000223
224if __name__ == '__main__':
225 import atexit
226 lldb.SBDebugger.Initialize()
227 atexit.register(lambda: lldb.SBDebugger.Terminate())
228 unittest2.main()