2021-12-13 13:53:40 +00:00
|
|
|
#!/usr/bin/env python3
|
2021-12-20 13:10:17 +00:00
|
|
|
# pragma pylint: disable=too-many-lines
|
2022-04-25 15:21:45 +00:00
|
|
|
# test line 1
|
2021-12-13 13:53:40 +00:00
|
|
|
import argparse
|
|
|
|
import fnmatch
|
2022-04-25 15:21:45 +00:00
|
|
|
import functools
|
2021-12-13 13:53:40 +00:00
|
|
|
import json
|
|
|
|
import os
|
2022-04-25 15:21:45 +00:00
|
|
|
import re
|
2021-12-13 13:53:40 +00:00
|
|
|
import subprocess
|
2021-12-21 14:23:03 +00:00
|
|
|
import sys
|
2021-12-13 13:53:40 +00:00
|
|
|
import traceback
|
2022-03-15 17:52:59 +00:00
|
|
|
from collections import namedtuple
|
|
|
|
from copy import deepcopy
|
2022-04-25 15:21:45 +00:00
|
|
|
from enum import Enum, auto
|
|
|
|
from itertools import islice
|
2022-04-25 13:35:24 +00:00
|
|
|
from pathlib import PurePath
|
2022-05-23 12:20:03 +00:00
|
|
|
from typing import Any, List, Optional, Tuple, Union, NewType
|
2022-03-15 17:52:59 +00:00
|
|
|
|
2022-04-25 15:21:45 +00:00
|
|
|
import colorama # Enables the use of SGR & CUP terminal VT sequences on Windows.
|
2021-12-13 13:53:40 +00:00
|
|
|
from deepdiff import DeepDiff
|
|
|
|
|
2022-04-25 15:21:45 +00:00
|
|
|
if os.name == 'nt':
|
|
|
|
# pragma pylint: disable=import-error
|
|
|
|
import msvcrt
|
|
|
|
else:
|
|
|
|
import tty
|
|
|
|
# Turn off user input buffering so we get the input immediately,
|
|
|
|
# not only after a line break
|
|
|
|
tty.setcbreak(sys.stdin.fileno())
|
|
|
|
|
|
|
|
|
2022-05-23 12:20:03 +00:00
|
|
|
# Type for the pure test name without .sol suffix or sub directory
|
|
|
|
TestName = NewType("TestName", str)
|
|
|
|
|
|
|
|
# Type for the test path, e.g. subdir/mytest.sol
|
|
|
|
RelativeTestPath = NewType("RelativeTestPath", str)
|
|
|
|
|
|
|
|
|
2022-04-25 15:21:45 +00:00
|
|
|
def escape_string(text: str) -> str:
|
|
|
|
"""
|
|
|
|
Trivially escapes given input string's \r \n and \\.
|
|
|
|
"""
|
|
|
|
return text.translate(str.maketrans({
|
|
|
|
"\r": r"\r",
|
|
|
|
"\n": r"\n",
|
|
|
|
"\\": r"\\"
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
2022-05-18 12:46:26 +00:00
|
|
|
def getCharFromStdin() -> str:
|
2022-04-25 15:21:45 +00:00
|
|
|
"""
|
|
|
|
Gets a single character from stdin without line-buffering.
|
|
|
|
"""
|
|
|
|
if os.name == 'nt':
|
|
|
|
# pragma pylint: disable=import-error
|
|
|
|
return msvcrt.getch().decode("utf-8")
|
|
|
|
else:
|
2022-05-18 12:46:26 +00:00
|
|
|
return sys.stdin.read(1)
|
2022-04-25 15:21:45 +00:00
|
|
|
|
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
"""
|
|
|
|
Named tuple that holds various regexes used to parse the test specification.
|
|
|
|
"""
|
|
|
|
TestRegexesTuple = namedtuple("TestRegexesTuple", [
|
|
|
|
"sendRequest", # regex to find requests to be sent & tested
|
|
|
|
"findQuotedTag", # regex to find tags wrapped in quotes
|
|
|
|
"findTag", # regex to find tags
|
|
|
|
"fileDiagnostics", # regex to find diagnostic expectations for a file
|
|
|
|
"diagnostic" # regex to find a single diagnostic within the file expectations
|
|
|
|
])
|
|
|
|
"""
|
|
|
|
Instance of the named tuple holding the regexes
|
|
|
|
"""
|
|
|
|
TEST_REGEXES = TestRegexesTuple(
|
|
|
|
re.compile(R'^// -> (?P<method>[\w\/]+) {'),
|
|
|
|
re.compile(R'(?P<tag>"@\w+")'),
|
|
|
|
re.compile(R'(?P<tag>@\w+)'),
|
2022-05-30 17:51:43 +00:00
|
|
|
re.compile(R'// (?P<testname>\S+):([ ](?P<diagnostics>[\w @]*))?'),
|
2022-03-15 17:52:59 +00:00
|
|
|
re.compile(R'(?P<tag>@\w+) (?P<code>\d\d\d\d)')
|
|
|
|
)
|
|
|
|
|
|
|
|
"""
|
|
|
|
Named tuple holding regexes to find tags in the solidity code
|
|
|
|
"""
|
|
|
|
TagRegexesTuple = namedtuple("TagRegexestuple", ["simpleRange", "multilineRange"])
|
|
|
|
TAG_REGEXES = TagRegexesTuple(
|
|
|
|
re.compile(R"(?P<range>[\^]+) (?P<tag>@\w+)"),
|
|
|
|
re.compile(R"\^(?P<delimiter>[()]{1,2}) (?P<tag>@\w+)$")
|
|
|
|
)
|
|
|
|
|
2022-04-27 13:22:26 +00:00
|
|
|
def split_path(path):
|
|
|
|
"""
|
|
|
|
Return the test name and the subdir path of the given path.
|
|
|
|
"""
|
2022-05-30 17:51:43 +00:00
|
|
|
sub_dir_separator = path.rfind("/")
|
2022-04-27 13:22:26 +00:00
|
|
|
|
|
|
|
if sub_dir_separator == -1:
|
|
|
|
return (path, None)
|
|
|
|
|
|
|
|
return (path[sub_dir_separator+1:], path[:sub_dir_separator])
|
2022-03-15 17:52:59 +00:00
|
|
|
|
|
|
|
def count_index(lines, start=0):
|
|
|
|
"""
|
|
|
|
Takes an iterable of lines and adds the current byte index so it's available
|
|
|
|
when iterating or looping.
|
|
|
|
"""
|
|
|
|
n = start
|
|
|
|
for elem in lines:
|
|
|
|
yield n, elem
|
|
|
|
n += 1 + len(elem)
|
|
|
|
|
|
|
|
def tags_only(lines, start=0):
|
|
|
|
"""
|
2022-05-30 17:51:43 +00:00
|
|
|
Filter the lines for tag comments and report the line number that the tags
|
|
|
|
_refer_ to (which is not the line they are on!).
|
2022-03-15 17:52:59 +00:00
|
|
|
"""
|
|
|
|
n = start
|
|
|
|
numCommentLines = 0
|
|
|
|
|
|
|
|
def hasTag(line):
|
|
|
|
if line.find("// ") != -1:
|
|
|
|
for _, regex in TAG_REGEXES._asdict().items():
|
|
|
|
if regex.search(line[len("// "):]) is not None:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
for line in lines:
|
|
|
|
if hasTag(line):
|
|
|
|
numCommentLines += 1
|
|
|
|
yield n - numCommentLines, line
|
|
|
|
else:
|
|
|
|
numCommentLines = 0
|
|
|
|
|
|
|
|
n += 1
|
|
|
|
|
|
|
|
|
|
|
|
def prepend_comments(sequence):
|
|
|
|
"""
|
|
|
|
Prepends a comment indicator to each element
|
|
|
|
"""
|
|
|
|
result = ""
|
|
|
|
for line in sequence.splitlines(True):
|
|
|
|
result = result + "// " + line
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2021-12-13 13:53:40 +00:00
|
|
|
# {{{ JsonRpcProcess
|
|
|
|
class BadHeader(Exception):
|
|
|
|
def __init__(self, msg: str):
|
|
|
|
super().__init__("Bad header: " + msg)
|
|
|
|
|
|
|
|
class JsonRpcProcess:
|
|
|
|
exe_path: str
|
|
|
|
exe_args: List[str]
|
|
|
|
process: subprocess.Popen
|
|
|
|
trace_io: bool
|
2022-05-23 12:20:03 +00:00
|
|
|
print_pid: bool
|
2021-12-13 13:53:40 +00:00
|
|
|
|
2022-05-23 12:20:03 +00:00
|
|
|
def __init__(self, exe_path: str, exe_args: List[str], trace_io: bool = True, print_pid = False):
|
2021-12-13 13:53:40 +00:00
|
|
|
self.exe_path = exe_path
|
|
|
|
self.exe_args = exe_args
|
|
|
|
self.trace_io = trace_io
|
2022-05-23 12:20:03 +00:00
|
|
|
self.print_pid = print_pid
|
2021-12-13 13:53:40 +00:00
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self.process = subprocess.Popen(
|
|
|
|
[self.exe_path, *self.exe_args],
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE
|
|
|
|
)
|
2022-05-23 12:20:03 +00:00
|
|
|
|
|
|
|
if self.print_pid:
|
|
|
|
print(f"solc pid: {self.process.pid}. Attach with sudo gdb -p {self.process.pid}")
|
|
|
|
|
2021-12-13 13:53:40 +00:00
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exception_type, exception_value, traceback) -> None:
|
|
|
|
self.process.kill()
|
|
|
|
self.process.wait(timeout=2.0)
|
|
|
|
|
|
|
|
def trace(self, topic: str, message: str) -> None:
|
|
|
|
if self.trace_io:
|
|
|
|
print(f"{SGR_TRACE}{topic}:{SGR_RESET} {message}")
|
|
|
|
|
|
|
|
def receive_message(self) -> Union[None, dict]:
|
|
|
|
# Note, we should make use of timeout to avoid infinite blocking if nothing is received.
|
|
|
|
CONTENT_LENGTH_HEADER = "Content-Length: "
|
|
|
|
CONTENT_TYPE_HEADER = "Content-Type: "
|
2021-12-21 14:25:14 +00:00
|
|
|
if self.process.stdout is None:
|
2021-12-13 13:53:40 +00:00
|
|
|
return None
|
|
|
|
message_size = None
|
|
|
|
while True:
|
|
|
|
# read header
|
|
|
|
line = self.process.stdout.readline()
|
2022-03-15 17:52:59 +00:00
|
|
|
if len(line) == 0:
|
2021-12-13 13:53:40 +00:00
|
|
|
# server quit
|
|
|
|
return None
|
|
|
|
line = line.decode("utf-8")
|
2022-04-25 15:21:45 +00:00
|
|
|
if self.trace_io:
|
|
|
|
print(f"Received header-line: {escape_string(line)}")
|
2021-12-13 13:53:40 +00:00
|
|
|
if not line.endswith("\r\n"):
|
|
|
|
raise BadHeader("missing newline")
|
2022-04-25 15:21:45 +00:00
|
|
|
# Safely remove the "\r\n".
|
|
|
|
line = line.rstrip("\r\n")
|
2021-12-13 13:53:40 +00:00
|
|
|
if line == '':
|
|
|
|
break # done with the headers
|
|
|
|
if line.startswith(CONTENT_LENGTH_HEADER):
|
|
|
|
line = line[len(CONTENT_LENGTH_HEADER):]
|
|
|
|
if not line.isdigit():
|
|
|
|
raise BadHeader("size is not int")
|
|
|
|
message_size = int(line)
|
|
|
|
elif line.startswith(CONTENT_TYPE_HEADER):
|
|
|
|
# nothing todo with type for now.
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise BadHeader("unknown header")
|
|
|
|
if message_size is None:
|
|
|
|
raise BadHeader("missing size")
|
|
|
|
rpc_message = self.process.stdout.read(message_size).decode("utf-8")
|
|
|
|
json_object = json.loads(rpc_message)
|
|
|
|
self.trace('receive_message', json.dumps(json_object, indent=4, sort_keys=True))
|
|
|
|
return json_object
|
|
|
|
|
|
|
|
def send_message(self, method_name: str, params: Optional[dict]) -> None:
|
2021-12-21 14:25:14 +00:00
|
|
|
if self.process.stdin is None:
|
2021-12-13 13:53:40 +00:00
|
|
|
return
|
|
|
|
message = {
|
|
|
|
'jsonrpc': '2.0',
|
|
|
|
'method': method_name,
|
|
|
|
'params': params
|
|
|
|
}
|
|
|
|
json_string = json.dumps(obj=message)
|
|
|
|
rpc_message = f"Content-Length: {len(json_string)}\r\n\r\n{json_string}"
|
|
|
|
self.trace(f'send_message ({method_name})', json.dumps(message, indent=4, sort_keys=True))
|
|
|
|
self.process.stdin.write(rpc_message.encode("utf-8"))
|
|
|
|
self.process.stdin.flush()
|
|
|
|
|
2022-07-11 14:57:08 +00:00
|
|
|
def call_method(self, method_name: str, params: Optional[dict], expects_response: bool = True) -> Any:
|
2021-12-13 13:53:40 +00:00
|
|
|
self.send_message(method_name, params)
|
2022-07-11 14:57:08 +00:00
|
|
|
if not expects_response:
|
|
|
|
return None
|
2021-12-13 13:53:40 +00:00
|
|
|
return self.receive_message()
|
|
|
|
|
|
|
|
def send_notification(self, name: str, params: Optional[dict] = None) -> None:
|
|
|
|
self.send_message(name, params)
|
|
|
|
|
|
|
|
# }}}
|
|
|
|
|
|
|
|
SGR_RESET = '\033[m'
|
|
|
|
SGR_TRACE = '\033[1;36m'
|
|
|
|
SGR_NOTICE = '\033[1;35m'
|
|
|
|
SGR_TEST_BEGIN = '\033[1;33m'
|
|
|
|
SGR_ASSERT_BEGIN = '\033[1;34m'
|
|
|
|
SGR_STATUS_OKAY = '\033[1;32m'
|
|
|
|
SGR_STATUS_FAIL = '\033[1;31m'
|
|
|
|
|
|
|
|
class ExpectationFailed(Exception):
|
2022-03-15 17:52:59 +00:00
|
|
|
class Part(Enum):
|
|
|
|
Diagnostics = auto()
|
|
|
|
Methods = auto()
|
|
|
|
|
|
|
|
def __init__(self, reason: str, part):
|
|
|
|
self.part = part
|
|
|
|
super().__init__(reason)
|
|
|
|
|
|
|
|
class JSONExpectationFailed(ExpectationFailed):
|
|
|
|
def __init__(self, actual, expected, part):
|
|
|
|
self.actual = actual
|
|
|
|
self.expected = expected
|
|
|
|
|
|
|
|
expected_pretty = ""
|
|
|
|
|
|
|
|
if expected is not None:
|
|
|
|
expected_pretty = json.dumps(expected, sort_keys=True)
|
|
|
|
|
|
|
|
diff = DeepDiff(actual, expected)
|
|
|
|
|
2021-12-13 13:53:40 +00:00
|
|
|
super().__init__(
|
2022-03-15 17:52:59 +00:00
|
|
|
f"\n\tExpected {expected_pretty}" + \
|
|
|
|
f"\n\tbut got {json.dumps(actual, sort_keys=True)}.\n\t{diff}",
|
|
|
|
part
|
2021-12-13 13:53:40 +00:00
|
|
|
)
|
|
|
|
|
2022-03-01 17:56:17 +00:00
|
|
|
|
2021-12-13 13:53:40 +00:00
|
|
|
def create_cli_parser() -> argparse.ArgumentParser:
|
|
|
|
parser = argparse.ArgumentParser(description="Solidity LSP Test suite")
|
2022-03-23 16:32:33 +00:00
|
|
|
parser.set_defaults(fail_fast=False)
|
|
|
|
parser.add_argument(
|
2022-05-30 17:51:43 +00:00
|
|
|
"-f", "--fail-fast",
|
2022-03-23 16:32:33 +00:00
|
|
|
dest="fail_fast",
|
|
|
|
action="store_true",
|
|
|
|
help="Terminates the running tests on first failure."
|
|
|
|
)
|
2022-05-30 17:51:43 +00:00
|
|
|
parser.set_defaults(non_interactive=False)
|
|
|
|
parser.add_argument(
|
|
|
|
"-n", "--non-interactive",
|
|
|
|
dest="non_interactive",
|
|
|
|
action="store_true",
|
|
|
|
help="Prevent interactive queries and just fail instead."
|
|
|
|
)
|
2022-05-23 12:20:03 +00:00
|
|
|
parser.set_defaults(print_solc_pid=False)
|
|
|
|
parser.add_argument(
|
|
|
|
"-p", "--print-solc-pid",
|
|
|
|
dest="print_solc_pid",
|
|
|
|
action="store_true",
|
|
|
|
help="Print pid of each started solc for debugging purposes."
|
|
|
|
)
|
2021-12-13 13:53:40 +00:00
|
|
|
parser.set_defaults(trace_io=False)
|
|
|
|
parser.add_argument(
|
2022-05-30 17:51:43 +00:00
|
|
|
"-T", "--trace-io",
|
2021-12-13 13:53:40 +00:00
|
|
|
dest="trace_io",
|
|
|
|
action="store_true",
|
|
|
|
help="Be more verbose by also printing assertions."
|
|
|
|
)
|
|
|
|
parser.set_defaults(print_assertions=False)
|
|
|
|
parser.add_argument(
|
2022-05-30 17:51:43 +00:00
|
|
|
"-v", "--print-assertions",
|
2021-12-13 13:53:40 +00:00
|
|
|
dest="print_assertions",
|
|
|
|
action="store_true",
|
|
|
|
help="Be more verbose by also printing assertions."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2022-05-30 17:51:43 +00:00
|
|
|
"-t", "--test-pattern",
|
2021-12-13 13:53:40 +00:00
|
|
|
dest="test_pattern",
|
|
|
|
type=str,
|
|
|
|
default="*",
|
|
|
|
help="Filters all available tests by matching against this test pattern (using globbing)",
|
|
|
|
nargs="?"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"solc_path",
|
|
|
|
type=str,
|
|
|
|
default="solc",
|
|
|
|
help="Path to solc binary to test against",
|
|
|
|
nargs="?"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"project_root_dir",
|
|
|
|
type=str,
|
|
|
|
default=f"{os.path.dirname(os.path.realpath(__file__))}/..",
|
|
|
|
help="Path to Solidity project's root directory (must be fully qualified).",
|
|
|
|
nargs="?"
|
|
|
|
)
|
|
|
|
return parser
|
|
|
|
|
|
|
|
class Counter:
|
|
|
|
total: int = 0
|
|
|
|
passed: int = 0
|
|
|
|
failed: int = 0
|
|
|
|
|
2022-03-01 17:56:17 +00:00
|
|
|
|
|
|
|
# Returns the given marker with the end extended by 'amount'
|
|
|
|
def extendEnd(marker, amount=1):
|
2022-03-15 17:52:59 +00:00
|
|
|
newMarker = deepcopy(marker)
|
|
|
|
newMarker["end"]["character"] += amount
|
|
|
|
return newMarker
|
|
|
|
|
|
|
|
class TestParserException(Exception):
|
|
|
|
def __init__(self, incompleteResult, msg: str):
|
|
|
|
self.result = incompleteResult
|
|
|
|
super().__init__("Failed to parse test specification: " + msg)
|
|
|
|
|
|
|
|
class TestParser:
|
|
|
|
"""
|
|
|
|
Parses test specifications.
|
|
|
|
Usage example:
|
|
|
|
|
|
|
|
parsed_testcases = TestParser(content).parse()
|
|
|
|
|
2022-05-23 12:20:03 +00:00
|
|
|
# First diagnostics are yielded.
|
|
|
|
# Type is "TestParser.Diagnostics"
|
2022-03-15 17:52:59 +00:00
|
|
|
expected_diagnostics = next(parsed_testcases)
|
2022-05-23 12:20:03 +00:00
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
...
|
|
|
|
# Now each request/response pair in the test definition
|
2022-05-23 12:20:03 +00:00
|
|
|
# Type is "TestParser.RequestAndResponse"
|
2022-03-15 17:52:59 +00:00
|
|
|
for testcase in self.parsed_testcases:
|
|
|
|
...
|
|
|
|
"""
|
|
|
|
RequestAndResponse = namedtuple('RequestAndResponse',
|
|
|
|
"method, request, response, responseBegin, responseEnd",
|
|
|
|
defaults=(None, None, None, None)
|
|
|
|
)
|
|
|
|
Diagnostics = namedtuple('Diagnostics', 'tests start end has_header')
|
|
|
|
Diagnostic = namedtuple('Diagnostic', 'marker code')
|
|
|
|
|
|
|
|
TEST_START = "// ----"
|
|
|
|
|
|
|
|
def __init__(self, content: str):
|
|
|
|
self.content = content
|
|
|
|
self.lines = None
|
|
|
|
self.current_line_tuple = None
|
|
|
|
|
|
|
|
def parse(self):
|
|
|
|
"""
|
|
|
|
Starts parsing the test specifications.
|
|
|
|
Will first yield with the diagnostics expectations as type 'Diagnostics'.
|
|
|
|
After that, it will yield once for every Request/Response pair found in
|
|
|
|
the file, each time as type 'RequestAndResponse'.
|
|
|
|
|
|
|
|
"""
|
|
|
|
testDefStartIdx = self.content.rfind(f"\n{self.TEST_START}\n")
|
|
|
|
|
|
|
|
if testDefStartIdx == -1:
|
|
|
|
# Set start/end to end of file if there is no test section
|
|
|
|
yield self.Diagnostics({}, len(self.content), len(self.content), False)
|
|
|
|
return
|
|
|
|
|
|
|
|
self.lines = islice(
|
|
|
|
count_index(self.content[testDefStartIdx+1:].splitlines(), testDefStartIdx+1),
|
|
|
|
1,
|
|
|
|
None
|
|
|
|
)
|
|
|
|
self.next_line()
|
|
|
|
|
|
|
|
yield self.parseDiagnostics()
|
|
|
|
|
|
|
|
while not self.at_end():
|
2022-05-23 12:20:03 +00:00
|
|
|
yield self.parseRequestAndResponse()
|
2022-03-15 17:52:59 +00:00
|
|
|
self.next_line()
|
|
|
|
|
|
|
|
|
2022-05-23 12:20:03 +00:00
|
|
|
def parseDiagnostics(self) -> Diagnostics:
|
2022-03-15 17:52:59 +00:00
|
|
|
"""
|
|
|
|
Parse diagnostic expectations specified in the file.
|
|
|
|
Returns a named tuple instance of "Diagnostics"
|
|
|
|
"""
|
|
|
|
diagnostics = { "tests": {}, "has_header": True }
|
|
|
|
|
|
|
|
diagnostics["start"] = self.position()
|
|
|
|
|
|
|
|
while not self.at_end():
|
|
|
|
fileDiagMatch = TEST_REGEXES.fileDiagnostics.match(self.current_line())
|
|
|
|
if fileDiagMatch is None:
|
|
|
|
break
|
|
|
|
|
|
|
|
testDiagnostics = []
|
|
|
|
|
2022-05-30 17:51:43 +00:00
|
|
|
diagnostics_string = fileDiagMatch.group("diagnostics")
|
|
|
|
if diagnostics_string is not None:
|
|
|
|
for diagnosticMatch in TEST_REGEXES.diagnostic.finditer(diagnostics_string):
|
|
|
|
testDiagnostics.append(self.Diagnostic(
|
|
|
|
diagnosticMatch.group("tag"),
|
|
|
|
int(diagnosticMatch.group("code"))
|
|
|
|
))
|
2022-03-15 17:52:59 +00:00
|
|
|
|
|
|
|
diagnostics["tests"][fileDiagMatch.group("testname")] = testDiagnostics
|
|
|
|
|
|
|
|
self.next_line()
|
|
|
|
|
|
|
|
diagnostics["end"] = self.position()
|
|
|
|
return self.Diagnostics(**diagnostics)
|
|
|
|
|
|
|
|
|
2022-05-23 12:20:03 +00:00
|
|
|
def parseRequestAndResponse(self) -> RequestAndResponse:
|
2022-03-15 17:52:59 +00:00
|
|
|
RESPONSE_START = "// <- "
|
|
|
|
REQUEST_END = "// }"
|
|
|
|
COMMENT_PREFIX = "// "
|
|
|
|
|
|
|
|
ret = {}
|
|
|
|
start_character = None
|
|
|
|
|
|
|
|
# Parse request header
|
|
|
|
requestResult = TEST_REGEXES.sendRequest.match(self.current_line())
|
2022-04-27 13:22:26 +00:00
|
|
|
if requestResult is None:
|
|
|
|
raise TestParserException(ret, "Method for request not found on line " + self.current_line())
|
|
|
|
|
|
|
|
ret["method"] = requestResult.group("method")
|
|
|
|
ret["request"] = "{\n"
|
2022-03-15 17:52:59 +00:00
|
|
|
|
|
|
|
self.next_line()
|
|
|
|
|
|
|
|
# Search for request block end
|
|
|
|
while not self.at_end():
|
|
|
|
line = self.current_line()
|
|
|
|
ret["request"] += line[len(COMMENT_PREFIX):] + "\n"
|
|
|
|
|
|
|
|
self.next_line()
|
|
|
|
|
|
|
|
if line.startswith(REQUEST_END):
|
|
|
|
break
|
|
|
|
|
|
|
|
# Reached end without finding request_end. Abort.
|
|
|
|
if self.at_end():
|
|
|
|
raise TestParserException(ret, "Request body not found")
|
|
|
|
|
2022-07-11 14:57:08 +00:00
|
|
|
if self.at_end():
|
|
|
|
return self.RequestAndResponse(**ret)
|
2022-03-15 17:52:59 +00:00
|
|
|
|
|
|
|
# Parse response header
|
|
|
|
if self.current_line().startswith(RESPONSE_START):
|
|
|
|
start_character = self.current_line()[len(RESPONSE_START)]
|
|
|
|
if start_character not in ("{", "["):
|
|
|
|
raise TestParserException(ret, "Response header malformed")
|
|
|
|
ret["response"] = self.current_line()[len(RESPONSE_START):] + "\n"
|
|
|
|
ret["responseBegin"] = self.position()
|
|
|
|
|
|
|
|
self.next_line()
|
|
|
|
|
|
|
|
end_character = "}" if start_character == "{" else "]"
|
|
|
|
|
|
|
|
# Search for request block end
|
|
|
|
while not self.at_end():
|
|
|
|
ret["response"] += self.current_line()[len(COMMENT_PREFIX):] + "\n"
|
|
|
|
|
|
|
|
if self.current_line().startswith(f"// {end_character}"):
|
|
|
|
ret["responseEnd"] = self.position() + len(self.current_line())
|
|
|
|
break
|
|
|
|
|
|
|
|
self.next_line()
|
|
|
|
|
|
|
|
# Reached end without finding block_end. Abort.
|
|
|
|
if self.at_end():
|
|
|
|
raise TestParserException(ret, "Response footer not found")
|
|
|
|
|
2022-05-23 12:20:03 +00:00
|
|
|
return self.RequestAndResponse(**ret)
|
2022-03-15 17:52:59 +00:00
|
|
|
|
|
|
|
def next_line(self):
|
|
|
|
self.current_line_tuple = next(self.lines, None)
|
|
|
|
|
|
|
|
def current_line(self):
|
|
|
|
return self.current_line_tuple[1]
|
|
|
|
|
|
|
|
def position(self):
|
|
|
|
"""
|
|
|
|
Returns current byte position
|
|
|
|
"""
|
|
|
|
if self.current_line_tuple is None:
|
|
|
|
return len(self.content)
|
|
|
|
return self.current_line_tuple[0]
|
|
|
|
|
|
|
|
def at_end(self):
|
|
|
|
"""
|
|
|
|
Returns True if we exhausted the lines
|
|
|
|
"""
|
|
|
|
return self.current_line_tuple is None
|
|
|
|
|
|
|
|
class FileTestRunner:
|
|
|
|
"""
|
|
|
|
Runs all tests in a given file.
|
|
|
|
It is required to call test_diagnostics() before calling test_methods().
|
|
|
|
|
|
|
|
When a test fails, asks the user how to proceed.
|
|
|
|
Offers automatic test expectation updates and rerunning of the tests.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class TestResult(Enum):
|
|
|
|
SuccessOrIgnored = auto()
|
|
|
|
Reparse = auto()
|
|
|
|
|
2022-04-27 13:22:26 +00:00
|
|
|
def __init__(self, test_name, sub_dir, solc, suite):
|
2022-03-15 17:52:59 +00:00
|
|
|
self.test_name = test_name
|
2022-04-27 13:22:26 +00:00
|
|
|
self.sub_dir = sub_dir
|
2022-03-15 17:52:59 +00:00
|
|
|
self.suite = suite
|
|
|
|
self.solc = solc
|
|
|
|
self.open_tests = []
|
2022-04-27 13:22:26 +00:00
|
|
|
self.content = self.suite.get_test_file_contents(self.test_name, self.sub_dir)
|
2022-05-23 12:20:03 +00:00
|
|
|
self.markers = self.suite.get_test_tags(self.test_name, self.sub_dir)
|
2022-03-15 17:52:59 +00:00
|
|
|
self.parsed_testcases = None
|
|
|
|
self.expected_diagnostics = None
|
|
|
|
|
|
|
|
def test_diagnostics(self):
|
|
|
|
"""
|
|
|
|
Test that the expected diagnostics match the actual diagnostics
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self.parsed_testcases = TestParser(self.content).parse()
|
|
|
|
|
|
|
|
# Process diagnostics first
|
|
|
|
self.expected_diagnostics = next(self.parsed_testcases)
|
2022-07-11 14:57:08 +00:00
|
|
|
assert isinstance(self.expected_diagnostics, TestParser.Diagnostics)
|
|
|
|
if not self.expected_diagnostics.has_header:
|
|
|
|
return
|
2022-03-15 17:52:59 +00:00
|
|
|
|
2022-05-30 17:51:43 +00:00
|
|
|
expected_diagnostics_per_file = self.expected_diagnostics.tests
|
2022-03-15 17:52:59 +00:00
|
|
|
|
|
|
|
# Add our own test diagnostics if they didn't exist
|
2022-05-30 17:51:43 +00:00
|
|
|
if self.test_name not in expected_diagnostics_per_file:
|
|
|
|
expected_diagnostics_per_file[self.test_name] = []
|
2022-03-15 17:52:59 +00:00
|
|
|
|
|
|
|
published_diagnostics = \
|
2022-04-27 13:22:26 +00:00
|
|
|
self.suite.open_file_and_wait_for_diagnostics(self.solc, self.test_name, self.sub_dir)
|
2022-03-15 17:52:59 +00:00
|
|
|
|
|
|
|
for diagnostics in published_diagnostics:
|
2022-04-25 15:03:31 +00:00
|
|
|
if not diagnostics["uri"].startswith(self.suite.project_root_uri + "/"):
|
|
|
|
raise Exception(
|
|
|
|
f"'{self.test_name}.sol' imported file outside of test directory: '{diagnostics['uri']}'"
|
|
|
|
)
|
2022-05-30 17:51:43 +00:00
|
|
|
self.open_tests.append(self.suite.normalizeUri(diagnostics["uri"]))
|
2022-03-15 17:52:59 +00:00
|
|
|
|
|
|
|
self.suite.expect_equal(
|
|
|
|
len(published_diagnostics),
|
2022-05-30 17:51:43 +00:00
|
|
|
len(expected_diagnostics_per_file),
|
2022-03-15 17:52:59 +00:00
|
|
|
description="Amount of reports does not match!")
|
|
|
|
|
2022-05-30 17:51:43 +00:00
|
|
|
for diagnostics_per_file in published_diagnostics:
|
|
|
|
testname, sub_dir = split_path(self.suite.normalizeUri(diagnostics_per_file['uri']))
|
|
|
|
|
|
|
|
# Clear all processed expectations so we can check at the end
|
|
|
|
# what's missing
|
|
|
|
expected_diagnostics = expected_diagnostics_per_file.pop(testname, {})
|
2022-03-15 17:52:59 +00:00
|
|
|
|
|
|
|
self.suite.expect_equal(
|
2022-05-30 17:51:43 +00:00
|
|
|
len(diagnostics_per_file["diagnostics"]),
|
2022-03-15 17:52:59 +00:00
|
|
|
len(expected_diagnostics),
|
|
|
|
description="Unexpected amount of diagnostics"
|
|
|
|
)
|
2022-05-23 12:20:03 +00:00
|
|
|
markers = self.suite.get_test_tags(testname, sub_dir)
|
2022-05-30 17:51:43 +00:00
|
|
|
for actual_diagnostic in diagnostics_per_file["diagnostics"]:
|
2022-03-15 17:52:59 +00:00
|
|
|
expected_diagnostic = next((diagnostic for diagnostic in
|
|
|
|
expected_diagnostics if actual_diagnostic['range'] ==
|
|
|
|
markers[diagnostic.marker]), None)
|
|
|
|
|
|
|
|
if expected_diagnostic is None:
|
|
|
|
raise ExpectationFailed(
|
|
|
|
f"Unexpected diagnostic: {json.dumps(actual_diagnostic, indent=4, sort_keys=True)}",
|
|
|
|
ExpectationFailed.Part.Diagnostics
|
|
|
|
)
|
|
|
|
|
|
|
|
self.suite.expect_diagnostic(
|
|
|
|
actual_diagnostic,
|
|
|
|
code=expected_diagnostic.code,
|
|
|
|
marker=markers[expected_diagnostic.marker]
|
|
|
|
)
|
|
|
|
|
2022-05-30 17:51:43 +00:00
|
|
|
if len(expected_diagnostics_per_file) > 0:
|
|
|
|
raise ExpectationFailed(
|
|
|
|
f"Expected diagnostics but received none for {expected_diagnostics_per_file}",
|
|
|
|
ExpectationFailed.Part.Diagnostics
|
|
|
|
)
|
|
|
|
|
2022-04-25 15:03:31 +00:00
|
|
|
except Exception:
|
2022-03-15 17:52:59 +00:00
|
|
|
self.close_all_open_files()
|
|
|
|
raise
|
|
|
|
|
|
|
|
def close_all_open_files(self):
|
2022-04-27 13:22:26 +00:00
|
|
|
for testpath in self.open_tests:
|
|
|
|
test, sub_dir = split_path(testpath)
|
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
self.solc.send_message(
|
|
|
|
'textDocument/didClose',
|
2022-04-27 13:22:26 +00:00
|
|
|
{ 'textDocument': { 'uri': self.suite.get_test_file_uri(test, sub_dir) }}
|
2022-03-15 17:52:59 +00:00
|
|
|
)
|
|
|
|
self.suite.wait_for_diagnostics(self.solc)
|
|
|
|
|
|
|
|
self.open_tests.clear()
|
|
|
|
|
|
|
|
def test_methods(self) -> bool:
|
|
|
|
"""
|
|
|
|
Test all methods. Returns False if a reparsing is required, else True
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
# Now handle each request/response pair in the test definition
|
|
|
|
for testcase in self.parsed_testcases:
|
|
|
|
try:
|
|
|
|
self.run_testcase(testcase)
|
|
|
|
except JSONExpectationFailed as e:
|
|
|
|
result = self.user_interaction_failed_method_test(testcase, e.actual, e.expected)
|
|
|
|
|
|
|
|
if result == self.TestResult.Reparse:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
except TestParserException as e:
|
|
|
|
print(e)
|
|
|
|
print(e.result)
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
self.close_all_open_files()
|
|
|
|
|
2022-05-23 12:20:03 +00:00
|
|
|
def user_interaction_failed_method_test(
|
|
|
|
self,
|
|
|
|
testcase: TestParser.RequestAndResponse,
|
|
|
|
actual,
|
|
|
|
expected
|
|
|
|
) -> TestResult:
|
|
|
|
|
2022-04-27 13:22:26 +00:00
|
|
|
actual_pretty = self.suite.replace_ranges_with_tags(actual, self.sub_dir)
|
2022-03-15 17:52:59 +00:00
|
|
|
|
|
|
|
if expected is None:
|
|
|
|
print("Failed to parse expected response, received:\n" + actual)
|
|
|
|
else:
|
|
|
|
print("Expected:\n" + \
|
2022-04-27 13:22:26 +00:00
|
|
|
self.suite.replace_ranges_with_tags(expected, self.sub_dir) + \
|
2022-03-15 17:52:59 +00:00
|
|
|
"\nbut got:\n" + actual_pretty
|
|
|
|
)
|
|
|
|
|
2022-06-02 14:45:25 +00:00
|
|
|
if self.suite.non_interactive:
|
|
|
|
return self.TestResult.SuccessOrIgnored
|
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
while True:
|
|
|
|
print("(u)pdate/(r)etry/(i)gnore?")
|
2022-04-25 15:21:45 +00:00
|
|
|
user_response = getCharFromStdin()
|
2022-03-15 17:52:59 +00:00
|
|
|
if user_response == "i":
|
|
|
|
return self.TestResult.SuccessOrIgnored
|
|
|
|
|
|
|
|
if user_response == "u":
|
|
|
|
actual = actual["result"]
|
|
|
|
self.content = self.content[:testcase.responseBegin] + \
|
2022-04-27 13:22:26 +00:00
|
|
|
prepend_comments(
|
|
|
|
"<- " + \
|
|
|
|
self.suite.replace_ranges_with_tags(actual, self.sub_dir)) + \
|
2022-03-15 17:52:59 +00:00
|
|
|
self.content[testcase.responseEnd:]
|
|
|
|
|
2022-04-27 13:22:26 +00:00
|
|
|
with open(self.suite.get_test_file_path(\
|
|
|
|
self.test_name, self.sub_dir), \
|
|
|
|
mode="w", \
|
|
|
|
encoding="utf-8", \
|
|
|
|
newline='') as f:
|
2022-03-15 17:52:59 +00:00
|
|
|
f.write(self.content)
|
|
|
|
return self.TestResult.Reparse
|
|
|
|
if user_response == "r":
|
|
|
|
return self.TestResult.Reparse
|
|
|
|
|
|
|
|
print("Invalid response.")
|
|
|
|
|
|
|
|
|
|
|
|
def run_testcase(self, testcase: TestParser.RequestAndResponse):
|
|
|
|
"""
|
|
|
|
Runs the given testcase.
|
|
|
|
"""
|
2022-05-23 12:20:03 +00:00
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
requestBodyJson = self.parse_json_with_tags(testcase.request, self.markers)
|
|
|
|
# add textDocument/uri if missing
|
|
|
|
if 'textDocument' not in requestBodyJson:
|
2022-04-27 13:22:26 +00:00
|
|
|
requestBodyJson['textDocument'] = { 'uri': self.suite.get_test_file_uri(self.test_name, self.sub_dir) }
|
2022-05-23 12:20:03 +00:00
|
|
|
|
2022-07-11 14:57:08 +00:00
|
|
|
actualResponseJson = self.solc.call_method(
|
|
|
|
testcase.method,
|
|
|
|
requestBodyJson,
|
|
|
|
expects_response=testcase.response is not None
|
|
|
|
)
|
|
|
|
|
|
|
|
if testcase.response is None:
|
|
|
|
return
|
2022-03-15 17:52:59 +00:00
|
|
|
|
|
|
|
# simplify response
|
2022-05-23 12:20:03 +00:00
|
|
|
if "result" in actualResponseJson:
|
|
|
|
if isinstance(actualResponseJson["result"], list):
|
|
|
|
for result in actualResponseJson["result"]:
|
|
|
|
if "uri" in result:
|
|
|
|
result["uri"] = result["uri"].replace(self.suite.project_root_uri + "/" + self.sub_dir + "/", "")
|
|
|
|
|
|
|
|
elif isinstance(actualResponseJson["result"], dict):
|
|
|
|
if "changes" in actualResponseJson["result"]:
|
|
|
|
changes = actualResponseJson["result"]["changes"]
|
|
|
|
for key in list(changes.keys()):
|
|
|
|
new_key = key.replace(self.suite.project_root_uri + "/", "")
|
|
|
|
changes[new_key] = changes[key]
|
|
|
|
del changes[key]
|
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
if "jsonrpc" in actualResponseJson:
|
|
|
|
actualResponseJson.pop("jsonrpc")
|
|
|
|
|
|
|
|
try:
|
|
|
|
expectedResponseJson = self.parse_json_with_tags(testcase.response, self.markers)
|
|
|
|
except json.decoder.JSONDecodeError:
|
|
|
|
expectedResponseJson = None
|
|
|
|
|
|
|
|
expectedResponseJson = { "result": expectedResponseJson }
|
|
|
|
|
|
|
|
self.suite.expect_equal(
|
|
|
|
actualResponseJson,
|
|
|
|
expectedResponseJson,
|
|
|
|
f"Request failed: \n{testcase.request}",
|
|
|
|
ExpectationFailed.Part.Methods
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_json_with_tags(self, content, markersFallback):
|
|
|
|
"""
|
|
|
|
Replaces any tags with their actual content and parsers the result as
|
|
|
|
json to return it.
|
|
|
|
"""
|
|
|
|
split_by_tag = TEST_REGEXES.findTag.split(content)
|
|
|
|
|
|
|
|
# add quotes so we can parse it as json
|
|
|
|
contentReplaced = '"'.join(split_by_tag)
|
|
|
|
contentJson = json.loads(contentReplaced)
|
|
|
|
|
|
|
|
def replace_tag(data, markers):
|
|
|
|
|
|
|
|
if isinstance(data, list):
|
|
|
|
for el in data:
|
|
|
|
replace_tag(el, markers)
|
|
|
|
return data
|
|
|
|
|
2022-04-27 13:22:26 +00:00
|
|
|
if not isinstance(data, dict):
|
|
|
|
return data
|
|
|
|
|
2022-05-23 12:20:03 +00:00
|
|
|
def findMarker(desired_tag):
|
|
|
|
if not isinstance(desired_tag, str):
|
|
|
|
return desired_tag
|
|
|
|
|
|
|
|
for tag, tagRange in markers.items():
|
|
|
|
if tag == desired_tag:
|
|
|
|
return tagRange
|
|
|
|
elif tag.lower() == desired_tag.lower():
|
|
|
|
raise Exception(f"Detected lower/upper case mismatch: Requested {desired_tag} but only found {tag}")
|
|
|
|
|
|
|
|
raise Exception(f"Marker {desired_tag} not found in file")
|
|
|
|
|
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
# Check if we need markers from a specific file
|
|
|
|
# Needs to be done before the loop or it might be called only after
|
|
|
|
# we found "range" or "position"
|
|
|
|
if "uri" in data:
|
2022-05-23 12:20:03 +00:00
|
|
|
markers = self.suite.get_test_tags(data["uri"][:-len(".sol")], self.sub_dir)
|
2022-03-15 17:52:59 +00:00
|
|
|
|
|
|
|
for key, val in data.items():
|
|
|
|
if key == "range":
|
2022-05-23 12:20:03 +00:00
|
|
|
data[key] = findMarker(val)
|
2022-03-15 17:52:59 +00:00
|
|
|
elif key == "position":
|
2022-05-23 12:20:03 +00:00
|
|
|
tag_range = findMarker(val)
|
|
|
|
if "start" in tag_range:
|
|
|
|
data[key] = tag_range["start"]
|
|
|
|
elif key == "changes":
|
|
|
|
for path, list_of_changes in val.items():
|
|
|
|
test_name, file_sub_dir = split_path(path)
|
|
|
|
markers = self.suite.get_test_tags(test_name[:-len(".sol")], file_sub_dir)
|
|
|
|
for change in list_of_changes:
|
|
|
|
if "range" in change:
|
|
|
|
change["range"] = findMarker(change["range"])
|
2022-03-15 17:52:59 +00:00
|
|
|
elif isinstance(val, dict):
|
|
|
|
replace_tag(val, markers)
|
|
|
|
elif isinstance(val, list):
|
|
|
|
for el in val:
|
|
|
|
replace_tag(el, markers)
|
|
|
|
return data
|
|
|
|
|
|
|
|
return replace_tag(contentJson, markersFallback)
|
2022-03-01 17:56:17 +00:00
|
|
|
|
|
|
|
|
2021-12-13 13:53:40 +00:00
|
|
|
class SolidityLSPTestSuite: # {{{
|
|
|
|
test_counter = Counter()
|
|
|
|
assertion_counter = Counter()
|
|
|
|
print_assertions: bool = False
|
|
|
|
trace_io: bool = False
|
2022-03-23 16:32:33 +00:00
|
|
|
fail_fast: bool = False
|
2021-12-13 13:53:40 +00:00
|
|
|
test_pattern: str
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
colorama.init()
|
|
|
|
args = create_cli_parser().parse_args()
|
|
|
|
self.solc_path = args.solc_path
|
|
|
|
self.project_root_dir = os.path.realpath(args.project_root_dir) + "/test/libsolidity/lsp"
|
2022-04-25 13:35:24 +00:00
|
|
|
self.project_root_uri = PurePath(self.project_root_dir).as_uri()
|
2021-12-13 13:53:40 +00:00
|
|
|
self.print_assertions = args.print_assertions
|
|
|
|
self.trace_io = args.trace_io
|
|
|
|
self.test_pattern = args.test_pattern
|
2022-03-23 16:32:33 +00:00
|
|
|
self.fail_fast = args.fail_fast
|
2022-05-30 17:51:43 +00:00
|
|
|
self.non_interactive = args.non_interactive
|
2022-05-23 12:20:03 +00:00
|
|
|
self.print_solc_pid = args.print_solc_pid
|
2021-12-13 13:53:40 +00:00
|
|
|
|
|
|
|
print(f"{SGR_NOTICE}test pattern: {self.test_pattern}{SGR_RESET}")
|
|
|
|
|
|
|
|
def main(self) -> int:
|
|
|
|
"""
|
|
|
|
Runs all test cases.
|
|
|
|
Returns 0 on success and the number of failing assertions (capped to 127) otherwise.
|
|
|
|
"""
|
|
|
|
all_tests = sorted([
|
|
|
|
str(name)[5:]
|
|
|
|
for name in dir(SolidityLSPTestSuite)
|
|
|
|
if callable(getattr(SolidityLSPTestSuite, name)) and name.startswith("test_")
|
|
|
|
])
|
|
|
|
filtered_tests = fnmatch.filter(all_tests, self.test_pattern)
|
2022-05-30 17:51:43 +00:00
|
|
|
if filtered_tests.count("generic") == 0:
|
|
|
|
filtered_tests.append("generic")
|
|
|
|
|
2021-12-13 13:53:40 +00:00
|
|
|
for method_name in filtered_tests:
|
|
|
|
test_fn = getattr(self, 'test_' + method_name)
|
|
|
|
title: str = test_fn.__name__[5:]
|
|
|
|
print(f"{SGR_TEST_BEGIN}Testing {title} ...{SGR_RESET}")
|
|
|
|
try:
|
2022-05-23 12:20:03 +00:00
|
|
|
with JsonRpcProcess(self.solc_path, ["--lsp"], trace_io=self.trace_io, print_pid=self.print_solc_pid) as solc:
|
2021-12-13 13:53:40 +00:00
|
|
|
test_fn(solc)
|
|
|
|
self.test_counter.passed += 1
|
2022-03-01 17:56:17 +00:00
|
|
|
except ExpectationFailed:
|
2021-12-13 13:53:40 +00:00
|
|
|
self.test_counter.failed += 1
|
|
|
|
print(traceback.format_exc())
|
2022-03-23 16:32:33 +00:00
|
|
|
if self.fail_fast:
|
|
|
|
break
|
2021-12-13 13:53:40 +00:00
|
|
|
except Exception as e: # pragma pylint: disable=broad-except
|
|
|
|
self.test_counter.failed += 1
|
|
|
|
print(f"Unhandled exception {e.__class__.__name__} caught: {e}")
|
|
|
|
print(traceback.format_exc())
|
2022-03-23 16:32:33 +00:00
|
|
|
if self.fail_fast:
|
|
|
|
break
|
2021-12-13 13:53:40 +00:00
|
|
|
|
|
|
|
print(
|
|
|
|
f"\n{SGR_NOTICE}Summary:{SGR_RESET}\n\n"
|
|
|
|
f" Test cases: {self.test_counter.passed} passed, {self.test_counter.failed} failed\n"
|
|
|
|
f" Assertions: {self.assertion_counter.passed} passed, {self.assertion_counter.failed} failed\n"
|
|
|
|
)
|
|
|
|
|
|
|
|
return min(max(self.test_counter.failed, self.assertion_counter.failed), 127)
|
|
|
|
|
|
|
|
def setup_lsp(self, lsp: JsonRpcProcess, expose_project_root=True):
|
|
|
|
"""
|
|
|
|
Prepares the solc LSP server by calling `initialize`,
|
|
|
|
and `initialized` methods.
|
|
|
|
"""
|
|
|
|
params = {
|
|
|
|
'processId': None,
|
|
|
|
'rootUri': self.project_root_uri,
|
2022-07-13 13:46:49 +00:00
|
|
|
# Enable traces to receive the amount of expected diagnostics before
|
|
|
|
# actually receiving them.
|
|
|
|
'trace': 'messages',
|
2021-12-13 13:53:40 +00:00
|
|
|
'initializationOptions': {},
|
|
|
|
'capabilities': {
|
|
|
|
'textDocument': {
|
|
|
|
'publishDiagnostics': {'relatedInformation': True}
|
|
|
|
},
|
|
|
|
'workspace': {
|
|
|
|
'applyEdit': True,
|
|
|
|
'configuration': True,
|
|
|
|
'didChangeConfiguration': {'dynamicRegistration': True},
|
|
|
|
'workspaceEdit': {'documentChanges': True},
|
|
|
|
'workspaceFolders': True
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-21 14:25:14 +00:00
|
|
|
if not expose_project_root:
|
2021-12-13 13:53:40 +00:00
|
|
|
params['rootUri'] = None
|
|
|
|
lsp.call_method('initialize', params)
|
|
|
|
lsp.send_notification('initialized')
|
|
|
|
|
|
|
|
# {{{ helpers
|
2022-04-27 13:22:26 +00:00
|
|
|
def get_test_file_path(self, test_case_name, sub_dir=None):
|
|
|
|
if sub_dir:
|
|
|
|
return f"{self.project_root_dir}/{sub_dir}/{test_case_name}.sol"
|
2021-12-13 13:53:40 +00:00
|
|
|
return f"{self.project_root_dir}/{test_case_name}.sol"
|
|
|
|
|
2022-04-27 13:22:26 +00:00
|
|
|
def get_test_file_uri(self, test_case_name, sub_dir=None):
|
|
|
|
return PurePath(self.get_test_file_path(test_case_name, sub_dir)).as_uri()
|
2021-12-13 13:53:40 +00:00
|
|
|
|
2022-04-27 13:22:26 +00:00
|
|
|
def get_test_file_contents(self, test_case_name, sub_dir=None):
|
2021-12-13 13:53:40 +00:00
|
|
|
"""
|
|
|
|
Reads the file contents from disc for a given test case.
|
|
|
|
The `test_case_name` will be the basename of the file
|
2022-04-27 13:22:26 +00:00
|
|
|
in the test path (test/libsolidity/lsp/{sub_dir}).
|
2021-12-13 13:53:40 +00:00
|
|
|
"""
|
2022-04-27 13:22:26 +00:00
|
|
|
with open(self.get_test_file_path(test_case_name, sub_dir), mode="r", encoding="utf-8", newline='') as f:
|
2022-04-25 15:21:45 +00:00
|
|
|
return f.read().replace("\r\n", "\n")
|
2021-12-13 13:53:40 +00:00
|
|
|
|
|
|
|
def require_params_for_method(self, method_name: str, message: dict) -> Any:
|
|
|
|
"""
|
|
|
|
Ensures the given RPC message does contain the
|
|
|
|
field 'method' with the given method name,
|
|
|
|
and then returns its passed params.
|
|
|
|
An exception is raised on expectation failures.
|
|
|
|
"""
|
|
|
|
assert message is not None
|
|
|
|
if 'error' in message.keys():
|
|
|
|
code = message['error']["code"]
|
|
|
|
text = message['error']['message']
|
|
|
|
raise RuntimeError(f"Error {code} received. {text}")
|
|
|
|
if 'method' not in message.keys():
|
|
|
|
raise RuntimeError("No method received but something else.")
|
2022-03-15 17:52:59 +00:00
|
|
|
self.expect_equal(message['method'], method_name, description="Ensure expected method name")
|
2021-12-13 13:53:40 +00:00
|
|
|
return message['params']
|
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
def wait_for_diagnostics(self, solc: JsonRpcProcess) -> List[dict]:
|
2021-12-13 13:53:40 +00:00
|
|
|
"""
|
2022-03-15 17:52:59 +00:00
|
|
|
Return all published diagnostic reports sorted by file URI.
|
2021-12-13 13:53:40 +00:00
|
|
|
"""
|
|
|
|
reports = []
|
2022-03-15 17:52:59 +00:00
|
|
|
|
|
|
|
num_files = solc.receive_message()["params"]["openFileCount"]
|
|
|
|
|
|
|
|
for _ in range(0, num_files):
|
2021-12-13 13:53:40 +00:00
|
|
|
message = solc.receive_message()
|
2022-03-15 17:52:59 +00:00
|
|
|
|
2021-12-13 13:53:40 +00:00
|
|
|
assert message is not None # This can happen if the server aborts early.
|
2022-03-15 17:52:59 +00:00
|
|
|
|
2021-12-13 13:53:40 +00:00
|
|
|
reports.append(
|
|
|
|
self.require_params_for_method(
|
|
|
|
'textDocument/publishDiagnostics',
|
|
|
|
message,
|
|
|
|
)
|
|
|
|
)
|
2022-03-15 17:52:59 +00:00
|
|
|
|
2021-12-13 13:53:40 +00:00
|
|
|
return sorted(reports, key=lambda x: x['uri'])
|
|
|
|
|
2022-05-30 17:51:43 +00:00
|
|
|
def normalizeUri(self, uri):
|
|
|
|
return uri.replace(self.project_root_uri + "/", "")[:-len(".sol")]
|
|
|
|
|
2022-04-27 13:22:26 +00:00
|
|
|
def fetch_and_format_diagnostics(self, solc: JsonRpcProcess, test, sub_dir=None):
|
2022-03-15 17:52:59 +00:00
|
|
|
expectations = ""
|
|
|
|
|
2022-04-27 13:22:26 +00:00
|
|
|
published_diagnostics = self.open_file_and_wait_for_diagnostics(solc, test, sub_dir)
|
2022-03-15 17:52:59 +00:00
|
|
|
|
2022-05-30 17:51:43 +00:00
|
|
|
for file_diagnostics in published_diagnostics:
|
|
|
|
testname, local_sub_dir = split_path(self.normalizeUri(file_diagnostics["uri"]))
|
2022-03-15 17:52:59 +00:00
|
|
|
|
|
|
|
# Skip empty diagnostics within the same file
|
2022-05-30 17:51:43 +00:00
|
|
|
if len(file_diagnostics["diagnostics"]) == 0 and testname == test:
|
2022-03-15 17:52:59 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
expectations += f"// {testname}:"
|
|
|
|
|
2022-05-30 17:51:43 +00:00
|
|
|
for diagnostic in file_diagnostics["diagnostics"]:
|
|
|
|
tag = self.find_tag_with_range(testname, local_sub_dir, diagnostic['range'])
|
2022-03-15 17:52:59 +00:00
|
|
|
|
|
|
|
if tag is None:
|
|
|
|
raise Exception(f"No tag found for diagnostic range {diagnostic['range']}")
|
|
|
|
|
|
|
|
expectations += f" {tag} {diagnostic['code']}"
|
|
|
|
expectations += "\n"
|
|
|
|
|
|
|
|
return expectations
|
|
|
|
|
|
|
|
def update_diagnostics_in_file(
|
|
|
|
self,
|
|
|
|
solc: JsonRpcProcess,
|
|
|
|
test,
|
2022-04-27 13:22:26 +00:00
|
|
|
sub_dir,
|
2022-03-15 17:52:59 +00:00
|
|
|
content,
|
|
|
|
current_diagnostics: TestParser.Diagnostics
|
|
|
|
):
|
|
|
|
test_header = ""
|
|
|
|
|
|
|
|
if not current_diagnostics.has_header:
|
|
|
|
test_header = f"{TestParser.TEST_START}\n"
|
|
|
|
|
|
|
|
content = content[:current_diagnostics.start] + \
|
|
|
|
test_header + \
|
2022-04-27 13:22:26 +00:00
|
|
|
self.fetch_and_format_diagnostics(solc, test, sub_dir) + \
|
2022-03-15 17:52:59 +00:00
|
|
|
content[current_diagnostics.end:]
|
|
|
|
|
2022-04-27 13:22:26 +00:00
|
|
|
with open(self.get_test_file_path(test, sub_dir), mode="w", encoding="utf-8", newline='') as f:
|
2022-03-15 17:52:59 +00:00
|
|
|
f.write(content)
|
|
|
|
|
|
|
|
return content
|
|
|
|
|
2021-12-13 13:53:40 +00:00
|
|
|
def open_file_and_wait_for_diagnostics(
|
|
|
|
self,
|
|
|
|
solc_process: JsonRpcProcess,
|
|
|
|
test_case_name: str,
|
2022-04-27 13:22:26 +00:00
|
|
|
sub_dir=None
|
2021-12-13 13:53:40 +00:00
|
|
|
) -> List[Any]:
|
|
|
|
"""
|
|
|
|
Opens file for given test case and waits for diagnostics to be published.
|
|
|
|
"""
|
|
|
|
solc_process.send_message(
|
|
|
|
'textDocument/didOpen',
|
|
|
|
{
|
|
|
|
'textDocument':
|
|
|
|
{
|
2022-04-27 13:22:26 +00:00
|
|
|
'uri': self.get_test_file_uri(test_case_name, sub_dir),
|
2021-12-13 13:53:40 +00:00
|
|
|
'languageId': 'Solidity',
|
|
|
|
'version': 1,
|
2022-04-27 13:22:26 +00:00
|
|
|
'text': self.get_test_file_contents(test_case_name, sub_dir)
|
2021-12-13 13:53:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2022-03-15 17:52:59 +00:00
|
|
|
return self.wait_for_diagnostics(solc_process)
|
2021-12-13 13:53:40 +00:00
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
def expect_equal(
|
|
|
|
self,
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
description="Equality",
|
|
|
|
part=ExpectationFailed.Part.Diagnostics
|
|
|
|
) -> None:
|
2021-12-13 13:53:40 +00:00
|
|
|
self.assertion_counter.total += 1
|
|
|
|
prefix = f"[{self.assertion_counter.total}] {SGR_ASSERT_BEGIN}{description}: "
|
|
|
|
diff = DeepDiff(actual, expected)
|
|
|
|
if len(diff) == 0:
|
|
|
|
self.assertion_counter.passed += 1
|
|
|
|
if self.print_assertions:
|
|
|
|
print(prefix + SGR_STATUS_OKAY + 'OK' + SGR_RESET)
|
|
|
|
return
|
|
|
|
|
|
|
|
# Failed assertions are always printed.
|
|
|
|
self.assertion_counter.failed += 1
|
|
|
|
print(prefix + SGR_STATUS_FAIL + 'FAILED' + SGR_RESET)
|
2022-03-15 17:52:59 +00:00
|
|
|
raise JSONExpectationFailed(actual, expected, part)
|
2021-12-13 13:53:40 +00:00
|
|
|
|
|
|
|
def expect_empty_diagnostics(self, published_diagnostics: List[dict]) -> None:
|
|
|
|
self.expect_equal(len(published_diagnostics), 1, "one publish diagnostics notification")
|
|
|
|
self.expect_equal(len(published_diagnostics[0]['diagnostics']), 0, "should not contain diagnostics")
|
|
|
|
|
|
|
|
def expect_diagnostic(
|
|
|
|
self,
|
|
|
|
diagnostic,
|
|
|
|
code: int,
|
2022-03-01 17:56:17 +00:00
|
|
|
lineNo: int = None,
|
|
|
|
startEndColumns: Tuple[int, int] = None,
|
|
|
|
marker: {} = None
|
2021-12-13 13:53:40 +00:00
|
|
|
):
|
2022-03-15 17:52:59 +00:00
|
|
|
self.expect_equal(
|
|
|
|
diagnostic['code'],
|
|
|
|
code,
|
|
|
|
ExpectationFailed.Part.Diagnostics,
|
|
|
|
f'diagnostic: {code}'
|
|
|
|
)
|
2022-03-01 17:56:17 +00:00
|
|
|
|
|
|
|
if marker:
|
2022-03-15 17:52:59 +00:00
|
|
|
self.expect_equal(
|
|
|
|
diagnostic['range'],
|
|
|
|
marker,
|
|
|
|
ExpectationFailed.Part.Diagnostics,
|
|
|
|
"diagnostic: check range"
|
|
|
|
)
|
|
|
|
|
2022-03-01 17:56:17 +00:00
|
|
|
else:
|
|
|
|
assert len(startEndColumns) == 2
|
|
|
|
[startColumn, endColumn] = startEndColumns
|
|
|
|
self.expect_equal(
|
|
|
|
diagnostic['range'],
|
|
|
|
{
|
|
|
|
'start': {'character': startColumn, 'line': lineNo},
|
|
|
|
'end': {'character': endColumn, 'line': lineNo}
|
|
|
|
},
|
2022-03-15 17:52:59 +00:00
|
|
|
ExpectationFailed.Part.Diagnostics,
|
2022-03-01 17:56:17 +00:00
|
|
|
"diagnostic: check range"
|
|
|
|
)
|
|
|
|
|
2021-12-20 13:10:17 +00:00
|
|
|
|
|
|
|
def expect_location(
|
|
|
|
self,
|
|
|
|
obj: dict,
|
|
|
|
uri: str,
|
|
|
|
lineNo: int,
|
|
|
|
startEndColumns: Tuple[int, int]
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
obj is an JSON object containing two keys:
|
|
|
|
- 'uri': a string of the document URI
|
|
|
|
- 'range': the location range, two child objects 'start' and 'end',
|
|
|
|
each having a 'line' and 'character' integer value.
|
|
|
|
"""
|
|
|
|
[startColumn, endColumn] = startEndColumns
|
|
|
|
self.expect_equal(obj['uri'], uri)
|
|
|
|
self.expect_equal(obj['range']['start']['line'], lineNo)
|
|
|
|
self.expect_equal(obj['range']['start']['character'], startColumn)
|
|
|
|
self.expect_equal(obj['range']['end']['line'], lineNo)
|
|
|
|
self.expect_equal(obj['range']['end']['character'], endColumn)
|
|
|
|
|
|
|
|
def expect_goto_definition_location(
|
|
|
|
self,
|
|
|
|
solc: JsonRpcProcess,
|
|
|
|
document_uri: str,
|
|
|
|
document_position: Tuple[int, int],
|
|
|
|
expected_uri: str,
|
|
|
|
expected_lineNo: int,
|
|
|
|
expected_startEndColumns: Tuple[int, int],
|
|
|
|
description: str
|
|
|
|
):
|
|
|
|
response = solc.call_method(
|
|
|
|
'textDocument/definition',
|
|
|
|
{
|
|
|
|
'textDocument': {
|
|
|
|
'uri': document_uri,
|
|
|
|
},
|
|
|
|
'position': {
|
|
|
|
'line': document_position[0],
|
|
|
|
'character': document_position[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
message = "Goto definition (" + description + ")"
|
|
|
|
self.expect_equal(len(response['result']), 1, message)
|
|
|
|
self.expect_location(response['result'][0], expected_uri, expected_lineNo, expected_startEndColumns)
|
2021-12-13 13:53:40 +00:00
|
|
|
|
|
|
|
|
2022-04-27 13:22:26 +00:00
|
|
|
def find_tag_with_range(self, test, sub_dir, target_range):
|
2022-03-15 17:52:59 +00:00
|
|
|
"""
|
|
|
|
Find and return the tag that represents the requested range otherwise
|
|
|
|
return None.
|
|
|
|
"""
|
2022-05-23 12:20:03 +00:00
|
|
|
markers = self.get_test_tags(test, sub_dir)
|
2021-12-13 13:53:40 +00:00
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
for tag, tag_range in markers.items():
|
|
|
|
if tag_range == target_range:
|
|
|
|
return str(tag)
|
2022-03-01 17:56:17 +00:00
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
return None
|
2021-12-13 13:53:40 +00:00
|
|
|
|
2022-04-27 13:22:26 +00:00
|
|
|
def replace_ranges_with_tags(self, content, sub_dir):
|
2022-03-15 17:52:59 +00:00
|
|
|
"""
|
|
|
|
Replace matching ranges with "@<tagname>".
|
2022-05-23 12:20:03 +00:00
|
|
|
|
|
|
|
Recognized patterns:
|
|
|
|
{ "changes": { "<uri>": { "range": "<range>" } } }
|
|
|
|
{ "uri": "<uri>", "range": "<range> }
|
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
"""
|
2021-12-13 13:53:40 +00:00
|
|
|
|
2022-05-23 12:20:03 +00:00
|
|
|
def replace_range(item: dict, markers):
|
|
|
|
for tag, tagRange in markers.items():
|
|
|
|
if "range" in item and tagRange == item["range"]:
|
|
|
|
item["range"] = str(tag)
|
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
def recursive_iter(obj):
|
|
|
|
if isinstance(obj, dict):
|
|
|
|
yield obj
|
|
|
|
for item in obj.values():
|
|
|
|
yield from recursive_iter(item)
|
|
|
|
elif any(isinstance(obj, t) for t in (list, tuple)):
|
|
|
|
for item in obj:
|
|
|
|
yield from recursive_iter(item)
|
|
|
|
|
|
|
|
for item in recursive_iter(content):
|
|
|
|
if "uri" in item and "range" in item:
|
2022-05-23 12:20:03 +00:00
|
|
|
try:
|
|
|
|
markers = self.get_test_tags(item["uri"][:-len(".sol")], sub_dir)
|
|
|
|
replace_range(item, markers)
|
|
|
|
except FileNotFoundError:
|
|
|
|
# Skip over errors as this is user provided input that can
|
|
|
|
# point to non-existing files
|
|
|
|
pass
|
|
|
|
elif "changes" in item:
|
|
|
|
for file, changes_for_file in item["changes"].items():
|
|
|
|
test_name, file_sub_dir = split_path(file)
|
|
|
|
try:
|
|
|
|
markers = self.get_test_tags(test_name[:-len(".sol")], file_sub_dir)
|
|
|
|
for change in changes_for_file:
|
|
|
|
replace_range(change, markers)
|
|
|
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
# Skip over errors as this is user provided input that can
|
|
|
|
# point to non-existing files
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
|
|
|
|
# Convert JSON to string and split it at the quoted tags
|
|
|
|
split_by_tag = TEST_REGEXES.findQuotedTag.split(json.dumps(content, indent=4, sort_keys=True))
|
|
|
|
|
|
|
|
# remove the quotes and return result
|
|
|
|
return "".join(map(lambda p: p[1:-1] if p.startswith('"@') else p, split_by_tag))
|
|
|
|
|
|
|
|
def user_interaction_failed_diagnostics(
|
|
|
|
self,
|
|
|
|
solc: JsonRpcProcess,
|
|
|
|
test,
|
2022-04-27 13:22:26 +00:00
|
|
|
sub_dir,
|
2022-03-15 17:52:59 +00:00
|
|
|
content,
|
|
|
|
current_diagnostics: TestParser.Diagnostics
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Asks the user how to proceed after an error.
|
|
|
|
Returns True if the test/file should be ignored, otherwise False
|
|
|
|
"""
|
2022-05-30 17:51:43 +00:00
|
|
|
|
|
|
|
# Prevent user interaction when in non-interactive mode
|
|
|
|
if self.non_interactive:
|
|
|
|
return False
|
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
while True:
|
|
|
|
print("(u)pdate/(r)etry/(s)kip file?")
|
2022-04-25 15:21:45 +00:00
|
|
|
user_response = getCharFromStdin()
|
2022-03-15 17:52:59 +00:00
|
|
|
if user_response == "u":
|
|
|
|
while True:
|
|
|
|
try:
|
2022-04-27 13:22:26 +00:00
|
|
|
self.update_diagnostics_in_file(solc, test, sub_dir, content, current_diagnostics)
|
2022-03-15 17:52:59 +00:00
|
|
|
return False
|
|
|
|
# pragma pylint: disable=broad-except
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2022-04-27 13:22:26 +00:00
|
|
|
if self.user_interaction_failed_autoupdate(test, sub_dir):
|
2022-04-25 15:21:45 +00:00
|
|
|
return True
|
2022-03-15 17:52:59 +00:00
|
|
|
elif user_response == 's':
|
|
|
|
return True
|
|
|
|
elif user_response == 'r':
|
|
|
|
return False
|
|
|
|
|
2022-04-27 13:22:26 +00:00
|
|
|
def user_interaction_failed_autoupdate(self, test, sub_dir):
|
2022-03-15 17:52:59 +00:00
|
|
|
print("(e)dit/(r)etry/(s)kip file?")
|
2022-04-25 15:21:45 +00:00
|
|
|
user_response = getCharFromStdin()
|
2022-03-15 17:52:59 +00:00
|
|
|
if user_response == "r":
|
|
|
|
print("retrying...")
|
|
|
|
# pragma pylint: disable=no-member
|
2022-05-23 12:20:03 +00:00
|
|
|
self.get_test_tags.cache_clear()
|
2022-03-15 17:52:59 +00:00
|
|
|
return False
|
|
|
|
if user_response == "e":
|
|
|
|
editor = os.environ.get('VISUAL', os.environ.get('EDITOR', 'vi'))
|
|
|
|
subprocess.run(
|
2022-04-27 13:22:26 +00:00
|
|
|
f'{editor} {self.get_test_file_path(test, sub_dir)}',
|
2022-03-15 17:52:59 +00:00
|
|
|
shell=True,
|
|
|
|
check=True
|
|
|
|
)
|
|
|
|
# pragma pylint: disable=no-member
|
2022-05-23 12:20:03 +00:00
|
|
|
self.get_test_tags.cache_clear()
|
2022-03-15 17:52:59 +00:00
|
|
|
elif user_response == "s":
|
|
|
|
print("skipping...")
|
2021-12-13 13:53:40 +00:00
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
return True
|
2021-12-13 13:53:40 +00:00
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
# }}}
|
2022-03-01 17:56:17 +00:00
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
# {{{ actual tests
|
2021-12-13 13:53:40 +00:00
|
|
|
|
|
|
|
def test_publish_diagnostics_errors_multiline(self, solc: JsonRpcProcess) -> None:
|
|
|
|
self.setup_lsp(solc)
|
|
|
|
TEST_NAME = 'publish_diagnostics_3'
|
|
|
|
published_diagnostics = self.open_file_and_wait_for_diagnostics(solc, TEST_NAME)
|
|
|
|
|
|
|
|
self.expect_equal(len(published_diagnostics), 1, "One published_diagnostics message")
|
|
|
|
report = published_diagnostics[0]
|
|
|
|
|
|
|
|
self.expect_equal(report['uri'], self.get_test_file_uri(TEST_NAME), "Correct file URI")
|
|
|
|
diagnostics = report['diagnostics']
|
|
|
|
|
2022-03-01 17:56:17 +00:00
|
|
|
self.expect_equal(len(diagnostics), 1, "1 diagnostic messages")
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(diagnostics[0]['code'], 3656, "diagnostic: check code")
|
|
|
|
self.expect_equal(
|
|
|
|
diagnostics[0]['range'],
|
|
|
|
{
|
2022-03-01 17:56:17 +00:00
|
|
|
'start': {'character': 0, 'line': 7},
|
|
|
|
'end': {'character': 1, 'line': 10}
|
2021-12-13 13:53:40 +00:00
|
|
|
},
|
|
|
|
"diagnostic: check range"
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_textDocument_didOpen_with_relative_import(self, solc: JsonRpcProcess) -> None:
|
|
|
|
self.setup_lsp(solc)
|
|
|
|
TEST_NAME = 'didOpen_with_import'
|
2022-03-15 17:52:59 +00:00
|
|
|
published_diagnostics = self.open_file_and_wait_for_diagnostics(solc, TEST_NAME)
|
2021-12-13 13:53:40 +00:00
|
|
|
|
|
|
|
self.expect_equal(len(published_diagnostics), 2, "Diagnostic reports for 2 files")
|
|
|
|
|
|
|
|
# primary file:
|
|
|
|
report = published_diagnostics[0]
|
|
|
|
self.expect_equal(report['uri'], self.get_test_file_uri(TEST_NAME), "Correct file URI")
|
|
|
|
self.expect_equal(len(report['diagnostics']), 0, "no diagnostics")
|
|
|
|
|
2022-04-27 13:22:26 +00:00
|
|
|
# imported file (goto/lib.sol):
|
2021-12-13 13:53:40 +00:00
|
|
|
report = published_diagnostics[1]
|
2022-04-27 13:22:26 +00:00
|
|
|
self.expect_equal(report['uri'], self.get_test_file_uri('lib', 'goto'), "Correct file URI")
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(report['diagnostics']), 1, "one diagnostic")
|
2022-05-23 12:20:03 +00:00
|
|
|
marker = self.get_test_tags("lib", "goto")["@diagnostics"]
|
2022-03-01 17:56:17 +00:00
|
|
|
self.expect_diagnostic(report['diagnostics'][0], code=2072, marker=marker)
|
|
|
|
|
2022-04-25 15:21:45 +00:00
|
|
|
@functools.lru_cache() # pragma pylint: disable=lru-cache-decorating-method
|
2022-05-23 12:20:03 +00:00
|
|
|
def get_test_tags(self, test_name: TestName, sub_dir=None, verbose=False):
|
2022-03-01 17:56:17 +00:00
|
|
|
"""
|
|
|
|
Finds all tags (e.g. @tagname) in the given test and returns them as a
|
|
|
|
dictionary having the following structure: {
|
|
|
|
"@tagname": {
|
|
|
|
"start": { "character": 3, "line": 2 },
|
|
|
|
"end": { "character": 30, "line": 2 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2022-04-27 13:22:26 +00:00
|
|
|
content = self.get_test_file_contents(test_name, sub_dir)
|
2022-03-01 17:56:17 +00:00
|
|
|
|
|
|
|
markers = {}
|
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
for lineNum, line in tags_only(content.splitlines()):
|
2022-03-01 17:56:17 +00:00
|
|
|
commentStart = line.find("//")
|
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
for kind, regex in TAG_REGEXES._asdict().items():
|
2022-03-01 17:56:17 +00:00
|
|
|
for match in regex.finditer(line[commentStart:]):
|
2022-03-15 17:52:59 +00:00
|
|
|
if kind == "simpleRange":
|
2022-03-01 17:56:17 +00:00
|
|
|
markers[match.group("tag")] = {
|
|
|
|
"start": {
|
|
|
|
"line": lineNum,
|
|
|
|
"character": match.start("range") + commentStart
|
|
|
|
},
|
|
|
|
"end": {
|
|
|
|
"line": lineNum,
|
|
|
|
"character": match.end("range") + commentStart
|
|
|
|
}}
|
2022-03-15 17:52:59 +00:00
|
|
|
elif kind == "multilineRange":
|
2022-03-01 17:56:17 +00:00
|
|
|
if match.group("delimiter") == "(":
|
|
|
|
markers[match.group("tag")] = \
|
|
|
|
{ "start": { "line": lineNum, "character": 0 } }
|
|
|
|
elif match.group("delimiter") == ")":
|
|
|
|
markers[match.group("tag")]["end"] = \
|
|
|
|
{ "line": lineNum, "character": 0 }
|
|
|
|
|
|
|
|
if verbose:
|
|
|
|
print(markers)
|
|
|
|
return markers
|
|
|
|
|
2021-12-13 13:53:40 +00:00
|
|
|
|
2022-07-11 11:30:04 +00:00
|
|
|
def test_custom_includes(self, solc: JsonRpcProcess) -> None:
|
|
|
|
self.setup_lsp(solc, expose_project_root=False)
|
|
|
|
solc.send_notification(
|
|
|
|
'workspace/didChangeConfiguration', {
|
|
|
|
'settings': {
|
|
|
|
'include-paths': [
|
|
|
|
f"{self.project_root_dir}/other-include-dir"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
published_diagnostics = self.open_file_and_wait_for_diagnostics(solc, 'include-paths/using-custom-includes')
|
|
|
|
|
|
|
|
self.expect_equal(len(published_diagnostics), 2, "Diagnostic reports for 2 files")
|
|
|
|
|
|
|
|
# test file
|
|
|
|
report = published_diagnostics[0]
|
|
|
|
self.expect_equal(report['uri'], self.get_test_file_uri('using-custom-includes', 'include-paths'))
|
|
|
|
diagnostics = report['diagnostics']
|
|
|
|
self.expect_equal(len(diagnostics), 0, "no diagnostics")
|
|
|
|
|
|
|
|
# imported file
|
|
|
|
report = published_diagnostics[1]
|
|
|
|
self.expect_equal(report['uri'], f"{self.project_root_uri}/other-include-dir/otherlib/otherlib.sol")
|
|
|
|
diagnostics = report['diagnostics']
|
|
|
|
self.expect_equal(len(diagnostics), 1, "no diagnostics")
|
|
|
|
self.expect_diagnostic(diagnostics[0], code=2018, lineNo=5, startEndColumns=(4, 62))
|
|
|
|
|
2021-12-13 13:53:40 +00:00
|
|
|
def test_didChange_in_A_causing_error_in_B(self, solc: JsonRpcProcess) -> None:
|
|
|
|
# Reusing another test but now change some file that generates an error in the other.
|
|
|
|
self.test_textDocument_didOpen_with_relative_import(solc)
|
2022-05-23 12:20:03 +00:00
|
|
|
marker = self.get_test_tags("lib", "goto")["@addFunction"]
|
2022-04-27 13:22:26 +00:00
|
|
|
self.open_file_and_wait_for_diagnostics(solc, 'lib', "goto")
|
2021-12-13 13:53:40 +00:00
|
|
|
solc.send_message(
|
|
|
|
'textDocument/didChange',
|
|
|
|
{
|
|
|
|
'textDocument':
|
|
|
|
{
|
2022-04-27 13:22:26 +00:00
|
|
|
'uri': self.get_test_file_uri('lib', 'goto')
|
2021-12-13 13:53:40 +00:00
|
|
|
},
|
|
|
|
'contentChanges':
|
|
|
|
[
|
|
|
|
{
|
2022-03-01 17:56:17 +00:00
|
|
|
'range': marker,
|
2021-12-13 13:53:40 +00:00
|
|
|
'text': "" # deleting function `add`
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
2022-03-15 17:52:59 +00:00
|
|
|
published_diagnostics = self.wait_for_diagnostics(solc)
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(published_diagnostics), 2, "Diagnostic reports for 2 files")
|
|
|
|
|
|
|
|
# Main file now contains a new diagnostic
|
|
|
|
report = published_diagnostics[0]
|
|
|
|
self.expect_equal(report['uri'], self.get_test_file_uri('didOpen_with_import'))
|
|
|
|
diagnostics = report['diagnostics']
|
2022-05-23 12:20:03 +00:00
|
|
|
marker = self.get_test_tags("didOpen_with_import")["@diagnostics"]
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(diagnostics), 1, "now, no diagnostics")
|
2022-03-01 17:56:17 +00:00
|
|
|
self.expect_diagnostic(diagnostics[0], code=9582, marker=marker)
|
2021-12-13 13:53:40 +00:00
|
|
|
|
|
|
|
# The modified file retains the same diagnostics.
|
|
|
|
report = published_diagnostics[1]
|
2022-04-27 13:22:26 +00:00
|
|
|
self.expect_equal(report['uri'], self.get_test_file_uri('lib', 'goto'))
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(report['diagnostics']), 0)
|
|
|
|
# The warning went away because the compiler aborts further processing after the error.
|
|
|
|
|
|
|
|
def test_textDocument_didOpen_with_relative_import_without_project_url(self, solc: JsonRpcProcess) -> None:
|
|
|
|
self.setup_lsp(solc, expose_project_root=False)
|
|
|
|
TEST_NAME = 'didOpen_with_import'
|
2022-03-15 17:52:59 +00:00
|
|
|
published_diagnostics = self.open_file_and_wait_for_diagnostics(solc, TEST_NAME)
|
2021-12-13 13:53:40 +00:00
|
|
|
self.verify_didOpen_with_import_diagnostics(published_diagnostics)
|
|
|
|
|
|
|
|
def verify_didOpen_with_import_diagnostics(
|
|
|
|
self,
|
|
|
|
published_diagnostics: List[Any],
|
|
|
|
main_file_name='didOpen_with_import'
|
|
|
|
):
|
|
|
|
self.expect_equal(len(published_diagnostics), 2, "Diagnostic reports for 2 files")
|
|
|
|
|
|
|
|
# primary file:
|
|
|
|
report = published_diagnostics[0]
|
|
|
|
self.expect_equal(report['uri'], self.get_test_file_uri(main_file_name), "Correct file URI")
|
|
|
|
self.expect_equal(len(report['diagnostics']), 0, "one diagnostic")
|
|
|
|
|
2022-04-27 13:22:26 +00:00
|
|
|
# imported file (./goto/lib.sol):
|
2021-12-13 13:53:40 +00:00
|
|
|
report = published_diagnostics[1]
|
2022-04-27 13:22:26 +00:00
|
|
|
self.expect_equal(report['uri'], self.get_test_file_uri('lib', 'goto'), "Correct file URI")
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(report['diagnostics']), 1, "one diagnostic")
|
2022-03-01 17:56:17 +00:00
|
|
|
|
2022-05-23 12:20:03 +00:00
|
|
|
markers = self.get_test_tags('lib', 'goto')
|
2022-03-15 17:52:59 +00:00
|
|
|
marker = markers["@diagnostics"]
|
2022-03-01 17:56:17 +00:00
|
|
|
self.expect_diagnostic(report['diagnostics'][0], code=2072, marker=marker)
|
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
def test_generic(self, solc: JsonRpcProcess) -> None:
|
|
|
|
self.setup_lsp(solc)
|
|
|
|
|
2022-04-27 13:22:26 +00:00
|
|
|
sub_dirs = filter(
|
2022-05-09 15:34:35 +00:00
|
|
|
lambda filepath: filepath.is_dir() and filepath.name != 'node_modules',
|
2022-04-27 13:22:26 +00:00
|
|
|
os.scandir(self.project_root_dir)
|
2022-03-15 17:52:59 +00:00
|
|
|
)
|
|
|
|
|
2022-04-27 13:22:26 +00:00
|
|
|
for sub_dir in map(lambda filepath: filepath.name, sub_dirs):
|
|
|
|
tests = map(
|
2022-07-11 14:57:08 +00:00
|
|
|
lambda file_object, sd=sub_dir: sd + "/" + file_object.name[:-len(".sol")],
|
|
|
|
filter(
|
|
|
|
lambda filepath: filepath.is_file() and filepath.name.endswith('.sol'),
|
|
|
|
os.scandir(f"{self.project_root_dir}/{sub_dir}")
|
|
|
|
)
|
2022-04-27 13:22:26 +00:00
|
|
|
)
|
2022-03-15 17:52:59 +00:00
|
|
|
|
2022-05-30 17:51:43 +00:00
|
|
|
tests = map(
|
|
|
|
lambda path, sd=sub_dir: path[len(sd)+1:],
|
|
|
|
fnmatch.filter(tests, self.test_pattern)
|
|
|
|
)
|
|
|
|
|
2022-04-27 13:22:26 +00:00
|
|
|
print(f"Running tests in subdirectory '{sub_dir}'...")
|
|
|
|
for test in tests:
|
|
|
|
try_again = True
|
|
|
|
print(f"\t{test}")
|
2022-03-15 17:52:59 +00:00
|
|
|
|
2022-04-27 13:22:26 +00:00
|
|
|
while try_again:
|
|
|
|
runner = FileTestRunner(test, sub_dir, solc, self)
|
|
|
|
|
|
|
|
try:
|
|
|
|
runner.test_diagnostics()
|
|
|
|
try_again = not runner.test_methods()
|
|
|
|
except ExpectationFailed as e:
|
|
|
|
print(e)
|
|
|
|
|
|
|
|
if e.part == e.Part.Diagnostics:
|
|
|
|
try_again = not self.user_interaction_failed_diagnostics(
|
|
|
|
solc,
|
|
|
|
test,
|
|
|
|
sub_dir,
|
|
|
|
runner.content,
|
|
|
|
runner.expected_diagnostics
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
raise
|
2021-12-13 13:53:40 +00:00
|
|
|
|
|
|
|
def test_textDocument_didChange_updates_diagnostics(self, solc: JsonRpcProcess) -> None:
|
|
|
|
self.setup_lsp(solc)
|
|
|
|
TEST_NAME = 'publish_diagnostics_1'
|
2022-04-27 13:22:26 +00:00
|
|
|
published_diagnostics = self.open_file_and_wait_for_diagnostics(solc, TEST_NAME, "goto")
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(published_diagnostics), 1, "One published_diagnostics message")
|
|
|
|
report = published_diagnostics[0]
|
2022-04-27 13:22:26 +00:00
|
|
|
self.expect_equal(report['uri'], self.get_test_file_uri(TEST_NAME, "goto"), "Correct file URI")
|
2021-12-13 13:53:40 +00:00
|
|
|
diagnostics = report['diagnostics']
|
|
|
|
self.expect_equal(len(diagnostics), 3, "3 diagnostic messages")
|
2022-05-23 12:20:03 +00:00
|
|
|
markers = self.get_test_tags(TEST_NAME, "goto")
|
2022-03-01 17:56:17 +00:00
|
|
|
self.expect_diagnostic(diagnostics[0], code=6321, marker=markers["@unusedReturnVariable"])
|
|
|
|
self.expect_diagnostic(diagnostics[1], code=2072, marker=markers["@unusedVariable"])
|
|
|
|
self.expect_diagnostic(diagnostics[2], code=2072, marker=markers["@unusedContractVariable"])
|
2021-12-13 13:53:40 +00:00
|
|
|
|
|
|
|
solc.send_message(
|
|
|
|
'textDocument/didChange',
|
|
|
|
{
|
|
|
|
'textDocument': {
|
2022-04-27 13:22:26 +00:00
|
|
|
'uri': self.get_test_file_uri(TEST_NAME, "goto")
|
2021-12-13 13:53:40 +00:00
|
|
|
},
|
|
|
|
'contentChanges': [
|
|
|
|
{
|
2022-03-01 17:56:17 +00:00
|
|
|
'range': extendEnd(markers["@unusedVariable"]),
|
2021-12-13 13:53:40 +00:00
|
|
|
'text': ""
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
2022-03-15 17:52:59 +00:00
|
|
|
published_diagnostics = self.wait_for_diagnostics(solc)
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(published_diagnostics), 1)
|
|
|
|
report = published_diagnostics[0]
|
2022-04-27 13:22:26 +00:00
|
|
|
self.expect_equal(report['uri'], self.get_test_file_uri(TEST_NAME, "goto"), "Correct file URI")
|
2021-12-13 13:53:40 +00:00
|
|
|
diagnostics = report['diagnostics']
|
|
|
|
self.expect_equal(len(diagnostics), 2)
|
2022-03-01 17:56:17 +00:00
|
|
|
self.expect_diagnostic(diagnostics[0], code=6321, marker=markers["@unusedReturnVariable"])
|
|
|
|
self.expect_diagnostic(diagnostics[1], code=2072, marker=markers["@unusedContractVariable"])
|
2021-12-13 13:53:40 +00:00
|
|
|
|
|
|
|
def test_textDocument_didChange_delete_line_and_close(self, solc: JsonRpcProcess) -> None:
|
|
|
|
# Reuse this test to prepare and ensure it is as expected
|
|
|
|
self.test_textDocument_didOpen_with_relative_import(solc)
|
2022-04-27 13:22:26 +00:00
|
|
|
self.open_file_and_wait_for_diagnostics(solc, 'lib', 'goto')
|
2022-03-01 17:56:17 +00:00
|
|
|
|
2022-05-23 12:20:03 +00:00
|
|
|
marker = self.get_test_tags('lib', 'goto')["@diagnostics"]
|
2022-03-01 17:56:17 +00:00
|
|
|
|
2021-12-13 13:53:40 +00:00
|
|
|
# lib.sol: Fix the unused variable message by removing it.
|
|
|
|
solc.send_message(
|
|
|
|
'textDocument/didChange',
|
|
|
|
{
|
|
|
|
'textDocument':
|
|
|
|
{
|
2022-04-27 13:22:26 +00:00
|
|
|
'uri': self.get_test_file_uri('lib', 'goto')
|
2021-12-13 13:53:40 +00:00
|
|
|
},
|
|
|
|
'contentChanges': # delete the in-body statement: `uint unused;`
|
|
|
|
[
|
|
|
|
{
|
2022-03-01 17:56:17 +00:00
|
|
|
'range': extendEnd(marker),
|
2021-12-13 13:53:40 +00:00
|
|
|
'text': ""
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
2022-03-15 17:52:59 +00:00
|
|
|
published_diagnostics = self.wait_for_diagnostics(solc)
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(published_diagnostics), 2, "published diagnostics count")
|
|
|
|
report1 = published_diagnostics[0]
|
|
|
|
self.expect_equal(report1['uri'], self.get_test_file_uri('didOpen_with_import'), "Correct file URI")
|
|
|
|
self.expect_equal(len(report1['diagnostics']), 0, "no diagnostics in didOpen_with_import.sol")
|
|
|
|
report2 = published_diagnostics[1]
|
2022-04-27 13:22:26 +00:00
|
|
|
self.expect_equal(report2['uri'], self.get_test_file_uri('lib', 'goto'), "Correct file URI")
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(report2['diagnostics']), 0, "no diagnostics in lib.sol")
|
|
|
|
|
|
|
|
# Now close the file and expect the warning to re-appear
|
|
|
|
solc.send_message(
|
|
|
|
'textDocument/didClose',
|
2022-04-27 13:22:26 +00:00
|
|
|
{ 'textDocument': { 'uri': self.get_test_file_uri('lib', 'goto') }}
|
2021-12-13 13:53:40 +00:00
|
|
|
)
|
|
|
|
|
2022-03-15 17:52:59 +00:00
|
|
|
published_diagnostics = self.wait_for_diagnostics(solc)
|
2021-12-13 13:53:40 +00:00
|
|
|
self.verify_didOpen_with_import_diagnostics(published_diagnostics)
|
|
|
|
|
|
|
|
def test_textDocument_opening_two_new_files_edit_and_close(self, solc: JsonRpcProcess) -> None:
|
|
|
|
"""
|
|
|
|
Open two new files A and B, let A import B, expect no error,
|
|
|
|
then close B and now expect the error of file B not being found.
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.setup_lsp(solc)
|
|
|
|
FILE_A_URI = 'file:///a.sol'
|
|
|
|
solc.send_message('textDocument/didOpen', {
|
|
|
|
'textDocument': {
|
|
|
|
'uri': FILE_A_URI,
|
|
|
|
'languageId': 'Solidity',
|
|
|
|
'version': 1,
|
|
|
|
'text': ''.join([
|
|
|
|
'// SPDX-License-Identifier: UNLICENSED\n',
|
|
|
|
'pragma solidity >=0.8.0;\n',
|
|
|
|
])
|
|
|
|
}
|
|
|
|
})
|
2022-03-15 17:52:59 +00:00
|
|
|
reports = self.wait_for_diagnostics(solc)
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(reports), 1, "one publish diagnostics notification")
|
|
|
|
self.expect_equal(len(reports[0]['diagnostics']), 0, "should not contain diagnostics")
|
|
|
|
|
|
|
|
FILE_B_URI = 'file:///b.sol'
|
|
|
|
solc.send_message('textDocument/didOpen', {
|
|
|
|
'textDocument': {
|
|
|
|
'uri': FILE_B_URI,
|
|
|
|
'languageId': 'Solidity',
|
|
|
|
'version': 1,
|
|
|
|
'text': ''.join([
|
|
|
|
'// SPDX-License-Identifier: UNLICENSED\n',
|
|
|
|
'pragma solidity >=0.8.0;\n',
|
|
|
|
])
|
|
|
|
}
|
|
|
|
})
|
2022-03-15 17:52:59 +00:00
|
|
|
reports = self.wait_for_diagnostics(solc)
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(reports), 2, "one publish diagnostics notification")
|
|
|
|
self.expect_equal(len(reports[0]['diagnostics']), 0, "should not contain diagnostics")
|
|
|
|
self.expect_equal(len(reports[1]['diagnostics']), 0, "should not contain diagnostics")
|
|
|
|
|
|
|
|
solc.send_message('textDocument/didChange', {
|
|
|
|
'textDocument': {
|
|
|
|
'uri': FILE_A_URI
|
|
|
|
},
|
|
|
|
'contentChanges': [
|
|
|
|
{
|
|
|
|
'range': {
|
|
|
|
'start': { 'line': 2, 'character': 0 },
|
|
|
|
'end': { 'line': 2, 'character': 0 }
|
|
|
|
},
|
|
|
|
'text': 'import "./b.sol";\n'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2022-03-15 17:52:59 +00:00
|
|
|
reports = self.wait_for_diagnostics(solc)
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(reports), 2, "one publish diagnostics notification")
|
|
|
|
self.expect_equal(len(reports[0]['diagnostics']), 0, "should not contain diagnostics")
|
|
|
|
self.expect_equal(len(reports[1]['diagnostics']), 0, "should not contain diagnostics")
|
|
|
|
|
|
|
|
solc.send_message(
|
|
|
|
'textDocument/didClose',
|
|
|
|
{ 'textDocument': { 'uri': FILE_B_URI }}
|
|
|
|
)
|
|
|
|
# We only get one diagnostics message since the diagnostics for b.sol was empty.
|
2022-03-15 17:52:59 +00:00
|
|
|
reports = self.wait_for_diagnostics(solc)
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(reports), 1, "one publish diagnostics notification")
|
|
|
|
self.expect_diagnostic(reports[0]['diagnostics'][0], 6275, 2, (0, 17)) # a.sol: File B not found
|
|
|
|
self.expect_equal(reports[0]['uri'], FILE_A_URI, "Correct uri")
|
|
|
|
|
|
|
|
def test_textDocument_closing_virtual_file_removes_imported_real_file(self, solc: JsonRpcProcess) -> None:
|
|
|
|
"""
|
|
|
|
We open a virtual file that imports a real file with a warning.
|
|
|
|
Once we close the virtual file, the warning is removed from the diagnostics,
|
|
|
|
since the real file is not considered part of the project anymore.
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.setup_lsp(solc)
|
2022-04-25 13:35:24 +00:00
|
|
|
FILE_A_URI = f'{self.project_root_uri}/a.sol'
|
2021-12-13 13:53:40 +00:00
|
|
|
solc.send_message('textDocument/didOpen', {
|
|
|
|
'textDocument': {
|
|
|
|
'uri': FILE_A_URI,
|
|
|
|
'languageId': 'Solidity',
|
|
|
|
'version': 1,
|
|
|
|
'text':
|
|
|
|
'// SPDX-License-Identifier: UNLICENSED\n'
|
|
|
|
'pragma solidity >=0.8.0;\n'
|
2022-04-27 13:22:26 +00:00
|
|
|
'import "./goto/lib.sol";\n'
|
2021-12-13 13:53:40 +00:00
|
|
|
}
|
|
|
|
})
|
2022-03-15 17:52:59 +00:00
|
|
|
reports = self.wait_for_diagnostics(solc)
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(reports), 2, '')
|
|
|
|
self.expect_equal(len(reports[0]['diagnostics']), 0, "should not contain diagnostics")
|
2022-03-01 17:56:17 +00:00
|
|
|
|
2022-05-23 12:20:03 +00:00
|
|
|
marker = self.get_test_tags("lib", 'goto')["@diagnostics"]
|
2022-03-01 17:56:17 +00:00
|
|
|
|
|
|
|
# unused variable in lib.sol
|
|
|
|
self.expect_diagnostic(reports[1]['diagnostics'][0], code=2072, marker=marker)
|
2021-12-13 13:53:40 +00:00
|
|
|
|
|
|
|
# Now close the file and expect the warning for lib.sol to be removed
|
|
|
|
solc.send_message(
|
|
|
|
'textDocument/didClose',
|
|
|
|
{ 'textDocument': { 'uri': FILE_A_URI }}
|
|
|
|
)
|
2022-03-15 17:52:59 +00:00
|
|
|
reports = self.wait_for_diagnostics(solc)
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(reports), 1, '')
|
2022-04-27 13:22:26 +00:00
|
|
|
self.expect_equal(reports[0]['uri'], f'{self.project_root_uri}/goto/lib.sol', "")
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(reports[0]['diagnostics']), 0, "should not contain diagnostics")
|
|
|
|
|
|
|
|
def test_textDocument_didChange_at_eol(self, solc: JsonRpcProcess) -> None:
|
|
|
|
"""
|
|
|
|
Append at one line and insert a new one below.
|
|
|
|
"""
|
|
|
|
self.setup_lsp(solc)
|
|
|
|
FILE_NAME = 'didChange_template'
|
|
|
|
FILE_URI = self.get_test_file_uri(FILE_NAME)
|
|
|
|
solc.send_message('textDocument/didOpen', {
|
|
|
|
'textDocument': {
|
|
|
|
'uri': FILE_URI,
|
|
|
|
'languageId': 'Solidity',
|
|
|
|
'version': 1,
|
|
|
|
'text': self.get_test_file_contents(FILE_NAME)
|
|
|
|
}
|
|
|
|
})
|
2022-03-15 17:52:59 +00:00
|
|
|
published_diagnostics = self.wait_for_diagnostics(solc)
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(published_diagnostics), 1, "one publish diagnostics notification")
|
|
|
|
self.expect_equal(len(published_diagnostics[0]['diagnostics']), 0, "no diagnostics")
|
|
|
|
solc.send_message('textDocument/didChange', {
|
|
|
|
'textDocument': {
|
|
|
|
'uri': FILE_URI
|
|
|
|
},
|
|
|
|
'contentChanges': [
|
|
|
|
{
|
|
|
|
'range': {
|
|
|
|
'start': { 'line': 6, 'character': 0 },
|
|
|
|
'end': { 'line': 6, 'character': 0 }
|
|
|
|
},
|
|
|
|
'text': " f"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2022-03-15 17:52:59 +00:00
|
|
|
published_diagnostics = self.wait_for_diagnostics(solc)
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(published_diagnostics), 1, "one publish diagnostics notification")
|
|
|
|
report2 = published_diagnostics[0]
|
|
|
|
self.expect_equal(report2['uri'], FILE_URI, "Correct file URI")
|
|
|
|
self.expect_equal(len(report2['diagnostics']), 1, "one diagnostic")
|
|
|
|
self.expect_diagnostic(report2['diagnostics'][0], 7858, 6, (1, 2))
|
|
|
|
|
|
|
|
solc.send_message('textDocument/didChange', {
|
|
|
|
'textDocument': { 'uri': FILE_URI },
|
|
|
|
'contentChanges': [
|
|
|
|
{
|
|
|
|
'range': {
|
|
|
|
'start': { 'line': 6, 'character': 2 },
|
|
|
|
'end': { 'line': 6, 'character': 2 }
|
|
|
|
},
|
|
|
|
'text': 'unction f() public {}'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2022-03-15 17:52:59 +00:00
|
|
|
published_diagnostics = self.wait_for_diagnostics(solc)
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(published_diagnostics), 1, "one publish diagnostics notification")
|
|
|
|
report3 = published_diagnostics[0]
|
|
|
|
self.expect_equal(report3['uri'], FILE_URI, "Correct file URI")
|
|
|
|
self.expect_equal(len(report3['diagnostics']), 1, "one diagnostic")
|
|
|
|
self.expect_diagnostic(report3['diagnostics'][0], 4126, 6, (1, 23))
|
|
|
|
|
|
|
|
def test_textDocument_didChange_empty_file(self, solc: JsonRpcProcess) -> None:
|
|
|
|
"""
|
|
|
|
Starts with an empty file and changes it to look like
|
|
|
|
the didOpen_with_import test case. Then we can use
|
|
|
|
the same verification calls to ensure it worked as expected.
|
|
|
|
"""
|
|
|
|
# This FILE_NAME must be alphabetically before lib.sol to not over-complify
|
|
|
|
# the test logic in verify_didOpen_with_import_diagnostics.
|
|
|
|
FILE_NAME = 'a_new_file'
|
|
|
|
FILE_URI = self.get_test_file_uri(FILE_NAME)
|
|
|
|
self.setup_lsp(solc)
|
|
|
|
solc.send_message('textDocument/didOpen', {
|
|
|
|
'textDocument': {
|
|
|
|
'uri': FILE_URI,
|
|
|
|
'languageId': 'Solidity',
|
|
|
|
'version': 1,
|
|
|
|
'text': ''
|
|
|
|
}
|
|
|
|
})
|
2022-03-15 17:52:59 +00:00
|
|
|
reports = self.wait_for_diagnostics(solc)
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(reports), 1)
|
|
|
|
report = reports[0]
|
|
|
|
published_diagnostics = report['diagnostics']
|
|
|
|
self.expect_equal(len(published_diagnostics), 2)
|
|
|
|
self.expect_diagnostic(published_diagnostics[0], code=1878, lineNo=0, startEndColumns=(0, 0))
|
|
|
|
self.expect_diagnostic(published_diagnostics[1], code=3420, lineNo=0, startEndColumns=(0, 0))
|
|
|
|
solc.send_message('textDocument/didChange', {
|
|
|
|
'textDocument': {
|
|
|
|
'uri': self.get_test_file_uri('a_new_file')
|
|
|
|
},
|
|
|
|
'contentChanges': [
|
|
|
|
{
|
|
|
|
'range': {
|
|
|
|
'start': { 'line': 0, 'character': 0 },
|
|
|
|
'end': { 'line': 0, 'character': 0 }
|
|
|
|
},
|
|
|
|
'text': self.get_test_file_contents('didOpen_with_import')
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2022-03-15 17:52:59 +00:00
|
|
|
published_diagnostics = self.wait_for_diagnostics(solc)
|
2021-12-13 13:53:40 +00:00
|
|
|
self.verify_didOpen_with_import_diagnostics(published_diagnostics, 'a_new_file')
|
|
|
|
|
|
|
|
def test_textDocument_didChange_multi_line(self, solc: JsonRpcProcess) -> None:
|
|
|
|
"""
|
|
|
|
Starts with an empty file and changes it to multiple times, changing
|
|
|
|
content across lines.
|
|
|
|
"""
|
|
|
|
self.setup_lsp(solc)
|
|
|
|
FILE_NAME = 'didChange_template'
|
|
|
|
FILE_URI = self.get_test_file_uri(FILE_NAME)
|
|
|
|
solc.send_message('textDocument/didOpen', {
|
|
|
|
'textDocument': {
|
|
|
|
'uri': FILE_URI,
|
|
|
|
'languageId': 'Solidity',
|
|
|
|
'version': 1,
|
|
|
|
'text': self.get_test_file_contents(FILE_NAME)
|
|
|
|
}
|
|
|
|
})
|
2022-03-15 17:52:59 +00:00
|
|
|
published_diagnostics = self.wait_for_diagnostics(solc)
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(published_diagnostics), 1, "one publish diagnostics notification")
|
|
|
|
self.expect_equal(len(published_diagnostics[0]['diagnostics']), 0, "no diagnostics")
|
|
|
|
solc.send_message('textDocument/didChange', {
|
|
|
|
'textDocument': { 'uri': FILE_URI },
|
|
|
|
'contentChanges': [
|
|
|
|
{
|
|
|
|
'range': {
|
|
|
|
'start': { 'line': 3, 'character': 3 },
|
|
|
|
'end': { 'line': 4, 'character': 1 }
|
|
|
|
},
|
|
|
|
'text': "tract D {\n\n uint x\n = -1; \n "
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2022-03-15 17:52:59 +00:00
|
|
|
published_diagnostics = self.wait_for_diagnostics(solc)
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(published_diagnostics), 1, "one publish diagnostics notification")
|
|
|
|
report2 = published_diagnostics[0]
|
|
|
|
self.expect_equal(report2['uri'], FILE_URI, "Correct file URI")
|
|
|
|
self.expect_equal(len(report2['diagnostics']), 1, "one diagnostic")
|
|
|
|
self.expect_diagnostic(report2['diagnostics'][0], 7407, 6, (3, 5))
|
|
|
|
|
|
|
|
# Now we are changing the part "x\n = -" of "uint x\n = -1;"
|
|
|
|
solc.send_message('textDocument/didChange', {
|
|
|
|
'textDocument': { 'uri': FILE_URI },
|
|
|
|
'contentChanges': [
|
|
|
|
{
|
|
|
|
'range': {
|
|
|
|
'start': { 'line': 5, 'character': 7 },
|
|
|
|
'end': { 'line': 6, 'character': 4 }
|
|
|
|
},
|
|
|
|
'text': "y\n = [\nuint(1),\n3,4]+"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2022-03-15 17:52:59 +00:00
|
|
|
published_diagnostics = self.wait_for_diagnostics(solc)
|
2021-12-13 13:53:40 +00:00
|
|
|
self.expect_equal(len(published_diagnostics), 1, "one publish diagnostics notification")
|
|
|
|
report3 = published_diagnostics[0]
|
|
|
|
self.expect_equal(report3['uri'], FILE_URI, "Correct file URI")
|
|
|
|
self.expect_equal(len(report3['diagnostics']), 2, "two diagnostics")
|
|
|
|
diagnostic = report3['diagnostics'][0]
|
|
|
|
self.expect_equal(diagnostic['code'], 2271, 'diagnostic: 2271')
|
|
|
|
# check multi-line error code
|
|
|
|
self.expect_equal(
|
|
|
|
diagnostic['range'],
|
|
|
|
{
|
|
|
|
'end': {'character': 6, 'line': 8},
|
|
|
|
'start': {'character': 3, 'line': 6}
|
|
|
|
},
|
|
|
|
"diagnostic: check range"
|
|
|
|
)
|
|
|
|
diagnostic = report3['diagnostics'][1]
|
|
|
|
self.expect_equal(diagnostic['code'], 7407, 'diagnostic: 7407')
|
|
|
|
# check multi-line error code
|
|
|
|
self.expect_equal(
|
|
|
|
diagnostic['range'],
|
|
|
|
{
|
|
|
|
'end': {'character': 6, 'line': 8},
|
|
|
|
'start': {'character': 3, 'line': 6}
|
|
|
|
},
|
|
|
|
"diagnostic: check range"
|
|
|
|
)
|
|
|
|
|
|
|
|
# }}}
|
|
|
|
# }}}
|
|
|
|
|
2022-04-25 15:21:45 +00:00
|
|
|
|
2021-12-13 13:53:40 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
suite = SolidityLSPTestSuite()
|
|
|
|
exit_code = suite.main()
|
2021-12-21 14:23:03 +00:00
|
|
|
sys.exit(exit_code)
|