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    @skipIfReproducer
28    @no_debug_info_test
29    def test_session_save(self):
30        raw = ""
31        interpreter = self.dbg.GetCommandInterpreter()
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        ]
38
39        for setting in settings:
40          interpreter.HandleCommand(setting, lldb.SBCommandReturnObject())
41
42        inputs = [
43          '# This is a comment',  # Comment
44          'help session',         # Valid command
45          'Lorem ipsum'           # Invalid command
46        ]
47
48        for cmd in inputs:
49          res = lldb.SBCommandReturnObject()
50          interpreter.HandleCommand(cmd, res)
51          raw += self.raw_transcript_builder(cmd, res)
52
53        self.assertTrue(interpreter.HasCommands())
54        self.assertTrue(len(raw) != 0)
55
56        # Check for error
57        cmd = 'session save /root/file'
58        interpreter.HandleCommand(cmd, res)
59        self.assertFalse(res.Succeeded())
60        raw += self.raw_transcript_builder(cmd, res)
61
62        tf = tempfile.NamedTemporaryFile()
63        output_file = tf.name
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('settings set interpreter.save-session-directory ' + td.name, res)
80        self.assertTrue(res.Succeeded())
81
82        res = lldb.SBCommandReturnObject()
83        interpreter.HandleCommand('session save', res)
84        self.assertTrue(res.Succeeded())
85        raw += self.raw_transcript_builder(cmd, res)
86
87        with open(os.path.join(td.name, os.listdir(td.name)[0]), "r") as file:
88          content = file.read()
89          # Exclude last line, since session won't record it's own output
90          lines = raw.splitlines()[:-1]
91          for line in lines:
92            self.assertIn(line, content)
93
94    @skipIfWindows
95    @skipIfReproducer
96    @no_debug_info_test
97    def test_session_save_on_quit(self):
98        raw = ""
99        interpreter = self.dbg.GetCommandInterpreter()
100
101        td = tempfile.TemporaryDirectory()
102
103        settings = [
104          'settings set interpreter.echo-commands true',
105          'settings set interpreter.echo-comment-commands true',
106          'settings set interpreter.stop-command-source-on-error false',
107          'settings set interpreter.save-session-on-quit true',
108          'settings set interpreter.save-session-directory ' + td.name,
109        ]
110
111        for setting in settings:
112          res = lldb.SBCommandReturnObject()
113          interpreter.HandleCommand(setting, res)
114          raw += self.raw_transcript_builder(setting, res)
115
116        self.dbg.Destroy(self.dbg)
117
118        with open(os.path.join(td.name, os.listdir(td.name)[0]), "r") as file:
119          content = file.read()
120          # Exclude last line, since session won't record it's own output
121          lines = raw.splitlines()[:-1]
122          for line in lines:
123            self.assertIn(line, content)
124
125
126
127