1""" 2Test the session save feature 3""" 4import os 5import tempfile 6 7import lldb 8from lldbsuite.test.decorators import * 9from lldbsuite.test.lldbtest import * 10from lldbsuite.test import lldbutil 11 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 raw 21 22 @no_debug_info_test 23 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 setings 28 # and commands are saved into the trasncript. Note that this cannot be 29 # a part of the `settings`, because this command itself won't be saved 30 # 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", # Comment 45 "help session", # Valid command 46 "Lorem ipsum", # Invalid command 47 ] 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 error 58 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 output 73 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, res 81 ) 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 output 94 lines = raw.splitlines()[:-1] 95 for line in lines: 96 self.assertIn(line, content) 97 98 @no_debug_info_test 99 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_test 130 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 setings 135 # and commands are saved into the trasncript. Note that this cannot be 136 # a part of the `settings`, because this command itself won't be saved 137 # 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 output 161 lines = raw.splitlines()[:-1] 162 for line in lines: 163 self.assertIn(line, content) 164