File: TestVSCode_runInTerminal.py

package info (click to toggle)
llvm-toolchain-14 1%3A14.0.6-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,496,180 kB
  • sloc: cpp: 5,593,972; ansic: 986,872; asm: 585,869; python: 184,223; objc: 72,530; lisp: 31,119; f90: 27,793; javascript: 9,780; pascal: 9,762; sh: 9,482; perl: 7,468; ml: 5,432; awk: 3,523; makefile: 2,538; xml: 953; cs: 573; fortran: 567
file content (186 lines) | stat: -rw-r--r-- 6,777 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""
Test lldb-vscode runInTerminal reverse request
"""


import unittest2
import vscode
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
import lldbvscode_testcase
import time
import os
import subprocess
import shutil
import json
from threading import Thread


class TestVSCode_runInTerminal(lldbvscode_testcase.VSCodeTestCaseBase):

    mydir = TestBase.compute_mydir(__file__)

    def readPidMessage(self, fifo_file):
        with open(fifo_file, "r") as file:
            self.assertIn("pid", file.readline())

    def sendDidAttachMessage(self, fifo_file):
        with open(fifo_file, "w") as file:
            file.write(json.dumps({"kind": "didAttach"}) + "\n")

    def readErrorMessage(self, fifo_file):
        with open(fifo_file, "r") as file:
            return file.readline()

    def isTestSupported(self):
        # For some strange reason, this test fails on python3.6
        if not (sys.version_info.major == 3 and sys.version_info.minor >= 7):
            return False
        try:
            # We skip this test for debug builds because it takes too long parsing lldb's own
            # debug info. Release builds are fine.
            # Checking the size of the lldb-vscode binary seems to be a decent proxy for a quick
            # detection. It should be far less than 1 MB in Release builds.
            if os.path.getsize(os.environ["LLDBVSCODE_EXEC"]) < 1000000:
                return True
        except:
            return False

    @skipIfWindows
    @skipIfRemote
    @skipIf(archs=no_match(['x86_64']))
    def test_runInTerminal(self):
        if not self.isTestSupported():
            return
        '''
            Tests the "runInTerminal" reverse request. It makes sure that the IDE can
            launch the inferior with the correct environment variables and arguments.
        '''
        program = self.getBuildArtifact("a.out")
        source = 'main.c'
        self.build_and_launch(
            program, stopOnEntry=True, runInTerminal=True, args=["foobar"],
            env=["FOO=bar"])

        breakpoint_line = line_number(source, '// breakpoint')

        self.set_source_breakpoints(source, [breakpoint_line])
        self.continue_to_next_stop()

        # We verify we actually stopped inside the loop
        counter = int(self.vscode.get_local_variable_value('counter'))
        self.assertTrue(counter > 0)

        # We verify we were able to set the launch arguments
        argc = int(self.vscode.get_local_variable_value('argc'))
        self.assertEqual(argc, 2)

        argv1 = self.vscode.request_evaluate('argv[1]')['body']['result']
        self.assertIn('foobar', argv1)

        # We verify we were able to set the environment
        env = self.vscode.request_evaluate('foo')['body']['result']
        self.assertIn('bar', env)

    @skipIfWindows
    @skipIfRemote
    @skipIf(archs=no_match(['x86_64']))
    def test_runInTerminalInvalidTarget(self):
        if not self.isTestSupported():
            return
        self.build_and_create_debug_adaptor()
        response = self.launch(
            "INVALIDPROGRAM", stopOnEntry=True, runInTerminal=True, args=["foobar"], env=["FOO=bar"], expectFailure=True)
        self.assertFalse(response['success'])
        self.assertIn("Could not create a target for a program 'INVALIDPROGRAM': unable to find executable",
            response['message'])

    @skipIfWindows
    @skipIfRemote
    @skipIf(archs=no_match(['x86_64']))
    def test_missingArgInRunInTerminalLauncher(self):
        if not self.isTestSupported():
            return
        proc = subprocess.run([self.lldbVSCodeExec,  "--launch-target", "INVALIDPROGRAM"],
            capture_output=True, universal_newlines=True)
        self.assertTrue(proc.returncode != 0)
        self.assertIn('"--launch-target" requires "--comm-file" to be specified', proc.stderr)

    @skipIfWindows
    @skipIfRemote
    @skipIf(archs=no_match(['x86_64']))
    def test_FakeAttachedRunInTerminalLauncherWithInvalidProgram(self):
        if not self.isTestSupported():
            return
        comm_file = os.path.join(self.getBuildDir(), "comm-file")
        os.mkfifo(comm_file)

        proc = subprocess.Popen(
            [self.lldbVSCodeExec, "--comm-file", comm_file, "--launch-target", "INVALIDPROGRAM"],
            universal_newlines=True, stderr=subprocess.PIPE)

        self.readPidMessage(comm_file)
        self.sendDidAttachMessage(comm_file)
        self.assertIn("No such file or directory", self.readErrorMessage(comm_file))
        
        _, stderr = proc.communicate()
        self.assertIn("No such file or directory", stderr)

    @skipIfWindows
    @skipIfRemote
    @skipIf(archs=no_match(['x86_64']))
    def test_FakeAttachedRunInTerminalLauncherWithValidProgram(self):
        if not self.isTestSupported():
            return
        comm_file = os.path.join(self.getBuildDir(), "comm-file")
        os.mkfifo(comm_file)

        proc = subprocess.Popen(
            [self.lldbVSCodeExec, "--comm-file", comm_file, "--launch-target", "echo", "foo"],
            universal_newlines=True, stdout=subprocess.PIPE)

        self.readPidMessage(comm_file)
        self.sendDidAttachMessage(comm_file)

        stdout, _ = proc.communicate()
        self.assertIn("foo", stdout)

    @skipIfWindows
    @skipIfRemote
    @skipIf(archs=no_match(['x86_64']))
    def test_FakeAttachedRunInTerminalLauncherAndCheckEnvironment(self):
        if not self.isTestSupported():
            return
        comm_file = os.path.join(self.getBuildDir(), "comm-file")
        os.mkfifo(comm_file)

        proc = subprocess.Popen(
            [self.lldbVSCodeExec, "--comm-file", comm_file, "--launch-target", "env"],
            universal_newlines=True, stdout=subprocess.PIPE,
            env={**os.environ, "FOO": "BAR"})
        
        self.readPidMessage(comm_file)
        self.sendDidAttachMessage(comm_file)

        stdout, _ = proc.communicate()
        self.assertIn("FOO=BAR", stdout)

    @skipIfWindows
    @skipIfRemote
    @skipIf(archs=no_match(['x86_64']))
    def test_NonAttachedRunInTerminalLauncher(self):
        if not self.isTestSupported():
            return
        comm_file = os.path.join(self.getBuildDir(), "comm-file")
        os.mkfifo(comm_file)

        proc = subprocess.Popen(
            [self.lldbVSCodeExec, "--comm-file", comm_file, "--launch-target", "echo", "foo"],
            universal_newlines=True, stderr=subprocess.PIPE,
            env={**os.environ, "LLDB_VSCODE_RIT_TIMEOUT_IN_MS": "1000"})

        self.readPidMessage(comm_file)
        
        _, stderr = proc.communicate()
        self.assertIn("Timed out trying to get messages from the debug adaptor", stderr)