forked from coala/coala-bears
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_packageTest.py
159 lines (121 loc) · 5.31 KB
/
generate_packageTest.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import os
import shutil
import sys
import unittest
from unittest.mock import patch
from bears.generate_package import (VERSION, touch, create_file_from_template,
create_file_structure_for_packages,
perform_register, perform_upload, main,
create_upload_parser)
class TouchTest(unittest.TestCase):
def setUp(self):
if os.path.exists('TestFile.py'):
os.remove('TestFile.py')
def test_file_doesnt_exist(self):
self.assertFalse(os.path.exists('TestFile.py'))
touch('TestFile.py')
self.assertTrue(os.path.exists('TestFile.py'))
def tearDown(self):
os.remove('TestFile.py')
class CreateFileFromTemplateTest(unittest.TestCase):
BASE_TEST_PATH = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'generate_package_input_files'))
SUBST_FILE = os.path.join(
BASE_TEST_PATH, 'substituted_file.py')
TEMPL_FILE = os.path.join(
BASE_TEST_PATH, 'template_file.py.in')
def test_output_file(self):
data = {'who': 'George', 'sport': 'swimming'}
create_file_from_template(self.TEMPL_FILE, self.SUBST_FILE, data)
with open(self.SUBST_FILE) as fl:
substituted_file = fl.read()
self.assertEqual(substituted_file, 'George has gone swimming again.\n')
def tearDown(self):
os.remove(self.SUBST_FILE)
class CreateFileStructureForPackagesTest(unittest.TestCase):
INIT_FILE_PATH = os.path.join('folder', 'Test', 'coalaTest', '__init__.py')
BEAR_FILE_PATH = os.path.join('folder', 'Test', 'coalaTest', 'Test.py')
def test_structure(self):
touch('TestFile.py')
create_file_structure_for_packages('folder', 'TestFile.py', 'Test')
self.assertTrue(os.path.exists(self.INIT_FILE_PATH))
self.assertTrue(os.path.exists(self.BEAR_FILE_PATH))
def tearDown(self):
shutil.rmtree('folder')
os.remove('TestFile.py')
class CreateUploadParserTest(unittest.TestCase):
def test_parser(self):
self.assertTrue(create_upload_parser().parse_args(['--upload']).upload)
class PerformRegisterTest(unittest.TestCase):
@patch('subprocess.call')
def test_command(self, call_mock):
perform_register('.', 'MarkdownBear-0.8.0.dev20160623094115')
call_mock.assert_called_with(
['twine', 'register', '-r', 'pypi', os.path.join(
'.', 'dist',
'MarkdownBear-0.8.0.dev20160623094115-py3-none-any.whl')])
class PerformUploadTest(unittest.TestCase):
@patch('subprocess.call')
def test_command(self, call_mock):
perform_upload('.')
call_mock.assert_called_with(['twine', 'upload', './dist/*'])
class MainTest(unittest.TestCase):
BEARS_PATH = os.path.abspath(os.path.join(
os.path.dirname(__file__), '..', 'bears'))
BEARS_UPLOAD_PATH = os.path.join(BEARS_PATH, 'upload')
CSS_BEAR_SETUP_PATH = os.path.join(
BEARS_UPLOAD_PATH, 'CSSLintBear', 'setup.py')
NO_BEAR_PATH = os.path.join(BEARS_PATH,
'BadBear', 'InvalidBear.py')
def setUp(self):
self.orig_dir = os.getcwd()
self.argv = ['generate_package.py']
argv_patcher = patch.object(sys, 'argv', self.argv)
self.addCleanup(argv_patcher.stop)
self.argv_mock = argv_patcher.start()
def test_main(self):
main()
self.assertTrue(os.path.exists(self.BEARS_UPLOAD_PATH))
with open(self.CSS_BEAR_SETUP_PATH) as fl:
setup_py = fl.read()
self.assertIn('Check code for syntactical or semantical', setup_py)
def test_main_bear_version_prod(self):
fake_prod_version = '99.99.99'
with patch('bears.generate_package.VERSION', fake_prod_version):
main()
with open(self.CSS_BEAR_SETUP_PATH) as fl:
setup_py = fl.read()
self.assertIn(fake_prod_version, setup_py)
self.assertNotIn(VERSION, setup_py)
def test_main_bear_version_dev(self):
fake_dev_version = '13.37.0.dev133713371337'
with patch('bears.generate_package.VERSION', fake_dev_version):
main()
with open(self.CSS_BEAR_SETUP_PATH) as fl:
setup_py = fl.read()
self.assertNotIn(fake_dev_version, setup_py)
self.assertIn('13.37.0', setup_py)
@patch('bears.generate_package.perform_upload')
def test_upload(self, call_mock):
self.argv.append('--upload')
main()
for call_object in call_mock.call_args_list:
self.assertRegex(call_object[0][0], r'.+Bear')
@patch('bears.generate_package.perform_register')
def test_register(self, call_mock):
self.argv.append('--register')
main()
for call_object in call_mock.call_args_list:
self.assertRegex(call_object[0][0], r'.+Bear')
def test_no_bear_object(self):
if not os.path.exists(self.NO_BEAR_PATH):
os.makedirs(os.path.join(self.BEARS_PATH, 'BadBear'))
touch(self.NO_BEAR_PATH)
main()
self.assertFalse(os.path.exists(os.path.join(
self.BEARS_UPLOAD_PATH, 'BadBear')))
shutil.rmtree(os.path.join(self.BEARS_PATH, 'BadBear'))
def tearDown(self):
shutil.rmtree(os.path.join(self.BEARS_UPLOAD_PATH))
os.chdir(self.orig_dir)