ghunit 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f8638f90dde2cbeef02f93482452a754bd4c5680
4
- data.tar.gz: d966cbfe9b37f23cf3c3e7cddba49cd64036bc06
3
+ metadata.gz: 4453afa37f08a27da84256ee07d229280a94d87e
4
+ data.tar.gz: a4ae97eed3cc96fa45cf2abbd696e41f119d04f4
5
5
  SHA512:
6
- metadata.gz: af75e8afaa8d464e198d47badf975fa7ceb52b09cbe0343fb5bc7fee87ba6dedfe6d7aaba0280e5bef392be89b2b01d3a1ea3ad222faf19d11b33814acff65aa
7
- data.tar.gz: bcc2c167b724589f66a7a88e862ecab7f58df9d383a07c43fdd3e83a1bc2129bf652255067ae9317e1963dfb6104d5c3ce35e9ec6a85ec9cb41ee6b608069aff
6
+ metadata.gz: aed987506648cabd4a3926f40d826667eca793d1b2d2b643e3247279bbf2363d43c0fc4390f10e110747e8c228bce8a71f69d68933f5227dc39d90709a16e729
7
+ data.tar.gz: c8dc8180e9919d98a3a25d0acb294a983f937c617bc0408dfbc21b36500c96a2e9ed9e7947765f612190623deee8b6fe12d3b75dfae19bc99e9c3cdc40c185ba
data/bin/ghunit CHANGED
@@ -2,35 +2,72 @@
2
2
 
3
3
  require 'slop'
4
4
  require 'ghunit'
5
- require 'colorize'
6
5
 
7
- opts = Slop.parse do
8
- banner "Usage: ghunit [options]"
6
+ def require_opt(opts, key, desc)
7
+ if !opts[key]
8
+ puts " "
9
+ puts desc.red
10
+ puts " "
11
+ puts opts.to_s
12
+ puts " "
13
+ exit 1
14
+ end
15
+ end
9
16
 
10
- on :n, :name=, "Project name"
11
- on :p, :path=, "Project path, defaults to <Project Name>.xcodeproj", argument: :optional
12
- on :t, :test_target=, "Test target name, defaults to Tests", argument: :optional
17
+ Slop.parse do
18
+ banner "Usage: ghunit install|run|add [options]"
13
19
 
14
- end
20
+ command "install" do
21
+ on :n, :name=, "Project name"
22
+ on :p, :path=, "Project path, defaults to <Project Name>.xcodeproj", argument: :optional
23
+ on :t, :test_target=, "Test target name, defaults to Tests", argument: :optional
15
24
 
16
- if !opts[:name]
17
- puts " "
18
- puts "Need to specify project name.".red
19
- puts " "
20
- puts opts.to_s
21
- puts " "
22
- exit 1
23
- end
25
+ run do |opts, args|
26
+ project = GHUnit::Project.open_from_opts(opts)
27
+ project.create_test_target
28
+ end
29
+ end
24
30
 
25
- target_name = opts[:name]
26
- project_path = opts[:path] || "#{target_name}.xcodeproj"
27
- test_target_name = opts[:test_target] || "Tests"
31
+ command "add" do
32
+ on :n, :name=, "Project name"
33
+ on :f, :file=, "Test name, MyTest"
34
+ #on :r, :framework=, "Test case framework (kiwi,ghunit), defaults to ghunit"
35
+ on :p, :path=, "Project path, defaults to <Project Name>.xcodeproj", argument: :optional
36
+ on :t, :test_target=, "Test target name, defaults to Tests", argument: :optional
28
37
 
29
- project = GHUnit::Project.open(project_path, target_name, test_target_name)
30
- if project
31
- puts " "
32
- puts " "
33
- project.create_test_target
34
- end
38
+ run do |opts, args|
39
+ require_opt(opts, :file, "Need to specify a test name with -f.")
40
+ project = GHUnit::Project.open_from_opts(opts)
41
+ project.create_test(opts[:file]) #, opts[:framework])
42
+ project.save
43
+ end
44
+ end
45
+
46
+ command "run" do
47
+ on :n, :name=, "Project name"
48
+ on :t, :test_target=, "Test target name, defaults to Tests", argument: :optional
49
+ on :p, :path=, "Workspace path, defaults to <Project Name>.xcworkspace", argument: :optional
35
50
 
51
+ run do |opts, args|
52
+ require_opt(opts, :name, "Need to specify a project name with -n.")
36
53
 
54
+ name = opts[:name]
55
+ # Scheme name should match the test target name
56
+ test_target = opts[:test_target] || "Tests"
57
+ path = opts[:path] || "#{name}.xcworkspace"
58
+ system("GHUNIT_CLI=1 xcodebuild -workspace #{path} -scheme #{test_target} -configuration Debug -sdk iphonesimulator build")
59
+ end
60
+ end
61
+
62
+ command "install_cli" do
63
+ on :n, :name=, "Project name"
64
+ on :t, :test_target=, "Test target name, defaults to Tests", argument: :optional
65
+ on :p, :path=, "Workspace path, defaults to <Project Name>.xcworkspace", argument: :optional
66
+
67
+ run do |opts, args|
68
+ project = GHUnit::Project.open_from_opts(opts)
69
+ project.install_run_tests_script
70
+ end
71
+ end
72
+
73
+ end
@@ -3,6 +3,14 @@ require 'xcodeproj/ext'
3
3
  require 'fileutils'
4
4
  require 'logger'
5
5
  require 'colorize'
6
+ require 'erb'
7
+ require 'ostruct'
8
+
9
+ class ErbalT < OpenStruct
10
+ def render(template)
11
+ ERB.new(template).result(binding)
12
+ end
13
+ end
6
14
 
7
15
  class GHUnit::Project
8
16
 
@@ -49,6 +57,9 @@ class GHUnit::Project
49
57
  end
50
58
 
51
59
  class << self
60
+
61
+ # Initialize and open a project.
62
+ #
52
63
  def open(project_path, target_name, test_target_name, logger=nil)
53
64
  project = GHUnit::Project.new(project_path, target_name, test_target_name, logger)
54
65
  if project.open
@@ -57,6 +68,27 @@ class GHUnit::Project
57
68
  nil
58
69
  end
59
70
  end
71
+
72
+ # Open from slop options.
73
+ # This is only meant to be called from the ghunit bin executable,
74
+ # it outputs to STDOUT and calls exit.
75
+ #
76
+ def open_from_opts(opts)
77
+ target_name = opts[:name]
78
+ project_path = opts[:path] || "#{target_name}.xcodeproj"
79
+ test_target_name = opts[:test_target] || "Tests"
80
+
81
+ if !target_name
82
+ puts " "
83
+ puts "Need to specify a project name.".red
84
+ puts " "
85
+ puts opts.to_s
86
+ puts " "
87
+ exit 1
88
+ end
89
+
90
+ self.open(project_path, target_name, test_target_name)
91
+ end
60
92
  end
61
93
 
62
94
  def find_test_target
@@ -101,16 +133,16 @@ class GHUnit::Project
101
133
  create_test_file("main.m", template("main.m"), true)
102
134
  create_test("MyTest")
103
135
 
136
+ # Use same resources build phase as main target
137
+ # Have to compare with class name because of funky const loading in xcodeproj gem
138
+ resources_build_phase = main_target.build_phases.select { |p|
139
+ p.class == Xcodeproj::Project::Object::PBXResourcesBuildPhase }.first
140
+ test_target.build_phases << resources_build_phase if resources_build_phase
141
+
104
142
  else
105
143
  logger.debug "Test target already exists, skipping..."
106
144
  end
107
145
 
108
- # Use same resources build phase as main target
109
- # Have to compare with class name because of funky const loading in xcodeproj gem
110
- resources_build_phase = main_target.build_phases.select { |p|
111
- p.class.to_s == "Xcodeproj::Project::Object::PBXResourcesBuildPhase" }.first
112
- test_target.build_phases << resources_build_phase if resources_build_phase
113
-
114
146
  # Get main target prefix header
115
147
  prefix_header = main_target.build_settings("Debug")["GCC_PREFIX_HEADER"]
116
148
 
@@ -121,6 +153,9 @@ class GHUnit::Project
121
153
  c.build_settings["GCC_PREFIX_HEADER"] = prefix_header if prefix_header
122
154
  end
123
155
 
156
+ # Write any test support files
157
+ write_test_file("RunTests.sh", template("RunTests.sh"), true)
158
+
124
159
  # Create test scheme if it doesn't exist
125
160
  logger.debug "Checking for Test scheme..."
126
161
  schemes = Xcodeproj::Project.schemes(project_path)
@@ -140,15 +175,19 @@ class GHUnit::Project
140
175
  check_pod
141
176
  end
142
177
 
143
- def template(name)
178
+ def template(name, template_vars=nil)
144
179
  template_path = File.join(File.dirname(__FILE__), "templates", name)
145
- File.read(template_path)
180
+ content = File.read(template_path)
181
+
182
+ if template_path.end_with?(".erb")
183
+ et = ErbalT.new(template_vars)
184
+ content = et.render(content)
185
+ end
186
+ content
146
187
  end
147
188
 
148
- # Create a file with content and add to the test target
149
- #
150
- def create_test_file(file_name, content, force=false)
151
- # Create main.m for test target
189
+ def write_test_file(file_name, content, force=false)
190
+ # Create file in tests dir
152
191
  path = File.join(test_target_name, file_name)
153
192
 
154
193
  if !force && File.exists?(path)
@@ -157,7 +196,13 @@ class GHUnit::Project
157
196
 
158
197
  logger.debug "Creating: #{path}"
159
198
  File.open(path, "w") { |f| f.write(content) }
199
+ path
200
+ end
160
201
 
202
+ # Create a file with content and add to the test target
203
+ #
204
+ def create_test_file(file_name, content, force=false)
205
+ path = write_test_file(file_name, content, force)
161
206
  add_test_file(path)
162
207
  path
163
208
  end
@@ -182,10 +227,31 @@ class GHUnit::Project
182
227
  true
183
228
  end
184
229
 
185
- def create_test(name)
186
- name = "#{name}.m" unless name.end_with?(".m")
187
- path = create_test_file(name, template("Test.m"))
188
- logger.debug "Created test: #{path}"
230
+ # Create test file and add it to the test target.
231
+ #
232
+ # @param name Name of test class and file name
233
+ # @param template_type nil or "kiwi"
234
+ #
235
+ def create_test(name, template_type=nil)
236
+ template_type ||= :ghunit
237
+
238
+ template_name = case template_type.to_sym
239
+ when :kiwi
240
+ "TestKiwi.m.erb"
241
+ when :ghunit
242
+ "Test.m.erb"
243
+ end
244
+
245
+ if name.end_with?(".m")
246
+ name = name[0...-2]
247
+ end
248
+ template_vars = {test_class_name: name}
249
+ path = create_test_file("#{name}.m", template(template_name, template_vars))
250
+ path
251
+ end
252
+
253
+ def save
254
+ @project.save
189
255
  end
190
256
 
191
257
  # Check the Podfile or just display some Podfile help
@@ -201,4 +267,21 @@ Make sure to open the .xcworkspace.
201
267
 
202
268
  EOS
203
269
  end
270
+
271
+ def install_run_tests_script
272
+ run_script_name = "Run Tests (CLI)"
273
+ # Find run script build phase and install RunTests.sh
274
+ test_target = find_test_target
275
+ run_script_phase = test_target.build_phases.select { |p|
276
+ p.class == Xcodeproj::Project::Object::PBXShellScriptBuildPhase &&
277
+ p.name == run_script_name }.first
278
+ if !run_script_phase
279
+ logger.debug "Installing run tests script..."
280
+ run_script_phase = project.new(Xcodeproj::Project::Object::PBXShellScriptBuildPhase)
281
+ run_script_phase.name = run_script_name
282
+ run_script_phase.shell_script = "sh Tests/RunTests.sh"
283
+ test_target.build_phases << run_script_phase
284
+ end
285
+ project.save
286
+ end
204
287
  end
@@ -0,0 +1,38 @@
1
+ #!/bin/sh
2
+
3
+ # If we aren't running from the command line, then exit
4
+ if [ "$GHUNIT_CLI" = "" ] && [ "$GHUNIT_AUTORUN" = "" ]; then
5
+ exit 0
6
+ fi
7
+
8
+ TEST_TARGET_EXECUTABLE_PATH="$TARGET_BUILD_DIR/$EXECUTABLE_FOLDER_PATH"
9
+
10
+ if [ ! -e "$TEST_TARGET_EXECUTABLE_PATH" ]; then
11
+ echo ""
12
+ echo " ------------------------------------------------------------------------"
13
+ echo " Missing executable path: "
14
+ echo " $TEST_TARGET_EXECUTABLE_PATH."
15
+ echo " The product may have failed to build."
16
+ echo " ------------------------------------------------------------------------"
17
+ echo ""
18
+ exit 1
19
+ fi
20
+
21
+ RUN_CMD="ios-sim launch \"$TEST_TARGET_EXECUTABLE_PATH\""
22
+
23
+ echo "Running: $RUN_CMD"
24
+ set +o errexit # Disable exiting on error so script continues if tests fail
25
+ eval $RUN_CMD
26
+ RETVAL=$?
27
+ set -o errexit
28
+
29
+ if [ -n "$WRITE_JUNIT_XML" ]; then
30
+ MY_TMPDIR=`/usr/bin/getconf DARWIN_USER_TEMP_DIR`
31
+ RESULTS_DIR="${MY_TMPDIR}test-results"
32
+
33
+ if [ -d "$RESULTS_DIR" ]; then
34
+ `$CP -r "$RESULTS_DIR" "$BUILD_DIR" && rm -r "$RESULTS_DIR"`
35
+ fi
36
+ fi
37
+
38
+ exit $RETVAL
@@ -0,0 +1,15 @@
1
+ //
2
+ // <%= test_class_name %>.m
3
+ //
4
+ #import <GHUnit/GHUnit.h>
5
+
6
+ @interface <%= test_class_name %> : GHTestCase
7
+ @end
8
+
9
+ @implementation <%= test_class_name %>
10
+
11
+ - (void)test {
12
+
13
+ }
14
+
15
+ @end
@@ -0,0 +1,16 @@
1
+ //
2
+ // <%= test_class_name %>.m
3
+ //
4
+ #import "Kiwi.h"
5
+
6
+ SPEC_BEGIN(<%= test_class_name %>)
7
+
8
+ describe(@"Math", ^{
9
+ it(@"is pretty cool", ^{
10
+ NSUInteger a = 16;
11
+ NSUInteger b = 26;
12
+ [[theValue(a + b) should] equal:theValue(43)];
13
+ });
14
+ });
15
+
16
+ SPEC_END
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ghunit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Handford
@@ -59,11 +59,13 @@ executables:
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - lib/ghunit.rb
63
62
  - lib/ghunit/project.rb
64
- - lib/ghunit/templates/Podfile
65
- - lib/ghunit/templates/Test.m
66
63
  - lib/ghunit/templates/main.m
64
+ - lib/ghunit/templates/Podfile
65
+ - lib/ghunit/templates/RunTests.sh
66
+ - lib/ghunit/templates/Test.m.erb
67
+ - lib/ghunit/templates/TestKiwi.m.erb
68
+ - lib/ghunit.rb
67
69
  - bin/ghunit
68
70
  homepage: https://github.com/gh-unit/gh-unit
69
71
  licenses:
@@ -1,12 +0,0 @@
1
- #import <GHUnit/GHUnit.h>
2
-
3
- @interface MyTest : GHTestCase
4
- @end
5
-
6
- @implementation MyTest
7
-
8
- - (void)test {
9
-
10
- }
11
-
12
- @end