|
| 1 | +require_relative "../../spec_helper" |
| 2 | + |
| 3 | +require "fileutils" |
| 4 | +require "tmpdir" |
| 5 | +require "busser/serverspec/rspec_binary" |
| 6 | + |
| 7 | +describe Busser::Serverspec::RspecBinary do |
| 8 | + def bindir_with_rspec(root, executable: true) |
| 9 | + bin = File.join(root, "bin") |
| 10 | + FileUtils.mkdir_p(bin) |
| 11 | + path = File.join(bin, "rspec") |
| 12 | + File.write(path, "#!/bin/sh\n") |
| 13 | + File.chmod(executable ? 0o755 : 0o644, path) |
| 14 | + path |
| 15 | + end |
| 16 | + |
| 17 | + describe ".candidate_bindirs" do |
| 18 | + it "looks at the running Ruby's bindir before any gem path" do |
| 19 | + dirs = Busser::Serverspec::RspecBinary.candidate_bindirs(["/gems/a"]) |
| 20 | + _(dirs.first).must_equal RbConfig::CONFIG["bindir"] |
| 21 | + end |
| 22 | + |
| 23 | + it "appends bin to each gem path" do |
| 24 | + dirs = Busser::Serverspec::RspecBinary.candidate_bindirs(["/gems/a", "/gems/b"]) |
| 25 | + _(dirs.last(2)).must_equal ["/gems/a/bin", "/gems/b/bin"] |
| 26 | + end |
| 27 | + |
| 28 | + it "copes with no gem paths at all" do |
| 29 | + _(Busser::Serverspec::RspecBinary.candidate_bindirs([])) |
| 30 | + .must_equal [RbConfig::CONFIG["bindir"]] |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + describe ".find" do |
| 35 | + # An empty stand-in for the running Ruby's bindir, so these do not depend on |
| 36 | + # whether the host Ruby happens to have an rspec next to it. |
| 37 | + let(:no_ruby_bin) { Dir.mktmpdir } |
| 38 | + after { FileUtils.remove_entry(no_ruby_bin) if File.directory?(no_ruby_bin) } |
| 39 | + it "returns the rspec in the first gem path that has one" do |
| 40 | + Dir.mktmpdir do |a| |
| 41 | + Dir.mktmpdir do |b| |
| 42 | + bindir_with_rspec(a) |
| 43 | + bindir_with_rspec(b) |
| 44 | + _(Busser::Serverspec::RspecBinary.find([a, b], no_ruby_bin)).must_equal File.join(a, "bin", "rspec") |
| 45 | + end |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + it "skips a gem path with no rspec" do |
| 50 | + Dir.mktmpdir do |empty| |
| 51 | + Dir.mktmpdir do |real| |
| 52 | + bindir_with_rspec(real) |
| 53 | + _(Busser::Serverspec::RspecBinary.find([empty, real], no_ruby_bin)) |
| 54 | + .must_equal File.join(real, "bin", "rspec") |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + # A non-executable file here would be handed to Rake as rspec_path and the |
| 60 | + # run would die with a permission error rather than falling back to PATH. |
| 61 | + it "skips a file that exists but is not executable" do |
| 62 | + Dir.mktmpdir do |a| |
| 63 | + Dir.mktmpdir do |b| |
| 64 | + bindir_with_rspec(a, executable: false) |
| 65 | + bindir_with_rspec(b) |
| 66 | + _(Busser::Serverspec::RspecBinary.find([a, b], no_ruby_bin)).must_equal File.join(b, "bin", "rspec") |
| 67 | + end |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + # nil is meaningful: the caller leaves rspec_path unset and Rake falls back |
| 72 | + # to whatever rspec is on PATH. |
| 73 | + it "returns nil when nothing is found, rather than a bogus path" do |
| 74 | + Dir.mktmpdir { |dir| _(Busser::Serverspec::RspecBinary.find([dir], no_ruby_bin)).must_be_nil } |
| 75 | + end |
| 76 | + end |
| 77 | +end |
0 commit comments