1"""
2Test breakpoint command with AT_comp_dir set to symbolic link.
3"""
4
5
6import os
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13_EXE_NAME = 'CompDirSymLink'  # Must match Makefile
14_SRC_FILE = 'relative.cpp'
15_COMP_DIR_SYM_LINK_PROP = 'symbols.debug-info-symlink-paths'
16
17
18class CompDirSymLinkTestCase(TestBase):
19
20    def setUp(self):
21        # Call super's setUp().
22        TestBase.setUp(self)
23        # Find the line number to break inside main().
24        self.line = line_number(
25            os.path.join(self.getSourceDir(), "main.cpp"),
26            '// Set break point at this line.')
27
28    @skipIf(hostoslist=["windows"])
29    def test_symlink_paths_set(self):
30        pwd_symlink = self.create_src_symlink()
31        self.doBuild(pwd_symlink, pwd_symlink)
32        src_path = self.getBuildArtifact(_SRC_FILE)
33        lldbutil.run_break_set_by_file_and_line(self, src_path, self.line)
34
35    @skipIf(hostoslist=no_match(["linux"]))
36    def test_symlink_paths_set_procselfcwd(self):
37        os.chdir(self.getBuildDir())
38        pwd_symlink = '/proc/self/cwd'
39        self.doBuild(pwd_symlink, pwd_symlink)
40        src_path = self.getBuildArtifact(_SRC_FILE)
41        # /proc/self/cwd points to a realpath form of current directory.
42        src_path = os.path.realpath(src_path)
43        lldbutil.run_break_set_by_file_and_line(self, src_path, self.line)
44
45    @skipIf(hostoslist=["windows"])
46    def test_symlink_paths_unset(self):
47        pwd_symlink = self.create_src_symlink()
48        self.doBuild(pwd_symlink, None)
49        src_path = self.getBuildArtifact(_SRC_FILE)
50        self.assertRaises(
51            AssertionError,
52            lldbutil.run_break_set_by_file_and_line,
53            self,
54            src_path,
55            self.line)
56
57    @skipIf(hostoslist=["windows"])
58    def test_symlink_paths_empty(self):
59        pwd_symlink = self.create_src_symlink()
60        self.doBuild(pwd_symlink, "")
61        src_path = self.getBuildArtifact(_SRC_FILE)
62        self.assertRaises(
63            AssertionError,
64            lldbutil.run_break_set_by_file_and_line,
65            self,
66            src_path,
67            self.line)
68
69    def create_src_symlink(self):
70        pwd_symlink = self.getBuildArtifact('pwd_symlink')
71        if os.path.exists(pwd_symlink):
72            os.unlink(pwd_symlink)
73        os.symlink(self.getBuildDir(), pwd_symlink)
74        self.addTearDownHook(lambda: os.remove(pwd_symlink))
75        return pwd_symlink
76
77    def doBuild(self, pwd_symlink, setting_value):
78        self.build(dictionary={'PWD': pwd_symlink})
79
80        if setting_value:
81            cmd = "settings set %s '%s'" % (_COMP_DIR_SYM_LINK_PROP, setting_value)
82        else:
83            cmd = "settings clear %s"%_COMP_DIR_SYM_LINK_PROP
84        self.runCmd(cmd)
85
86        exe = self.getBuildArtifact(_EXE_NAME)
87        self.runCmd('file ' + exe, CURRENT_EXECUTABLE_SET)
88