Skip to content

Commit fd76e66

Browse files
authored
Refactor: Main refactored, tests added for main (#52)
1 parent 893aae5 commit fd76e66

File tree

2 files changed

+76
-17
lines changed

2 files changed

+76
-17
lines changed

email_validator/__init__.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -525,32 +525,31 @@ def main():
525525
import sys
526526
import json
527527

528+
def __utf8_input_shim(input_str):
529+
if sys.version_info < (3,):
530+
return input_str.decode("utf-8")
531+
return input_str
532+
533+
def __utf8_output_shim(output_str):
534+
if sys.version_info < (3,):
535+
return unicode_class(output_str).encode("utf-8")
536+
return output_str
537+
528538
if len(sys.argv) == 1:
529-
# Read lines for STDIN and validate the email address on each line.
530-
allow_smtputf8 = True
531539
for line in sys.stdin:
540+
email = __utf8_input_shim(line.strip())
532541
try:
533-
email = line.strip()
534-
if sys.version_info < (3,):
535-
email = email.decode("utf8") # assume utf8 in input
536-
validate_email(email, allow_smtputf8=allow_smtputf8)
542+
validate_email(email)
537543
except EmailNotValidError as e:
538-
print(email, e)
544+
print(__utf8_output_shim("{} {}".format(email, e)))
539545
else:
540546
# Validate the email address passed on the command line.
541-
email = sys.argv[1]
542-
allow_smtputf8 = True
543-
check_deliverability = True
544-
if sys.version_info < (3,):
545-
email = email.decode("utf8") # assume utf8 in input
547+
email = __utf8_input_shim(sys.argv[1])
546548
try:
547-
result = validate_email(email, allow_smtputf8=allow_smtputf8, check_deliverability=check_deliverability)
549+
result = validate_email(email)
548550
print(json.dumps(result.as_dict(), indent=2, sort_keys=True, ensure_ascii=False))
549551
except EmailNotValidError as e:
550-
if sys.version_info < (3,):
551-
print(unicode_class(e).encode("utf8"))
552-
else:
553-
print(e)
552+
print(__utf8_output_shim(e))
554553

555554

556555
if __name__ == "__main__":

tests/test_main.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from email_validator import EmailSyntaxError, EmailUndeliverableError, \
33
validate_email, validate_email_deliverability, \
44
ValidatedEmail
5+
# Let's test main but rename it to be clear
6+
from email_validator import main as validator_main
57

68

79
@pytest.mark.parametrize(
@@ -284,3 +286,61 @@ def test_deliverability_dns_timeout():
284286
assert response.get("unknown-deliverability") == "timeout"
285287
validate_email('test@gmail.com')
286288
del validate_email_deliverability.TEST_CHECK_TIMEOUT
289+
290+
291+
def test_main_single_good_input(monkeypatch, capsys):
292+
import json
293+
test_email = "test@example.com"
294+
monkeypatch.setattr('sys.argv', ['email_validator', test_email])
295+
validator_main()
296+
stdout, _ = capsys.readouterr()
297+
output = json.loads(str(stdout))
298+
assert isinstance(output, dict)
299+
assert validate_email(test_email).original_email == output["original_email"]
300+
301+
302+
def test_main_single_bad_input(monkeypatch, capsys):
303+
bad_email = 'test@..com'
304+
monkeypatch.setattr('sys.argv', ['email_validator', bad_email])
305+
validator_main()
306+
stdout, _ = capsys.readouterr()
307+
assert stdout == 'An email address cannot have a period immediately after the @-sign.\n'
308+
309+
310+
def test_main_multi_input(monkeypatch, capsys):
311+
import io
312+
test_cases = ["test@example.com", "test2@example.com", "test@.com", "test3@.com"]
313+
test_input = io.StringIO("\n".join(test_cases))
314+
monkeypatch.setattr('sys.stdin', test_input)
315+
monkeypatch.setattr('sys.argv', ['email_validator'])
316+
validator_main()
317+
stdout, _ = capsys.readouterr()
318+
assert test_cases[0] not in stdout
319+
assert test_cases[1] not in stdout
320+
assert test_cases[2] in stdout
321+
assert test_cases[3] in stdout
322+
323+
324+
def test_main_input_shim(monkeypatch, capsys):
325+
import json
326+
monkeypatch.setattr('sys.version_info', (2, 7))
327+
test_email = b"test@example.com"
328+
monkeypatch.setattr('sys.argv', ['email_validator', test_email])
329+
validator_main()
330+
stdout, _ = capsys.readouterr()
331+
output = json.loads(str(stdout))
332+
assert isinstance(output, dict)
333+
assert validate_email(test_email).original_email == output["original_email"]
334+
335+
336+
def test_main_output_shim(monkeypatch, capsys):
337+
monkeypatch.setattr('sys.version_info', (2, 7))
338+
test_email = b"test@.com"
339+
monkeypatch.setattr('sys.argv', ['email_validator', test_email])
340+
validator_main()
341+
stdout, _ = capsys.readouterr()
342+
343+
# This looks bad but it has to do with the way python 2.7 prints vs py3
344+
# The \n is part of the print statement, not part of the string, which is what the b'...' is
345+
# Since we're mocking py 2.7 here instead of actually using 2.7, this was the closest I could get
346+
assert stdout == "b'An email address cannot have a period immediately after the @-sign.'\n"

0 commit comments

Comments
 (0)