File: which_spec.rb

package info (click to toggle)
ruby-rghost 0.9.9-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,188 kB
  • sloc: ruby: 3,374; makefile: 6; sh: 1
file content (57 lines) | stat: -rw-r--r-- 1,869 bytes parent folder | download
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
# frozen_string_literal: true

RSpec.describe RGhost::Which do
  subject(:which) { described_class.new(env: fake_env) }
  let(:fake_env) { {"PATH" => path.join(File::PATH_SEPARATOR)} }
  let(:path) { ["/tmp/fake_dir"] }
  let(:project_root_dir) { Pathname(__dir__).join("../..") }
  let(:fixture_path) { project_root_dir.join("spec/fixtures").to_path }

  it "finds the executable when it exists on the first entry in $PATH" do
    path.prepend(fixture_path)

    expect(which.call("sweet")).to eq(project_root_dir.join("spec/fixtures/sweet").to_path)
  end

  it "does not find the executable when the file does not have execute permissions" do
    path.prepend(fixture_path)

    expect(which.call("sweet_non_exe")).to be_nil
  end

  it "finds the executable when it exists on a middle entry in $PATH" do
    path.prepend(fixture_path)
    path.prepend("/tmp/some/other/path")

    expect(which.call("sweet")).to eq(project_root_dir.join("spec/fixtures/sweet").to_path)
  end

  it "finds the executable when it exists on the last entry in $PATH" do
    path.prepend("/tmp/some/other/path")
    path.append(fixture_path)

    expect(which.call("sweet")).to eq(project_root_dir.join("spec/fixtures/sweet").to_path)
  end

  it "does not find the executable when the file is not in $PATH" do
    expect(which.call("sweet")).to be_nil
  end

  it "finds the executable when an absolute path is given" do
    absolute_path = project_root_dir.join("spec/fixtures/sweet").to_path

    expect(which.call(absolute_path)).to eq(absolute_path)
  end

  context "on Windows" do
    before do
      path.prepend(fixture_path)

      fake_env["PATHEXT"] = ".exe;.bat;.cmd"
    end

    it "finds the executable with the extensions defined by $PATHEXT" do
      expect(which.call("sweet_win")).to eq(project_root_dir.join("spec/fixtures/sweet_win.bat").to_path)
    end
  end
end