Skip to content

Commit 86caba8

Browse files
committed
fixing empty preamble trimming and add a test
1 parent 07ead6f commit 86caba8

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

json_to_models/cli.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,9 @@ def set_args(
240240

241241
self.dict_keys_regex = [re.compile(rf"^{r}$") for r in dict_keys_regex] if dict_keys_regex else ()
242242
self.dict_keys_fields = dict_keys_fields or ()
243-
self.preamble = preamble.strip() or None
243+
if preamble:
244+
preamble = preamble.strip()
245+
self.preamble = preamble or None
244246
self.initialized = True
245247

246248
@classmethod

test/test_cli/test_script.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,53 @@ def test_script_custom(command):
168168

169169
@pytest.mark.parametrize("command", test_commands)
170170
def test_add_preamble(command):
171+
171172
PREAMBLE_TEXT = """
172173
# this is some test code
173174
# to be added to the file
174175
175176
176177
# let's see if it works
177-
"""
178178
179-
command += ' --preamble "' + PREAMBLE_TEXT + '"'
180-
stdout = execute_test(command)
179+
180+
"""
181+
stdout = execute_test(command + ' --preamble "' + PREAMBLE_TEXT + '"')
181182
assert "let's see if it works" in stdout
182183

183184

185+
@pytest.mark.parametrize("command", test_commands)
186+
def test_add_trim_preamble(command):
187+
188+
def trim_header(line_string):
189+
"""remove the quoted command and everything from the first class declaration onwards"""
190+
lines = line_string.splitlines()
191+
start = 0
192+
end = 0
193+
line_no = 0
194+
for l in lines:
195+
if l.startswith('"""'):
196+
start = line_no
197+
if l.startswith('class '):
198+
end = line_no
199+
break
200+
line_no += 1
201+
202+
return lines[start:end]
203+
204+
expected_result = execute_test(command)
205+
206+
BLANK_SPACE = """
207+
208+
209+
210+
211+
"""
212+
# ensure blank space does not get propagated
213+
stdout = execute_test(command + ' --preamble "' + BLANK_SPACE + '"')
214+
215+
assert trim_header(expected_result) == trim_header(stdout)
216+
217+
184218
wrong_arguments_commands = [
185219
pytest.param(f"""{executable} -l Model items "{test_data_path / 'photos.json'}" \
186220
-l Model - "{test_data_path / 'users.json'}" """, id="duplicate_name"),

0 commit comments

Comments
 (0)