qunited 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -10,43 +10,79 @@ Tests are run with PhantomJS if available, otherwise Rhino (Java) is used. Give
10
10
  $ gem install qunited
11
11
  ```
12
12
 
13
- ## Running Tests
13
+ ## Configuration
14
14
 
15
- Add the QUnited Rake task to your Rakefile.
15
+ Add the QUnited Rake task to your Rakefile. Specify your source and test JavaScript files.
16
16
 
17
17
  ```ruby
18
18
  require 'qunited/rake_task'
19
19
 
20
20
  QUnited::RakeTask.new do |t|
21
- t.source_files_pattern = 'lib/js/**/*.js'
21
+ t.source_files = ['lib/js/jquery.js', 'lib/js/my_app_1.js', 'lib/js/my_app_2.js']
22
22
  t.test_files_pattern = 'test/js/**/*.js'
23
23
  end
24
24
  ```
25
25
 
26
- Source and test files can also be configured as an array of file names. This may be desirable for source files since the order of their execution is often important. A glob pattern may not order the files correctly but configuring the task with an array can guarantee they are executed in the correct order.
26
+ Source and test files can be configured either as an array of file names or a glob pattern (using the ```_pattern``` version). Using an array is usually desirable for source files since the order of their execution is often important. Note that all JavaScript dependencies will have to be loaded with source files, in the correct order, to match your production environment.
27
27
 
28
- Note that all JavaScript dependencies will have to be loaded with source files. They will often need to be loaded before your own code so using an array to configure source files may be appropriate.
28
+ You can also use an array to configure the test files but a glob pattern might be more convenient since test files usually do not need to be loaded in a particular order.
29
29
 
30
- ```ruby
31
- require 'qunited/rake_task'
30
+ ### Specifying a driver
31
+
32
+ QUnited uses various drivers to set up the environment the tests run in (see below for more details). By default it tries to Just Work and find an available driver to use. You may want to lock down the driver (recommended) so your tests are consistent. To do this add a bit more configuration to the Rake task.
32
33
 
34
+ ```ruby
33
35
  QUnited::RakeTask.new do |t|
34
- t.source_files = ['lib/js/jquery.js', 'lib/js/my_utils.js', 'lib/js/my_app.js']
35
- t.test_files = ['test/js/test_my_utils.js', 'test/js/test_my_app.js']
36
+ t.source_files = ['lib/js/jquery.js', 'lib/js/my_app']
37
+ t.test_files_pattern = 'test/js/**/*.js'
38
+ t.driver = :phantomjs # Always use PhantomJS to run tests. Fail if it's not available.
36
39
  end
37
40
  ```
38
41
 
39
- Note that you can also use an array to configure the test files but a glob pattern is usually more convenient since test files usually do not need to be loaded in a particular order.
42
+ Available drivers are ```:phantomjs``` and ```:rhino```. If no driver is specified QUnited will run tests with the best available driver, looking for them in that order.
43
+
44
+ ## Running tests
45
+
46
+ Once the Rake task is configured as described above, run tests like this:
47
+
48
+ ```
49
+ $ rake qunited
50
+ ```
51
+
52
+ You should get output similar to minitest or Test::Unit
53
+
54
+ ```
55
+ $ rake qunited
56
+ qunited --driver phantomjs lib/js/jquery-1.4.2.js lib/js/helper.js test/js/app.js -- test/js/test_app.js
57
+
58
+ # Running JavaScript tests with PhantomJS:
59
+
60
+ .............
61
+
62
+ Finished in 0.009 seconds, 1444.44 tests/s, 7777.78 assertions/s.
63
+
64
+ 13 tests, 70 assertions, 0 failures, 0 errors, 0 skips
65
+ ```
66
+
67
+ ## Drivers
68
+
69
+ A JavaScript interpreter with browser APIs is necessary to run tests. Various drivers are provided to interface with programs to set up this environment.
70
+
71
+ ### PhantomJS
72
+
73
+ PhantomJS is a headless WebKit. It is fast and provides an accurate browser environment (since it _is_ a browser).
74
+
75
+ Find out how to install it [here](http://phantomjs.org/) or just ```brew install phantomjs``` if you have Homebrew.
40
76
 
41
- ## Dependencies
77
+ This driver is considered available if the ```phantomjs``` executable is on your $PATH.
42
78
 
43
- - PhantomJS
79
+ ### Rhino + Envjs
44
80
 
45
- OR
81
+ Rhino is a JavaScript interpreter that runs on the JVM. Envjs provides a simulated browser environment in Rhino. Rhino+Envjs should be considered a fallback since it is slower and has some minor incompatibility with most browsers. However, most tests will run fine.
46
82
 
47
- - Java (version 1.1 minimum)
83
+ Install Java 1.1 or greater to make this work.
48
84
 
49
- PhantomJS is preferred since it uses real WebKit and is faster. Running Rhino on Java should be considered a fallback.
85
+ This driver is considered available if you have Java 1.1 or greater and the ```java``` executable is on your $PATH.
50
86
 
51
87
  ## Credits
52
88
 
@@ -1,6 +1,9 @@
1
1
  module QUnited
2
2
  module Driver
3
3
  class Base
4
+ # Path of the common (to all drivers) supporting files directory
5
+ SUPPORT_DIR = File.expand_path('../support', __FILE__)
6
+
4
7
  attr_reader :results, :source_files, :test_files
5
8
 
6
9
  # Finds an executable on the PATH. Returns the absolute path of the
@@ -16,11 +19,6 @@ module QUnited
16
19
  return nil
17
20
  end
18
21
 
19
- # Get the path of the common (to all drivers) supporting files directory
20
- def self.support_dir
21
- @@support_dir = File.expand_path('../support', __FILE__)
22
- end
23
-
24
22
  # Array of file names? Glob pattern?
25
23
  def initialize(source_files, test_files)
26
24
  @source_files = if source_files.is_a? String
@@ -41,7 +39,7 @@ module QUnited
41
39
  end
42
40
 
43
41
  def support_file_path(filename)
44
- File.join(self.class.support_dir, filename)
42
+ File.join(SUPPORT_DIR, filename)
45
43
  end
46
44
 
47
45
  def support_file_contents(filename)
@@ -7,6 +7,7 @@ require 'open3'
7
7
  module QUnited
8
8
  module Driver
9
9
  class PhantomJs < Base
10
+ SUPPORT_DIR = File.expand_path('../support', __FILE__)
10
11
 
11
12
  # Determines whether this driver available to use.
12
13
  # Checks whether phantomjs is on the PATH.
@@ -26,7 +27,7 @@ module QUnited
26
27
  results_file = Tempfile.new('qunited_results')
27
28
  results_file.close
28
29
 
29
- cmd = %{phantomjs "#{File.expand_path('../support/runner.js', __FILE__)}" }
30
+ cmd = %{phantomjs "#{File.join(SUPPORT_DIR, 'runner.js')}" }
30
31
  cmd << %{#{tests_file.path} #{results_file.path}}
31
32
 
32
33
  Open3.popen3(cmd) do |stdin, stdout, stderr|
@@ -36,7 +37,7 @@ module QUnited
36
37
  end
37
38
  end
38
39
 
39
- @results = ::QUnited::Results.from_javascript_produced_json(IO.read(results_file))
40
+ @results = ::QUnited::Results.from_javascript_produced_json(IO.read(results_file.path))
40
41
  end
41
42
 
42
43
  private
@@ -44,11 +45,11 @@ module QUnited
44
45
  attr_accessor :tests_file
45
46
 
46
47
  def tests_page_content
47
- ERB.new(IO.read(File.expand_path('../support/tests_page.html.erb', __FILE__))).result(binding)
48
+ ERB.new(IO.read(File.join(SUPPORT_DIR, 'tests_page.html.erb'))).result(binding)
48
49
  end
49
50
 
50
51
  def script_tag(file)
51
- js_file_path, tests_file_path = Pathname.new(file).realpath, Pathname.new(tests_file)
52
+ js_file_path, tests_file_path = Pathname.new(file).realpath, Pathname.new(tests_file.path)
52
53
  begin
53
54
  rel_path = js_file_path.relative_path_from(tests_file_path)
54
55
  # Attempt to convert paths to relative URLs if Windows... should really test this
@@ -5,6 +5,7 @@ require 'open3'
5
5
  module QUnited
6
6
  module Driver
7
7
  class Rhino < Base
8
+ SUPPORT_DIR = File.expand_path('../support', __FILE__)
8
9
 
9
10
  # Determines whether this driver available to use. Checks whether java
10
11
  # is on the PATH and whether Java is version 1.1 or greater.
@@ -22,8 +23,7 @@ module QUnited
22
23
  end
23
24
 
24
25
  def run
25
- support_dir = File.expand_path('../support', __FILE__)
26
- js_jar, runner = File.join(support_dir, 'js.jar'), File.join(support_dir, 'runner.js')
26
+ js_jar, runner = File.join(SUPPORT_DIR, 'js.jar'), File.join(SUPPORT_DIR, 'runner.js')
27
27
 
28
28
  source_files_args = @source_files.map { |sf| %{"#{sf}"} }.join(' ')
29
29
  test_files_args = @test_files.map { |tf| %{"#{tf}"} }.join(' ')
@@ -32,7 +32,7 @@ module QUnited
32
32
  results_file.close
33
33
 
34
34
  cmd = %{java -jar "#{js_jar}" -opt -1 "#{runner}" }
35
- cmd << %{"#{QUnited::Driver::Base.support_dir}" "#{support_dir}" "#{results_file.path}"}
35
+ cmd << %{"#{QUnited::Driver::Base::SUPPORT_DIR}" "#{SUPPORT_DIR}" "#{results_file.path}"}
36
36
  cmd << " #{source_files_args} -- #{test_files_args}"
37
37
 
38
38
  # Swallow stdout but allow stderr to get blasted out to console - if there are uncaught
@@ -44,7 +44,7 @@ module QUnited
44
44
  unless (err = stderr.read).strip.empty? then $stderr.puts(err) end
45
45
  end
46
46
 
47
- @results = ::QUnited::Results.from_javascript_produced_json(IO.read(results_file))
47
+ @results = ::QUnited::Results.from_javascript_produced_json(IO.read(results_file.path))
48
48
  end
49
49
  end
50
50
  end
@@ -62,7 +62,10 @@ QUnited.collectedTestResults = function() {
62
62
 
63
63
  /* Module results as a JSON string */
64
64
  QUnited.collectedTestResultsAsJson = function() {
65
- return JSON.stringify(QUnited.collectedTestResults());
65
+ // The 3rd argument here adds spaces to the generated JSON string. This is necessary
66
+ // for YAML parsers that are not compliant with YAML 1.2 (the version where YAML became
67
+ // a true superset of JSON).
68
+ return JSON.stringify(QUnited.collectedTestResults(), null, 1);
66
69
  };
67
70
 
68
71
  })();
@@ -122,8 +122,11 @@ module QUnited
122
122
  end
123
123
 
124
124
  def times_line
125
- "Finished in #{"%.6g" % total_time} seconds, #{"%.6g" % (total_tests/total_time)} tests/s, " +
126
- "#{"%.6g" % (total_assertions/total_time)} assertions/s."
125
+ tests_per = (total_time > 0) ? (total_tests / total_time) : total_tests
126
+ assertions_per = (total_time > 0) ? (total_assertions / total_time) : total_assertions
127
+
128
+ "Finished in #{"%.6g" % total_time} seconds, #{"%.6g" % tests_per} tests/s, " +
129
+ "#{"%.6g" % assertions_per} assertions/s."
127
130
  end
128
131
 
129
132
  def failures_output
@@ -43,8 +43,8 @@ module QUnited
43
43
  end
44
44
 
45
45
  def get_driver(klass)
46
- if ::QUnited::Driver.constants.reject { |d| d == :Base }.include?(klass)
47
- ::QUnited::Driver.const_get(klass)
46
+ if ::QUnited::Driver.constants.reject { |d| d == :Base }.include?(klass.to_s)
47
+ ::QUnited::Driver.const_get(klass.to_s)
48
48
  end
49
49
  end
50
50
 
@@ -1,3 +1,3 @@
1
1
  module QUnited
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/test/test_helper.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'rubygems'
1
2
  require 'minitest/autorun'
2
3
 
3
4
  require File.join(File.dirname(__FILE__), *%w[.. lib qunited])
@@ -10,14 +10,14 @@ end
10
10
 
11
11
  class TestRunner < MiniTest::Unit::TestCase
12
12
  def test_raises_exception_with_nonexistent_driver
13
- runner = QUnited::Runner.new(['source.js'], ['test.js'], { driver: :doesNotExist })
13
+ runner = QUnited::Runner.new(['source.js'], ['test.js'], { :driver => :doesNotExist })
14
14
  assert_raises(QUnited::UsageError) do
15
15
  runner.resolve_driver_class
16
16
  end
17
17
  end
18
18
 
19
19
  def test_specified_driver_can_be_used_if_available
20
- runner = QUnited::Runner.new(['source.js'], ['test.js'], { driver: :AvailableDriver })
20
+ runner = QUnited::Runner.new(['source.js'], ['test.js'], { :driver => :AvailableDriver })
21
21
 
22
22
  def runner.get_driver(klass)
23
23
  if klass == :AvailableDriver
@@ -30,7 +30,7 @@ class TestRunner < MiniTest::Unit::TestCase
30
30
  end
31
31
 
32
32
  def test_raises_exception_when_not_available_driver_is_specified
33
- runner = QUnited::Runner.new(['source.js'], ['test.js'], { driver: :NotAvailableDriver })
33
+ runner = QUnited::Runner.new(['source.js'], ['test.js'], { :driver => :NotAvailableDriver })
34
34
 
35
35
  def runner.get_driver(klass)
36
36
  if klass == :NotAvailableDriver
metadata CHANGED
@@ -1,24 +1,33 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: qunited
3
- version: !ruby/object:Gem::Version
4
- version: 0.2.0
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 1
10
+ version: 0.2.1
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Aaron Royer
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2012-07-08 00:00:00.000000000 Z
17
+
18
+ date: 2012-12-16 00:00:00 Z
13
19
  dependencies: []
20
+
14
21
  description: QUnited runs headless QUnit tests as part of your normal build
15
- email:
22
+ email:
16
23
  - aaronroyer@gmail.com
17
- executables:
24
+ executables:
18
25
  - qunited
19
26
  extensions: []
27
+
20
28
  extra_rdoc_files: []
21
- files:
29
+
30
+ files:
22
31
  - .gitignore
23
32
  - Gemfile
24
33
  - MIT-LICENSE
@@ -66,29 +75,38 @@ files:
66
75
  - test/unit/test_runner.rb
67
76
  homepage: https://github.com/aaronroyer/qunited
68
77
  licenses: []
78
+
69
79
  post_install_message:
70
80
  rdoc_options: []
71
- require_paths:
81
+
82
+ require_paths:
72
83
  - lib
73
- required_ruby_version: !ruby/object:Gem::Requirement
84
+ required_ruby_version: !ruby/object:Gem::Requirement
74
85
  none: false
75
- requirements:
76
- - - ! '>='
77
- - !ruby/object:Gem::Version
78
- version: '0'
79
- required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
94
  none: false
81
- requirements:
82
- - - ! '>='
83
- - !ruby/object:Gem::Version
84
- version: '0'
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
85
102
  requirements: []
103
+
86
104
  rubyforge_project: qunited
87
- rubygems_version: 1.8.11
105
+ rubygems_version: 1.8.15
88
106
  signing_key:
89
107
  specification_version: 3
90
108
  summary: QUnit tests in your build
91
- test_files:
109
+ test_files:
92
110
  - test/fixtures/basic_project/app/assets/javascripts/application.js
93
111
  - test/fixtures/basic_project/test/javascripts/test_basics.js
94
112
  - test/fixtures/basic_project/test/javascripts/test_math.js