164 lines · python
1"""2Test the session save feature3"""4import os5import tempfile6 7import lldb8from lldbsuite.test.decorators import *9from lldbsuite.test.lldbtest import *10from lldbsuite.test import lldbutil11 12 13class SessionSaveTestCase(TestBase):14 def raw_transcript_builder(self, cmd, res):15 raw = "(lldb) " + cmd + "\n"16 if res.GetOutputSize():17 raw += res.GetOutput()18 if res.GetErrorSize():19 raw += res.GetError()20 return raw21 22 @no_debug_info_test23 def test_session_save(self):24 raw = ""25 interpreter = self.dbg.GetCommandInterpreter()26 27 # Make sure "save-transcript" is on, so that all the following setings28 # and commands are saved into the trasncript. Note that this cannot be29 # a part of the `settings`, because this command itself won't be saved30 # into the transcript.31 self.runCmd("settings set interpreter.save-transcript true")32 33 settings = [34 "settings set interpreter.echo-commands true",35 "settings set interpreter.echo-comment-commands true",36 "settings set interpreter.stop-command-source-on-error false",37 "settings set interpreter.open-transcript-in-editor false",38 ]39 40 for setting in settings:41 interpreter.HandleCommand(setting, lldb.SBCommandReturnObject())42 43 inputs = [44 "# This is a comment", # Comment45 "help session", # Valid command46 "Lorem ipsum", # Invalid command47 ]48 49 for cmd in inputs:50 res = lldb.SBCommandReturnObject()51 interpreter.HandleCommand(cmd, res)52 raw += self.raw_transcript_builder(cmd, res)53 54 self.assertTrue(interpreter.HasCommands())55 self.assertNotEqual(len(raw), 0)56 57 # Check for error58 cmd = "session save /root/file"59 interpreter.HandleCommand(cmd, res)60 self.assertFalse(res.Succeeded())61 raw += self.raw_transcript_builder(cmd, res)62 63 output_file = self.getBuildArtifact('my-session')64 65 res = lldb.SBCommandReturnObject()66 interpreter.HandleCommand("session save " + output_file, res)67 self.assertTrue(res.Succeeded())68 raw += self.raw_transcript_builder(cmd, res)69 70 with open(output_file, "r") as file:71 content = file.read()72 # Exclude last line, since session won't record it's own output73 lines = raw.splitlines()[:-1]74 for line in lines:75 self.assertIn(line, content)76 77 td = tempfile.TemporaryDirectory()78 res = lldb.SBCommandReturnObject()79 interpreter.HandleCommand(80 "settings set interpreter.save-session-directory " + td.name, res81 )82 self.assertTrue(res.Succeeded())83 84 res = lldb.SBCommandReturnObject()85 interpreter.HandleCommand("session save", res)86 self.assertTrue(res.Succeeded())87 raw += self.raw_transcript_builder(cmd, res)88 # Also check that we don't print an error message about an empty transcript.89 self.assertNotIn("interpreter.save-transcript is set to false", res.GetError())90 91 with open(os.path.join(td.name, os.listdir(td.name)[0]), "r") as file:92 content = file.read()93 # Exclude last line, since session won't record it's own output94 lines = raw.splitlines()[:-1]95 for line in lines:96 self.assertIn(line, content)97 98 @no_debug_info_test99 def test_session_save_no_transcript_warning(self):100 interpreter = self.dbg.GetCommandInterpreter()101 102 self.runCmd("settings set interpreter.save-transcript false")103 104 # These commands won't be saved, so are arbitrary.105 commands = [106 "settings set interpreter.open-transcript-in-editor false",107 "p 1",108 "settings set interpreter.save-session-on-quit true",109 "fr v",110 "settings set interpreter.echo-comment-commands true",111 ]112 113 for command in commands:114 res = lldb.SBCommandReturnObject()115 interpreter.HandleCommand(command, res)116 117 output_file = self.getBuildArtifact("my-session")118 119 res = lldb.SBCommandReturnObject()120 interpreter.HandleCommand("session save " + output_file, res)121 self.assertTrue(res.Succeeded())122 # We should warn about the setting being false.123 self.assertIn("interpreter.save-transcript is set to false", res.GetError())124 self.assertTrue(125 os.path.getsize(output_file) == 0,126 "Output file should be empty since we didn't save the transcript.",127 )128 129 @no_debug_info_test130 def test_session_save_on_quit(self):131 raw = ""132 interpreter = self.dbg.GetCommandInterpreter()133 134 # Make sure "save-transcript" is on, so that all the following setings135 # and commands are saved into the trasncript. Note that this cannot be136 # a part of the `settings`, because this command itself won't be saved137 # into the transcript.138 self.runCmd("settings set interpreter.save-transcript true")139 140 td = tempfile.TemporaryDirectory()141 142 settings = [143 "settings set interpreter.echo-commands true",144 "settings set interpreter.echo-comment-commands true",145 "settings set interpreter.stop-command-source-on-error false",146 "settings set interpreter.save-session-on-quit true",147 "settings set interpreter.save-session-directory " + td.name,148 "settings set interpreter.open-transcript-in-editor false",149 ]150 151 for setting in settings:152 res = lldb.SBCommandReturnObject()153 interpreter.HandleCommand(setting, res)154 raw += self.raw_transcript_builder(setting, res)155 156 self.dbg.Destroy(self.dbg)157 158 with open(os.path.join(td.name, os.listdir(td.name)[0]), "r") as file:159 content = file.read()160 # Exclude last line, since session won't record it's own output161 lines = raw.splitlines()[:-1]162 for line in lines:163 self.assertIn(line, content)164