Browse Source

add test coverage and start to split up cask.rb

ahhhhh the joys of refactoring under test coverage
phinze 13 years ago
parent
commit
f35cda322c
  1. 42
      lib/cask.rb
  2. 19
      lib/cask/cli.rb
  3. 2
      lib/cask/cli/list.rb
  4. 2
      lib/cask/cli/search.rb
  5. 28
      lib/cask/scopes.rb
  6. 8
      test/cask_test.rb
  7. 31
      test/cli/list_test.rb
  8. 17
      test/cli/search_test.rb

42
lib/cask.rb

@ -14,9 +14,12 @@ require 'cask/cli/linkapps'
require 'cask/cli/list'
require 'cask/cli/search'
require 'cask/exceptions'
require 'cask/scopes'
require 'plist/parser'
class Cask
include Cask::Scopes
def self.tapspath
HOMEBREW_PREFIX.join "Library", "Taps"
end
@ -24,42 +27,7 @@ class Cask
def self.cellarpath
HOMEBREW_CELLAR
end
def self.all
cask_titles = Dir[tapspath.join("*", "Casks", "*.rb")]
cask_titles.map { |c|
# => "/usr/local/Library/Taps/example-tap/Casks/example.rb"
c.sub! /\.rb$/, ''
# => ".../example"
c = c.split("/").last 3
# => ["example-tap", "Casks", "example"]
c.delete_at 1
# => ["example-tap", "example"]
c = c.join "/"
# => "example-tap/example"
self.load c
c
}
end
def self.nice_listing(cask_list)
casks = {}
cask_list.each { |c|
repo, name = c.split "/"
casks[name] ||= []
casks[name].push repo
}
list = []
casks.each { |name,repos|
if repos.length == 1
list.push name
else
repos.each { |r| list.push [r,name].join "/" }
end
}
list.sort
end
def self.init
HOMEBREW_CACHE.mkpath
HOME_APPS.mkpath
@ -70,10 +38,6 @@ class Cask
end
def homepage; self.class.homepage; end
def self.installed
self.all.select { |c| load(c).installed? }
end
def self.path(cask_title)
cask_title = all.grep(/#{cask_title}$/).first unless cask_title =~ /\//
tapspath.join(cask_title.sub("/", "/Casks/") + ".rb") unless cask_title.nil?

19
lib/cask/cli.rb

@ -17,6 +17,25 @@ class Cask::CLI
lookup_command(command).run(*rest)
end
def self.nice_listing(cask_list)
casks = {}
cask_list.each { |c|
repo, name = c.split "/"
casks[name] ||= []
casks[name].push repo
}
list = []
casks.each { |name,repos|
if repos.length == 1
list.push name
else
repos.each { |r| list.push [r,name].join "/" }
end
}
list.sort
end
class NullCommand
def initialize(attempted_name)
@attempted_name = attempted_name

2
lib/cask/cli/list.rb

@ -1,6 +1,6 @@
class Cask::CLI::List
def self.run(*arguments)
puts_columns Cask.nice_listing(Cask.installed)
puts_columns Cask::CLI.nice_listing(Cask.installed)
end
def self.help

2
lib/cask/cli/search.rb

@ -2,7 +2,7 @@ class Cask::CLI::Search
def self.run(*arguments)
search_term, *rest = *arguments
casks = {}
puts_columns Cask.nice_listing(Cask.all.grep(/#{search_term}/))
puts_columns Cask::CLI.nice_listing(Cask.all.grep(/#{search_term}/))
end
def self.help

28
lib/cask/scopes.rb

@ -0,0 +1,28 @@
module Cask::Scopes
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def all
cask_titles = Dir[tapspath.join("*", "Casks", "*.rb")]
cask_titles.map { |c|
# => "/usr/local/Library/Taps/example-tap/Casks/example.rb"
c.sub! /\.rb$/, ''
# => ".../example"
c = c.split("/").last 3
# => ["example-tap", "Casks", "example"]
c.delete_at 1
# => ["example-tap", "example"]
c = c.join "/"
# => "example-tap/example"
self.load c
c
}
end
def installed
all.select { |c| load(c).installed? }
end
end
end

8
test/cask_test.rb

@ -14,4 +14,12 @@ describe Cask do
}.must_raise(CaskUnavailableError)
end
end
describe "all" do
it "returns every cask that there is as a string" do
all_casks = Cask.all
all_casks.count.must_be :>, 20
all_casks.each { |cask| cask.must_be_kind_of String }
end
end
end

31
test/cli/list_test.rb

@ -0,0 +1,31 @@
require 'test_helper'
describe Cask::CLI::List do
it "lists the installed casks in a pretty fashion" do
Cask.stubs(:installed).returns(%w[
phinze-cask/adium
phinze-cask/google-chrome
])
lambda {
Cask::CLI::List.run
}.must_output <<-OUTPUT.gsub(/^ */, '')
adium
google-chrome
OUTPUT
end
it "lists the taps for casks that show up in two" do
Cask.stubs(:installed).returns(%w[
phinze-cask/adium
phinze-cask/google-chrome
passcod-cask/adium
])
lambda {
Cask::CLI::List.run
}.must_output <<-OUTPUT.gsub(/^ */, '')
google-chrome
passcod-cask/adium
phinze-cask/adium
OUTPUT
end
end

17
test/cli/search_test.rb

@ -0,0 +1,17 @@
require 'test_helper'
describe Cask::CLI::Search do
it "lists the available casks that match the search term" do
Cask.stubs(:all).returns(%w[
phinze-cask/foo
phinze-cask/bar
phinze-cask/baz
])
lambda {
Cask::CLI::Search.run('ba')
}.must_output <<-OUTPUT.gsub(/^ */, '')
bar
baz
OUTPUT
end
end
Loading…
Cancel
Save