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
15    mydir = TestBase.compute_mydir(__file__)
16
17    def raw_transcript_builder(self, cmd, res):
18        raw = "(lldb) " + cmd + "\n"
19        if res.GetOutputSize():
20          raw += res.GetOutput()
21        if res.GetErrorSize():
22          raw += res.GetError()
23        return raw
24
25
26    @skipIfWindows
27    @no_debug_info_test
28    def test_session_save(self):
29        raw = ""
30        interpreter = self.dbg.GetCommandInterpreter()
31
32        settings = [
33          'settings set interpreter.echo-commands true',
34          'settings set interpreter.echo-comment-commands true',
35          'settings set interpreter.stop-command-source-on-error false'
36        ]
37
38        for setting in settings:
39          interpreter.HandleCommand(setting, lldb.SBCommandReturnObject())
40
41        inputs = [
42          '# This is a comment',  # Comment
43          'help session',         # Valid command
44          'Lorem ipsum'           # Invalid command
45        ]
46
47        for cmd in inputs:
48          res = lldb.SBCommandReturnObject()
49          interpreter.HandleCommand(cmd, res)
50          raw += self.raw_transcript_builder(cmd, res)
51
52        self.assertTrue(interpreter.HasCommands())
53        self.assertTrue(len(raw) != 0)
54
55        # Check for error
56        cmd = 'session save /root/file'
57        interpreter.HandleCommand(cmd, res)
58        self.assertFalse(res.Succeeded())
59        raw += self.raw_transcript_builder(cmd, res)
60
61        tf = tempfile.NamedTemporaryFile()
62        output_file = tf.name
63
64        res = lldb.SBCommandReturnObject()
65        interpreter.HandleCommand('session save ' + output_file, res)
66        self.assertTrue(res.Succeeded())
67        raw += self.raw_transcript_builder(cmd, res)
68
69        with open(output_file, "r") as file:
70          content = file.read()
71          # Exclude last line, since session won't record it's own output
72          lines = raw.splitlines()[:-1]
73          for line in lines:
74            self.assertIn(line, content)
75
76        td = tempfile.TemporaryDirectory()
77        res = lldb.SBCommandReturnObject()
78        interpreter.HandleCommand('settings set interpreter.save-session-directory ' + td.name, res)
79        self.assertTrue(res.Succeeded())
80
81        res = lldb.SBCommandReturnObject()
82        interpreter.HandleCommand('session save', res)
83        self.assertTrue(res.Succeeded())
84        raw += self.raw_transcript_builder(cmd, res)
85
86        with open(os.path.join(td.name, os.listdir(td.name)[0]), "r") as file:
87          content = file.read()
88          # Exclude last line, since session won't record it's own output
89          lines = raw.splitlines()[:-1]
90          for line in lines:
91            self.assertIn(line, content)
92
93    @skipIfWindows
94    @no_debug_info_test
95    def test_session_save_on_quit(self):
96        raw = ""
97        interpreter = self.dbg.GetCommandInterpreter()
98
99        td = tempfile.TemporaryDirectory()
100
101        settings = [
102          'settings set interpreter.echo-commands true',
103          'settings set interpreter.echo-comment-commands true',
104          'settings set interpreter.stop-command-source-on-error false',
105          'settings set interpreter.save-session-on-quit true',
106          'settings set interpreter.save-session-directory ' + td.name,
107        ]
108
109        for setting in settings:
110          res = lldb.SBCommandReturnObject()
111          interpreter.HandleCommand(setting, res)
112          raw += self.raw_transcript_builder(setting, res)
113
114        self.dbg.Destroy(self.dbg)
115
116        with open(os.path.join(td.name, os.listdir(td.name)[0]), "r") as file:
117          content = file.read()
118          # Exclude last line, since session won't record it's own output
119          lines = raw.splitlines()[:-1]
120          for line in lines:
121            self.assertIn(line, content)
122
123
124
125