Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _tree_invariant_check(input_code, formatted_code):
input_code_parse_tree = parser.parse(input_code)
formatted_code_parse_tree = parser.parse(formatted_code)
loosen_tree_transformer = LoosenTreeTransformer()
input_code_parse_tree = loosen_tree_transformer.transform(input_code_parse_tree)
formatted_code_parse_tree = loosen_tree_transformer.transform(
formatted_code_parse_tree
)
diff = "\n".join(
difflib.unified_diff(
str(input_code_parse_tree.pretty()).splitlines(),
str(formatted_code_parse_tree.pretty()).splitlines(),
)
)
assert input_code_parse_tree == formatted_code_parse_tree, diff
def _tree_invariant_check(input_code, formatted_code):
input_code_parse_tree = parser.parse(input_code)
formatted_code_parse_tree = parser.parse(formatted_code)
loosen_tree_transformer = LoosenTreeTransformer()
input_code_parse_tree = loosen_tree_transformer.transform(input_code_parse_tree)
formatted_code_parse_tree = loosen_tree_transformer.transform(
formatted_code_parse_tree
)
diff = "\n".join(
difflib.unified_diff(
str(input_code_parse_tree.pretty()).splitlines(),
str(formatted_code_parse_tree.pretty()).splitlines(),
)
)
assert input_code_parse_tree == formatted_code_parse_tree, diff
def test_parsing_failure(gdscript_nok_path):
with open(gdscript_nok_path, "r") as fh:
code = fh.read()
try:
parser.parse(code)
except: # pylint: disable=bare-except
return
raise Exception("shall fail")
def test_real_world_script_e2e(test_name):
this_dir = os.path.dirname(os.path.abspath(__file__))
input_file_path = os.path.join(this_dir, DATA_DIR, test_name)
with open(input_file_path, "r") as input_fh:
input_code = input_fh.read()
formatted_code = format_code(input_code, 100)
check_formatting_safety(input_code, formatted_code, 100)
def format_and_compare(input_code, expected_output_code):
formatted_code = format_code(input_code, max_line_length=MAX_LINE_LENGTH)
_compare(formatted_code, expected_output_code)
code_formatted_again = format_code(formatted_code, max_line_length=MAX_LINE_LENGTH)
_compare_again(code_formatted_again, formatted_code)
_tree_invariant_check(input_code, formatted_code)
input_code_comment_stats = _gather_comment_statistics_from_code(input_code)
formatted_code_comments = _gather_comments_from_code(formatted_code)
_comment_preservation_check(input_code_comment_stats, formatted_code_comments)
check_comment_persistence=False,
check_tree_invariant=False,
check_formatting_stability=False,
):
formatted_code = format_code(input_code, max_line_length=MAX_LINE_LENGTH)
if check_comment_persistence:
input_code_comment_stats = _gather_comment_statistics_from_code(input_code)
formatted_code_comments = _gather_comments_from_code(formatted_code)
_comment_preservation_check(input_code_comment_stats, formatted_code_comments)
if check_tree_invariant:
_tree_invariant_check(input_code, formatted_code)
if check_formatting_stability:
code_formatted_again = format_code(
formatted_code, max_line_length=MAX_LINE_LENGTH
)
_compare_again(code_formatted_again, formatted_code)
def format_and_compare(input_code, expected_output_code):
formatted_code = format_code(input_code, max_line_length=MAX_LINE_LENGTH)
_compare(formatted_code, expected_output_code)
code_formatted_again = format_code(formatted_code, max_line_length=MAX_LINE_LENGTH)
_compare_again(code_formatted_again, formatted_code)
_tree_invariant_check(input_code, formatted_code)
input_code_comment_stats = _gather_comment_statistics_from_code(input_code)
formatted_code_comments = _gather_comments_from_code(formatted_code)
_comment_preservation_check(input_code_comment_stats, formatted_code_comments)
def simple_ok_check(code, **kwargs):
extra_disable = [] if "disable" not in kwargs else kwargs["disable"]
config = DEFAULT_CONFIG.copy()
config.update({"disable": extra_disable})
outcome = lint_code(code, config)
assert len(outcome) == 0, outcome
def simple_nok_check(code, check_name, line=2, **kwargs):
extra_disable = [] if "disable" not in kwargs else kwargs["disable"]
config_w_disable = DEFAULT_CONFIG.copy()
config_w_disable.update({"disable": [check_name] + extra_disable})
assert len(lint_code(code, config_w_disable)) == 0
config = DEFAULT_CONFIG.copy()
config.update({"disable": extra_disable})
outcome = lint_code(code, config)
assert len(outcome) == 1
assert outcome[0].name == check_name
assert outcome[0].line == line
def simple_nok_check(code, check_name, line=2, **kwargs):
extra_disable = [] if "disable" not in kwargs else kwargs["disable"]
config_w_disable = DEFAULT_CONFIG.copy()
config_w_disable.update({"disable": [check_name] + extra_disable})
assert len(lint_code(code, config_w_disable)) == 0
config = DEFAULT_CONFIG.copy()
config.update({"disable": extra_disable})
outcome = lint_code(code, config)
assert len(outcome) == 1
assert outcome[0].name == check_name
assert outcome[0].line == line