133 changes: 122 additions & 11 deletions lldb/test/API/commands/settings/TestSettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,15 @@ def test_disassembler_settings(self):

@skipIfDarwinEmbedded # <rdar://problem/34446098> debugserver on ios etc can't write files
def test_run_args_and_env_vars(self):
self.do_test_run_args_and_env_vars(use_launchsimple=False)

@skipIfDarwinEmbedded # <rdar://problem/34446098> debugserver on ios etc can't write files
def test_launchsimple_args_and_env_vars(self):
self.do_test_run_args_and_env_vars(use_launchsimple=True)

def do_test_run_args_and_env_vars(self, use_launchsimple):
"""Test that run-args and env-vars are passed to the launched process."""
self.build()
exe = self.getBuildArtifact("a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

# Set the run-args and the env-vars.
# And add hooks to restore the settings during tearDown().
Expand All @@ -218,7 +223,29 @@ def test_run_args_and_env_vars(self):
self.addTearDownHook(
lambda: self.runCmd("settings clear target.env-vars"))

self.runCmd("process launch --working-dir '{0}'".format(self.get_process_working_directory()),
exe = self.getBuildArtifact("a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

target = self.dbg.GetTargetAtIndex(0)
launch_info = target.GetLaunchInfo()
found_env_var = False
for i in range(0, launch_info.GetNumEnvironmentEntries()):
if launch_info.GetEnvironmentEntryAtIndex(i) == "MY_ENV_VAR=YES":
found_env_var = True
break
self.assertTrue(found_env_var,
"MY_ENV_VAR was not set in LunchInfo object")

self.expect(
'target show-launch-environment',
substrs=["MY_ENV_VAR=YES"])

wd = self.get_process_working_directory()
if use_launchsimple:
process = target.LaunchSimple(None, None, wd)
self.assertTrue(process)
else:
self.runCmd("process launch --working-dir '{0}'".format(wd),
RUN_SUCCEEDED)

# Read the output file produced by running the program.
Expand All @@ -233,11 +260,46 @@ def test_run_args_and_env_vars(self):
"argv[3] matches",
"Environment variable 'MY_ENV_VAR' successfully passed."])

# Check that env-vars overrides unset-env-vars.
self.runCmd('settings set target.unset-env-vars MY_ENV_VAR')

self.expect(
'target show-launch-environment',
'env-vars overrides unset-env-vars',
substrs=["MY_ENV_VAR=YES"])

wd = self.get_process_working_directory()
if use_launchsimple:
process = target.LaunchSimple(None, None, wd)
self.assertTrue(process)
else:
self.runCmd("process launch --working-dir '{0}'".format(wd),
RUN_SUCCEEDED)

# Read the output file produced by running the program.
output = lldbutil.read_file_from_process_wd(self, "output2.txt")

self.expect(
output,
exe=False,
substrs=[
"Environment variable 'MY_ENV_VAR' successfully passed."])

@skipIfRemote # it doesn't make sense to send host env to remote target
def test_pass_host_env_vars(self):
"""Test that the host env vars are passed to the launched process."""
self.build()

# Set some host environment variables now.
os.environ["MY_HOST_ENV_VAR1"] = "VAR1"
os.environ["MY_HOST_ENV_VAR2"] = "VAR2"

# This is the function to unset the two env variables set above.
def unset_env_variables():
os.environ.pop("MY_HOST_ENV_VAR1")
os.environ.pop("MY_HOST_ENV_VAR2")
self.addTearDownHook(unset_env_variables)

exe = self.getBuildArtifact("a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

Expand All @@ -247,16 +309,33 @@ def test_pass_host_env_vars(self):
"Default inherit-env is 'true'",
startstr="target.inherit-env (boolean) = true")

# Set some host environment variables now.
os.environ["MY_HOST_ENV_VAR1"] = "VAR1"
os.environ["MY_HOST_ENV_VAR2"] = "VAR2"
self.expect(
'target show-launch-environment',
'Host environment is passed correctly',
substrs=['MY_HOST_ENV_VAR1=VAR1', 'MY_HOST_ENV_VAR2=VAR2'])
self.runCmd("process launch --working-dir '{0}'".format(self.get_process_working_directory()),
RUN_SUCCEEDED)

# This is the function to unset the two env variables set above.
def unset_env_variables():
os.environ.pop("MY_HOST_ENV_VAR1")
os.environ.pop("MY_HOST_ENV_VAR2")
# Read the output file produced by running the program.
output = lldbutil.read_file_from_process_wd(self, "output1.txt")

self.expect(
output,
exe=False,
substrs=[
"The host environment variable 'MY_HOST_ENV_VAR1' successfully passed.",
"The host environment variable 'MY_HOST_ENV_VAR2' successfully passed."])

# Now test that we can prevent the inferior from inheriting the
# environment.
self.runCmd('settings set target.inherit-env false')

self.expect(
'target show-launch-environment',
'target.inherit-env affects `target show-launch-environment`',
matching=False,
substrs = ['MY_HOST_ENV_VAR1=VAR1', 'MY_HOST_ENV_VAR2=VAR2'])

self.addTearDownHook(unset_env_variables)
self.runCmd("process launch --working-dir '{0}'".format(self.get_process_working_directory()),
RUN_SUCCEEDED)

Expand All @@ -266,10 +345,42 @@ def unset_env_variables():
self.expect(
output,
exe=False,
matching=False,
substrs=[
"The host environment variable 'MY_HOST_ENV_VAR1' successfully passed.",
"The host environment variable 'MY_HOST_ENV_VAR2' successfully passed."])

# Now test that we can unset variables from the inherited environment.
self.runCmd('settings set target.inherit-env true')
self.runCmd('settings set target.unset-env-vars MY_HOST_ENV_VAR1')
self.runCmd("process launch --working-dir '{0}'".format(self.get_process_working_directory()),
RUN_SUCCEEDED)

# Read the output file produced by running the program.
output = lldbutil.read_file_from_process_wd(self, "output1.txt")

self.expect(
'target show-launch-environment',
'MY_HOST_ENV_VAR1 is unset, it shouldn\'t be in `target show-launch-environment`',
matching=False,
substrs = ['MY_HOST_ENV_VAR1=VAR1'])
self.expect(
'target show-launch-environment',
'MY_HOST_ENV_VAR2 shouldn be in `target show-launch-environment`',
substrs = ['MY_HOST_ENV_VAR2=VAR2'])

self.expect(
output,
exe=False,
matching=False,
substrs=[
"The host environment variable 'MY_HOST_ENV_VAR1' successfully passed."])
self.expect(
output,
exe=False,
substrs=[
"The host environment variable 'MY_HOST_ENV_VAR2' successfully passed."])

@skipIfDarwinEmbedded # <rdar://problem/34446098> debugserver on ios etc can't write files
def test_set_error_output_path(self):
"""Test that setting target.error/output-path for the launched process works."""
Expand Down