Skip to content

Commit bc5d704

Browse files
authored
feat: rewrite init tests (#34)
* wip: rewrite init tests Work in progress... * feat: init and intall tests * wip: rewrite init tests Work in progress... * feat: init and intall tests * feat: init and install tests
1 parent 1853150 commit bc5d704

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

tests/cli/create/test_init.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import os
2+
3+
from core.base_test.tns_test import TnsTest
4+
from core.enums.os_type import OSType
5+
from core.settings import Settings
6+
from core.utils.file_utils import Folder, File
7+
from core.utils.npm import Npm
8+
from products.nativescript.app import App
9+
from products.nativescript.tns import Tns
10+
11+
APP_NAME = Settings.AppName.DEFAULT
12+
APP_PATH = os.path.join(Settings.TEST_RUN_HOME, APP_NAME)
13+
14+
15+
# noinspection PyMethodMayBeStatic
16+
class InitAndInstallTests(TnsTest):
17+
18+
def setUp(self):
19+
TnsTest.setUp(self)
20+
Folder.clean(APP_PATH)
21+
22+
def test_201_init_defaults(self):
23+
Folder.create(APP_PATH)
24+
result = Tns.exec_command(command='init --force', cwd=APP_PATH)
25+
self.verify_initialized(output=result.output)
26+
27+
def test_202_init_path(self):
28+
result = Tns.exec_command(command='init --force', path=APP_NAME)
29+
self.verify_initialized(output=result.output)
30+
31+
def test_203_init_update_existing_file(self):
32+
self.test_202_init_path()
33+
34+
# Modify existing file
35+
package_json = os.path.join(Settings.TEST_RUN_HOME, APP_NAME, 'package.json')
36+
File.replace(path=package_json, old_string=APP_NAME, new_string='QApp')
37+
assert 'QApp' in File.read(package_json)
38+
39+
# Overwrite changes
40+
self.test_202_init_path()
41+
42+
def test_204_install_defaults(self):
43+
self.test_202_init_path()
44+
result = Tns.exec_command(command='install', path=APP_PATH)
45+
self.verify_installed(output=result.output)
46+
47+
def test_205_install_external_packages(self):
48+
self.test_202_init_path()
49+
50+
# Add packages
51+
Npm.install(package='lodash', option='--save', folder=APP_PATH)
52+
Npm.install(package='gulp', option='--save-dev', folder=APP_PATH)
53+
assert App.is_dependency(app_name=APP_NAME, dependency='lodash')
54+
assert App.is_dev_dependency(app_name=APP_NAME, dependency='gulp')
55+
56+
# Clean node modules
57+
Folder.clean(os.path.join(APP_PATH, 'node_modules'))
58+
Folder.clean(os.path.join(APP_PATH, 'platforms'))
59+
60+
# Install and verify common packages
61+
result = Tns.exec_command(command='install', path=APP_PATH)
62+
self.verify_installed(output=result.output)
63+
64+
# Verify external packages are also installed
65+
assert Folder.exists(os.path.join(APP_PATH, 'node_modules', 'lodash'))
66+
assert Folder.exists(os.path.join(APP_PATH, 'node_modules', 'gulp'))
67+
68+
def test_300_install_and_prepare(self):
69+
self.test_202_init_path()
70+
71+
# Add packages
72+
Npm.install(package='lodash', option='--save', folder=APP_PATH)
73+
Npm.install(package='gulp', option='--save-dev', folder=APP_PATH)
74+
assert App.is_dependency(app_name=APP_NAME, dependency='lodash')
75+
assert App.is_dev_dependency(app_name=APP_NAME, dependency='gulp')
76+
77+
# Call install and verify it do not fail if everything is already installed
78+
result = Tns.exec_command(command='install', path=APP_PATH)
79+
self.verify_installed(output=result.output)
80+
assert Folder.exists(os.path.join(APP_PATH, 'node_modules', 'lodash'))
81+
assert Folder.exists(os.path.join(APP_PATH, 'node_modules', 'gulp'))
82+
83+
# Copy app folder and app resources
84+
Folder.copy(source=os.path.join(Settings.TEST_RUN_HOME, 'assets', 'template-min', 'app'),
85+
target=os.path.join(APP_PATH, 'app'))
86+
87+
# Prepare project
88+
Tns.prepare_android(app_name=APP_NAME)
89+
if Settings.HOST_OS == OSType.OSX:
90+
Tns.prepare_ios(app_name=APP_NAME)
91+
92+
# Verify prepare
93+
base_path = os.path.join(APP_PATH, 'platforms', 'android', 'app', 'src', 'main', 'assets', 'app', 'tns_modules')
94+
assert Folder.exists(os.path.join(base_path, 'lodash')), 'Dependency not available in platforms.'
95+
assert not Folder.exists(os.path.join(base_path, 'gulp')), 'DevDependency available in platforms.'
96+
# TODO: Verify prepare for iOS
97+
98+
def test_400_install_in_not_existing_folder(self):
99+
result = Tns.exec_command(command='install', path=APP_NAME)
100+
assert "No project found" in result.output
101+
102+
def verify_initialized(self, output):
103+
# Verify output
104+
assert 'Project successfully initialized.' in output
105+
106+
# Verify app folder do not exists
107+
assert not Folder.exists(os.path.join(APP_PATH, 'app'))
108+
assert not Folder.exists(os.path.join(APP_PATH, 'src'))
109+
assert not Folder.exists(os.path.join(APP_PATH, 'node_modules'))
110+
assert not Folder.exists(os.path.join(APP_PATH, 'platforms'))
111+
112+
# Verify package.json
113+
json = App.get_package_json(app_name=APP_NAME)
114+
assert json['nativescript']['id'] == 'org.nativescript.{0}'.format(APP_NAME)
115+
assert json['nativescript']['tns-android']['version'] is not None
116+
if Settings.HOST_OS == OSType.OSX:
117+
assert json['nativescript']['tns-ios']['version'] is not None
118+
assert json['dependencies']['tns-core-modules'] is not None
119+
120+
def verify_installed(self, output):
121+
# Verify output
122+
assert 'Copying template files' in output
123+
assert 'Platform android successfully added' in output
124+
if Settings.HOST_OS == OSType.OSX:
125+
assert 'Platform ios successfully added' in output
126+
127+
# Verify files
128+
assert Folder.exists(os.path.join(APP_PATH, 'node_modules', 'tns-core-modules'))
129+
assert Folder.exists(os.path.join(APP_PATH, 'platforms', 'android'))
130+
assert File.exists(os.path.join(APP_PATH, 'platforms', 'android', 'build.gradle'))
131+
if Settings.HOST_OS == OSType.OSX:
132+
assert Folder.exists(os.path.join(APP_PATH, 'platforms', 'ios'))
133+
assert File.exists(os.path.join(APP_PATH, 'platforms', 'ios', 'TestApp.xcodeproj'))
134+
135+
# App resources not created, see https://github.com/NativeScript/nativescript-cli/issues/4341

0 commit comments

Comments
 (0)