aboutsummaryrefslogtreecommitdiffstats
path: root/Library
diff options
context:
space:
mode:
authorMarkus Reiter2017-04-27 08:48:29 +0200
committerMarkus Reiter2017-04-30 21:11:27 +0200
commita16746906d463ce9e4dc129bc5a76b81585ee1dd (patch)
tree02dfa56a4f8f4355389818121ac1e52ffca74f27 /Library
parente221d0481a8f6bc324c507ece8e698b5f41c2d07 (diff)
downloadbrew-a16746906d463ce9e4dc129bc5a76b81585ee1dd.tar.bz2
Add `PATH` class.
Diffstat (limited to 'Library')
-rw-r--r--Library/Homebrew/PATH.rb61
-rw-r--r--Library/Homebrew/global.rb1
-rw-r--r--Library/Homebrew/test/PATH_spec.rb52
3 files changed, 114 insertions, 0 deletions
diff --git a/Library/Homebrew/PATH.rb b/Library/Homebrew/PATH.rb
new file mode 100644
index 000000000..d6b549a1e
--- /dev/null
+++ b/Library/Homebrew/PATH.rb
@@ -0,0 +1,61 @@
+class PATH
+ def initialize(*paths)
+ @paths = parse(*paths)
+ end
+
+ def prepend(*paths)
+ @paths.unshift(*parse(*paths))
+ self
+ end
+
+ def append(*paths)
+ @paths.concat(parse(*paths))
+ self
+ end
+
+ def to_ary
+ @paths
+ end
+ alias to_a to_ary
+
+ def to_str
+ @paths.join(File::PATH_SEPARATOR)
+ end
+ alias to_s to_str
+
+ def eql?(other)
+ if other.respond_to?(:to_ary)
+ return true if to_ary == other.to_ary
+ end
+
+ if other.respond_to?(:to_str)
+ return true if to_str == other.to_str
+ end
+
+ false
+ end
+ alias == eql?
+
+ def empty?
+ @paths.empty?
+ end
+
+ def inspect
+ "<PATH##{to_str}>"
+ end
+
+ def validate
+ self.class.new(@paths.select(&File.method(:directory?)))
+ end
+
+ private
+
+ def parse(*paths)
+ paths
+ .flatten
+ .flat_map { |p| p.respond_to?(:to_str) ? p.to_str.split(File::PATH_SEPARATOR): p }
+ .compact
+ .map { |p| p.respond_to?(:to_path) ? p.to_path : p.to_str }
+ .uniq
+ end
+end
diff --git a/Library/Homebrew/global.rb b/Library/Homebrew/global.rb
index 108ca0cb7..b0bf647a1 100644
--- a/Library/Homebrew/global.rb
+++ b/Library/Homebrew/global.rb
@@ -3,6 +3,7 @@ require "extend/fileutils"
require "extend/pathname"
require "extend/git_repository"
require "extend/ARGV"
+require "PATH"
require "extend/string"
require "os"
require "utils"
diff --git a/Library/Homebrew/test/PATH_spec.rb b/Library/Homebrew/test/PATH_spec.rb
new file mode 100644
index 000000000..d1b1f074d
--- /dev/null
+++ b/Library/Homebrew/test/PATH_spec.rb
@@ -0,0 +1,52 @@
+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
+ 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
+ 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
+ end
+
+ describe "#validate" 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.validate.to_ary).to eq(["/path1"])
+ expect(path.to_ary).to eq(["/path1", "/path2"])
+ end
+ end
+end