Skip to content

Commit d1d4b4c

Browse files
authored
feat: tests for ng g commands (#60)
**New:** - Test for `ng g` commands **Fixes:** - Fix Travis to fail when there is an issue with linter - Fix linter errors caused by previous commits in master
1 parent ec2c68f commit d1d4b4c

File tree

11 files changed

+272
-13
lines changed

11 files changed

+272
-13
lines changed

.travis.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ install:
1010
- python -m pip install --upgrade pip
1111
- pip install --upgrade -r requirements.txt
1212
script:
13-
- ./scripts/test.sh
13+
- export ANDROID_HOME=$HOME
14+
- python -m nose core_tests/unit
15+
- python -m flake8 --max-line-length=120 core core_tests data products tests
16+
- python -m pylint --disable=locally-disabled --rcfile=.pylintrc core data products
17+
- find core_tests | grep .py | grep -v .pyc | xargs python -m pylint --disable=locally-disabled --rcfile=.pylintrc
18+
- find tests | grep .py | grep -v .pyc | xargs python -m pylint --disable=locally-disabled --min-similarity-lines=15 --rcfile=.pylintrc

core/utils/file_utils.py

+2
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,10 @@ def extract_part_of_text(text, key_word):
190190

191191
@staticmethod
192192
def unpack_tar(file_path, dest_dir):
193+
# noinspection PyBroadException
193194
try:
194195
tar_file = tarfile.open(file_path, 'r:gz')
195196
tar_file.extractall(dest_dir)
197+
# pylint: disable=broad-except
196198
except Exception:
197199
Log.debug('Failed to unpack .tar file {0}'.format(file_path))

core_tests/unit/product/test_preview_helpers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
# noinspection PyMethodMayBeStatic
9-
class SyncMessagesTests(unittest.TestCase):
9+
class PreviewHelperTests(unittest.TestCase):
1010
current_folder = os.path.dirname(os.path.realpath(__file__))
1111

1212
def test_01_constants(self):

core_tests/unit/utils/run_tests.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import os
2+
import time
23
import unittest
34

4-
import time
55
from nose.tools import timed
66

7+
from core.enums.os_type import OSType
78
from core.settings import Settings
89
from core.utils.file_utils import File
910
from core.utils.process import Process
@@ -36,6 +37,7 @@ def test_02_run_command_with_redirect(self):
3637
assert result.output == '', 'Output should be empty.'
3738
assert self.current_file in File.read(path=out_file)
3839

40+
@unittest.skipIf(Settings.HOST_OS == OSType.WINDOWS, 'Skip on Windows.')
3941
def test_03_run_command_with_pipe(self):
4042
result = run(cmd='echo "test case" | wc -w ', wait=True, timeout=1)
4143
assert result.exit_code == 0, 'Wrong exit code of successful command.'

products/nativescript/preview_helpers.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,16 @@ def install_playground_app(device_info, platform):
5050
elif platform is Platform.ANDROID:
5151
Adb.install(package_android, device_info.id)
5252

53+
# noinspection PyUnresolvedReferences
5354
@staticmethod
5455
def get_url(output):
55-
"""Get preview URL form tns log.This is the url you need to load in Preview app
56-
in order to see and sync your project"""
56+
# pylint: disable=no-member
57+
# pylint: disable=no-name-in-module
58+
# pylint: disable=import-error
59+
"""
60+
Get preview URL form tns log.
61+
This is the url you need to load in Preview app in order to see and sync your project.
62+
"""
5763
url = re.findall(r"(nsplay[^\s']+)", output)[0]
5864
if Settings.PYTHON_VERSION < 3:
5965
import urllib
@@ -67,14 +73,16 @@ def get_url(output):
6773

6874
@staticmethod
6975
def run_app(url, device_id, platform):
70-
"""Runs your project in the Preview App on simulator or emulator"""
76+
"""
77+
Runs project in the Preview App on simulator or emulator.
78+
"""
7179
if platform is Platform.IOS:
7280
cmd = "xcrun simctl openurl {0} {1}.".format(device_id, url)
7381
result = run(cmd)
7482
assert 'error' not in result.output
7583
elif platform is Platform.ANDROID:
76-
cmd = '{0} -s {1} shell am start -a android.intent.action.VIEW -d \
77-
"{2}" org.nativescript.preview'.format(ADB_PATH, device_id, url)
84+
cmd = '{0} -s {1} shell am start -a android.intent.action.VIEW -d "{2}" org.nativescript.preview' \
85+
.format(ADB_PATH, device_id, url)
7886
result = run(cmd)
7987
assert 'error' not in result.output
8088

products/nativescript/tns.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
from core.utils.run import run
1414
from products.nativescript.app import App
1515
from products.nativescript.tns_assert import TnsAssert
16-
from products.nativescript.tns_paths import TnsPaths
1716
from products.nativescript.tns_logs import TnsLogs
17+
from products.nativescript.tns_paths import TnsPaths
1818

1919

2020
class Tns(object):
@@ -388,8 +388,8 @@ def preview(app_name, bundle=False, hmr=False, aot=False, uglify=False, wait=Fal
388388
result = Tns.exec_command(command='preview', path=app_name, bundle=bundle, hmr=hmr, aot=aot, uglify=uglify,
389389
wait=wait, log_trace=log_trace, timeout=timeout)
390390
if verify:
391-
strings = ['Use NativeScript Playground app and scan the QR code above to preview '\
392-
'the application on your device']
393-
TnsLogs.wait_for_log(result.log_file, strings)
391+
strings = [
392+
'Use NativeScript Playground app and scan the QR code above to preview the application on your device']
393+
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)
394394

395395
return result

scripts/test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ python -m nose core_tests/unit
77
python -m flake8 --max-line-length=120 core core_tests data products tests
88
python -m pylint --disable=locally-disabled --rcfile=.pylintrc core data products
99
find core_tests | grep .py | grep -v .pyc | xargs python -m pylint --disable=locally-disabled --rcfile=.pylintrc
10-
find tests | grep .py | grep -v .pyc | xargs python -m pylint --disable=locally-disabled --min-similarity-lines=15 --rcfile=.pylintrc
10+
find tests | grep .py | grep -v .pyc | xargs python -m pylint --disable=locally-disabled --min-similarity-lines=15 --rcfile=.pylintrc
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""
2+
Tests for `ng g` during livesync.
3+
"""
4+
import os
5+
import unittest
6+
7+
from core.base_test.tns_run_test import TnsRunTest
8+
from core.enums.app_type import AppType
9+
from core.enums.os_type import OSType
10+
from core.enums.platform_type import Platform
11+
from core.settings import Settings
12+
from core.utils.file_utils import Folder, File
13+
from products.angular.ng import NG, NS_SCHEMATICS
14+
from products.nativescript.tns import Tns
15+
from products.nativescript.tns_assert import TnsAssert
16+
from products.nativescript.tns_logs import TnsLogs
17+
from products.nativescript.tns_paths import TnsPaths
18+
19+
20+
class NGGenE2ETestsNS(TnsRunTest):
21+
app_name = Settings.AppName.DEFAULT
22+
app_path = TnsPaths.get_app_path(app_name=app_name)
23+
24+
@classmethod
25+
def setUpClass(cls):
26+
TnsRunTest.setUpClass()
27+
NG.kill()
28+
29+
def setUp(self):
30+
TnsRunTest.setUp(self)
31+
NG.kill()
32+
33+
def tearDown(self):
34+
NG.kill()
35+
TnsRunTest.tearDown(self)
36+
37+
def test_100_ns_generate_during_run_android(self):
38+
NGGenE2ETestsNS.workflow(app_name=self.app_name, device=self.emu, platform=Platform.ANDROID, shared=False)
39+
40+
@unittest.skipIf(Settings.HOST_OS != OSType.OSX, 'Skip iOS tests on non macOS machines.')
41+
def test_100_ns_generate_during_run_ios(self):
42+
NGGenE2ETestsNS.workflow(app_name=self.app_name, device=self.sim, platform=Platform.IOS, shared=False)
43+
44+
def test_200_shared_generate_during_run_android(self):
45+
NGGenE2ETestsNS.workflow(app_name=self.app_name, device=self.emu, platform=Platform.ANDROID, shared=True)
46+
47+
@unittest.skipIf(Settings.HOST_OS != OSType.OSX, 'Skip iOS tests on non macOS machines.')
48+
def test_200_shared_generate_during_run_ios(self):
49+
NGGenE2ETestsNS.workflow(app_name=self.app_name, device=self.sim, platform=Platform.IOS, shared=True)
50+
51+
@staticmethod
52+
def workflow(app_name, device, platform, shared):
53+
# Create an app
54+
app_path = TnsPaths.get_app_path(app_name=app_name)
55+
Folder.clean(app_path)
56+
NG.new(collection=NS_SCHEMATICS, project=app_name, shared=shared)
57+
TnsAssert.created(app_name=app_name, app_data=None)
58+
59+
# Run app initially
60+
text = 'TAP'
61+
if shared:
62+
text = 'Welcome to'
63+
result = Tns.run(app_name=app_name, platform=platform, emulator=True, hmr=True)
64+
strings = TnsLogs.run_messages(app_name=app_name, platform=platform, bundle=True, hmr=True, app_type=AppType.NG)
65+
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=300)
66+
device.wait_for_text(text=text)
67+
68+
# Generate module and component
69+
NG.exec_command(command='g m module-test', cwd=app_path)
70+
NG.exec_command(command='g c module-test/component-test', cwd=app_path)
71+
72+
# Update app.modules.ts
73+
app_module_name = 'app.module.ts'
74+
app_module_path = os.path.join(app_path, 'app', app_module_name)
75+
if shared:
76+
app_module_name = 'app.module.tns.ts'
77+
app_module_path = os.path.join(app_path, 'src', 'app', app_module_name)
78+
old_string = "import { HomeComponent } from './home/home.component';"
79+
new_string = "import { ComponentTestComponent } from './module-test/component-test/component-test.component';"
80+
File.replace(path=app_module_path, old_string=old_string, new_string=new_string)
81+
File.replace(path=app_module_path, old_string='HomeComponent,', new_string='ComponentTestComponent,')
82+
83+
# Update app-routing.module.ts
84+
app_routing_module_name = 'app-routing.module.ts'
85+
app_routing_module_path = os.path.join(app_path, 'app', app_routing_module_name)
86+
if shared:
87+
app_routing_module_name = 'app.routes.ts'
88+
app_routing_module_path = os.path.join(app_path, 'src', 'app', app_routing_module_name)
89+
old_string = "import { HomeComponent } from './home/home.component';"
90+
new_string = "import { ComponentTestComponent } from './module-test/component-test/component-test.component';"
91+
File.replace(path=app_routing_module_path, old_string=old_string, new_string=new_string)
92+
File.replace(path=app_routing_module_path, old_string='HomeComponent', new_string='ComponentTestComponent')
93+
94+
# Verify app is updated
95+
logs = [app_module_name.replace('.tns', ''), app_routing_module_name.replace('.tns', ''),
96+
'Successfully synced application']
97+
TnsLogs.wait_for_log(log_file=result.log_file, string_list=logs, timeout=120)
98+
device.wait_for_text(text='component-test works!')
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
Tests for `ng g` in context of NativeScript only application.
3+
"""
4+
import unittest
5+
6+
from core.base_test.tns_test import TnsTest
7+
from core.settings import Settings
8+
from core.utils.file_utils import Folder
9+
from products.angular.ng import NG, NS_SCHEMATICS
10+
from products.nativescript.tns_assert import TnsAssert
11+
from products.nativescript.tns_paths import TnsPaths
12+
13+
14+
# noinspection PyMethodMayBeStatic
15+
class NGGenerateNGTests(TnsTest):
16+
app_name = Settings.AppName.DEFAULT
17+
app_path = TnsPaths.get_app_path(app_name=app_name)
18+
19+
@classmethod
20+
def setUpClass(cls):
21+
TnsTest.setUpClass()
22+
NG.kill()
23+
Folder.clean(cls.app_path)
24+
25+
# Create app
26+
NG.new(collection=NS_SCHEMATICS, project=cls.app_name, shared=False)
27+
TnsAssert.created(app_name=cls.app_name, app_data=None)
28+
29+
def setUp(self):
30+
TnsTest.setUpClass()
31+
NG.kill()
32+
33+
def tearDown(self):
34+
NG.kill()
35+
TnsTest.tearDown(self)
36+
37+
def test_001_generate_component(self):
38+
result = NG.exec_command(command='g c component-test', cwd=self.app_path)
39+
assert 'nsconfig.json not found. Assuming this is a {N} only project' in result.output
40+
assert 'CREATE app/component-test/component-test.component.html' in result.output
41+
assert 'CREATE app/component-test/component-test.component.ts' in result.output
42+
assert 'CREATE app/component-test/component-test.component.css' in result.output
43+
assert 'UPDATE app/app.module.ts' in result.output
44+
45+
def test_002_generate_module(self):
46+
result = NG.exec_command(command='g m module-test', cwd=self.app_path)
47+
assert 'nsconfig.json not found. Assuming this is a {N} only project' in result.output
48+
assert 'CREATE app/module-test/module-test.module.ts' in result.output
49+
50+
def test_003_generate_component_in_existing_modules(self):
51+
result = NG.exec_command(command='g m module-test2', cwd=self.app_path)
52+
assert 'nsconfig.json not found. Assuming this is a {N} only project' in result.output
53+
assert 'CREATE app/module-test2/module-test2.module.ts' in result.output
54+
55+
result = NG.exec_command(command='g c module-test2/component-name', cwd=self.app_path)
56+
assert 'nsconfig.json not found. Assuming this is a {N} only project' in result.output
57+
assert 'CREATE app/module-test2/component-name/component-name.component.html' in result.output
58+
assert 'CREATE app/module-test2/component-name/component-name.component.ts' in result.output
59+
assert 'CREATE app/module-test2/component-name/component-name.component.css' in result.output
60+
assert 'UPDATE app/module-test2/module-test2.module.ts' in result.output
61+
62+
@unittest.skip('Skip because of https://github.com/NativeScript/nativescript-schematics/issues/194')
63+
def test_004_generate_master_detail(self):
64+
result = NG.exec_command(command='g master-detail --master=dogs --detail=dog', cwd=self.app_path)
65+
assert 'CREATE app/dogs/dog-detail/dog-detail.component.html' in result.output
66+
assert 'CREATE app/dogs/dogs/dogs.component.html' in result.output
67+
assert 'data.service.ts' in result.output
68+
assert 'dogs.module.ts' in result.output
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
Tests for `ng g` in context of shared application.
3+
"""
4+
import unittest
5+
6+
from core.base_test.tns_test import TnsTest
7+
from core.settings import Settings
8+
from core.utils.file_utils import Folder
9+
from products.angular.ng import NG, NS_SCHEMATICS
10+
from products.nativescript.tns_assert import TnsAssert
11+
from products.nativescript.tns_paths import TnsPaths
12+
13+
14+
# noinspection PyMethodMayBeStatic
15+
class NGGenerateSharedTests(TnsTest):
16+
app_name = Settings.AppName.DEFAULT
17+
app_path = TnsPaths.get_app_path(app_name=app_name)
18+
19+
@classmethod
20+
def setUpClass(cls):
21+
TnsTest.setUpClass()
22+
NG.kill()
23+
Folder.clean(cls.app_path)
24+
25+
# Create app
26+
NG.new(collection=NS_SCHEMATICS, project=cls.app_name, shared=True)
27+
TnsAssert.created(app_name=cls.app_name, app_data=None)
28+
29+
def setUp(self):
30+
TnsTest.setUpClass()
31+
NG.kill()
32+
33+
def tearDown(self):
34+
NG.kill()
35+
TnsTest.tearDown(self)
36+
37+
def test_001_generate_component(self):
38+
result = NG.exec_command(command='g c component-test', cwd=self.app_path)
39+
assert 'CREATE src/app/component-test/component-test.component.html' in result.output
40+
assert 'CREATE src/app/component-test/component-test.component.ts' in result.output
41+
assert 'CREATE src/app/component-test/component-test.component.css' in result.output
42+
assert 'CREATE src/app/component-test/component-test.component.tns.css' in result.output
43+
assert 'CREATE src/app/component-test/component-test.component.tns.html' in result.output
44+
assert 'UPDATE src/app/app.module.ts' in result.output
45+
assert 'UPDATE src/app/app.module.tns.ts' in result.output
46+
47+
def test_002_generate_module(self):
48+
result = NG.exec_command(command='g m module-test', cwd=self.app_path)
49+
assert 'CREATE src/app/module-test/module-test.module.ts' in result.output
50+
assert 'CREATE src/app/module-test/module-test.module.tns.ts' in result.output
51+
assert 'CREATE src/app/module-test/module-test.common.ts' in result.output
52+
53+
def test_003_generate_component_in_existing_modules(self):
54+
result = NG.exec_command(command='g m module-test2', cwd=self.app_path)
55+
assert 'CREATE src/app/module-test2/module-test2.module.ts' in result.output
56+
assert 'CREATE src/app/module-test2/module-test2.module.tns.ts' in result.output
57+
assert 'CREATE src/app/module-test2/module-test2.common.ts' in result.output
58+
59+
result = NG.exec_command(command='g c module-test2/component-name', cwd=self.app_path)
60+
assert 'CREATE src/app/module-test2/component-name/component-name.component.html' in result.output
61+
assert 'CREATE src/app/module-test2/component-name/component-name.component.ts' in result.output
62+
assert 'CREATE src/app/module-test2/component-name/component-name.component.css' in result.output
63+
assert 'CREATE src/app/module-test2/component-name/component-name.component.tns.css' in result.output
64+
assert 'CREATE src/app/module-test2/component-name/component-name.component.tns.html' in result.output
65+
assert 'UPDATE src/app/module-test2/module-test2.module.ts' in result.output
66+
assert 'UPDATE src/app/module-test2/module-test2.module.tns.ts' in result.output
67+
68+
@unittest.skip('Skip because of https://github.com/NativeScript/nativescript-schematics/issues/194')
69+
def test_004_generate_master_detail(self):
70+
result = NG.exec_command(command='g master-detail --master=dogs --detail=dog', cwd=self.app_path)
71+
assert 'CREATE src/app/dogs/dog-detail/dog-detail.component.html' in result.output
72+
assert 'CREATE src/app/dogs/dog-detail/dog-detail.component.tns.html' in result.output
73+
assert 'CREATE src/app/dogs/dogs/dogs.component.html' in result.output
74+
assert 'CREATE src/app/dogs/dogs/dogs.component.tns.html' in result.output
75+
assert 'data.service.ts' in result.output
76+
assert 'dogs.module.ts' in result.output

0 commit comments

Comments
 (0)