1"""
2Test some lldb command abbreviations.
3"""
4
5
6import lldb
7import os
8import sys
9import json
10from lldbsuite.test.decorators import *
11from lldbsuite.test.lldbtest import *
12from lldbsuite.test import lldbutil
13from lldbsuite.test import lldbplatformutil
14
15
16class TestPaths(TestBase):
17
18    mydir = TestBase.compute_mydir(__file__)
19
20    @no_debug_info_test
21    def test_paths(self):
22        '''Test to make sure no file names are set in the lldb.SBFileSpec objects returned by lldb.SBHostOS.GetLLDBPath() for paths that are directories'''
23        dir_path_types = [lldb.ePathTypeLLDBShlibDir,
24                          lldb.ePathTypeSupportExecutableDir,
25                          lldb.ePathTypeHeaderDir,
26                          lldb.ePathTypePythonDir,
27                          lldb.ePathTypeLLDBSystemPlugins,
28                          lldb.ePathTypeLLDBUserPlugins,
29                          lldb.ePathTypeLLDBTempSystemDir,
30                          lldb.ePathTypeClangDir]
31
32        for path_type in dir_path_types:
33            f = lldb.SBHostOS.GetLLDBPath(path_type)
34            # No directory path types should have the filename set
35            self.assertIsNone(f.GetFilename())
36
37        shlib_dir = lldb.SBHostOS.GetLLDBPath(lldb.ePathTypeLLDBShlibDir).GetDirectory()
38        if lldbplatformutil.getHostPlatform() == 'windows':
39            filenames = ['liblldb.dll']
40        elif lldbplatformutil.getHostPlatform() == 'darwin':
41            filenames = ['LLDB', 'liblldb.dylib']
42        else:
43            filenames = ['liblldb.so']
44        self.assertTrue(any([os.path.exists(os.path.join(shlib_dir, f)) for f in
45            filenames]), "shlib_dir = " + shlib_dir)
46
47    @no_debug_info_test
48    def test_interpreter_info(self):
49        info_sd = self.dbg.GetScriptInterpreterInfo(self.dbg.GetScriptingLanguage("python"))
50        self.assertTrue(info_sd.IsValid())
51        stream = lldb.SBStream()
52        self.assertTrue(info_sd.GetAsJSON(stream).Success())
53        info = json.loads(stream.GetData())
54        if os.name == 'nt': #FIXME
55            return
56        prefix = info['prefix']
57        self.assertEqual(os.path.realpath(sys.prefix), os.path.realpath(prefix))
58        self.assertEqual(
59            os.path.realpath(os.path.join(info['lldb-pythonpath'], 'lldb')),
60            os.path.realpath(os.path.dirname(lldb.__file__)))
61        self.assertTrue(os.path.exists(info['executable']))
62        self.assertEqual(info['language'], 'python')
63
64
65    @no_debug_info_test
66    def test_directory_doesnt_end_with_slash(self):
67        current_directory_spec = lldb.SBFileSpec(os.path.curdir)
68        current_directory_string = current_directory_spec.GetDirectory()
69        self.assertNotEqual(current_directory_string[-1:], '/')
70
71    @skipUnlessPlatform(["windows"])
72    @no_debug_info_test
73    def test_windows_double_slash(self):
74        '''Test to check the path with double slash is handled correctly '''
75        # Create a path and see if lldb gets the directory and file right
76        fspec = lldb.SBFileSpec("C:\\dummy1\\dummy2//unknown_file", True)
77        self.assertEqual(
78            os.path.normpath(
79                fspec.GetDirectory()),
80            os.path.normpath("C:/dummy1/dummy2"))
81        self.assertEqual(fspec.GetFilename(), "unknown_file")
82