1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
# frozen_string_literal: true
require "spec_helper"
require "pathname"
require "mock_application"
describe Launchy do
before do
Launchy.reset_global_options
@stderr = $stderr
$stderr = StringIO.new
@stdout = $stdout
$stdout = StringIO.new
@invalid_url = "blah://example.com/invalid"
end
after do
Launchy.reset_global_options
$stderr = @stderr
$stdout = @stdout
end
it "logs to stderr when LAUNCHY_DEBUG environment variable is set" do
ENV["LAUNCHY_DEBUG"] = "true"
old_stderr = $stderr
$stderr = StringIO.new
Launchy.log "This is a test log message"
_($stderr.string.strip).must_equal "LAUNCHY_DEBUG: This is a test log message"
$stderr = old_stderr
ENV["LAUNCHY_DEBUG"] = nil
end
it "sets the global option :dry_run to true if LAUNCHY_DRY_RUN environment variable is 'true'" do
ENV["LAUNCHY_DRY_RUN"] = "true"
Launchy.extract_global_options({})
_(Launchy.dry_run?).must_equal true
ENV["LAUNCHY_DRY_RUN"] = nil
end
it "sets the global option :debug to true if LAUNCHY_DEBUG environment variable is 'true'" do
ENV["LAUNCHY_DEBUG"] = "true"
Launchy.extract_global_options({})
_(Launchy.debug?).must_equal true
ENV["LAUNCHY_DEBUG"] = nil
end
it "has the global option :debug" do
Launchy.extract_global_options({ debug: "true" })
_(Launchy.debug?).must_equal true
Launchy.extract_global_options({ debug: true })
_(Launchy.debug?).must_equal true
end
it "has the global option :dry_run" do
Launchy.extract_global_options({ dry_run: "true" })
_(Launchy.dry_run?).must_equal true
Launchy.extract_global_options({ dry_run: true })
_(Launchy.dry_run?).must_equal true
end
it "has the global option :application" do
Launchy.extract_global_options({ application: "wibble" })
_(Launchy.application).must_equal "wibble"
end
it "has the global option :host_os" do
Launchy.extract_global_options({ host_os: "my-special-os-v2" })
_(Launchy.host_os).must_equal "my-special-os-v2"
end
it "raises an exception if no scheme is found for the given uri" do
_(-> { Launchy.open(@invalid_url) }).must_raise Launchy::ApplicationNotFoundError
end
it "asssumes we open a local file if we have an exception if we have an invalid scheme and a valid path" do
uri = "blah://example.com/#{__FILE__}"
Launchy.open(uri, dry_run: true)
parts = $stdout.string.strip.split
_(parts.size).must_be :>, 1
_(parts.last).must_equal uri
end
it "opens a local file if we have a drive letter and a valid path on windows" do
uri = "C:#{__FILE__}"
Launchy.open(uri, dry_run: true, host_os: "windows")
_($stdout.string.strip).must_equal "start \"launchy\" /b #{uri}"
end
it "opens a data url with a forced browser application" do
uri = "data:text/html,hello%20world"
Launchy.open(uri, dry_run: true, application: "browser")
_($stdout.string.strip).must_match(/open/) # /usr/bin/open or xdg-open
end
it "calls the block if instead of raising an exception if there is an error" do
Launchy.open(@invalid_url) { $stderr.puts "oops had an error opening #{@invalid_url}" }
_($stderr.string.strip).must_equal "oops had an error opening #{@invalid_url}"
end
it "calls the block with the values passed to launchy and the error" do
options = { dry_run: true }
Launchy.open(@invalid_url, dry_run: true) do |e|
$stderr.puts "had an error opening #{@invalid_url} with options #{options}: #{e}"
end
_($stderr.string.strip).must_equal "had an error opening #{@invalid_url} with options #{options}: No application found to handle '#{@invalid_url}'"
end
it "raises the error in the called block" do
_(-> { Launchy.open(@invalid_url) { raise StandardError, "KABOOM!" } }).must_raise StandardError
end
it "can force a specific application to be used" do
result = Launchy.open("http://example.com", application: "mockapplication")
_(result).must_equal "MockApplication opened http://example.com"
end
["www.example.com", "www.example.com/foo/bar", "C:#{__FILE__}"].each do |x|
it "picks a Browser for #{x}" do
app = Launchy.app_for_uri_string(x)
_(app).must_equal(Launchy::Application::Browser)
end
end
it "can use a Pathname as the URI" do
path = Pathname.new(Dir.pwd)
app = Launchy.app_for_uri_string(path)
_(app).must_equal(Launchy::Application::Browser)
end
%w[BROWSER bRoWsEr browser Browser].each do |x|
it "can find the browser by name #{x}" do
app = Launchy.app_for_name(x)
_(app).must_equal(Launchy::Application::Browser)
end
end
end
|