Skip to content

Commit 7809b62

Browse files
committed
spec changes
0 parents  commit 7809b62

File tree

6 files changed

+260
-0
lines changed

6 files changed

+260
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/*

Rakefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require "rake/testtask"
2+
3+
task :default => :test
4+
Rake::TestTask.new do |t|
5+
t.pattern = "test/*test.rb"
6+
t.verbose = false
7+
end
8+
9+
task :build do
10+
system "mkdir -p dist"
11+
system "gem build browserstack-local.gemspec"
12+
system "mv browserstack-local-*.gem dist"
13+
system "gem install ./dist/browserstack-local-*.gem"
14+
end

browserstack-local.gemspec

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Gem::Specification.new do |s|
2+
s.name = 'browserstack-local'
3+
s.version = '0.0.0'
4+
s.date = '2016-02-10'
5+
s.summary = "BrowserStack Local"
6+
s.description = "Local wrapper for BrowserStack Tunnel"
7+
s.authors = [""]
8+
s.email = ''
9+
s.files = ["lib/browserstack/local.rb", "lib/browserstack/localbinary.rb"]
10+
s.homepage =
11+
'http://rubygems.org/gems/browserstack-local'
12+
s.license = 'MIT'
13+
end
14+

lib/browserstack/local.rb

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
require 'browserstack/localbinary'
2+
3+
module BrowserStack
4+
5+
class Local
6+
attr_reader :pid
7+
8+
def initialize(key = nil, binary_path = nil)
9+
@key = key
10+
@binary_path = if binary_path.nil?
11+
BrowserStack::LocalBinary.new.binary_path
12+
else
13+
binary_path
14+
end
15+
end
16+
17+
def add_args(key, value=nil)
18+
if key == "key"
19+
@key = value
20+
elsif key == "v" && value.to_s != "false"
21+
@verbose_flag = "-v"
22+
elsif key == "force" && value.to_s != "false"
23+
@force_flag = "-force"
24+
elsif key == "only" && value.to_s != "false"
25+
@only_flag = "-only"
26+
elsif key == "onlyAutomate" && value.to_s != "false"
27+
@only_automate_flag = "-onlyAutomate"
28+
elsif key == "forcelocal" && value.to_s != "false"
29+
@force_local_flag = "-forcelocal"
30+
elsif key == "localIdentifier"
31+
@local_identifier_flag = "-localIdentifier '#{value}'"
32+
elsif key == "f"
33+
@folder_flag = "-f"
34+
@folder_path = "'#{value}'"
35+
elsif key == "proxyHost"
36+
@proxy_host = "-proxyHost '#{value}'"
37+
elsif key == "proxyPort"
38+
@proxy_port = "-proxyPort #{value}"
39+
elsif key == "proxyUser"
40+
@proxy_user = "-proxyUser '#{value}'"
41+
elsif key == "proxyPass"
42+
@proxy_pass = "-proxyPass '#{value}'"
43+
elsif key == "hosts"
44+
@hosts = value
45+
end
46+
end
47+
48+
def start(options = {})
49+
options.each_pair do |key, value|
50+
self.add_args(key, value)
51+
end
52+
53+
@process = IO.popen(command, "w+")
54+
55+
while true
56+
line = @process.readline
57+
break if line.nil?
58+
if line.match(/\*\*\* Error\:/)
59+
@process.close
60+
raise BrowserStack::LocalException.new(line)
61+
return
62+
end
63+
if line.strip == "Press Ctrl-C to exit"
64+
@pid = @process.pid
65+
return
66+
end
67+
end
68+
69+
while true
70+
break if self.isRunning
71+
sleep 1
72+
end
73+
end
74+
75+
def isRunning
76+
resp = Net::HTTP.get(URI.parse("http://localhost:45691/check")) rescue nil
77+
resp && !resp.match(/running/i).nil?
78+
end
79+
80+
def stop
81+
return if @pid.nil?
82+
Process.kill("INT", @pid)
83+
@process.close
84+
end
85+
86+
def command
87+
"#{@binary_path} #{@folder_flag} #{@key} #{@folder_path} #{@force_local_flag} #{@local_identifier_flag} #{@only_flag} #{@only_automate_flag} #{@proxy_host} #{@proxy_port} #{@proxy_user} #{@proxy_pass} #{@force_flag} #{@verbose_flag} #{@hosts}".strip
88+
end
89+
end
90+
91+
class LocalException < Exception
92+
end
93+
94+
end

lib/browserstack/localbinary.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require 'net/http'
2+
require 'rbconfig'
3+
require 'openssl'
4+
5+
module BrowserStack
6+
7+
class LocalBinary
8+
def initialize
9+
host_os = RbConfig::CONFIG['host_os']
10+
@http_path = case host_os
11+
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
12+
@windows = true
13+
"https://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal.exe"
14+
when /darwin|mac os/
15+
"https://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-darwin-x64"
16+
when /linux/
17+
if 1.size == 8
18+
"https://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-linux-x64"
19+
else
20+
"https://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-linux-ia32"
21+
end
22+
end
23+
end
24+
25+
def download
26+
dest_parent_dir = File.join(File.expand_path('~'), '.browserstack')
27+
unless File.exists? dest_parent_dir
28+
Dir.mkdir dest_parent_dir
29+
end
30+
uri = URI.parse(@http_path)
31+
binary_path = File.join(dest_parent_dir, "BrowserStackLocal#{".exe" if @windows}")
32+
http = Net::HTTP.new(uri.host, uri.port)
33+
http.use_ssl = true
34+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
35+
http.request_get(uri.path) do |res|
36+
file = open(binary_path, 'w')
37+
res.read_body do |chunk|
38+
file.write(res.body)
39+
end
40+
file.close
41+
FileUtils.chmod "+x", binary_path
42+
end
43+
binary_path
44+
end
45+
46+
def binary_path
47+
dest_parent_dir = File.join(File.expand_path('~'), '.browserstack')
48+
binary_path = File.join(dest_parent_dir, "BrowserStackLocal#{".exe" if @windows}")
49+
if File.exists? binary_path
50+
binary_path
51+
else
52+
download
53+
end
54+
end
55+
end
56+
57+
end

test/browserstack-local-test.rb

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
require 'rubygems'
2+
require 'minitest/autorun'
3+
require 'browserstack/local'
4+
5+
class BrowserStackLocalTest < Minitest::Test
6+
def setup
7+
@bs_local = BrowserStack::Local.new(ENV["BROWSERSTACK_ACCESS_KEY"])
8+
end
9+
10+
def test_check_pid
11+
@bs_local.start
12+
refute_nil @bs_local.pid, 0
13+
end
14+
15+
def test_is_running
16+
@bs_local.start
17+
assert_equal true, @bs_local.isRunning
18+
end
19+
20+
def test_multiple_binary
21+
@bs_local.start
22+
bs_local_2 = BrowserStack::Local.new(ENV["BROWSERSTACK_ACCESS_KEY"])
23+
assert_raises BrowserStack::LocalException do
24+
bs_local_2.start
25+
end
26+
end
27+
28+
def test_enable_verbose
29+
@bs_local.add_args('v')
30+
assert_match /\-v/, @bs_local.command
31+
end
32+
33+
def test_set_folder
34+
@bs_local.add_args 'f', "/"
35+
assert_match /\-f/, @bs_local.command
36+
assert_match /\'\/\'/, @bs_local.command
37+
end
38+
39+
def test_enable_force
40+
@bs_local.add_args "force"
41+
assert_match /\-force/, @bs_local.command
42+
end
43+
44+
def test_enable_only
45+
@bs_local.add_args "only"
46+
assert_match /\-only/, @bs_local.command
47+
end
48+
49+
def test_enable_only_automate
50+
@bs_local.add_args "onlyAutomate"
51+
assert_match /\-onlyAutomate/, @bs_local.command
52+
end
53+
54+
def test_enable_force_local
55+
@bs_local.add_args "forcelocal"
56+
assert_match /\-forcelocal/, @bs_local.command
57+
end
58+
59+
def test_set_local_identifier
60+
@bs_local.add_args "localIdentifier", "randomString"
61+
assert_match /\-localIdentifier \'randomString\'/, @bs_local.command
62+
end
63+
64+
def test_set_proxy
65+
@bs_local.add_args "proxyHost", "localhost"
66+
@bs_local.add_args "proxyPort", 8080
67+
@bs_local.add_args "proxyUser", "user"
68+
@bs_local.add_args "proxyPass", "pass"
69+
assert_match /\-proxyHost \'localhost\' \-proxyPort 8080 \-proxyUser \'user\' \-proxyPass \'pass\'/, @bs_local.command
70+
end
71+
72+
def test_hosts
73+
@bs_local.add_args "hosts", "localhost,8080,0"
74+
assert_match /localhost\,8080\,0/, @bs_local.command
75+
end
76+
77+
def teardown
78+
@bs_local.stop
79+
end
80+
end

0 commit comments

Comments
 (0)