craftvscruft /
menderbot
| 1 | import os |
||
| 2 | from typing import Iterable |
||
| 3 | |||
| 4 | from approvaltests.approvals import verify |
||
| 5 | |||
| 6 | from menderbot.code import LANGUAGE_STRATEGIES, function_indent, reindent |
||
| 7 | from menderbot.doc import document_file |
||
| 8 | from menderbot.source_file import Insertion, SourceFile, insert_in_lines |
||
| 9 | |||
| 10 | |||
| 11 | class FakeSourceFile(SourceFile): |
||
| 12 | def __init__(self, path: str, text: str): |
||
| 13 | self.path = path |
||
| 14 | self.encoding = None |
||
| 15 | self._initial_modified_time = None |
||
| 16 | self.text = text |
||
| 17 | self.original_text = text |
||
| 18 | |||
| 19 | def update_file(self, insertions: Iterable[Insertion], suffix: str) -> None: |
||
| 20 | del suffix |
||
| 21 | if len(insertions) == 0: |
||
| 22 | return |
||
| 23 | # Split keeping delimiter, so each line will have a "\n" |
||
| 24 | old_lines = self.text.splitlines(True) |
||
| 25 | new_lines = list(insert_in_lines(lines=old_lines, insertions=insertions)) |
||
| 26 | self.text = "".join(new_lines) |
||
| 27 | |||
| 28 | def load_source_as_utf8(self): |
||
| 29 | return bytes(self.text, "utf-8") |
||
| 30 | |||
| 31 | def is_unicode(self): |
||
| 32 | return True |
||
| 33 | |||
| 34 | def modified_after_loaded(self): |
||
| 35 | return False |
||
| 36 | |||
| 37 | |||
| 38 | def test_parse_with_fake_source_file(): |
||
| 39 | source_file = FakeSourceFile( |
||
| 40 | "my/path.py", |
||
| 41 | """ |
||
| 42 | def foo(): |
||
| 43 | print('Hello World') |
||
| 44 | |||
| 45 | def foo(): |
||
| 46 | \"\"\"Hello\"\"\" |
||
| 47 | print('Hello World') |
||
| 48 | """, |
||
| 49 | ) |
||
| 50 | |||
| 51 | path = source_file.path |
||
| 52 | _, file_extension = os.path.splitext(path) |
||
| 53 | language_strategy = LANGUAGE_STRATEGIES.get(file_extension) |
||
| 54 | assert language_strategy |
||
| 55 | |||
| 56 | source = source_file.load_source_as_utf8() |
||
| 57 | tree = language_strategy.parse_source_to_tree(source) |
||
| 58 | function_nodes = language_strategy.get_function_nodes(tree) |
||
| 59 | assert len(function_nodes) == 2 |
||
| 60 | assert not language_strategy.function_has_comment(function_nodes[0]) |
||
| 61 | assert language_strategy.function_has_comment(function_nodes[1]) |
||
| 62 | |||
| 63 | |||
| 64 | def test_do_nothing_for_unknown_extension(): |
||
| 65 | def generate_fake_doc(code, file_extension): |
||
| 66 | del code |
||
| 67 | del file_extension |
||
| 68 | raise AssertionError("Should not be called") |
||
| 69 | |||
| 70 | source_file = FakeSourceFile( |
||
| 71 | "my/path.unknown", |
||
| 72 | """ |
||
| 73 | def foo(): |
||
| 74 | print('Hello World') |
||
| 75 | """, |
||
| 76 | ) |
||
| 77 | |||
| 78 | insertions = document_file(source_file, generate_fake_doc) |
||
| 79 | source_file.update_file(insertions, suffix="") |
||
| 80 | assert source_file.text == source_file.original_text |
||
| 81 | |||
| 82 | |||
| 83 | def test_adds_python_doc(): |
||
| 84 | def generate_fake_doc(code, file_extension): |
||
| 85 | assert file_extension == ".py" |
||
| 86 | indent = function_indent(code) |
||
| 87 | return reindent('"""Hi!"""', indent) |
||
| 88 | |||
| 89 | source_file = FakeSourceFile( |
||
| 90 | "my/path.py", |
||
| 91 | """ |
||
| 92 | def foo(): |
||
| 93 | print('Hello World') |
||
| 94 | """, |
||
| 95 | ) |
||
| 96 | |||
| 97 | insertions = document_file(source_file, generate_fake_doc) |
||
| 98 | source_file.update_file(insertions, suffix="") |
||
| 99 | verify(source_file.text) |
||
| 100 | |||
| 101 | |||
| 102 | def test_passes_in_code_to_generator(): |
||
| 103 | code_received = None |
||
| 104 | |||
| 105 | def generate_fake_doc(code, file_extension): |
||
| 106 | nonlocal code_received |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 107 | assert file_extension == ".py" |
||
| 108 | code_received = code |
||
| 109 | return ' """Hi!"""' |
||
| 110 | |||
| 111 | source_file = FakeSourceFile( |
||
| 112 | "my/path.py", |
||
| 113 | """ |
||
| 114 | def foo(): |
||
| 115 | print('Hello World') |
||
| 116 | """, |
||
| 117 | ) |
||
| 118 | |||
| 119 | document_file(source_file, generate_fake_doc) |
||
| 120 | assert code_received == "def foo():\n print('Hello World')\n\n" |
||
| 121 |