Skip to content

Commit 22f624b

Browse files
committed
Make PATH enumerable.
1 parent e70f2ec commit 22f624b

File tree

7 files changed

+52
-8
lines changed

7 files changed

+52
-8
lines changed

Library/Homebrew/PATH.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
class PATH
2+
include Enumerable
3+
extend Forwardable
4+
5+
def_delegator :@paths, :each
6+
27
def initialize(*paths)
38
@paths = parse(*paths)
49
end
@@ -13,6 +18,11 @@ def append(*paths)
1318
self
1419
end
1520

21+
def insert(index, *paths)
22+
@paths = parse(*@paths.insert(index, *paths))
23+
self
24+
end
25+
1626
def to_ary
1727
@paths
1828
end

Library/Homebrew/cmd/sh.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def sh
2323
ENV.setup_build_environment
2424
if superenv?
2525
# superenv stopped adding brew's bin but generally users will want it
26-
ENV["PATH"] = PATH.new(PATH.new(ENV["PATH"]).to_a.insert(1, HOMEBREW_PREFIX/"bin"))
26+
ENV["PATH"] = PATH.new(ENV["PATH"]).insert(1, HOMEBREW_PREFIX/"bin")
2727
end
2828
ENV["PS1"] = 'brew \[\033[1;32m\]\w\[\033[0m\]$ '
2929
ENV["VERBOSE"] = "1"

Library/Homebrew/diagnostic.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ def check_for_installed_developer_tools
100100

101101
# See https://github.com/Homebrew/legacy-homebrew/pull/9986
102102
def check_path_for_trailing_slashes
103-
all_paths = PATH.new(ENV["PATH"]).to_a
104-
bad_paths = all_paths.select { |p| p[-1..-1] == "/" }
103+
bad_paths = PATH.new(ENV["PATH"]).select { |p| p[-1..-1] == "/" }
105104
return if bad_paths.empty?
106105

107106
inject_file_list bad_paths, <<-EOS.undent

Library/Homebrew/global.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def raise_deprecation_exceptions?
5656
require "compat" unless ARGV.include?("--no-compat") || ENV["HOMEBREW_NO_COMPAT"]
5757

5858
ENV["HOMEBREW_PATH"] ||= ENV["PATH"]
59-
ORIGINAL_PATHS = PATH.new(ENV["HOMEBREW_PATH"]).to_a.map do |p|
59+
ORIGINAL_PATHS = PATH.new(ENV["HOMEBREW_PATH"]).map do |p|
6060
begin
6161
Pathname.new(p).expand_path
6262
rescue

Library/Homebrew/requirement.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def modify_build_environment
9696
# PATH.
9797
parent = satisfied_result_parent
9898
return unless parent
99-
return if PATH.new(ENV["PATH"]).to_a.include?(parent.to_s)
99+
return if PATH.new(ENV["PATH"]).include?(parent.to_s)
100100
ENV.append_path("PATH", parent)
101101
end
102102

Library/Homebrew/test/PATH_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,41 @@
5151
end
5252
end
5353

54+
describe "#insert" do
55+
it "inserts a path at a given index" do
56+
expect(described_class.new("/path1").insert(0, "/path2").to_str).to eq("/path2:/path1")
57+
end
58+
59+
it "can insert multiple paths" do
60+
expect(described_class.new("/path1").insert(0, "/path2", "/path3")).to eq("/path2:/path3:/path1")
61+
end
62+
end
63+
64+
describe "#include?" do
65+
it "returns true if a path is included" do
66+
path = described_class.new("/path1", "/path2")
67+
expect(path).to include("/path1")
68+
expect(path).to include("/path2")
69+
end
70+
71+
it "returns false if a path is not included" do
72+
expect(described_class.new("/path1")).not_to include("/path2")
73+
end
74+
75+
it "returns false if the given string contains a separator" do
76+
expect(described_class.new("/path1", "/path2")).not_to include("/path1:")
77+
end
78+
end
79+
80+
describe "#each" do
81+
it "loops through each path" do
82+
enum = described_class.new("/path1", "/path2").each
83+
84+
expect(enum.next).to eq("/path1")
85+
expect(enum.next).to eq("/path2")
86+
end
87+
end
88+
5489
describe "#validate" do
5590
it "returns a new PATH without non-existent paths" do
5691
allow(File).to receive(:directory?).with("/path1").and_return(true)

Library/Homebrew/utils.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def quiet_system(cmd, *args)
293293
end
294294

295295
def which(cmd, path = ENV["PATH"])
296-
PATH.new(path).to_a.each do |p|
296+
PATH.new(path).each do |p|
297297
begin
298298
pcmd = File.expand_path(cmd, p)
299299
rescue ArgumentError
@@ -307,7 +307,7 @@ def which(cmd, path = ENV["PATH"])
307307
end
308308

309309
def which_all(cmd, path = ENV["PATH"])
310-
PATH.new(path).to_a.map do |p|
310+
PATH.new(path).map do |p|
311311
begin
312312
pcmd = File.expand_path(cmd, p)
313313
rescue ArgumentError
@@ -416,7 +416,7 @@ def nostdout
416416
end
417417

418418
def paths(env_path = ENV["PATH"])
419-
@paths ||= PATH.new(env_path).to_a.collect do |p|
419+
@paths ||= PATH.new(env_path).collect do |p|
420420
begin
421421
File.expand_path(p).chomp("/")
422422
rescue ArgumentError

0 commit comments

Comments
 (0)