diff options
| -rw-r--r-- | Library/Homebrew/cmd/commands.rb | 26 | ||||
| -rw-r--r-- | Library/Homebrew/test/test_commands.rb | 62 |
2 files changed, 81 insertions, 7 deletions
diff --git a/Library/Homebrew/cmd/commands.rb b/Library/Homebrew/cmd/commands.rb index 6630e7134..88290a43d 100644 --- a/Library/Homebrew/cmd/commands.rb +++ b/Library/Homebrew/cmd/commands.rb @@ -27,18 +27,30 @@ module Homebrew end def internal_commands - (HOMEBREW_LIBRARY_PATH/"cmd").children.select(&:file?).map { |f| f.basename(".rb").to_s } + find_internal_commands HOMEBREW_LIBRARY_PATH/"cmd" end def internal_development_commands - (HOMEBREW_LIBRARY_PATH/"dev-cmd").children.select(&:file?).map { |f| f.basename(".rb").to_s } + find_internal_commands HOMEBREW_LIBRARY_PATH/"dev-cmd" end def external_commands - paths.flat_map { |p| Dir["#{p}/brew-*"] }. - select { |f| File.executable?(f) }. - map { |f| File.basename(f, ".rb")[5..-1] }. - reject { |f| f =~ /\./ }. - sort + paths.reduce([]) do |cmds, path| + Dir["#{path}/brew-*"].each do |file| + next unless File.executable?(file) + cmd = File.basename(file, ".rb")[5..-1] + cmds << cmd unless cmd.include?(".") + end + cmds + end.sort + end + + private + + def find_internal_commands(directory) + directory.children.reduce([]) do |cmds, f| + cmds << f.basename.to_s.sub(/\.(?:rb|sh)$/, "") if f.file? + cmds + end end end diff --git a/Library/Homebrew/test/test_commands.rb b/Library/Homebrew/test/test_commands.rb new file mode 100644 index 000000000..0fb46dd9c --- /dev/null +++ b/Library/Homebrew/test/test_commands.rb @@ -0,0 +1,62 @@ +require "testing_env" +require "cmd/commands" +require "fileutils" + +class CommandsCommandTests < Homebrew::TestCase + def setup + @cmds = [ + # internal commands + HOMEBREW_LIBRARY_PATH/"cmd/rbcmd.rb", + HOMEBREW_LIBRARY_PATH/"cmd/shcmd.sh", + + # internal development commands + HOMEBREW_LIBRARY_PATH/"dev-cmd/rbdevcmd.rb", + HOMEBREW_LIBRARY_PATH/"dev-cmd/shdevcmd.sh", + ] + + @cmds.each { |f| FileUtils.touch f } + end + + def teardown + @cmds.each { |f| f.unlink } + end + + def test_internal_commands + cmds = Homebrew.internal_commands + assert cmds.include?("rbcmd"), "Ruby commands files should be recognized" + assert cmds.include?("shcmd"), "Shell commands files should be recognized" + refute cmds.include?("rbdevcmd"), "Dev commands shouldn't be included" + end + + def test_internal_development_commands + cmds = Homebrew.internal_development_commands + assert cmds.include?("rbdevcmd"), "Ruby commands files should be recognized" + assert cmds.include?("shdevcmd"), "Shell commands files should be recognized" + refute cmds.include?("rbcmd"), "Non-dev commands shouldn't be included" + end + + def test_external_commands + env = ENV.to_hash + + mktmpdir do |dir| + %w[brew-t1 brew-t2.rb brew-t3.py].each do |file| + path = "#{dir}/#{file}" + FileUtils.touch path + FileUtils.chmod 0744, path + end + + FileUtils.touch "#{dir}/t4" + + ENV["PATH"] = "#{ENV["PATH"]}#{File::PATH_SEPARATOR}#{dir}" + cmds = Homebrew.external_commands + + assert cmds.include?("t1"), "Executable files should be included" + assert cmds.include?("t2"), "Executable Ruby files should be included" + refute cmds.include?("t3"), + "Executable files with a non Ruby extension shoudn't be included" + refute cmds.include?("t4"), "Non-executable files shouldn't be included" + end + ensure + ENV.replace(env) + end +end |
