boojs 0.0.16 → 0.0.17

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: df1e3ef0cab1f3a40d88ef059199d65a600550a9
4
- data.tar.gz: 551424a6c0d8d2a61927616e34e0d334b865ff53
3
+ metadata.gz: e7fd9c711425ee30d94d93e2783416e5b20c0f5c
4
+ data.tar.gz: 0a56b113946a5b6adfb68c15c3094361e528df99
5
5
  SHA512:
6
- metadata.gz: 689c720b9532fbae542155d9ca8b1a903fb67fab2f4265650c5d84a2f4fd93bfab5a7b64d1d246ff164391dda247710ccb2f51dffb5508a16dccf328ccf47357
7
- data.tar.gz: 4d12b58e0a93162b8ce6f4e62615528c8a2faaea5587777096f6d93bd1073c8b793c474915507341e91468bbedb89df53bc23d4099256215aff9e392cdc6d80f
6
+ metadata.gz: 53fc0ef3f58d229db48ef8cb23d8bee35f7961fd403f1e0995ff4328e2cb9ba088bbab731581f88804461368fc4c7fecb7b0c3a214826445e03276205b9f2c0e
7
+ data.tar.gz: 06064c7b0fd99e359ee59eb2e2c419ea826b17b837d076e102190bba2a8d1fd49b6beca0087cd4bb4f7aeffbf3cdc328dbfa6f119028b0d74278e77ed74d026a
data/README.md CHANGED
@@ -24,12 +24,13 @@ gem install boojs
24
24
  # Usage
25
25
  #### SYNOPSIS
26
26
  ```sh
27
- boojs [-e statement] [-v file] [file]
27
+ boojs [-e statement] [-t timeout] [-v file] [file]
28
28
  ```
29
29
 
30
30
  #### DESCRIPTION
31
31
  The following options are available:
32
- * `-e` - Pass a javascript statement to execute after the file (if a file is provided) and then immediately terminate.
32
+ * `-e` - Pass a javascript statement to execute after the file (if a file is provided) and then immediately terminate unless `-t` is set.
33
+ * `-t` - Close the program after N seconds have passed, unless an exception is raised
33
34
  * `-v` - Verify that a file contains no javascript syntax errors. Returns 0 if there are no errors.
34
35
 
35
36
  #### EXAMPLES
@@ -48,6 +49,12 @@ Execute a javascript statement, and then immediately exit. Exceptions will retur
48
49
  (sh)>boojs -e "console.log(document);"
49
50
  ```
50
51
 
52
+ Execute a javascript statement, and then wait 4 seconds before exiting. Exceptions will return 1 and end execution early.
53
+ ```sh
54
+ (sh)>boojs -e "console.log(document); -t 4"
55
+ ```
56
+
57
+
51
58
  Verify that a file contains no javascript runtime initialization errors
52
59
  ```sh
53
60
  (sh)>boojs -v code.js
data/bin/boojs CHANGED
@@ -2,6 +2,7 @@
2
2
  require 'boojs'
3
3
  require 'optparse'
4
4
  require 'phantomjs'
5
+ require 'timeout'
5
6
 
6
7
  #Parse
7
8
  ##########################################
@@ -17,19 +18,42 @@ parser = OptionParser.new do |opts|
17
18
  opts.on "-e COMMAND" do |c|
18
19
  @c = c
19
20
  end
21
+
22
+ #Timeout
23
+ opts.on "-t TIMEOUT" do |t|
24
+ @t = t.to_i
25
+ end
20
26
  end
21
27
  parser.parse!
22
28
  file = ARGV.pop
23
29
  ##########################################
24
30
 
25
31
 
32
+ #Pipe with with a optional file and optional execute command
33
+ pipe_proc = Proc.new do
34
+ if file
35
+ BooJS.pipe File.read(file), @c, !@t.nil?
36
+ else
37
+ BooJS.pipe nil, @c, !@t.nil?
38
+ end
39
+ end
40
+
41
+ #This takes a long time
42
+ $phantomjs_path = Phantomjs.path
43
+
26
44
  #Run verify or run stdin/stdout mode?
27
45
  if @vfile
28
46
  BooJS.verify File.read(@file)
29
47
  else
30
- if file
31
- BooJS.pipe File.read(file), @c
48
+ if @t
49
+ begin
50
+ Timeout::timeout(@t) do
51
+ pipe_proc.call
52
+ end
53
+ rescue Timeout::Error
54
+ exit 0
55
+ end
32
56
  else
33
- BooJS.pipe nil, @c
57
+ pipe_proc.call
34
58
  end
35
59
  end
data/lib/boojs/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module BooJS
2
- VERSION = '0.0.16'
2
+ VERSION = '0.0.17'
3
3
  end
data/lib/boojs.rb CHANGED
@@ -30,7 +30,7 @@ module BooJS
30
30
 
31
31
  #Optionally, accept code to inject and a command to run
32
32
  #If the command is nil, this is not executed as a oneshot
33
- def self.pipe(str=nil, cmd=nil)
33
+ def self.pipe(str=nil, cmd=nil, will_timeout=false)
34
34
  js = %{
35
35
  var system = require('system');
36
36
  function __spec_ping(str) {
@@ -56,15 +56,18 @@ module BooJS
56
56
  #Repl
57
57
  if cmd
58
58
  js += "\n#{cmd}"
59
- js += "\nphantom.exit(1)"
59
+
60
+ js += "\nphantom.exit(1)" unless will_timeout
60
61
  else
61
62
  js += "\nwhile (true) { var line = system.stdin.readLine(); eval(line); }"
62
63
  end
63
64
 
64
- phantom = Phantomjs.path
65
65
  tmp = Tempfile.new(SecureRandom.hex)
66
66
  tmp.puts js
67
67
  tmp.close
68
- exit system("#{phantom} #{tmp.path}")
68
+ ret = system("#{$phantomjs_path} #{tmp.path}")
69
+
70
+ tmp.unlink
71
+ exit ret
69
72
  end
70
73
  end
data/spec/cli_spec.rb CHANGED
@@ -5,7 +5,7 @@ require 'securerandom'
5
5
  describe "CLI" do
6
6
  before(:each) do
7
7
  #Laziness
8
- `ps -ax | grep phantomjs | grep -v phantomjs | awk '{print $1}' | xargs kill -9`
8
+ `ps -ax | grep phantomjs | grep -v grep | awk '{print $1}' | xargs kill -9`
9
9
  end
10
10
 
11
11
  it "Will not exit in pipe mode" do
@@ -19,31 +19,45 @@ describe "CLI" do
19
19
  expect(@finished).to eq(false)
20
20
  end
21
21
 
22
- #it "Will exit in pipe mode if -t is set" do
23
- #@finished = false
24
- #Thread.new do
25
- #`ruby -I./lib ./bin/boojs -t 2`
26
- #@finished = true
27
- #end
28
-
29
- #sleep 2
30
- #expect(@finished).to eq(false)
31
- #sleep 2
32
- #expect(@finished).to eq(true)
33
- #end
34
-
35
- #it "Will exit in pipe mode if -t is set and evaluating script" do
36
- #@finished = false
37
- #Thread.new do
38
- #`ruby -I./lib ./bin/boojs -t 2`
39
- #@finished = true
40
- #end
41
-
42
- #sleep 2
43
- #expect(@finished).to eq(false)
44
- #sleep 2
45
- #expect(@finished).to eq(true)
46
- #end
22
+ it "Will exit in pipe mode if -t is set" do
23
+ @finished = false
24
+ Thread.new do
25
+ system("ruby -I./lib ./bin/boojs -t 2")
26
+ @finished = true
27
+ end
28
+
29
+ expect(@finished).to eq(false)
30
+ sleep 4
31
+ expect(@finished).to eq(true)
32
+ end
33
+
34
+ it "Will exit in execute mode if -t is set and return a 0 value" do
35
+ @finished = false
36
+ Thread.new do
37
+ @return = system %{ruby -I./lib ./bin/boojs -t 2 -e "console.log('hello');"}
38
+ @finished = true
39
+ end
40
+
41
+ expect(@finished).to eq(false)
42
+ sleep 4
43
+ expect(@finished).to eq(true)
44
+
45
+ expect(@return).to eq(true)
46
+ end
47
+
48
+ it "Will exit in execute mode if -t is set and return a 1 value for a bad command" do
49
+ @finished = false
50
+ Thread.new do
51
+ @return = system %{ruby -I./lib ./bin/boojs -t 2 -e "no_such_variable;"}
52
+ @finished = true
53
+ end
54
+
55
+ expect(@finished).to eq(false)
56
+ sleep 4
57
+ expect(@finished).to eq(true)
58
+
59
+ expect(@return).not_to eq(true)
60
+ end
47
61
 
48
62
  it "Replies with pong$KEY when given ping('$key')" do
49
63
  key = SecureRandom.hex
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boojs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.16
4
+ version: 0.0.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - seo