1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
# -*- coding: utf-8 -*-
"""
Tests for the nudatus module.
"""
import builtins
import sys
import tokenize
from unittest import mock
import nudatus
import pytest
def test_get_version():
"""
Ensure a call to get_version returns the expected string.
"""
result = nudatus.get_version()
assert result == ".".join([str(i) for i in nudatus._VERSION])
def test_mangle_script():
"""
Check that mangle returns the expected output
"""
script = ""
real_mangled = b""
with open("tests/bigscript.py") as f:
script = f.read()
assert len(script) > 0
with open("tests/bigscript_mangled.py") as f:
real_mangled = f.read()
assert len(real_mangled) > 0
mangled = nudatus.mangle(script)
assert mangled == real_mangled
def test_mangle_with_bad_syntax():
"""
Check that mangle throws an
exception for a badly formatted script
"""
script = ""
with open("tests/bigscript_bad.py") as f:
script = f.read()
assert len(script) > 0
with pytest.raises(tokenize.TokenError):
nudatus.mangle(script)
def test_main_without_file(capfd):
with mock.patch("sys.argv", ["nudatus"]):
with pytest.raises(SystemExit) as ex:
nudatus.main()
assert ex.value.code == 1
out, err = capfd.readouterr()
assert len(out) == 0
assert err.strip() == "No file specified"
def test_main_with_file_without_output_file(capfd):
script = ""
with open("tests/bigscript_mangled.py") as f:
script = f.read()
assert len(script) > 0
with mock.patch("sys.argv", ["nudatus", "tests/bigscript.py"]):
nudatus.main()
out, err = capfd.readouterr()
assert len(err) == 0
assert out == script
def test_main_with_file_with_output_file(capfd):
expected = ""
with open("tests/bigscript_mangled.py") as f:
expected = f.read()
assert len(expected) > 0
script = ""
with open("tests/bigscript.py") as f:
script = f.read()
assert len(script) > 0
with mock.patch("sys.argv", ["nudatus", "tests/bigscript.py", "testout.py"]):
m = mock.mock_open(read_data=script)
with mock.patch.object(builtins, "open", m):
nudatus.main()
m.assert_called_with("testout.py", "w")
handle = m()
handle.write.assert_called_with(expected)
out, err = capfd.readouterr()
assert len(err) == 0
assert len(out) == 0
def test_main_with_bad_script(capfd):
with pytest.raises(SystemExit) as ex:
with mock.patch("sys.argv", ["nudatus", "tests/bigscript_bad.py"]):
nudatus.main()
assert ex.value.code == 1
out, err = capfd.readouterr()
assert len(out) == 0
assert len(err) > 0
assert err.startswith("Error mangling tests/bigscript_bad.py:")
|