Skip to content

Commit a167469

Browse files
committed
Add PATH class.
1 parent e221d04 commit a167469

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

Library/Homebrew/PATH.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class PATH
2+
def initialize(*paths)
3+
@paths = parse(*paths)
4+
end
5+
6+
def prepend(*paths)
7+
@paths.unshift(*parse(*paths))
8+
self
9+
end
10+
11+
def append(*paths)
12+
@paths.concat(parse(*paths))
13+
self
14+
end
15+
16+
def to_ary
17+
@paths
18+
end
19+
alias to_a to_ary
20+
21+
def to_str
22+
@paths.join(File::PATH_SEPARATOR)
23+
end
24+
alias to_s to_str
25+
26+
def eql?(other)
27+
if other.respond_to?(:to_ary)
28+
return true if to_ary == other.to_ary
29+
end
30+
31+
if other.respond_to?(:to_str)
32+
return true if to_str == other.to_str
33+
end
34+
35+
false
36+
end
37+
alias == eql?
38+
39+
def empty?
40+
@paths.empty?
41+
end
42+
43+
def inspect
44+
"<PATH##{to_str}>"
45+
end
46+
47+
def validate
48+
self.class.new(@paths.select(&File.method(:directory?)))
49+
end
50+
51+
private
52+
53+
def parse(*paths)
54+
paths
55+
.flatten
56+
.flat_map { |p| p.respond_to?(:to_str) ? p.to_str.split(File::PATH_SEPARATOR): p }
57+
.compact
58+
.map { |p| p.respond_to?(:to_path) ? p.to_path : p.to_str }
59+
.uniq
60+
end
61+
end

Library/Homebrew/global.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "extend/pathname"
44
require "extend/git_repository"
55
require "extend/ARGV"
6+
require "PATH"
67
require "extend/string"
78
require "os"
89
require "utils"

Library/Homebrew/test/PATH_spec.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
require "PATH"
2+
3+
describe PATH do
4+
describe "#initialize" do
5+
it "can take multiple arguments" do
6+
expect(described_class.new("/path1", "/path2")).to eq("/path1:/path2")
7+
end
8+
9+
it "can parse a mix of arrays and arguments" do
10+
expect(described_class.new(["/path1", "/path2"], "/path3")).to eq("/path1:/path2:/path3")
11+
end
12+
13+
it "splits an existing PATH" do
14+
expect(described_class.new("/path1:/path2")).to eq(["/path1", "/path2"])
15+
end
16+
end
17+
18+
describe "#to_ary" do
19+
it "returns a PATH array" do
20+
expect(described_class.new("/path1", "/path2").to_ary).to eq(["/path1", "/path2"])
21+
end
22+
end
23+
24+
describe "#to_str" do
25+
it "returns a PATH string" do
26+
expect(described_class.new("/path1", "/path2").to_str).to eq("/path1:/path2")
27+
end
28+
end
29+
30+
describe "#prepend" do
31+
it "prepends a path to a PATH" do
32+
expect(described_class.new("/path1").prepend("/path2").to_str).to eq("/path2:/path1")
33+
end
34+
end
35+
36+
describe "#append" do
37+
it "prepends a path to a PATH" do
38+
expect(described_class.new("/path1").append("/path2").to_str).to eq("/path1:/path2")
39+
end
40+
end
41+
42+
describe "#validate" do
43+
it "returns a new PATH without non-existent paths" do
44+
allow(File).to receive(:directory?).with("/path1").and_return(true)
45+
allow(File).to receive(:directory?).with("/path2").and_return(false)
46+
47+
path = described_class.new("/path1", "/path2")
48+
expect(path.validate.to_ary).to eq(["/path1"])
49+
expect(path.to_ary).to eq(["/path1", "/path2"])
50+
end
51+
end
52+
end

0 commit comments

Comments
 (0)