guard-jstd 0.0.1 → 0.1.0

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.
data/.gitignore CHANGED
@@ -2,3 +2,4 @@ pkg/*
2
2
  *.gem
3
3
  .bundle
4
4
  Gemfile.lock
5
+ notes.txt
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 aRailsDemo.com
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Guard::Jstd provides autotest functionality for your test driven JavaScript development with JsTestDriver. This gem was inspired by the [jstdutil gem](http://cjohansen.no/en/javascript/jstdutil_a_ruby_wrapper_over_jstestdriver) and modeled after the [Guard::RSpec gem.](https://github.com/guard/guard-rspec)
4
4
 
5
- ## Installing the gem:
5
+ ## Installing the gem
6
6
 
7
7
  From the command line:
8
8
 
@@ -24,6 +24,32 @@ See the [Guard](http://github.com/guard/guard) gem README for more information a
24
24
 
25
25
  If you want to use CoffeeScript in your development, add the [guard-coffeescript gem.](https://github.com/guard/guard-coffeescript)
26
26
 
27
+ ## Configuration
28
+
29
+ You can specify some configuration options by passing a block to Jstd.configure. This must appear after the 'guard "jstd"' definition in your Guardfile. An example configuration is given below.
30
+
31
+ guard "jstd" do
32
+ # mappings here
33
+ end
34
+
35
+ Jstd.configure do |c|
36
+ c.java_path = "~/my/path/JsTestDriver-1.3.2.jar"
37
+ c.browser_paths = "\`which open\`"
38
+ c.jstd_config_path = 'someJsTestDriver.conf'
39
+ c.start_server = false
40
+ c.capture_browser = false # false is the default
41
+ end
42
+
43
+ ## Defaults
44
+
45
+ If you have a "$JSTESTDRIVER_HOME" environment variable set on your system, Jstd will automatically look for your JsTestDriver .jar file there. Otherwise, you have to configure the path with "c.java_path".
46
+
47
+ On start up, Jstd will attempt to start the JsTestDriver server. You can prevent this with "c.start_server = false". The server_port is determined from your JsTestDriver configuration file. If this file does not exist, then the default port is 4224. If you really want Jstd to start the server in a specified port, then you can do that with "c.server_port = 1234".
48
+
49
+ If you set "c.capture_browser = true", then Jstd will attempt to capture the browser(s) defined in "c.browser_path" when the server is started. Otherwise, you have to manually capture browsers after the server starts.
50
+
51
+ The default JsTestDriver configuration file name is 'jsTestDriver.conf'. If you are using something else, then use "c.jstd_config_path".
52
+
27
53
  ## JsTestDriver
28
54
 
29
- Currently, Guard::Jstd requires that you define an environment variable, $JSTESTDRIVER_HOME, to specify where JsTestDriver .jar file is located at on your system. More information about setting up JsTestDriver on your system can be [found here.](http://www.arailsdemo.com/posts/46) or on the JsTestDriver [homepage.](http://code.google.com/p/js-test-driver/)
55
+ Information about setting up JsTestDriver on your system can be [found here](http://www.arailsdemo.com/posts/46) or on the JsTestDriver [homepage.](http://code.google.com/p/js-test-driver/)
@@ -0,0 +1,71 @@
1
+ module Guard
2
+ class Jstd
3
+ module Configuration
4
+ class << self
5
+ METHODS = [:java_path, :browser_paths, :server_port,
6
+ :jstd_config_path, :start_server, :capture_browser]
7
+
8
+ METHODS.each do |method|
9
+ attr_writer method
10
+ end
11
+
12
+ def reset!
13
+ METHODS.each { |method| instance_variable_set("@#{method.to_s}", nil) }
14
+ @jar_home = nil
15
+ end
16
+
17
+ def java_path
18
+ @java_path ||= jar_path
19
+ end
20
+
21
+ def browser_paths
22
+ @browser_paths ||= "\`which open\`"
23
+ end
24
+
25
+ def jstd_config_path
26
+ @jstd_config_path ||= 'jsTestDriver.conf'
27
+ end
28
+
29
+ def server_port
30
+ @server_port ||= default_server_port
31
+ end
32
+
33
+ def start_server
34
+ @start_server == nil ? true : @start_server
35
+ end
36
+
37
+ def capture_browser
38
+ @capture_browser == nil ? false : @capture_browser
39
+ end
40
+
41
+ def default_server_port
42
+ begin
43
+ conf = File.read(jstd_config_path)
44
+ conf.scan(/server:.*:(\d+)$/).to_s
45
+ rescue
46
+ '4224'
47
+ end
48
+ end
49
+
50
+ def jar_path
51
+ path = File.join(jar_home, 'jstest*.jar')
52
+ files = Dir.glob(File.expand_path(path), File::FNM_CASEFOLD)
53
+ if files.empty?
54
+ alert = "\e[31mGuard::Jstd could not find your JsTestDriver.jar file. "
55
+ alert << "Please specify it in the configuration.\e[0m"
56
+ UI.info(alert, :reset => true)
57
+ exit
58
+ else
59
+ files.first
60
+ end
61
+ end
62
+
63
+ def jar_home
64
+ return @jar_home if @jar_home
65
+ echo = `echo $JSTESTDRIVER_HOME`
66
+ @jar_home = echo == "" ? '~/bin' : echo.gsub!("\n", "")
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -1,16 +1,38 @@
1
+ require 'Forwardable'
2
+
1
3
  module Guard
2
4
  class Jstd
3
5
  module Runner
4
6
  class << self
5
- def java_command
6
- "java -jar $JSTESTDRIVER_HOME/JsTestDriver-1.3.2.jar --tests"
7
+ extend Forwardable
8
+
9
+ [:java_path, :browser_paths, :server_port,
10
+ :jstd_config_path].each do |config|
11
+ def_delegator Configuration, config, config
7
12
  end
8
13
 
9
14
  def run(tests="all")
10
15
  UI.info("Running #{tests}")
11
- results = `#{java_command} #{tests}`
16
+ results = `#{java_command} --tests #{tests}`
12
17
  Formatter.notify(results)
13
18
  end
19
+
20
+ def start_server
21
+ if Configuration.start_server
22
+ browser_opt = Configuration.capture_browser ?
23
+ " --browser #{browser_paths}" : ""
24
+ pid = fork {
25
+ trap('QUIT', 'IGNORE')
26
+ `#{java_command} --port #{server_port}#{browser_opt}`
27
+ }
28
+ Process.detach(pid)
29
+ UI.info "JsTestDriver server started on port #{server_port}"
30
+ end
31
+ end
32
+
33
+ def java_command
34
+ "java -jar #{java_path} --config #{jstd_config_path}"
35
+ end
14
36
  end
15
37
  end
16
38
  end
@@ -2,3 +2,9 @@ guard "jstd" do
2
2
  watch(%r{^javascripts/.+_test\.js})
3
3
  watch(%r{^javascripts/src/(.+)\.js}) { |m| "javascripts/test/#{m[1]}_test.js"}
4
4
  end
5
+
6
+ # See the README for configuration options.
7
+ Jstd.configure do |c|
8
+ c.browser_paths = "\`which open\`"
9
+ c.capture_browser = true
10
+ end
@@ -1,5 +1,5 @@
1
1
  module Guard
2
2
  module JstdVersion
3
- VERSION = "0.0.1"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
data/lib/guard/jstd.rb CHANGED
@@ -6,9 +6,11 @@ module Guard
6
6
  autoload :Runner, 'guard/jstd/runner'
7
7
  autoload :CaseFinder, 'guard/jstd/case_finder'
8
8
  autoload :Formatter, 'guard/jstd/formatter'
9
+ autoload :Configuration, 'guard/jstd/configuration'
9
10
 
10
11
  def start
11
12
  UI.info "Guard::Jstd is running."
13
+ Runner.start_server
12
14
  end
13
15
 
14
16
  def run_all
@@ -19,5 +21,9 @@ module Guard
19
21
  cases = CaseFinder.find(paths)
20
22
  Runner.run(cases)
21
23
  end
24
+
25
+ def self.configure
26
+ yield Configuration
27
+ end
22
28
  end
23
29
  end
@@ -0,0 +1,118 @@
1
+ require "spec_helper"
2
+
3
+ describe Guard::Jstd::Configuration do
4
+ let(:_module) { Guard::Jstd::Configuration }
5
+
6
+ after do
7
+ subject.reset!
8
+ end
9
+
10
+ describe ".jar_home" do
11
+ it "uses $JSTESTDRIVER_HOME when present" do
12
+ dir = "path\n"
13
+ subject.should_receive("`").with(
14
+ "echo $JSTESTDRIVER_HOME"
15
+ ).and_return(dir)
16
+ subject.jar_home.should == "path"
17
+ end
18
+
19
+ it "uses the default when $JSTESTDRIVER_HOME isn't present" do
20
+ subject.should_receive("`").with(
21
+ "echo $JSTESTDRIVER_HOME"
22
+ ).and_return("")
23
+ subject.jar_home.should == '~/bin'
24
+ end
25
+ end
26
+
27
+ describe ".jar_path" do
28
+ before { subject.stub(:jar_home).and_return("/path") }
29
+
30
+ it "finds the .jar file in the jar_home" do
31
+ Dir.should_receive(:glob).with("/path/jstest*.jar", File::FNM_CASEFOLD) { ["file.jar"] }
32
+ subject.jar_path.should == "file.jar"
33
+ end
34
+
35
+ it "exits if the .jar file isn't found" do
36
+ Dir.should_receive(:glob).with("/path/jstest*.jar", File::FNM_CASEFOLD) { [] }
37
+ Guard::UI.should_receive(:info)
38
+ expect { subject.jar_path }.to raise_error
39
+ end
40
+ end
41
+
42
+ describe ".default_server_port" do
43
+ def conf
44
+ <<-FILE
45
+ server: http://0.0.0.0:2345
46
+
47
+ load:
48
+ - javascripts/lib/*.js
49
+ - javascripts/src/*.js
50
+ - javascripts/test/*.js
51
+ FILE
52
+ end
53
+
54
+ before do
55
+ subject.stub(:jstd_config_path) { 'foo' }
56
+ end
57
+
58
+ it "gets the default from the JsTestDriver configuration file" do
59
+ File.should_receive(:read).with("foo") { conf }
60
+ subject.default_server_port.should == "2345"
61
+ end
62
+
63
+ it "returns 4224 if there is no configuration file" do
64
+ subject.default_server_port.should == "4224"
65
+ end
66
+ end
67
+
68
+ describe ".reset!" do
69
+ it "sets the java_path to the default" do
70
+ subject.stub(:jar_path) { "home" }
71
+
72
+ subject.java_path = 'foo/bar.jar'
73
+ subject.java_path.should == 'foo/bar.jar'
74
+ subject.reset!
75
+
76
+ subject.java_path.should == 'home'
77
+ end
78
+
79
+ it "sets the browser_paths to the default" do
80
+ subject.browser_paths = 'path'
81
+ subject.browser_paths.should == 'path'
82
+ subject.reset!
83
+
84
+ subject.browser_paths.should == "\`which open\`"
85
+ end
86
+
87
+ it "sets the server_port to the default" do
88
+ subject.stub(:default_server_port) { 123 }
89
+ subject.reset!
90
+
91
+ subject.server_port.should == 123
92
+ end
93
+
94
+ it "sets the jstd_config_path to the default" do
95
+ subject.jstd_config_path = 'hi'
96
+ subject.jstd_config_path.should == 'hi'
97
+ subject.reset!
98
+
99
+ subject.jstd_config_path.should == 'jsTestDriver.conf'
100
+ end
101
+
102
+ it "sets the start_server option to true" do
103
+ subject.start_server = false
104
+ subject.start_server.should == false
105
+ subject.reset!
106
+
107
+ subject.start_server.should == true
108
+ end
109
+
110
+ it "sets the capture_browser option to false" do
111
+ subject.capture_browser = true
112
+ subject.capture_browser.should == true
113
+ subject.reset!
114
+
115
+ subject.capture_browser.should == false
116
+ end
117
+ end
118
+ end
@@ -85,7 +85,7 @@ describe Guard::Jstd::Formatter do
85
85
  subject.notify(failed)
86
86
  end
87
87
 
88
- it "call #display" do
88
+ it "call #to_terminal" do
89
89
  @formatter.should_receive(:to_terminal)
90
90
  subject.notify(failed)
91
91
  end
@@ -1,6 +1,8 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Guard::Jstd::Runner do
4
+ let(:config) { Guard::Jstd::Configuration }
5
+
4
6
  describe ".run" do
5
7
  let(:test) { "HelloTest" }
6
8
 
@@ -10,21 +12,33 @@ describe Guard::Jstd::Runner do
10
12
  subject.run
11
13
  end
12
14
 
13
- context "--JsTestDriver command--" do
14
- let(:java_command) { Guard::Jstd::Runner::java_command }
15
+ it ".java_command uses Configuration java_path and jstd_config_path" do
16
+ Guard::Jstd::Formatter.stub(:notify)
17
+ config.java_path = 'foo'
18
+ config.jstd_config_path = 'bar'
19
+ subject.should_receive(:"`").with(
20
+ "java -jar foo --config bar --tests all"
21
+ )
22
+ subject.run
23
+ Guard::Jstd::Configuration.reset!
24
+ end
15
25
 
16
- before { Guard::Jstd::Formatter.stub(:notify) }
26
+ context "--JsTestDriver command--" do
27
+ before do
28
+ subject.stub(:java_command) { 'hooha' }
29
+ Guard::Jstd::Formatter.stub(:notify)
30
+ end
17
31
 
18
32
  it "can run all tests" do
19
33
  subject.should_receive(:"`").with(
20
- "#{java_command} all"
34
+ "hooha --tests all"
21
35
  )
22
36
  subject.run
23
37
  end
24
38
 
25
39
  it "can run a single TestCase" do
26
40
  subject.should_receive(:"`").with(
27
- "#{java_command} #{test}"
41
+ "hooha --tests #{test}"
28
42
  )
29
43
  subject.run(test)
30
44
  end
@@ -39,4 +53,66 @@ describe Guard::Jstd::Runner do
39
53
  subject.run(test)
40
54
  end
41
55
  end
56
+
57
+ describe ".start_server" do
58
+ context "--given some configuration--" do
59
+ before do
60
+ config.java_path = 'java'
61
+ config.browser_paths = 'browsers'
62
+ config.server_port = 1234
63
+ end
64
+
65
+ after { config.reset! }
66
+
67
+ it "does not start the server if configuration is false" do
68
+ config.start_server = false
69
+ subject.should_not_receive(:fork)
70
+ subject.start_server
71
+ end
72
+
73
+ it "detaches a forked process" do
74
+ subject.stub(:fork) { 1 }
75
+ Process.should_receive(:detach).with(1)
76
+ subject.start_server
77
+ end
78
+
79
+ it "sends a message to UI" do
80
+ subject.stub(:fork)
81
+ Process.stub(:detach)
82
+ Guard::UI.should_receive(:info).with(
83
+ "JsTestDriver server started on port 1234"
84
+ )
85
+ subject.start_server
86
+ end
87
+
88
+ context "--in the forked process--" do
89
+ before do
90
+ subject.stub(:java_command) { 'hooha' }
91
+ Process.stub(:detach)
92
+ end
93
+
94
+ it "starts the JsTestDriver server" do
95
+ subject.should_receive("`").with('hooha --port 1234') { 'hooha' }
96
+ subject.should_receive(:fork) { |&block|
97
+ block.call.should == 'hooha'
98
+ }
99
+ subject.start_server
100
+ end
101
+
102
+ it "opens the browser if configurated to do so" do
103
+ config.capture_browser = true
104
+ subject.should_receive("`").with('hooha --port 1234 --browser browsers') { 'hooha' }
105
+ subject.should_receive(:fork) { |&block| block.call }
106
+ subject.start_server
107
+ end
108
+
109
+ it "traps the 'QUIT' signal before sending it to the child process" do
110
+ subject.should_receive("`").with('hooha --port 1234') { 'hooha' }
111
+ subject.should_receive(:trap).with('QUIT', 'IGNORE')
112
+ subject.should_receive(:fork) { |&block| block.call }
113
+ subject.start_server
114
+ end
115
+ end
116
+ end
117
+ end
42
118
  end
@@ -7,11 +7,19 @@ describe Guard::Jstd do
7
7
  klass.ancestors.should include Guard::Guard
8
8
  end
9
9
 
10
- it "#start sends a message to UI" do
11
- ::Guard::UI.should_receive(:info).with(
12
- "Guard::Jstd is running."
13
- )
14
- subject.start
10
+ describe "#start" do
11
+ it "sends a message to UI" do
12
+ klass::Runner.stub(:start_server)
13
+ ::Guard::UI.should_receive(:info).with(
14
+ "Guard::Jstd is running."
15
+ )
16
+ subject.start
17
+ end
18
+
19
+ it "calls Runner.start_server" do
20
+ klass::Runner.should_receive(:start_server)
21
+ subject.start
22
+ end
15
23
  end
16
24
 
17
25
  it " #run_all runs all tests" do
@@ -28,4 +36,10 @@ describe Guard::Jstd do
28
36
  subject.run_on_change(paths)
29
37
  end
30
38
  end
39
+
40
+ it ".configure yields the Configuration module" do
41
+ Guard::Jstd.configure do |c|
42
+ c.should == Guard::Jstd::Configuration
43
+ end
44
+ end
31
45
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guard-jstd
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 1
10
- version: 0.0.1
9
+ - 0
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - aRailsDemo
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-07 00:00:00 Z
18
+ date: 2011-04-08 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: guard
@@ -62,16 +62,19 @@ files:
62
62
  - .gitignore
63
63
  - Gemfile
64
64
  - Guardfile
65
+ - LICENSE
65
66
  - README.md
66
67
  - Rakefile
67
68
  - guard-jstd.gemspec
68
69
  - lib/guard/jstd.rb
69
70
  - lib/guard/jstd/case_finder.rb
71
+ - lib/guard/jstd/configuration.rb
70
72
  - lib/guard/jstd/formatter.rb
71
73
  - lib/guard/jstd/runner.rb
72
74
  - lib/guard/jstd/templates/Guardfile
73
75
  - lib/guard/jstd/version.rb
74
76
  - spec/guard/jstd/case_finder_spec.rb
77
+ - spec/guard/jstd/configuration_spec.rb
75
78
  - spec/guard/jstd/formatter_spec.rb
76
79
  - spec/guard/jstd/runner_spec.rb
77
80
  - spec/guard/jstd_spec.rb
@@ -111,6 +114,7 @@ specification_version: 3
111
114
  summary: A Guard for JsTestDriver
112
115
  test_files:
113
116
  - spec/guard/jstd/case_finder_spec.rb
117
+ - spec/guard/jstd/configuration_spec.rb
114
118
  - spec/guard/jstd/formatter_spec.rb
115
119
  - spec/guard/jstd/runner_spec.rb
116
120
  - spec/guard/jstd_spec.rb