diff options
| author | Markus Reiter | 2017-04-28 12:39:00 +0200 | 
|---|---|---|
| committer | Markus Reiter | 2017-04-30 21:11:28 +0200 | 
| commit | 22f624b373d77ede5c15db6f62672bdd81e6c9da (patch) | |
| tree | 2e74f0456b0d8e9707b19c545735208f939c00fe | |
| parent | e70f2ec33233422b70db047338aa85d9e2088042 (diff) | |
| download | brew-22f624b373d77ede5c15db6f62672bdd81e6c9da.tar.bz2 | |
Make `PATH` enumerable.
| -rw-r--r-- | Library/Homebrew/PATH.rb | 10 | ||||
| -rw-r--r-- | Library/Homebrew/cmd/sh.rb | 2 | ||||
| -rw-r--r-- | Library/Homebrew/diagnostic.rb | 3 | ||||
| -rw-r--r-- | Library/Homebrew/global.rb | 2 | ||||
| -rw-r--r-- | Library/Homebrew/requirement.rb | 2 | ||||
| -rw-r--r-- | Library/Homebrew/test/PATH_spec.rb | 35 | ||||
| -rw-r--r-- | Library/Homebrew/utils.rb | 6 | 
7 files changed, 52 insertions, 8 deletions
| diff --git a/Library/Homebrew/PATH.rb b/Library/Homebrew/PATH.rb index 6c5cd9e41..0bee1a5f2 100644 --- a/Library/Homebrew/PATH.rb +++ b/Library/Homebrew/PATH.rb @@ -1,4 +1,9 @@  class PATH +  include Enumerable +  extend Forwardable + +  def_delegator :@paths, :each +    def initialize(*paths)      @paths = parse(*paths)    end @@ -13,6 +18,11 @@ class PATH      self    end +  def insert(index, *paths) +    @paths = parse(*@paths.insert(index, *paths)) +    self +  end +    def to_ary      @paths    end diff --git a/Library/Homebrew/cmd/sh.rb b/Library/Homebrew/cmd/sh.rb index 1e86784cb..69f329cb3 100644 --- a/Library/Homebrew/cmd/sh.rb +++ b/Library/Homebrew/cmd/sh.rb @@ -23,7 +23,7 @@ module Homebrew      ENV.setup_build_environment      if superenv?        # superenv stopped adding brew's bin but generally users will want it -      ENV["PATH"] = PATH.new(PATH.new(ENV["PATH"]).to_a.insert(1, HOMEBREW_PREFIX/"bin")) +      ENV["PATH"] = PATH.new(ENV["PATH"]).insert(1, HOMEBREW_PREFIX/"bin")      end      ENV["PS1"] = 'brew \[\033[1;32m\]\w\[\033[0m\]$ '      ENV["VERBOSE"] = "1" diff --git a/Library/Homebrew/diagnostic.rb b/Library/Homebrew/diagnostic.rb index 7657af193..1022e5d1d 100644 --- a/Library/Homebrew/diagnostic.rb +++ b/Library/Homebrew/diagnostic.rb @@ -100,8 +100,7 @@ module Homebrew        # See https://github.com/Homebrew/legacy-homebrew/pull/9986        def check_path_for_trailing_slashes -        all_paths = PATH.new(ENV["PATH"]).to_a -        bad_paths = all_paths.select { |p| p[-1..-1] == "/" } +        bad_paths = PATH.new(ENV["PATH"]).select { |p| p[-1..-1] == "/" }          return if bad_paths.empty?          inject_file_list bad_paths, <<-EOS.undent diff --git a/Library/Homebrew/global.rb b/Library/Homebrew/global.rb index c73d66c58..877253072 100644 --- a/Library/Homebrew/global.rb +++ b/Library/Homebrew/global.rb @@ -56,7 +56,7 @@ HOMEBREW_PULL_OR_COMMIT_URL_REGEX = %r[https://github\.com/([\w-]+)/([\w-]+)?/(?  require "compat" unless ARGV.include?("--no-compat") || ENV["HOMEBREW_NO_COMPAT"]  ENV["HOMEBREW_PATH"] ||= ENV["PATH"] -ORIGINAL_PATHS = PATH.new(ENV["HOMEBREW_PATH"]).to_a.map do |p| +ORIGINAL_PATHS = PATH.new(ENV["HOMEBREW_PATH"]).map do |p|    begin      Pathname.new(p).expand_path    rescue diff --git a/Library/Homebrew/requirement.rb b/Library/Homebrew/requirement.rb index 6146382a3..6c20e7917 100644 --- a/Library/Homebrew/requirement.rb +++ b/Library/Homebrew/requirement.rb @@ -96,7 +96,7 @@ class Requirement      # PATH.      parent = satisfied_result_parent      return unless parent -    return if PATH.new(ENV["PATH"]).to_a.include?(parent.to_s) +    return if PATH.new(ENV["PATH"]).include?(parent.to_s)      ENV.append_path("PATH", parent)    end diff --git a/Library/Homebrew/test/PATH_spec.rb b/Library/Homebrew/test/PATH_spec.rb index 7e42670f2..07caae617 100644 --- a/Library/Homebrew/test/PATH_spec.rb +++ b/Library/Homebrew/test/PATH_spec.rb @@ -51,6 +51,41 @@ describe PATH do      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 "#validate" do      it "returns a new PATH without non-existent paths" do        allow(File).to receive(:directory?).with("/path1").and_return(true) diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 98a6ce6da..257d24784 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -293,7 +293,7 @@ def quiet_system(cmd, *args)  end  def which(cmd, path = ENV["PATH"]) -  PATH.new(path).to_a.each do |p| +  PATH.new(path).each do |p|      begin        pcmd = File.expand_path(cmd, p)      rescue ArgumentError @@ -307,7 +307,7 @@ def which(cmd, path = ENV["PATH"])  end  def which_all(cmd, path = ENV["PATH"]) -  PATH.new(path).to_a.map do |p| +  PATH.new(path).map do |p|      begin        pcmd = File.expand_path(cmd, p)      rescue ArgumentError @@ -416,7 +416,7 @@ def nostdout  end  def paths(env_path = ENV["PATH"]) -  @paths ||= PATH.new(env_path).to_a.collect do |p| +  @paths ||= PATH.new(env_path).collect do |p|      begin        File.expand_path(p).chomp("/")      rescue ArgumentError | 
