aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test/PATH_spec.rb
blob: 68233c23c7f3bbf81488fd12b0954de8331e174f (plain)
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
require "PATH"

describe PATH do
  describe "#initialize" do
    it "can take multiple arguments" do
      expect(described_class.new("/path1", "/path2")).to eq("/path1:/path2")
    end

    it "can parse a mix of arrays and arguments" do
      expect(described_class.new(["/path1", "/path2"], "/path3")).to eq("/path1:/path2:/path3")
    end

    it "splits an existing PATH" do
      expect(described_class.new("/path1:/path2")).to eq(["/path1", "/path2"])
    end

    it "removes duplicates" do
      expect(described_class.new("/path1", "/path1")).to eq("/path1")
    end
  end

  describe "#to_ary" do
    it "returns a PATH array" do
      expect(described_class.new("/path1", "/path2").to_ary).to eq(["/path1", "/path2"])
    end
  end

  describe "#to_str" do
    it "returns a PATH string" do
      expect(described_class.new("/path1", "/path2").to_str).to eq("/path1:/path2")
    end
  end

  describe "#prepend" do
    it "prepends a path to a PATH" do
      expect(described_class.new("/path1").prepend("/path2").to_str).to eq("/path2:/path1")
    end

    it "removes duplicates" do
      expect(described_class.new("/path1").prepend("/path1").to_str).to eq("/path1")
    end
  end

  describe "#append" do
    it "prepends a path to a PATH" do
      expect(described_class.new("/path1").append("/path2").to_str).to eq("/path1:/path2")
    end

    it "removes duplicates" do
      expect(described_class.new("/path1").append("/path1").to_str).to eq("/path1")
    end
  end

  describe "#insert" do
    it "inserts a path at a given index" do
      expect(described_class.new("/path1").insert(0, "/path2").to_str).to eq("/path2:/path1")
    end

    it "can insert multiple paths" do
      expect(described_class.new("/path1").insert(0, "/path2", "/path3")).to eq("/path2:/path3:/path1")
    end
  end

  describe "#include?" do
    it "returns true if a path is included" do
      path = described_class.new("/path1", "/path2")
      expect(path).to include("/path1")
      expect(path).to include("/path2")
    end

    it "returns false if a path is not included" do
      expect(described_class.new("/path1")).not_to include("/path2")
    end

    it "returns false if the given string contains a separator" do
      expect(described_class.new("/path1", "/path2")).not_to include("/path1:")
    end
  end

  describe "#each" do
    it "loops through each path" do
      enum = described_class.new("/path1", "/path2").each

      expect(enum.next).to eq("/path1")
      expect(enum.next).to eq("/path2")
    end
  end

  describe "#select" do
    it "returns an object of the same class instead of an Array" do
      expect(described_class.new.select { true }).to be_a(described_class)
    end
  end

  describe "#reject" do
    it "returns an object of the same class instead of an Array" do
      expect(described_class.new.reject { true }).to be_a(described_class)
    end
  end

  describe "#existing" do
    it "returns a new PATH without non-existent paths" do
      allow(File).to receive(:directory?).with("/path1").and_return(true)
      allow(File).to receive(:directory?).with("/path2").and_return(false)

      path = described_class.new("/path1", "/path2")
      expect(path.existing.to_ary).to eq(["/path1"])
      expect(path.to_ary).to eq(["/path1", "/path2"])
    end

    it "returns nil instead of an empty #{described_class}" do
      expect(described_class.new.existing).to be nil
    end
  end
end