forked from dashingsoft/pyarmor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pyfeatures.py
More file actions
executable file
·98 lines (75 loc) · 2.94 KB
/
test_pyfeatures.py
File metadata and controls
executable file
·98 lines (75 loc) · 2.94 KB
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
# -*- coding: utf-8 -*-
import glob
import logging
import os
import shutil
import unittest
from test.support import script_helper
class BaseTestCase(unittest.TestCase):
def setUp(self):
self.home = os.path.expanduser('~/.pyarmor')
self.local_path = '.pyarmor'
self.default_output = 'dist'
def tearDown(self):
shutil.rmtree(self.local_path, ignore_errors=True)
shutil.rmtree(self.default_output, ignore_errors=True)
def assert_python_ok(self, *args):
kwargs = {
'__isolated': False
}
return script_helper.assert_python_ok(*args, **kwargs)
def assert_python_failure(self, *args):
kwargs = {
'__isolated': False
}
return script_helper.assert_python_failure(*args, **kwargs)
def pyarmor_cmd(self, options):
args = ['-m', 'pyarmor.cli'] + options
rc, stdout, stderr = self.assert_python_ok(*args)
self.assertIn(b'obfuscate scripts OK', stderr)
def pyarmor_cfg(self, options):
args = ['-m', 'pyarmor.cli'] + options
rc, stdout, stderr = self.assert_python_ok(*args)
def verify_script_pass(self, script, expected=None):
rc, stdout, stderr = self.assert_python_ok(script)
if expected:
self.assertIn(expected, stdout)
def verify_script_fail(self, script, errmsg=None):
rc, stdout, stderr = self.assert_python_failure(script)
if errmsg:
self.assertIn(errmsg, stderr)
class UnitTestCases(BaseTestCase):
def _update_dist_script(self, script):
with open(script, 'a') as f:
f.write('pi = 3.1415926')
def test_simple_issues(self):
prefix = 'samples/pyfeatures/is_'
scripts = glob.glob(prefix + '*.py')
expected = b'All test passed'
for script in scripts:
args = ['g', script]
self.pyarmor_cmd(args)
with self.subTest(issue=script[len(prefix):-3]):
self.verify_script_pass(script, expected)
def test_wrapmode_2(self):
script = 'samples/pyfeatures/wrap2.py'
expected = b'All test passed'
self.pyarmor_cfg(['cfg', 'wrap_mode', '2'])
self.pyarmor_cmd(['g', script])
self.pyarmor_cfg(['cfg', 'wrap_mode', '1'])
self.verify_script_pass(script, expected)
def test_rft_issues(self):
prefix = 'samples/pyfeatures/rft_'
scripts = glob.glob(prefix + '*.py')
for script in scripts:
args = ['g', '--enable-rft', script]
self.pyarmor_cmd(args)
with self.subTest(issue=script[len(prefix):-3]):
self.verify_script_pass(script)
if __name__ == '__main__':
logging.getLogger().addHandler(logging.NullHandler())
verbosity = 1
loader = unittest.TestLoader()
# loader.testMethodPrefix = 'test_mix_str'
suite = loader.loadTestsFromTestCase(UnitTestCases)
result = unittest.TextTestRunner(verbosity=verbosity).run(suite)