1
1
#! /usr/bin/env bash
2
-
2
+ #
3
3
# unittests.sh
4
- # Usage example: ./unittests.sh [--no-clean]
4
+ # Usage example: ./unittests.sh [--no-clean | --no-clean-on-fail ]
5
5
6
6
# Set Script Variables
7
7
8
8
SCRIPT=" $( " $( dirname " $0 " ) /resolvepath.sh" " $0 " ) "
9
9
SCRIPTS_DIR=" $( dirname " $SCRIPT " ) "
10
10
ROOT_DIR=" $( dirname " $SCRIPTS_DIR " ) "
11
- CURRENT_DIR=" $( pwd -P) "
12
11
13
12
TEMP_DIR=" $( mktemp -d) "
14
13
PROJECT_NAME=" $( basename " $( mktemp -u $TEMP_DIR /ProjectTemplateUnitTests_XXXXXX) " ) "
15
14
OUTPUT_DIR=" $TEMP_DIR /$PROJECT_NAME "
16
15
16
+ NO_CLEAN=0
17
+ NO_CLEAN_ON_FAIL=0
18
+
17
19
EXIT_CODE=0
18
20
EXIT_MESSAGE=" "
19
21
20
- if [ " $1 " == " --no-clean" ]; then
21
- NO_CLEAN=1
22
- fi
22
+ # Parse Arguments
23
23
24
- # Function Definitions
24
+ while [[ $# -gt 0 ]]; do
25
+ case " $1 " in
26
+ --no-clean)
27
+ NO_CLEAN=1
28
+ shift # --no-clean
29
+ ;;
25
30
26
- function cleanup() {
27
- if [ -z ${NO_CLEAN+x} ]; then
28
- cd " $CURRENT_DIR "
29
- rm -rf " $TEMP_DIR "
30
- else
31
- echo " Test Project: $OUTPUT_DIR "
32
- fi
31
+ --no-clean-on-fail)
32
+ NO_CLEAN_ON_FAIL=1
33
+ shift # --no-clean-on-fail
34
+ ;;
33
35
34
- #
36
+ * )
37
+ echo " Unknown argument: $1 "
38
+ print_help
39
+ esac
40
+ done
35
41
36
- local CARTHAGE_CACHE=" $HOME /Library/Caches/org.carthage.CarthageKit"
37
- if [ -e " $CARTHAGE_CACHE " ]; then
38
- if [ -e " $CARTHAGE_CACHE /dependencies/$PROJECT_NAME " ]; then
39
- rm -rf " $CARTHAGE_CACHE /dependencies/$PROJECT_NAME "
40
- fi
42
+ # Function Definitions
41
43
42
- for DIR in $( find " $CARTHAGE_CACHE /DerivedData " -mindepth 1 -maxdepth 1 -type d ) ; do
43
- if [ -e " $DIR / $PROJECT_NAME " ]; then
44
- rm -rf " $DIR / $PROJECT_NAME "
45
- fi
46
- done
44
+ function cleanup() {
45
+ if [[ " $NO_CLEAN " == " 1 " ]] || [[ " $NO_CLEAN_ON_FAIL " == " 1 " && " $EXIT_CODE " != " 0 " ] ]; then
46
+ echo " Test Project: $OUTPUT_DIR "
47
+ else
48
+ rm -rf " $TEMP_DIR "
47
49
fi
48
50
49
- #
50
-
51
51
if [ ${# EXIT_MESSAGE} -gt 0 ]; then
52
52
echo -e " $EXIT_MESSAGE "
53
53
fi
@@ -75,44 +75,10 @@ printstep "Setting Up Test Project..."
75
75
" $SCRIPTS_DIR /config.sh" -name " $PROJECT_NAME " -output " $TEMP_DIR "
76
76
checkresult $? " 'config.sh' script failed"
77
77
78
- # Check For Unit Test Dependencies
79
-
80
- cd " $OUTPUT_DIR "
81
- printstep " Checking Dependencies..."
78
+ # Check For Dependencies
82
79
83
- # ## Carthage
80
+ printstep " Checking for Configuration Dependencies... "
84
81
85
- if which carthage > /dev/null; then
86
- CARTHAGE_VERSION=" $( carthage version) "
87
- echo " Carthage: $CARTHAGE_VERSION "
88
-
89
- " $SCRIPTS_DIR /versions.sh" " $CARTHAGE_VERSION " " 0.37.0"
90
-
91
- if [ $? -lt 0 ]; then
92
- echo -e " \033[33mCarthage version of at least 0.37.0 is recommended for running these unit tests\033[0m"
93
- fi
94
- else
95
- checkresult -1 " Carthage is not installed and is required for running unit tests: \033[4;34mhttps://github.com/Carthage/Carthage#installing-carthage"
96
- fi
97
-
98
- # ## CocoaPods
99
-
100
- if which pod > /dev/null; then
101
- PODS_VERSION=" $( pod --version) "
102
- " $SCRIPTS_DIR /versions.sh" " $PODS_VERSION " " 1.7.3"
103
-
104
- if [ $? -ge 0 ]; then
105
- echo " CocoaPods: $( pod --version) "
106
- else
107
- checkresult -1 " These unit tests require version 1.7.3 or later of CocoaPods: \033[4;34mhttps://guides.cocoapods.org/using/getting-started.html#updating-cocoapods"
108
- fi
109
- else
110
- checkresult -1 " CocoaPods is not installed and is required for running unit tests: \033[4;34mhttps://guides.cocoapods.org/using/getting-started.html#installation"
111
- fi
112
-
113
- # ## Xcodeproj
114
-
115
- # This should be installed with CocoaPods, but we'll include it just in case CocoaPods removes it from its dependencies in a future release
116
82
if which xcodeproj > /dev/null; then
117
83
echo " Xcodeproj: $( xcodeproj --version) "
118
84
else
@@ -126,150 +92,22 @@ printstep "Configuring Test Project..."
126
92
echo -e " class SomeClass {\n var value: Int = 3\n}" > " $OUTPUT_DIR /Sources/$PROJECT_NAME /SomeClass.swift"
127
93
echo -e " @testable import $PROJECT_NAME \nimport XCTest\n\nclass SomeClassTests: XCTestCase {\n\n func testSomeClass() {\n let object = SomeClass()\n XCTAssertEqual(object.value, 3)\n }\n}" > " $OUTPUT_DIR /Tests/${PROJECT_NAME} Tests/SomeClassTests.swift"
128
94
129
- echo " SCRIPTS_DIR: $SCRIPTS_DIR "
130
95
ruby " $SCRIPTS_DIR /unittests.rb" " $OUTPUT_DIR /$PROJECT_NAME .xcodeproj" " $OUTPUT_DIR /Sources/$PROJECT_NAME /SomeClass.swift" " $OUTPUT_DIR /Tests/${PROJECT_NAME} Tests/SomeClassTests.swift"
131
96
checkresult $? " Unable to configure the test project for testing"
132
97
133
- # Run Unit Tests
134
-
135
- printstep " Running Unit Tests..."
136
-
137
- # ## Carthage Workflow
138
-
139
- printstep " Testing 'carthage.yml' Workflow..."
140
-
141
- git add .
142
- git commit -m " Commit" --no-gpg-sign
143
- git tag | xargs git tag -d
144
- git tag --no-sign 1.0
145
- checkresult $? " Unable to tag local git repo for running Carthage unit tests"
146
-
147
- echo " git \" file://$OUTPUT_DIR \" " > ./Cartfile
148
-
149
- ./scripts/carthage.sh update
150
- checkresult $? " Carthage Unit Test failed: \" Build\" "
151
-
152
- printstep " 'carthage.yml' Workflow Tests Passed\n"
153
-
154
- # ## CocoaPods Workflow
155
-
156
- printstep " Testing 'cocoapods.yml' Workflow..."
157
-
158
- pod lib lint
159
- checkresult $? " CocoaPods Unit Test failed: \" Lint (Dynamic Library)\" "
160
-
161
- pod lib lint --use-libraries
162
- checkresult $? " CocoaPods Unit Test failed: \" Lint (Static Library)\" "
98
+ # Run Tests
163
99
164
- printstep " 'cocoapods.yml' Workflow Tests Passed\n "
100
+ ARGS=(--is-running-in-temp-env --project-name " $PROJECT_NAME " )
165
101
166
- # ## Swift Package Workflow
167
-
168
- printstep " Testing 'swift-package.yml' Workflow..."
169
-
170
- swift build -v
171
- checkresult $? " Swift Package Unit Test failed: \" Build\" "
172
-
173
- swift test -v
174
- checkresult $? " Swift Package Unit Test failed: \" Test\" "
175
-
176
- printstep " 'swift-package.yml' Workflow Tests Passed\n"
177
-
178
- # ## XCFramework Workflow
179
-
180
- printstep " Testing 'xcframework.yml' Workflow..."
181
-
182
- ./scripts/xcframework.sh -output " ./$PROJECT_NAME .xcframework"
183
- checkresult $? " XCFramework Unit Test failed: \" Build\" "
184
-
185
- printstep " 'xcframework.yml' Workflow Tests Passed\n"
186
-
187
- # ## Upload Assets Workflow
188
-
189
- printstep " Testing 'upload-assets.yml' Workflow..."
190
-
191
- zip -rX " $PROJECT_NAME .xcframework.zip" " $PROJECT_NAME .xcframework"
192
- checkresult $? " XCFramework Unit Test failed: \" Zip\" "
193
-
194
- tar -zcvf " $PROJECT_NAME .xcframework.tar.gz" " $PROJECT_NAME .xcframework"
195
- checkresult $? " XCFramework Unit Test failed: \" Tar\" "
196
-
197
- printstep " 'upload-assets.yml' Workflow Tests Passed\n"
198
-
199
- # ## Xcodebuild Workflow
200
-
201
- printstep " Testing 'xcodebuild.yml' Workflow..."
202
-
203
- xcodebuild -project " $PROJECT_NAME .xcodeproj" -scheme " $PROJECT_NAME " -destination " generic/platform=iOS" -configuration Debug
204
- checkresult $? " Xcodebuild Unit Test failed: \" Build iOS\" "
205
-
206
- xcodebuild -project " $PROJECT_NAME .xcodeproj" -scheme " $PROJECT_NAME " -destination " generic/platform=iOS Simulator" -configuration Debug
207
- checkresult $? " Xcodebuild Unit Test failed: \" Build iOS Simulator\" "
208
-
209
- IOS_SIM=" $( xcrun simctl list devices available | grep " iPhone [0-9]" | sort -rV | head -n 1 | sed -E ' s/(.+)[ ]*\([^)]*\)[ ]*\([^)]*\)/\1/' | awk ' {$1=$1};1' ) "
210
-
211
- xcodebuild -project " $PROJECT_NAME .xcodeproj" -scheme " $PROJECT_NAME " -testPlan " ${PROJECT_NAME} Tests" -destination " platform=iOS Simulator,name=$IOS_SIM " -configuration Debug ONLY_ACTIVE_ARCH=YES test
212
- checkresult $? " Xcodebuild Unit Test failed: \" Test iOS\" "
213
-
214
- xcodebuild -project " $PROJECT_NAME .xcodeproj" -scheme " ${PROJECT_NAME} Tests" -testPlan " ${PROJECT_NAME} Tests" -destination " platform=iOS Simulator,name=$IOS_SIM " -configuration Debug ONLY_ACTIVE_ARCH=YES test
215
- checkresult $? " Xcodebuild Unit Test failed: \" Test iOS\" "
216
-
217
- # ##
218
-
219
- xcodebuild -project " $PROJECT_NAME .xcodeproj" -scheme " $PROJECT_NAME " -destination " generic/platform=macOS,variant=Mac Catalyst" -configuration Debug
220
- checkresult $? " Xcodebuild Unit Test failed: \" Build MacCatalyst\" "
221
-
222
- xcodebuild -project " $PROJECT_NAME .xcodeproj" -scheme " $PROJECT_NAME " -testPlan " ${PROJECT_NAME} Tests" -destination " platform=macOS,variant=Mac Catalyst" -configuration Debug ONLY_ACTIVE_ARCH=YES test
223
- checkresult $? " Xcodebuild Unit Test failed: \" Test MacCatalyst\" "
224
-
225
- xcodebuild -project " $PROJECT_NAME .xcodeproj" -scheme " ${PROJECT_NAME} Tests" -testPlan " ${PROJECT_NAME} Tests" -destination " platform=macOS,variant=Mac Catalyst" -configuration Debug ONLY_ACTIVE_ARCH=YES test
226
- checkresult $? " Xcodebuild Unit Test failed: \" Test MacCatalyst\" "
227
-
228
- # ##
229
-
230
- xcodebuild -project " $PROJECT_NAME .xcodeproj" -scheme " $PROJECT_NAME macOS" -destination " generic/platform=macOS" -configuration Debug
231
- checkresult $? " Xcodebuild Unit Test failed: \" Build macOS\" "
232
-
233
- xcodebuild -project " $PROJECT_NAME .xcodeproj" -scheme " $PROJECT_NAME macOS" -testPlan " $PROJECT_NAME macOS Tests" -configuration Debug ONLY_ACTIVE_ARCH=YES test
234
- checkresult $? " Xcodebuild Unit Test failed: \" Test macOS\" "
235
-
236
- xcodebuild -project " $PROJECT_NAME .xcodeproj" -scheme " $PROJECT_NAME macOS Tests" -testPlan " $PROJECT_NAME macOS Tests" -configuration Debug ONLY_ACTIVE_ARCH=YES test
237
- checkresult $? " Xcodebuild Unit Test failed: \" Test macOS\" "
238
-
239
- # ##
240
-
241
- xcodebuild -project " $PROJECT_NAME .xcodeproj" -scheme " $PROJECT_NAME tvOS" -destination " generic/platform=tvOS" -configuration Debug
242
- checkresult $? " Xcodebuild Unit Test failed: \" Build tvOS\" "
243
-
244
- xcodebuild -project " $PROJECT_NAME .xcodeproj" -scheme " $PROJECT_NAME tvOS" -destination " generic/platform=tvOS Simulator" -configuration Debug
245
- checkresult $? " Xcodebuild Unit Test failed: \" Build tvOS Simulator\" "
246
-
247
- TVOS_SIM=" $( xcrun simctl list devices available | grep " Apple TV" | sort -V | head -n 1 | sed -E ' s/(.+)[ ]*\([^)]*\)[ ]*\([^)]*\)/\1/' | awk ' {$1=$1};1' ) "
248
-
249
- xcodebuild -project " $PROJECT_NAME .xcodeproj" -scheme " $PROJECT_NAME tvOS" -testPlan " $PROJECT_NAME tvOS Tests" -destination " platform=tvOS Simulator,name=$TVOS_SIM " -configuration Debug ONLY_ACTIVE_ARCH=YES test
250
- checkresult $? " Xcodebuild Unit Test failed: \" Test tvOS\" "
251
-
252
- xcodebuild -project " $PROJECT_NAME .xcodeproj" -scheme " $PROJECT_NAME tvOS Tests" -testPlan " $PROJECT_NAME tvOS Tests" -destination " platform=tvOS Simulator,name=$TVOS_SIM " -configuration Debug ONLY_ACTIVE_ARCH=YES test
253
- checkresult $? " Xcodebuild Unit Test failed: \" Test tvOS\" "
254
-
255
- # ##
256
-
257
- xcodebuild -project " $PROJECT_NAME .xcodeproj" -scheme " $PROJECT_NAME watchOS" -destination " generic/platform=watchOS" -configuration Debug
258
- checkresult $? " Xcodebuild Unit Test failed: \" Build watchOS\" "
259
-
260
- xcodebuild -project " $PROJECT_NAME .xcodeproj" -scheme " $PROJECT_NAME watchOS" -destination " generic/platform=watchOS Simulator" -configuration Debug
261
- checkresult $? " Xcodebuild Unit Test failed: \" Build watchOS Simulator\" "
262
-
263
- WATCHOS_SIM=" $( xcrun simctl list devices available | grep " Apple Watch" | sort -rV | head -n 1 | sed -E ' s/(.+)[ ]*\([^)]*\)[ ]*\([^)]*\)/\1/' | awk ' {$1=$1};1' ) "
264
-
265
- xcodebuild -project " $PROJECT_NAME .xcodeproj" -scheme " $PROJECT_NAME watchOS" -testPlan " $PROJECT_NAME watchOS Tests" -destination " platform=watchOS Simulator,name=$WATCHOS_SIM " -configuration Debug ONLY_ACTIVE_ARCH=YES test
266
- checkresult $? " Xcodebuild Unit Test failed: \" Test watchOS\" "
267
-
268
- xcodebuild -project " $PROJECT_NAME .xcodeproj" -scheme " $PROJECT_NAME watchOS Tests" -testPlan " $PROJECT_NAME watchOS Tests" -destination " platform=watchOS Simulator,name=$WATCHOS_SIM " -configuration Debug ONLY_ACTIVE_ARCH=YES test
269
- checkresult $? " Xcodebuild Unit Test failed: \" Test watchOS\" "
102
+ if [ " $NO_CLEAN " == " 1" ]; then
103
+ ARGS+=(--no-clean)
104
+ elif [ " $NO_CLEAN_ON_FAIL " == " 1" ]; then
105
+ ARGS+=(--no-clean-on-fail)
106
+ fi
270
107
271
- printstep " 'xcodebuild.yml' Workflow Tests Passed\n"
108
+ " $OUTPUT_DIR /$( basename " $SCRIPTS_DIR " ) /workflowtests.sh" " ${ARGS[@]} "
109
+ checkresult $? " Unit tests failed"
272
110
273
- # ## Success
111
+ # Success
274
112
275
113
cleanup
0 commit comments