Skip to content

feat: tests for ng g commands #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ install:
- python -m pip install --upgrade pip
- pip install --upgrade -r requirements.txt
script:
- ./scripts/test.sh
- export ANDROID_HOME=$HOME
- python -m nose core_tests/unit
- python -m flake8 --max-line-length=120 core core_tests data products tests
- python -m pylint --disable=locally-disabled --rcfile=.pylintrc core data products
- find core_tests | grep .py | grep -v .pyc | xargs python -m pylint --disable=locally-disabled --rcfile=.pylintrc
- find tests | grep .py | grep -v .pyc | xargs python -m pylint --disable=locally-disabled --min-similarity-lines=15 --rcfile=.pylintrc
2 changes: 2 additions & 0 deletions core/utils/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,10 @@ def extract_part_of_text(text, key_word):

@staticmethod
def unpack_tar(file_path, dest_dir):
# noinspection PyBroadException
try:
tar_file = tarfile.open(file_path, 'r:gz')
tar_file.extractall(dest_dir)
# pylint: disable=broad-except
except Exception:
Log.debug('Failed to unpack .tar file {0}'.format(file_path))
2 changes: 1 addition & 1 deletion core_tests/unit/product/test_preview_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


# noinspection PyMethodMayBeStatic
class SyncMessagesTests(unittest.TestCase):
class PreviewHelperTests(unittest.TestCase):
current_folder = os.path.dirname(os.path.realpath(__file__))

def test_01_constants(self):
Expand Down
4 changes: 3 additions & 1 deletion core_tests/unit/utils/run_tests.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
import time
import unittest

import time
from nose.tools import timed

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

@unittest.skipIf(Settings.HOST_OS == OSType.WINDOWS, 'Skip on Windows.')
def test_03_run_command_with_pipe(self):
result = run(cmd='echo "test case" | wc -w ', wait=True, timeout=1)
assert result.exit_code == 0, 'Wrong exit code of successful command.'
Expand Down
18 changes: 13 additions & 5 deletions products/nativescript/preview_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,16 @@ def install_playground_app(device_info, platform):
elif platform is Platform.ANDROID:
Adb.install(package_android, device_info.id)

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

@staticmethod
def run_app(url, device_id, platform):
"""Runs your project in the Preview App on simulator or emulator"""
"""
Runs project in the Preview App on simulator or emulator.
"""
if platform is Platform.IOS:
cmd = "xcrun simctl openurl {0} {1}.".format(device_id, url)
result = run(cmd)
assert 'error' not in result.output
elif platform is Platform.ANDROID:
cmd = '{0} -s {1} shell am start -a android.intent.action.VIEW -d \
"{2}" org.nativescript.preview'.format(ADB_PATH, device_id, url)
cmd = '{0} -s {1} shell am start -a android.intent.action.VIEW -d "{2}" org.nativescript.preview' \
.format(ADB_PATH, device_id, url)
result = run(cmd)
assert 'error' not in result.output

Expand Down
8 changes: 4 additions & 4 deletions products/nativescript/tns.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from core.utils.run import run
from products.nativescript.app import App
from products.nativescript.tns_assert import TnsAssert
from products.nativescript.tns_paths import TnsPaths
from products.nativescript.tns_logs import TnsLogs
from products.nativescript.tns_paths import TnsPaths


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

return result
2 changes: 1 addition & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ python -m nose core_tests/unit
python -m flake8 --max-line-length=120 core core_tests data products tests
python -m pylint --disable=locally-disabled --rcfile=.pylintrc core data products
find core_tests | grep .py | grep -v .pyc | xargs python -m pylint --disable=locally-disabled --rcfile=.pylintrc
find tests | grep .py | grep -v .pyc | xargs python -m pylint --disable=locally-disabled --min-similarity-lines=15 --rcfile=.pylintrc
find tests | grep .py | grep -v .pyc | xargs python -m pylint --disable=locally-disabled --min-similarity-lines=15 --rcfile=.pylintrc
97 changes: 97 additions & 0 deletions tests/code_sharing/test_ng_generate_e2e.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"""
Tests for `ng g` during livesync.
"""
import os
import unittest

from core.base_test.tns_run_test import TnsRunTest
from core.enums.app_type import AppType
from core.enums.os_type import OSType
from core.enums.platform_type import Platform
from core.settings import Settings
from core.utils.file_utils import Folder, File
from products.angular.ng import NG, NS_SCHEMATICS
from products.nativescript.tns import Tns
from products.nativescript.tns_assert import TnsAssert
from products.nativescript.tns_logs import TnsLogs
from products.nativescript.tns_paths import TnsPaths


class NGGenE2ETestsNS(TnsRunTest):
ns_app_name = Settings.AppName.DEFAULT
shared_app_name = 'SharedApp'
ns_app_path = TnsPaths.get_app_path(app_name=ns_app_name)
shared_app_path = TnsPaths.get_app_path(app_name=shared_app_name)

@classmethod
def setUpClass(cls):
TnsRunTest.setUpClass()
NG.kill()

def setUp(self):
TnsRunTest.setUpClass()
NG.kill()

def tearDown(self):
NG.kill()
TnsRunTest.tearDown(self)

def test_100_ns_generate_during_run_android(self):
NGGenE2ETestsNS.workflow(app_name=self.ns_app_name, device=self.emu, platform=Platform.ANDROID, shared=False)

@unittest.skipIf(Settings.HOST_OS != OSType.OSX, 'Skip iOS tests on non macOS machines.')
def test_100_ns_generate_during_run_ios(self):
NGGenE2ETestsNS.workflow(app_name=self.ns_app_name, device=self.sim, platform=Platform.IOS, shared=False)

def test_200_shared_generate_during_run_android(self):
NGGenE2ETestsNS.workflow(app_name=self.ns_app_name, device=self.emu, platform=Platform.ANDROID, shared=True)

@unittest.skipIf(Settings.HOST_OS != OSType.OSX, 'Skip iOS tests on non macOS machines.')
def test_200_shared_generate_during_run_ios(self):
NGGenE2ETestsNS.workflow(app_name=self.ns_app_name, device=self.sim, platform=Platform.IOS, shared=True)

@staticmethod
def workflow(app_name, device, platform, shared):
# Create an app
app_path = TnsPaths.get_app_path(app_name=app_name)
Folder.clean(app_path)
NG.new(collection=NS_SCHEMATICS, project=app_name, shared=shared)
TnsAssert.created(app_name=app_name, app_data=None)

# Run app initially
result = Tns.run_android(app_name=app_name, emulator=True, hmr=True)
strings = TnsLogs.run_messages(app_name=app_name, platform=platform, bundle=True, hmr=True, app_type=AppType.NG)
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=300)
device.wait_for_text(text='TAP')

# Generate module and component
NG.exec_command(command='g m module-test', cwd=app_path)
NG.exec_command(command='g c module-test/component-test', cwd=app_path)

# Update app.modules.ts
app_module_name = 'app.module.ts'
app_module_path = os.path.join(app_path, 'app', app_module_name)
if shared:
app_module_name = 'app.module.tns.ts'
app_module_path = os.path.join(app_path, 'src', 'app', app_module_name)
old_string = "import { HomeComponent } from './home/home.component';"
new_string = "import { ComponentTestComponent } from './module-test/component-test/component-test.component';"
File.replace(path=app_module_path, old_string=old_string, new_string=new_string)
File.replace(path=app_module_path, old_string='HomeComponent,', new_string='ComponentTestComponent,')

# Update app-routing.module.ts
app_routing_module_name = 'app-routing.module.ts'
app_routing_module_path = os.path.join(app_path, 'app', app_routing_module_name)
if shared:
app_routing_module_name = 'app.routes.ts'
app_routing_module_path = os.path.join(app_path, 'src', 'app', app_routing_module_name)
old_string = "import { HomeComponent } from './home/home.component';"
new_string = "import { ComponentTestComponent } from './module-test/component-test/component-test.component';"
File.replace(path=app_routing_module_path, old_string=old_string, new_string=new_string)
File.replace(path=app_routing_module_path, old_string='HomeComponent', new_string='ComponentTestComponent')

# Verify app is updated
logs = [app_module_name.replace('.tns', ''), app_routing_module_name.replace('.tns', ''),
'Successfully synced application']
TnsLogs.wait_for_log(log_file=result.log_file, string_list=logs, timeout=120)
device.wait_for_text(text='component-test works!')
68 changes: 68 additions & 0 deletions tests/code_sharing/test_ng_generate_ns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
Tests for `ng g` in context of NativeScript only application.
"""
import unittest

from core.base_test.tns_test import TnsTest
from core.settings import Settings
from core.utils.file_utils import Folder
from products.angular.ng import NG, NS_SCHEMATICS
from products.nativescript.tns_assert import TnsAssert
from products.nativescript.tns_paths import TnsPaths


# noinspection PyMethodMayBeStatic
class NGGenerateNGTests(TnsTest):
app_name = Settings.AppName.DEFAULT
app_path = TnsPaths.get_app_path(app_name=app_name)

@classmethod
def setUpClass(cls):
TnsTest.setUpClass()
NG.kill()
Folder.clean(cls.app_path)

# Create app
NG.new(collection=NS_SCHEMATICS, project=cls.app_name, shared=False)
TnsAssert.created(app_name=cls.app_name, app_data=None)

def setUp(self):
TnsTest.setUpClass()
NG.kill()

def tearDown(self):
NG.kill()
TnsTest.tearDown(self)

def test_001_generate_component(self):
result = NG.exec_command(command='g c component-test', cwd=self.app_path)
assert 'nsconfig.json not found. Assuming this is a {N} only project' in result.output
assert 'CREATE app/component-test/component-test.component.html' in result.output
assert 'CREATE app/component-test/component-test.component.ts' in result.output
assert 'CREATE app/component-test/component-test.component.css' in result.output
assert 'UPDATE app/app.module.ts' in result.output

def test_002_generate_module(self):
result = NG.exec_command(command='g m module-test', cwd=self.app_path)
assert 'nsconfig.json not found. Assuming this is a {N} only project' in result.output
assert 'CREATE app/module-test/module-test.module.ts' in result.output

def test_003_generate_component_in_existing_modules(self):
result = NG.exec_command(command='g m module-test2', cwd=self.app_path)
assert 'nsconfig.json not found. Assuming this is a {N} only project' in result.output
assert 'CREATE app/module-test2/module-test2.module.ts' in result.output

result = NG.exec_command(command='g c module-test2/component-name', cwd=self.app_path)
assert 'nsconfig.json not found. Assuming this is a {N} only project' in result.output
assert 'CREATE app/module-test2/component-name/component-name.component.html' in result.output
assert 'CREATE app/module-test2/component-name/component-name.component.ts' in result.output
assert 'CREATE app/module-test2/component-name/component-name.component.css' in result.output
assert 'UPDATE app/module-test2/module-test2.module.ts' in result.output

@unittest.skip('Skip because of https://github.com/NativeScript/nativescript-schematics/issues/194')
def test_004_generate_master_detail(self):
result = NG.exec_command(command='g master-detail --master=dogs --detail=dog', cwd=self.app_path)
assert 'CREATE app/dogs/dog-detail/dog-detail.component.html' in result.output
assert 'CREATE app/dogs/dogs/dogs.component.html' in result.output
assert 'data.service.ts' in result.output
assert 'dogs.module.ts' in result.output
76 changes: 76 additions & 0 deletions tests/code_sharing/test_ng_generate_shared.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""
Tests for `ng g` in context of shared application.
"""
import unittest

from core.base_test.tns_test import TnsTest
from core.settings import Settings
from core.utils.file_utils import Folder
from products.angular.ng import NG, NS_SCHEMATICS
from products.nativescript.tns_assert import TnsAssert
from products.nativescript.tns_paths import TnsPaths


# noinspection PyMethodMayBeStatic
class NGGenerateSharedTests(TnsTest):
app_name = Settings.AppName.DEFAULT
app_path = TnsPaths.get_app_path(app_name=app_name)

@classmethod
def setUpClass(cls):
TnsTest.setUpClass()
NG.kill()
Folder.clean(cls.app_path)

# Create app
NG.new(collection=NS_SCHEMATICS, project=cls.app_name, shared=True)
TnsAssert.created(app_name=cls.app_name, app_data=None)

def setUp(self):
TnsTest.setUpClass()
NG.kill()

def tearDown(self):
NG.kill()
TnsTest.tearDown(self)

def test_001_generate_component(self):
result = NG.exec_command(command='g c component-test', cwd=self.app_path)
assert 'CREATE src/app/component-test/component-test.component.html' in result.output
assert 'CREATE src/app/component-test/component-test.component.ts' in result.output
assert 'CREATE src/app/component-test/component-test.component.css' in result.output
assert 'CREATE src/app/component-test/component-test.component.tns.css' in result.output
assert 'CREATE src/app/component-test/component-test.component.tns.html' in result.output
assert 'UPDATE src/app/app.module.ts' in result.output
assert 'UPDATE src/app/app.module.tns.ts' in result.output

def test_002_generate_module(self):
result = NG.exec_command(command='g m module-test', cwd=self.app_path)
assert 'CREATE src/app/module-test/module-test.module.ts' in result.output
assert 'CREATE src/app/module-test/module-test.module.tns.ts' in result.output
assert 'CREATE src/app/module-test/module-test.common.ts' in result.output

def test_003_generate_component_in_existing_modules(self):
result = NG.exec_command(command='g m module-test2', cwd=self.app_path)
assert 'CREATE src/app/module-test2/module-test2.module.ts' in result.output
assert 'CREATE src/app/module-test2/module-test2.module.tns.ts' in result.output
assert 'CREATE src/app/module-test2/module-test2.common.ts' in result.output

result = NG.exec_command(command='g c module-test2/component-name', cwd=self.app_path)
assert 'CREATE src/app/module-test2/component-name/component-name.component.html' in result.output
assert 'CREATE src/app/module-test2/component-name/component-name.component.ts' in result.output
assert 'CREATE src/app/module-test2/component-name/component-name.component.css' in result.output
assert 'CREATE src/app/module-test2/component-name/component-name.component.tns.css' in result.output
assert 'CREATE src/app/module-test2/component-name/component-name.component.tns.html' in result.output
assert 'UPDATE src/app/module-test2/module-test2.module.ts' in result.output
assert 'UPDATE src/app/module-test2/module-test2.module.tns.ts' in result.output

@unittest.skip('Skip because of https://github.com/NativeScript/nativescript-schematics/issues/194')
def test_004_generate_master_detail(self):
result = NG.exec_command(command='g master-detail --master=dogs --detail=dog', cwd=self.app_path)
assert 'CREATE src/app/dogs/dog-detail/dog-detail.component.html' in result.output
assert 'CREATE src/app/dogs/dog-detail/dog-detail.component.tns.html' in result.output
assert 'CREATE src/app/dogs/dogs/dogs.component.html' in result.output
assert 'CREATE src/app/dogs/dogs/dogs.component.tns.html' in result.output
assert 'data.service.ts' in result.output
assert 'dogs.module.ts' in result.output