sayso-js-test-driver-rails 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,232 @@
1
+ module JsTestDriver
2
+ # The Runner class is used
3
+ class Runner
4
+
5
+ def initialize(attributes = {})
6
+ self.attributes = attributes
7
+ end
8
+
9
+ # configuration, by default it's parsed from config_path
10
+ attr_writer :config
11
+
12
+ def config
13
+ @config ||= parse_config
14
+ end
15
+
16
+ attr_writer :generated_files_dir
17
+
18
+ # this is where the generated files will be saved, by default this is `pwd`/.js_test_driver
19
+ #
20
+ # the generated files are the config yml file and the fixture files
21
+ def generated_files_dir
22
+ @generated_files_dir || default_generated_files_dir
23
+ end
24
+
25
+ # this is the path of the config file, by default its `pwd`/config/js_test_driver.rb
26
+ attr_writer :config_path
27
+
28
+ def config_path
29
+ @config_path ||= default_config_path
30
+ end
31
+
32
+ # this is the path to the js test driver jar file, by default it's stored relatively to this file
33
+ attr_writer :jar_path
34
+
35
+ def jar_path
36
+ @jar_path ||= default_jar_path
37
+ end
38
+
39
+ # this is where the config yml file will be saved, by default its saved in the generated files
40
+ # directory under the name jsTestDriver.conf
41
+ attr_writer :tmp_path
42
+
43
+ def config_yml_path
44
+ @tmp_path ||= default_config_yml_path
45
+ end
46
+
47
+ # starts the server on the default port specified in the config file
48
+ def start_server
49
+ command = execute_jar
50
+
51
+ add_start_server(command)
52
+
53
+ command.run
54
+ end
55
+
56
+ # captures the browsers
57
+ #
58
+ # by default it will capture the browsers specified in the config,
59
+ # but you can pass an argument like 'opera,chrome,firefox' to capture opera, chrome and firefox
60
+ def capture_browsers(browsers = nil)
61
+ browsers ||= ''
62
+ browsers = browsers.split(',')
63
+ browsers = config.browsers if browsers.empty?
64
+
65
+ url = config.server + "/capture"
66
+
67
+ browsers.each do |browser|
68
+ spawn("#{browser} \'#{url}\'")
69
+ end
70
+ end
71
+
72
+ # runs the tests specified by the argument
73
+ #
74
+ # by default it will run all the tests, but you can pass a string like:
75
+ # 'TestCase' or 'TestCase.test'
76
+ # to run either a single test case or a single test
77
+ def run_tests(tests = nil)
78
+ command = execute_jar
79
+
80
+ add_with_config(command)
81
+ add_run_tests(command, tests)
82
+
83
+ command.run
84
+ end
85
+
86
+ def start_server_capture_and_run(tests = nil, browsers = nil, output_xml_path = nil, console = nil)
87
+ command = execute_jar
88
+
89
+ add_start_server(command)
90
+ add_runner_mode(command)
91
+ add_with_config(command)
92
+ add_capture_browsers(command, browsers)
93
+ add_run_tests(command, tests)
94
+ add_output_directory(command, output_xml_path) if output_xml_path
95
+ add_capture_console(command) if console
96
+
97
+ result = command.run
98
+
99
+ if config.measure_coverage? && output_xml_path
100
+ generate_html_coverage_report(output_xml_path)
101
+ end
102
+
103
+ return result
104
+ end
105
+
106
+ def add_start_server(command)
107
+ command.option('--port', config.port)
108
+ end
109
+
110
+ def add_runner_mode(command)
111
+ command.option('--runnerMode', ENV['RUNNER_MODE']) unless ENV['RUNNER_MODE'].nil? || ENV['RUNNER_MODE'].empty?
112
+ end
113
+
114
+ def add_run_tests(command, tests)
115
+ command.option('--tests', tests || "all")
116
+ end
117
+
118
+ def add_capture_browsers(command, browsers)
119
+ browsers ||= config.browsers.join(',')
120
+ raise ArgumentError.new("No browsers defined!") if browsers == ""
121
+ command.option('--browser', browsers)
122
+ end
123
+
124
+ def add_output_directory(command, path)
125
+ path = File.expand_path(path)
126
+ FileUtils.mkdir_p(path) unless File.exists?(path)
127
+ command.option('--testOutput', path)
128
+ end
129
+
130
+ def add_capture_console(command)
131
+ command.option('--captureConsole')
132
+ end
133
+
134
+ def add_with_config(command)
135
+ save_config_file(config_yml_path)
136
+ command.option('--config', config_yml_path)
137
+ end
138
+
139
+ def execute_jar
140
+ Command.new('java').option('-jar', jar_path)
141
+ end
142
+
143
+ def generate_html_coverage_report(output_path)
144
+ unless genhtml_installed?
145
+ puts "Could not find genhtml. You must install lcov (sudo apt-get install lcov)"
146
+ return
147
+ end
148
+
149
+ output_path = File.expand_path(output_path)
150
+
151
+ config_file_name = File.basename(config_yml_path)
152
+ coverage_file_name = config_file_name + '-coverage.dat'
153
+ coverage_file_path = File.join(output_path, coverage_file_name)
154
+ coverage_out_dir = File.join(output_path, 'coverage')
155
+
156
+ system("genhtml -o #{coverage_out_dir} #{coverage_file_path}")
157
+ end
158
+
159
+ protected
160
+
161
+ def genhtml_installed?
162
+ !%x[which genhtml].strip.empty?
163
+ end
164
+
165
+ def parse_config
166
+ source = ""
167
+ if File.exist?(config_path)
168
+ source = File.read(config_path)
169
+ else
170
+ warn("Could not find JS Test Driver config: '#{config_path}', assuming empty config file!")
171
+ end
172
+ config = JsTestDriver::Config.parse(source)
173
+ config.config_dir = generated_files_dir
174
+ return config
175
+ end
176
+
177
+ def default_config_path
178
+ root = defined?(::Rails) ? ::Rails.root.to_s : '.'
179
+ return File.expand_path(File.join(root, 'config', 'js_test_driver.rb'))
180
+ end
181
+
182
+ def default_jar_path
183
+ current_dir = File.dirname(__FILE__)
184
+ path = File.join(current_dir, '..', '..', 'vendor', 'js_test_driver.jar')
185
+ return File.expand_path(path)
186
+ end
187
+
188
+ def default_config_yml_path
189
+ return File.expand_path("jsTestDriver.conf", generated_files_dir)
190
+ end
191
+
192
+ def default_generated_files_dir
193
+ return File.expand_path(".js_test_driver")
194
+ end
195
+
196
+ private
197
+
198
+ def save_config_file(path)
199
+ Dir.mkdir(generated_files_dir) unless File.exists?(generated_files_dir)
200
+ File.open(path, "w+") { |f| f.puts config.to_s }
201
+ config.save_fixtures
202
+ end
203
+
204
+ def attributes=(values)
205
+ values.each do |attr, value|
206
+ self.send("#{attr}=", value)
207
+ end
208
+ end
209
+
210
+ class Command
211
+ def initialize(executable)
212
+ @command = "#{executable}"
213
+ end
214
+
215
+ def option(name, value = nil)
216
+ value = "'#{value}'" if value && value =~ /\s/
217
+ @command = [@command, name, value].compact.join(' ')
218
+ self
219
+ end
220
+
221
+ def run
222
+ system(self.to_s)
223
+ end
224
+
225
+ def to_s
226
+ return @command
227
+ end
228
+ end
229
+
230
+ end
231
+
232
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'js_test_driver'))
2
+
3
+ namespace :js_test_driver do
4
+
5
+ desc "Starts the server using the provided configuration variables"
6
+ task :start_server do
7
+ JsTestDriver::Runner.new.start_server
8
+ end
9
+
10
+ desc "Runs the javascript tests"
11
+ task :run_tests do
12
+ exit(1) unless JsTestDriver::Runner.new.run_tests(ENV['TESTS'])
13
+ end
14
+
15
+ desc "Capture the browsers defined in config"
16
+ task :capture_browsers do
17
+ JsTestDriver::Runner.new.capture_browsers(ENV['BROWSERS'])
18
+ end
19
+
20
+ desc "Starts the server, captures the browsers, runs the tests - all at the same time"
21
+ task :run do
22
+ config = JsTestDriver::Runner.new
23
+ output_path = ENV['OUTPUT_PATH']
24
+ output_path = File.join(config.generated_files_dir, 'tests') if ENV['OUTPUT_XML']
25
+ config.start_server_capture_and_run(ENV['TESTS'], ENV['BROWSERS'], output_path, ENV['CAPTURE_CONSOLE'])
26
+ end
27
+
28
+ end
@@ -0,0 +1,3 @@
1
+ module JsTestDriver
2
+ VERSION = "0.4.3"
3
+ end
@@ -0,0 +1 @@
1
+ <p>This is the file <b>a.html</b></p>
@@ -0,0 +1 @@
1
+ <p>This is the file <b>b.html</b></p>
@@ -0,0 +1 @@
1
+ <p>This is the file <b>baz/a.html</b></p>
@@ -0,0 +1 @@
1
+ <p>This is the file <b>c.html</b></p>
@@ -0,0 +1 @@
1
+ <p>This is the file <b>foo/a.html</b></p>
@@ -0,0 +1 @@
1
+ <p>This is the file <b>foo/bar/a.html</b></p>
@@ -0,0 +1 @@
1
+ This file is not a HTML file, so it will not be used as a fixture.
@@ -0,0 +1,15 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
2
+
3
+ module JsTestDriver
4
+ class SampleTest < Test::Unit::TestCase
5
+
6
+ def test_running_sample_specs
7
+ config = JsTestDriver::Runner.new(:config_path => File.expand_path('../../sample/config/js_test_driver.rb', __FILE__))
8
+
9
+ output_path = File.expand_path('../../../.js_test_driver/', __FILE__)
10
+
11
+ assert config.start_server_capture_and_run(nil, nil, output_path), 'running tests should return a success'
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ includes 'test/sample/lib/*.js'
2
+
3
+ ['google-chrome', 'firefox', 'chromium-browser'].select{|b| !%x[which #{b}].strip.empty? }.each do |b|
4
+ browser b
5
+ end
6
+
7
+ enable_jasmine
8
+ measure_coverage
9
+
10
+ includes 'test/sample/specs/spec_helper.js'
11
+ includes 'test/sample/specs/**/*_spec.js'
@@ -0,0 +1,15 @@
1
+ function Calculator(initialValue) {
2
+ this.value = initialValue;
3
+ }
4
+
5
+ Calculator.prototype = {
6
+ add: function(val) {
7
+ this.value += val;
8
+ },
9
+ multiply: function(val) {
10
+ this.value *= val;
11
+ },
12
+ sub: function(val) {
13
+ this.value -= val;
14
+ }
15
+ };
@@ -0,0 +1,19 @@
1
+ describe('Calculator', function () {
2
+ var calc;
3
+
4
+ beforeEach(function () {
5
+ calc = new Calculator(2);
6
+ });
7
+
8
+ it('adds values', function() {
9
+ calc.add(2);
10
+
11
+ expect(calc.value).toEqual(4);
12
+ });
13
+
14
+ it('multiplies values', function() {
15
+ calc.multiply(3);
16
+
17
+ expect(calc.value).toEqual(6);
18
+ });
19
+ });
@@ -0,0 +1,4 @@
1
+ // nothing to see here
2
+
3
+ function hello() {
4
+ }
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.setup
5
+
6
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'js_test_driver'))
7
+
8
+ require 'test/unit'
9
+ require 'mocha'
10
+
11
+ class Test::Unit::TestCase
12
+
13
+ def setup
14
+ clean_up_saved_config_files
15
+ end
16
+
17
+ def fixture_dir
18
+ File.expand_path('fixtures', File.dirname(__FILE__))
19
+ end
20
+
21
+ def clean_up_saved_config_files
22
+ root_dir = File.expand_path('..', File.dirname(__FILE__))
23
+ Dir["#{root_dir}/**/.js_test_driver"].each do |file|
24
+ FileUtils.rm_rf(file)
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,270 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
2
+
3
+ module JsTestDriver
4
+ class ConfigTest < Test::Unit::TestCase
5
+
6
+ def default_result
7
+ {'server' => 'http://localhost:4224', 'basepath' => '/'}
8
+ end
9
+
10
+ def path(relative_file_name)
11
+ return File.expand_path(relative_file_name).gsub(/^\//, '')
12
+ end
13
+
14
+ def paths(arr)
15
+ arr.map{|s| path(s)}.sort
16
+ end
17
+
18
+ def assert_config_includes(config, hash)
19
+ expected = default_result.merge(hash)
20
+ assert_equal expected, YAML::load(config.to_s)
21
+ end
22
+
23
+ def given_an_empty_config
24
+ JsTestDriver::Config.new
25
+ end
26
+
27
+ def test_empty_config
28
+ # given
29
+ config = given_an_empty_config
30
+
31
+ # then
32
+ assert_config_includes(config, default_result)
33
+ end
34
+
35
+ def test_custom_port
36
+ # given
37
+ config = given_an_empty_config
38
+
39
+ # when
40
+ config.port 666
41
+
42
+ # then
43
+ assert_config_includes config, 'server' => 'http://localhost:666'
44
+ end
45
+
46
+ def test_custom_host
47
+ # given
48
+ config = given_an_empty_config
49
+
50
+ # when
51
+ config.host 'example.com'
52
+
53
+ # then
54
+ assert_config_includes config, 'server' => 'http://example.com:4224'
55
+ end
56
+
57
+ def test_custom_server
58
+ # given
59
+ config = given_an_empty_config
60
+
61
+ # when
62
+ config.server 'https://example.com:443'
63
+
64
+ # then
65
+ assert_config_includes config, 'server' => 'https://example.com:443'
66
+ end
67
+
68
+ def test_server_should_override_host_and_port
69
+ # given
70
+ config = given_an_empty_config
71
+
72
+ # when
73
+ config.port 666
74
+ config.host 'test.com'
75
+ config.server 'https://example.com:443'
76
+
77
+ # then
78
+ assert_config_includes config, 'server' => 'https://example.com:443'
79
+ end
80
+
81
+ def test_config_with_includes
82
+ # given
83
+ config = given_an_empty_config
84
+
85
+ # when
86
+ config.includes('src/foo.js')
87
+
88
+ # then
89
+ assert_config_includes config, 'load' => paths(['src/foo.js'])
90
+ end
91
+
92
+ def test_config_with_includes_with_globbing
93
+ # given
94
+ config = given_an_empty_config
95
+
96
+ # when
97
+ config.includes('test/fixtures/foo/**/*.html')
98
+
99
+ # then
100
+ assert_config_includes config, 'load' => paths(['test/fixtures/foo/bar/a.html', 'test/fixtures/foo/a.html'])
101
+ end
102
+
103
+ def test_config_with_browsers
104
+ # given
105
+ config = given_an_empty_config
106
+
107
+ # when
108
+ config.browser('firefox')
109
+ config.browser('chrome')
110
+
111
+ # then
112
+ assert_equal ['firefox', 'chrome'], config.browsers
113
+ end
114
+
115
+ def test_config_with_multiple_includes
116
+ # given
117
+ config = given_an_empty_config
118
+
119
+ # when
120
+ ['a', 'b', 'c'].each do |name|
121
+ config.includes(name)
122
+ end
123
+
124
+ # then
125
+ assert_config_includes config, 'load' => paths(['a', 'b', 'c'])
126
+ end
127
+
128
+ def test_config_with_excludes
129
+ # given
130
+ config = given_an_empty_config
131
+
132
+ # when
133
+ config.excludes('test/fixtures/foo/**/*.html')
134
+
135
+ # then
136
+ assert_config_includes config, 'exclude' => paths(['test/fixtures/foo/bar/a.html', 'test/fixtures/foo/a.html'])
137
+ end
138
+
139
+ def test_config_with_excludes_with_globbing
140
+ # given
141
+ config = given_an_empty_config
142
+
143
+ # when
144
+ config.excludes('src/foo.js')
145
+
146
+ # then
147
+ assert_config_includes config, 'exclude' => paths(['src/foo.js'])
148
+ end
149
+
150
+ def test_empty_config_file
151
+ # given
152
+ str = ""
153
+
154
+ # when
155
+ config = JsTestDriver::Config.parse(str)
156
+
157
+ # then
158
+ assert_equal [], config.included_files
159
+ assert_equal [], config.excluded_files
160
+ assert_equal "http://localhost:4224", config.server
161
+ end
162
+
163
+ def test_sample_config_file
164
+ # given
165
+ str = <<-CONFIG
166
+ includes 'a', 'b'
167
+ includes 'c'
168
+
169
+ excludes 'd'
170
+
171
+ server 'http://example.com:666'
172
+ CONFIG
173
+
174
+ # when
175
+ config = JsTestDriver::Config.parse(str)
176
+
177
+ # then
178
+ assert_equal ['a', 'b', 'c'].map{|p| File.expand_path(p)}, config.included_files
179
+ assert_equal [File.expand_path('d')], config.excluded_files
180
+ assert_equal 'http://example.com:666', config.server
181
+ end
182
+
183
+ def test_given_a_config_path_should_expand_the_includes
184
+ # given
185
+ config = given_an_empty_config
186
+ config.config_dir = "/foo/bbb/ccc"
187
+ pwd = File.expand_path('.')
188
+ config.includes "a/a", "/b/b", "../c"
189
+
190
+ # then
191
+ assert_config_includes config, 'load' => ["a/a", '/b/b', "../c"].map{|p| path(p)}
192
+ end
193
+
194
+ def test_config_with_html_fixtures
195
+ # given
196
+ config = given_an_empty_config
197
+ config.config_dir = File.expand_path("configs")
198
+
199
+ # when
200
+ config.fixtures "fixture/directory", :name => "fixture_name", :namespace => "fixture_namespace"
201
+
202
+ # then
203
+ assert_config_includes config, 'load' => paths(["configs/fixtures/fixture_namespace/fixture_name.js"])
204
+ end
205
+
206
+ def test_should_save_fixtures
207
+ # given
208
+ config = given_an_empty_config
209
+ config.config_dir = "tmp/stuff"
210
+ config.fixtures fixture_dir, :name => "fixture_name", :namespace => "fixture_namespace"
211
+
212
+ # when
213
+ config.save_fixtures
214
+
215
+ # then
216
+ name = "tmp/stuff/fixtures/fixture_namespace/fixture_name.js"
217
+ assert File.exists?(name)
218
+ assert_equal File.read(name), config.html_fixtures.first.to_s
219
+ ensure
220
+ FileUtils.rm_rf("tmp/stuff")
221
+ end
222
+
223
+ def test_should_raise_argument_error_if_the_same_fixture_defined_twice
224
+ # given
225
+ config = given_an_empty_config
226
+ config.fixtures "fixture/directory"
227
+
228
+ assert_raises(ArgumentError) do
229
+ config.fixtures "fixture/some_other_directory"
230
+ end
231
+ end
232
+
233
+ def test_should_not_raise_argument_error_for_two_fixtures_with_different_names
234
+ # given
235
+ config = given_an_empty_config
236
+ config.fixtures "fixture/directory"
237
+
238
+ assert_nothing_raised do
239
+ config.fixtures "fixture/some_other_directory", :name => "some_other_name"
240
+ end
241
+ end
242
+
243
+ def test_enable_jasmine_adds_jasmine_and_jasmine_adapter
244
+ # given
245
+ config = given_an_empty_config
246
+
247
+ # when
248
+ config.enable_jasmine
249
+
250
+ # then
251
+ assert_equal 2, config.included_files.size
252
+ assert File.exists?(config.included_files[0])
253
+ assert Dir[config.included_files[1]].size > 0
254
+ end
255
+
256
+ def test_with_coverage_measuring_enabled
257
+ # given
258
+ config = given_an_empty_config
259
+
260
+ # when
261
+ config.measure_coverage
262
+
263
+ # then
264
+ assert_config_includes config, 'plugin' => [{'name' => 'coverage',
265
+ 'jar' => File.expand_path('../../../vendor/coverage.jar', __FILE__),
266
+ 'module' => 'com.google.jstestdriver.coverage.CoverageModule'}]
267
+ end
268
+
269
+ end
270
+ end