Skip to content

Commit d31e4dc

Browse files
lsp: Finishing last missing test wrt complex nested project directory structure and specifying custom includes, while using some (one) of them.
1 parent 122fbc6 commit d31e4dc

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity >=0.8.0;
3+
4+
import "otherlib/second.sol";
5+
6+
contract C
7+
{
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity >=0.8.0;
3+
4+
contract B
5+
{
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity >=0.8.0;
3+
4+
contract A
5+
{
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity >=0.8.0;
3+
4+
contract RootContract
5+
{
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity >=0.8.0;
3+
4+
library Second
5+
{
6+
function f(uint n) public pure returns (uint) { return n + 1; }
7+
}

test/lsp.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,7 @@ def setup_lsp(
915915
lsp: JsonRpcProcess,
916916
expose_project_root=True,
917917
file_load_strategy: FileLoadStrategy=FileLoadStrategy.DirectlyOpenedAndOnImport,
918+
custom_include_paths: list[str] = [],
918919
project_root_subdir=None
919920
):
920921
"""
@@ -944,11 +945,19 @@ def setup_lsp(
944945
}
945946
}
946947
}
948+
947949
if file_load_strategy != FileLoadStrategy.Undefined:
948950
params['initializationOptions'] = {}
949951
params['initializationOptions']['file-load-strategy'] = file_load_strategy.lsp_name()
952+
953+
if len(custom_include_paths) != 0:
954+
if params['initializationOptions'] is None:
955+
params['initializationOptions'] = {}
956+
params['initializationOptions']['include-paths'] = custom_include_paths
957+
950958
if not expose_project_root:
951959
params['rootUri'] = None
960+
952961
lsp.call_method('initialize', params)
953962
lsp.send_notification('initialized')
954963

@@ -1383,6 +1392,40 @@ def test_analyze_all_project_files2(self, solc: JsonRpcProcess) -> None:
13831392
self.expect_true(report['uri'] in EXPECTED_URIS, "Correct file URI")
13841393
self.expect_equal(len(report['diagnostics']), 0, "no diagnostics")
13851394

1395+
def test_analyze_all_project_files3(self, solc: JsonRpcProcess) -> None:
1396+
"""
1397+
Same as first test on that matter but with deeper nesting levels.
1398+
"""
1399+
SUBDIR = 'include-paths-nested-2'
1400+
EXPECTED_FILES = [
1401+
"A/B/C/foo",
1402+
"A/B/foo",
1403+
"A/foo",
1404+
"foo",
1405+
]
1406+
IMPLICITLY_LOADED_FILE_COUNT = 1
1407+
EXPECTED_URIS = [self.get_test_file_uri(x, SUBDIR) for x in EXPECTED_FILES]
1408+
self.setup_lsp(
1409+
solc,
1410+
file_load_strategy=FileLoadStrategy.ProjectDirectory,
1411+
project_root_subdir=SUBDIR,
1412+
custom_include_paths=[f"{self.project_root_dir}/other-include-dir"]
1413+
)
1414+
published_diagnostics = self.wait_for_diagnostics(solc)
1415+
self.expect_equal(len(published_diagnostics), len(EXPECTED_FILES) + IMPLICITLY_LOADED_FILE_COUNT, "Test number of files analyzed.")
1416+
1417+
# All but the last report should be from expected files
1418+
for report in published_diagnostics[:-IMPLICITLY_LOADED_FILE_COUNT]:
1419+
self.expect_true(report['uri'] in EXPECTED_URIS, "Correct file URI")
1420+
self.expect_equal(len(report['diagnostics']), 0, "no diagnostics")
1421+
1422+
# Check last report (should be the custom imported lib).
1423+
# This file is analyzed because it was imported via "A/B/C/foo.sol".
1424+
report = published_diagnostics[len(EXPECTED_URIS)]
1425+
self.expect_equal(report['uri'], f"{self.project_root_uri}/other-include-dir/otherlib/second.sol", "Correct file URI")
1426+
self.expect_equal(len(report['diagnostics']), 0, "no diagnostics")
1427+
1428+
13861429
def test_publish_diagnostics_errors_multiline(self, solc: JsonRpcProcess) -> None:
13871430
self.setup_lsp(solc)
13881431
TEST_NAME = 'publish_diagnostics_3'
@@ -1495,6 +1538,43 @@ def test_custom_includes(self, solc: JsonRpcProcess) -> None:
14951538
self.expect_equal(len(diagnostics), 1, "no diagnostics")
14961539
self.expect_diagnostic(diagnostics[0], code=2018, lineNo=5, startEndColumns=(4, 62))
14971540

1541+
def test_custom_includes_with_full_project(self, solc: JsonRpcProcess) -> None:
1542+
"""
1543+
Tests loading all all project files while having custom include directories configured.
1544+
In such a scenario, all project files should be analyzed and those being included via search path
1545+
but not those include files that are not directly nor indirectly included.
1546+
"""
1547+
self.setup_lsp(
1548+
solc,
1549+
expose_project_root=True,
1550+
project_root_subdir=''
1551+
)
1552+
solc.send_notification(
1553+
'workspace/didChangeConfiguration', {
1554+
'settings': {
1555+
'include-paths': [
1556+
f"{self.project_root_dir}/other-include-dir"
1557+
]
1558+
}
1559+
}
1560+
)
1561+
published_diagnostics = self.open_file_and_wait_for_diagnostics(solc, 'include-paths/using-custom-includes')
1562+
1563+
self.expect_equal(len(published_diagnostics), 2, "Diagnostic reports for 2 files")
1564+
1565+
# test file
1566+
report = published_diagnostics[0]
1567+
self.expect_equal(report['uri'], self.get_test_file_uri('using-custom-includes', 'include-paths'))
1568+
diagnostics = report['diagnostics']
1569+
self.expect_equal(len(diagnostics), 0, "no diagnostics")
1570+
1571+
# imported file
1572+
report = published_diagnostics[1]
1573+
self.expect_equal(report['uri'], f"{self.project_root_uri}/other-include-dir/otherlib/otherlib.sol")
1574+
diagnostics = report['diagnostics']
1575+
self.expect_equal(len(diagnostics), 1, "no diagnostics")
1576+
self.expect_diagnostic(diagnostics[0], code=2018, lineNo=5, startEndColumns=(4, 62))
1577+
14981578
def test_didChange_in_A_causing_error_in_B(self, solc: JsonRpcProcess) -> None:
14991579
# Reusing another test but now change some file that generates an error in the other.
15001580
self.test_textDocument_didOpen_with_relative_import(solc)

0 commit comments

Comments
 (0)