Skip to content

Commit 3817b73

Browse files
authored
refactor: create tests (#22)
* refactor: create tests Refactor: - Tests in create_tests.py Fix: - Verifications for created app in `tns_assert.py` * fix: do not update default app
1 parent 4ec4f6a commit 3817b73

File tree

4 files changed

+83
-92
lines changed

4 files changed

+83
-92
lines changed

products/nativescript/tns.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,11 @@ def create(app_name=Settings.AppName.DEFAULT, template=None, path=None, app_id=N
145145

146146
# Verify app is created properly
147147
if verify is not False:
148-
TnsAssert.created(app_name=app_name, output=result.output, app_data=app_data)
148+
# Usually we do not pass path on tns create, which actually equals to cwd.
149+
# In such cases pass correct path to TnsAssert.created()
150+
if path is None:
151+
path = Settings.TEST_RUN_HOME
152+
TnsAssert.created(app_name=app_name, output=result.output, app_data=app_data, path=path)
149153

150154
return result
151155

products/nativescript/tns_assert.py

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@
22

33
from core.enums.app_type import AppType
44
from core.settings import Settings
5-
from core.utils.file_utils import Folder
65
from core.utils.file_utils import File
6+
from core.utils.file_utils import Folder
77
from core.utils.json_utils import JsonUtils
88
from core.utils.perf_utils import PerfUtils
99
from products.nativescript.tns_helpers import TnsHelpers
1010

1111

1212
class TnsAssert(object):
13-
NODE_MODULES = 'node_modules'
14-
TNS_MODULES = os.path.join(NODE_MODULES, 'tns-core-modules')
15-
HOOKS = 'hooks'
1613

1714
@staticmethod
18-
def created(app_name, output=None, app_data=None):
19-
15+
def created(app_name, output=None, app_data=None, path=Settings.TEST_RUN_HOME):
16+
"""
17+
Verify app is created properly.
18+
:param app_name: Name of the app.
19+
:param output: Console output of `tns create` command.
20+
:param app_data: AppInfo object.
21+
:param path: Base path where app is created.
22+
"""
2023
# Assert app exists
21-
app_path = os.path.join(Settings.TEST_RUN_HOME, app_name)
24+
app_path = os.path.join(path, app_name)
2225
assert Folder.exists(app_path), 'Failed to create app. ' + os.linesep + app_path + ' do not exists!'
2326

2427
# Assert output
@@ -31,24 +34,30 @@ def created(app_name, output=None, app_data=None):
3134

3235
# Assert app data
3336
if app_data is not None:
34-
# Assert app type
35-
assert Folder.exists(os.path.join(app_path, TnsAssert.TNS_MODULES))
36-
assert Folder.exists(os.path.join(app_path, TnsAssert.NODE_MODULES, 'nativescript-theme-core'))
37-
assert Folder.exists(os.path.join(app_path, TnsAssert.NODE_MODULES, 'nativescript-dev-webpack'))
38-
39-
if app_data.app_type is AppType.JS:
40-
pass
41-
elif app_data.app_type is AppType.TS:
42-
TnsAssert.__verify_created_ts()
43-
elif app_data.app_type is AppType.NG:
44-
TnsAssert.__verify_created_ng()
45-
elif app_data.app_type is AppType.VUE:
46-
pass
47-
elif app_data.app_type is AppType.SHARED_NG:
48-
pass
37+
# Verify modules installed
38+
node_path = TnsHelpers.get_app_node_modules_path(app_name=app_name, path=path)
39+
assert Folder.exists(os.path.join(node_path, 'tns-core-modules')), '{N} theme do not exists in app.'
40+
assert File.exists(os.path.join(node_path, 'tns-core-modules', 'tns-core-modules.d.ts'))
41+
42+
# Verify {N} core theme is installed
43+
assert Folder.exists(os.path.join(node_path, 'nativescript-theme-core')), '{N} theme do not exists in app.'
44+
45+
# Verify webpack is installed
46+
before_watch_hooks = os.path.join(app_path, 'hooks', 'before-watch')
47+
assert Folder.exists(os.path.join(node_path, 'nativescript-dev-webpack')), 'Webpack not installed in app.'
48+
assert File.exists(os.path.join(app_path, 'webpack.config.js')), 'Missing webpack config.'
49+
assert File.exists(os.path.join(before_watch_hooks, 'nativescript-dev-webpack.js')), 'Hooks not installed.'
50+
51+
# Verify typescript in TS and NG apps:
52+
if app_data.app_type in {AppType.TS, AppType.NG, AppType.SHARED_NG}:
53+
assert Folder.exists(os.path.join(node_path, 'nativescript-dev-typescript')), 'TS not installed in app.'
54+
assert File.exists(os.path.join(app_path, 'tsconfig.json')), 'Missing config.'
55+
assert File.exists(os.path.join(app_path, 'tsconfig.tns.json')), 'Missing config.'
56+
assert File.exists(os.path.join(before_watch_hooks, 'nativescript-dev-typescript.js')), \
57+
'Hooks not installed.'
4958

5059
# Assert app id
51-
if app_data.app_id is not None:
60+
if app_data.bundle_id is not None:
5261
pass
5362

5463
# Assert size
@@ -95,29 +104,3 @@ def build(app_name, platform=None, release=False, provision=Settings.IOS.DEV_PRO
95104
# Assert size
96105
if app_data.size is not None:
97106
pass
98-
99-
@staticmethod
100-
def __verify_created_ts():
101-
app_path = TnsHelpers.get_app_path(app_name='TestAppTS')
102-
assert File.exists(os.path.join(app_path, 'tsconfig.json'))
103-
assert File.exists(os.path.join(app_path, 'webpack.config.js'))
104-
assert File.exists(os.path.join(app_path, 'tsconfig.tns.json'))
105-
assert not File.exists(os.path.join(app_path, 'references.d.ts'))
106-
assert File.exists(os.path.join(app_path, TnsAssert.TNS_MODULES, 'tns-core-modules.d.ts'))
107-
assert File.exists(os.path.join(app_path, TnsAssert.HOOKS, 'before-prepare',
108-
'nativescript-dev-typescript.js'))
109-
assert File.exists(os.path.join(app_path, TnsAssert.HOOKS, 'before-watch',
110-
'nativescript-dev-typescript.js'))
111-
112-
@staticmethod
113-
def __verify_created_ng():
114-
app_path = TnsHelpers.get_app_path(app_name='TestAppNG')
115-
assert File.exists(os.path.join(app_path, 'tsconfig.json'))
116-
assert File.exists(os.path.join(app_path, 'webpack.config.js'))
117-
assert File.exists(os.path.join(app_path, 'tsconfig.tns.json'))
118-
assert not File.exists(os.path.join(app_path, 'references.d.ts'))
119-
assert File.exists(os.path.join(app_path, TnsAssert.TNS_MODULES, 'tns-core-modules.d.ts'))
120-
assert File.exists(os.path.join(app_path, TnsAssert.HOOKS, 'before-prepare',
121-
'nativescript-dev-typescript.js'))
122-
assert File.exists(os.path.join(app_path, TnsAssert.HOOKS, 'before-watch',
123-
'nativescript-dev-typescript.js'))

products/nativescript/tns_helpers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
class TnsHelpers(object):
1313

1414
@staticmethod
15-
def get_app_path(app_name):
16-
app_path = os.path.join(Settings.TEST_RUN_HOME, app_name)
17-
return app_path
15+
def get_app_path(app_name, path=Settings.TEST_RUN_HOME):
16+
return os.path.join(path, app_name)
17+
18+
@staticmethod
19+
def get_app_node_modules_path(app_name, path=Settings.TEST_RUN_HOME):
20+
return os.path.join(TnsHelpers.get_app_path(app_name=app_name, path=path), 'node_modules')
1821

1922
@staticmethod
2023
def get_apk(app_name):

tests/cli/create/create_tests.py

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,76 +8,77 @@
88
from products.nativescript.tns import Tns
99

1010

11+
# noinspection PyMethodMayBeStatic
1112
class CreateTests(TnsTest):
12-
app_data_JS = Apps.HELLO_WORLD_JS
13-
app_data_TS = Apps.HELLO_WORLD_TS
14-
app_data_NG = Apps.HELLO_WORLD_NG
15-
16-
js_app = Settings.AppName.DEFAULT + 'JS'
17-
ts_app = Settings.AppName.DEFAULT + 'TS'
18-
ng_app = Settings.AppName.DEFAULT + 'NG'
19-
20-
js_app_space = Settings.AppName.WITH_SPACE
21-
js_app_dash = Settings.AppName.WITH_DASH
22-
js_app_number = Settings.AppName.WITH_NUMBER
2313

2414
@classmethod
2515
def setUpClass(cls):
2616
TnsTest.setUpClass()
2717

2818
def setUp(self):
2919
TnsTest.setUp(self)
30-
Folder.clean(os.path.join(Settings.TEST_RUN_HOME, 'folder'))
31-
Folder.clean(os.path.join(Settings.TEST_RUN_HOME, 'js_app_space'))
32-
Folder.clean(os.path.join(Settings.TEST_RUN_HOME, 'js_app_dash'))
33-
Folder.clean(os.path.join(Settings.TEST_RUN_HOME, 'js_app_number'))
20+
CreateTests.__clean_folders()
3421

3522
@classmethod
3623
def tearDownClass(cls):
3724
TnsTest.tearDownClass()
25+
CreateTests.__clean_folders()
3826

3927
def test_001_create_app_like_real_user(self):
40-
Tns.create(app_name=self.js_app, app_data=None)
28+
"""Create app with no any params"""
29+
Tns.create(app_name=Settings.AppName.DEFAULT, app_data=Apps.HELLO_WORLD_JS, update=False)
4130

4231
def test_002_create_app_template_js(self):
43-
"""Create app with --template js project without update modules"""
44-
Tns.create(app_name=self.js_app, template=Template.HELLO_WORLD_JS.local_package,
45-
app_data=self.app_data_JS, update=False, verify=False)
32+
"""Create app with --template js project"""
33+
Tns.create(app_name=Settings.AppName.DEFAULT, template=Template.HELLO_WORLD_JS.local_package,
34+
app_data=Apps.HELLO_WORLD_JS, update=False)
4635

4736
def test_003_create_app_template_ts(self):
48-
"""Create app with --template ts project without update modules"""
49-
Tns.create(app_name=self.ts_app, template=Template.HELLO_WORLD_TS.local_package,
50-
app_data=self.app_data_TS, update=False, verify=False)
37+
"""Create app with --template ts project"""
38+
Tns.create(app_name=Settings.AppName.DEFAULT, template=Template.HELLO_WORLD_TS.local_package,
39+
app_data=Apps.HELLO_WORLD_TS, update=False)
5140

5241
def test_004_create_app_template_ng(self):
53-
"""Create app with --template ng project without update modules"""
54-
Tns.create(app_name=self.ng_app, template=Template.HELLO_WORLD_NG.local_package,
55-
app_data=self.app_data_NG, update=False, verify=False)
42+
"""Create app with --template ng project"""
43+
Tns.create(app_name=Settings.AppName.DEFAULT, template=Template.HELLO_WORLD_NG.local_package,
44+
app_data=Apps.HELLO_WORLD_NG, update=False)
5645

5746
def test_005_create_project_with_path(self):
5847
"""Create project with --path option"""
59-
Tns.create(app_name=self.js_app, template=Template.HELLO_WORLD_JS.local_package,
60-
app_data=self.app_data_JS, path=os.path.join(Settings.TEST_RUN_HOME, 'folder', 'subfolder'),
61-
update=False, verify=False)
62-
assert Folder.exists(os.path.join(Settings.TEST_RUN_HOME, 'folder', 'subfolder', 'TestAppJS'))
48+
Tns.create(app_name=Settings.AppName.DEFAULT,
49+
template=Template.HELLO_WORLD_JS.local_package,
50+
app_data=Apps.HELLO_WORLD_JS,
51+
path=os.path.join(Settings.TEST_RUN_HOME, 'folder', 'subfolder'),
52+
update=False)
6353

64-
def test_006_create_project_with_space(self):
54+
def test_006_create_project_with_dash(self):
6555
""" Create project with space is possible, but packageId will skip the space symbol"""
66-
Tns.create(app_name=self.js_app_space, template=Template.HELLO_WORLD_JS.local_package,
67-
app_data=self.app_data_JS, update=False, verify=False)
56+
Tns.create(app_name=Settings.AppName.WITH_DASH, template=Template.HELLO_WORLD_JS.local_package,
57+
app_data=Apps.HELLO_WORLD_JS, update=False)
6858

6959
def test_007_create_project_with_space(self):
7060
""" Create project with dash is possible, but packageId will skip the dash symbol"""
71-
Tns.create(app_name=self.js_app_dash, template=Template.HELLO_WORLD_JS.local_package,
72-
app_data=self.app_data_JS, update=False, verify=False)
61+
Tns.create(app_name=Settings.AppName.WITH_SPACE, template=Template.HELLO_WORLD_JS.local_package,
62+
app_data=Apps.HELLO_WORLD_JS, update=False)
7363

7464
def test_008_create_project_named_123(self):
7565
"""Create app starting with digits should not be possible without --force option"""
76-
Tns.create(app_name=self.js_app_number, template=Template.HELLO_WORLD_JS.local_package,
77-
app_data=self.app_data_JS, update=False, verify=False)
78-
# TODO: package_json contains
66+
result = Tns.create(app_name=Settings.AppName.WITH_NUMBER, template=Template.HELLO_WORLD_JS.local_package,
67+
app_data=Apps.HELLO_WORLD_JS, update=False, verify=False)
68+
assert 'The project name does not start with letter and will fail to build for Android.' in result.output
69+
assert 'If You want to create project with this name add --force to the create command.' in result.output
70+
71+
Tns.create(app_name=Settings.AppName.WITH_NUMBER, template=Template.HELLO_WORLD_JS.local_package,
72+
app_data=Apps.HELLO_WORLD_JS, force=True, update=False)
7973

8074
def test_009_create_project_with_appid(self):
8175
"""Create project with --appid option"""
82-
Tns.create(app_name=self.js_app, template=Template.HELLO_WORLD_JS.local_package, app_data=self.app_data_JS,
83-
update=False, verify=False, app_id='org.nativescript.MyApp')
76+
Tns.create(app_name=Settings.AppName.DEFAULT, template=Template.HELLO_WORLD_JS.local_package,
77+
app_data=Apps.HELLO_WORLD_JS, update=False, app_id='org.nativescript.MyApp')
78+
79+
@staticmethod
80+
def __clean_folders():
81+
Folder.clean(os.path.join(Settings.TEST_RUN_HOME, 'folder'))
82+
Folder.clean(os.path.join(Settings.TEST_RUN_HOME, Settings.AppName.WITH_SPACE))
83+
Folder.clean(os.path.join(Settings.TEST_RUN_HOME, Settings.AppName.WITH_DASH))
84+
Folder.clean(os.path.join(Settings.TEST_RUN_HOME, Settings.AppName.WITH_NUMBER))

0 commit comments

Comments
 (0)