-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathng_new_tests.py
195 lines (166 loc) · 8.4 KB
/
ng_new_tests.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"""
Tests for `ng new` command.
- Check if we generate correct shared project
- We are able to run them and build in release.
"""
import os
import unittest
from core.base_test.tns_run_test import TnsRunTest
from core.enums.env import EnvironmentType
from core.enums.os_type import OSType
from core.enums.platform_type import Platform
from core.enums.styling_type import StylingType
from core.settings import Settings
from core.utils.file_utils import Folder
from core.utils.json_utils import JsonUtils
from core.utils.docker import Docker
from data.apps import Apps
from data.const import Colors
from products.angular.ng import NG, NS_SCHEMATICS
from products.nativescript.app import App
from products.nativescript.tns import Tns
from products.nativescript.tns_assert import TnsAssert
from products.nativescript.tns_paths import TnsPaths
# noinspection PyMethodMayBeStatic
class NGNewTests(TnsRunTest):
app_name = Settings.AppName.DEFAULT
app_path = TnsPaths.get_app_path(app_name=app_name)
@classmethod
def setUpClass(cls):
TnsRunTest.setUpClass()
Docker.start()
def setUp(self):
TnsRunTest.setUp(self)
NG.kill()
Folder.clean(self.app_path)
def tearDown(self):
NG.kill()
TnsRunTest.tearDown(self)
@classmethod
def tearDownClass(cls):
Docker.stop()
def test_001_simple(self):
NGNewTests.create_and_run(shared=False)
NGNewTests.build_release()
def test_010_shared(self):
NGNewTests.create_and_run(shared=True)
NG.serve(project=self.app_name)
NG.kill()
NGNewTests.build_release()
NG.serve(project=self.app_name, prod=True)
def test_100_shared_with_sample(self):
NGNewTests.create_and_run(shared=True, sample=True)
def test_200_simple_no_theme(self):
NGNewTests.create_and_run(shared=False, theme=False)
def test_201_shared_with_sass(self):
NGNewTests.create_and_run(shared=False, style=StylingType.SCSS)
def test_202_shared_with_custom_source_dir_and_prefix(self):
NGNewTests.create_and_run(shared=True, prefix='myapp', source_dir='mysrc', style=StylingType.CSS)
@unittest.skip('Ignore because of https://github.com/NativeScript/nativescript-schematics/issues/157')
def test_300_help_ng_new(self):
output = NG.exec_command('new --collection={0} --help'.format(NS_SCHEMATICS)).output
assert '--sample' in output
assert 'Specifies whether a sample master detail should be generated' in output
assert '--shared' in output
assert 'Specifies whether to generate a shared project or a {N} only' in output
assert '--source-dir (-sd)' in output
assert 'The path of the source directory' in output
assert '--style' in output
assert 'The file extension to be used for style files. Supported are css and scss' in output
assert '--theme' in output
assert 'Specifies whether the {N} theme for styling should be included' in output
assert '--webpack' in output
assert 'Specifies whether the new application has webpack set up' in output
@staticmethod
def create_and_run(shared=True, sample=False, theme=True, style=None, prefix=None, source_dir=None, webpack=True):
# Get test data based on app type
app_data = Apps.SCHEMATICS_NS
if shared:
if sample:
app_data = Apps.SCHEMATICS_SHARED_SAMPLE
else:
app_data = Apps.SCHEMATICS_SHARED
# Create app
NGNewTests.create_app(app_data=app_data, shared=shared, sample=sample, theme=theme, style=style,
prefix=prefix, source_dir=source_dir, webpack=webpack)
# Update the app
if Settings.ENV != EnvironmentType.LIVE:
App.update(app_name=NGNewTests.app_name, modules=True, angular=True, typescript=True, web_pack=True)
# Run the app
NGNewTests.run_bundle(app_data=app_data, webpack=webpack, shared=shared, theme=theme,
emu=NGNewTests.emu, sim=NGNewTests.sim)
@staticmethod
def create_app(app_data, shared, sample, theme, style, prefix, source_dir, webpack):
# Create shared project with sample data
result = NG.new(collection=NS_SCHEMATICS, project=NGNewTests.app_name, theme=theme, shared=shared,
sample=sample, style=style, prefix=prefix, source_dir=source_dir, webpack=webpack)
# Verify valid {N} app is created
# Temporary do not assert theme is created because in schematics we still use nativescript-theme-core@1
# TODO: Replace with theme=theme when schematics use @nativescript/theme
TnsAssert.created(app_name=NGNewTests.app_name, app_data=app_data, theme=False, webpack=webpack)
assert 'Directory is already under version control. Skipping initialization of git.' in result.output, \
'Git init should be skipped because app is created already existing repo (the one with tests).'
# Check sample
if sample:
# TODO: Implement it
pass
# Check theme
if theme:
assert App.is_dependency(app_name=NGNewTests.app_name, dependency='@nativescript/theme')
else:
assert not App.is_dependency(app_name=NGNewTests.app_name, dependency='@nativescript/theme')
# Check styling
if style is None or style is StylingType.CSS:
assert 'app.css' in result.output
else:
assert 'app.android.scss' in result.output
assert 'app.ios.scss' in result.output
assert '_app-common.scss' in result.output
# Check webpack
if webpack:
assert App.is_dev_dependency(app_name=NGNewTests.app_name, dependency='nativescript-dev-webpack')
else:
assert not App.is_dev_dependency(app_name=NGNewTests.app_name, dependency='nativescript-dev-webpack')
# Check prefix
if prefix is None:
prefix = 'app'
path = os.path.join(Settings.TEST_RUN_HOME, NGNewTests.app_name, 'angular.json')
actual_prefix = JsonUtils.read(file_path=path)['projects'][NGNewTests.app_name]['prefix']
assert str(actual_prefix) == prefix, 'Prefix not set in angular.json'
# Check source dir exists (applicable only for shared projects).
if shared:
if source_dir is None:
source_dir = 'src'
assert Folder.exists(os.path.join(Settings.TEST_RUN_HOME, NGNewTests.app_name, source_dir))
@staticmethod
def run_bundle(app_data, webpack, shared, theme, emu, sim):
# Run android (if webpack is available -> use --bundle)
Tns.run(app_name=NGNewTests.app_name, platform=Platform.ANDROID, emulator=True, bundle=webpack)
for text in app_data.texts:
emu.wait_for_text(text=text, timeout=300)
# Check if theme is really applied (only for non shared projects, shared is not good example to check)
if not shared:
blue_pixels = emu.get_pixels_by_color(color=Colors.LIGHT_BLUE)
if theme:
assert blue_pixels > 1000, 'Default {N} theme is NOT applied on Android.'
else:
assert blue_pixels == 0, 'Default {N} theme is applied, but it should not.'
# Run ios (if webpack is available -> use --bundle)
if Settings.HOST_OS is OSType.OSX:
Tns.run(app_name=NGNewTests.app_name, platform=Platform.IOS, emulator=True, bundle=webpack)
for text in app_data.texts:
sim.wait_for_text(text=text, timeout=300)
# Check if theme is really applied (only for non shared projects, shared is not good example to check)
if not shared:
blue_pixels = emu.get_pixels_by_color(color=Colors.LIGHT_BLUE)
if theme:
assert blue_pixels > 1000, 'Default {N} theme is NOT applied on iOS.'
else:
assert blue_pixels == 0, 'Default {N} theme is applied, but it should not.'
@staticmethod
def build_release():
Tns.build(app_name=NGNewTests.app_name, platform=Platform.ANDROID, release=True,
bundle=True, aot=True, uglify=True, snapshot=True)
if Settings.HOST_OS is OSType.OSX:
Tns.build(app_name=NGNewTests.app_name, platform=Platform.IOS, release=True, for_device=True,
bundle=True, aot=True, uglify=True)