diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5fff1d9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +pkg diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..80765be --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2009 Eric Budd + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.markdown b/README.markdown deleted file mode 100644 index ecfe933..0000000 --- a/README.markdown +++ /dev/null @@ -1,62 +0,0 @@ -Grep-Fu -======= - -Grep-Fu is a very fast, Rails-oriented command-line helper script for grep. It's a ruby wrapper for grep for speeding up text searches within the files of a Rails project. The simplest, common usage: - - grep-fu "def account_deletion" - -*IMPORTANT:* Grep-Fu will only work as expected if you are in the root directory of a Rails project! - -It's a standalone script, so you can just drop it in any PATHed directory, chmod 777 it (or 700, for the paranoid) and go to town. - -Even Faster ------------ - -For more targeted (faster) searches, you can specify one of the following flags to narrow down the search: - - a - app - m - app/models - c - app/controllers - v - app/views - h - app/helpers - - l - lib - - p - public - css - public/stylesheets - js - public/javascripts - - s - spec - t - test - - vp - vendor/plugins - mig - db/migrate - -So to search only your helpers for the term "helpless": - - grep-fu h helpless - -Multiple word searches should be surrounded by quotes: - - grep-fu s "it should be tested" - -Running grep-fu without a search will show all available options. - -I want to see what it found! ----------------------------- - -For more detail, you can add the '--verbose' flag to command: - - grep-fu c budget_dragon --verbose - -This will output the filename, line number, and found line. This should be used for fairly narrow searches, as it can produce a whole lot of output. - -Thanks go out to [Scotty Moon](http://github.com/scottymoon) for this feature. - -Technical mumbo-jumbo ---------------------- - -Grep-Fu speeds up the searching process by only searching the files you care about. It does this by constructing a find for grepping which "prunes" unwanted directories, such as logs and vendor plugins. Unfortunately, find's prune options are some of the ugliest in the CLI world. Using Ruby allows us to construct a giant ugly command that does exactly what we want. - - - diff --git a/README.md b/README.md new file mode 100644 index 0000000..fabeb14 --- /dev/null +++ b/README.md @@ -0,0 +1,181 @@ +Grep-Fu +======= + +Grep-Fu is a very fast, Rails-oriented command-line helper script for grep. It's a ruby wrapper for speeding up text searches within the files of a Rails project. The simplest, common usage: + + grep-fu account_deletion + +This will display a list of files which contain the search text: + + ./app/models/account.rb + ./app/controllers/accounts_controller.rb + +*NOTE:* Grep-Fu will only work as expected if you are in the root directory of a Rails project! + +Installation +------------ + +Grep-Fu is now a gem: + + gem install grep-fu + +Often, it can help reduce typing by aliasing the admittedly lengthy "grep-fu" command: + + alias g='grep-fu' + +Putting this (or a similar shortcut) in your .bashrc or .profile will allow you to use a shorthand version of the command: + + g account_deletion + +Even Faster +----------- + +For more targeted (faster) searches, you can specify one of the following flags to narrow down the search: + + a - app + m - app/models + c - app/controllers + v - app/views + h - app/helpers + + l - lib + + p - public + css - public/stylesheets + js - public/javascripts + + s - spec + t - test + + vp - vendor/plugins + mig - db/migrate + +So to search only your helpers for the term "helpless": + + grep-fu h helpless + +Multiple word searches and searches containing special regex characters should be surrounded by quotes: + + grep-fu s "should be tested" + + grep-fu "^[^\?] fishy fishy fishy fish$" + +Running grep-fu without parameters will show you what options are available. + +Off the Beaten Path +---------------------------- + +Occasionally, you'll want to search a directory that's not defined in one of the targeted paths, but you don't want to scan the entire Rails project. In that case, you can provide a specific directory to grep: + + grep-fu lib/tasks troweler + +You can even step up out of the current directory. Grep-Fu will use any path shortcuts your underlying OS recognizes. + + grep-fu ../../different_rails_project "def similar_method" + + grep-fu ~/Projects/roger_tracking last_known_location + +I want to see what it found! +---------------------------- + +For more detail, you can add the '--verbose' flag to command: + + grep-fu c budget_dragon --verbose + +This will output the filename, line number, and found line. + + ./app/model/budget.rb:18: + budget_dragon # Bob in accounting's been drinking again + +This should be used for fairly narrow searches, as it can produce a whole lot of output. + +Thanks go out to [Scotty Moon](http://github.com/scottymoon) for this feature. + +Colors +------ + +If you'd like to see grep-fu's output in color, add the '--color' flag: + + grep-fu mig ExtraBiggened --color --verbose + +Under most color schemes, the --color option is only useful if --verbose is used as well. + +If you'd rather have color all the time, you can change the setting COLOR_ON_BY_DEFAULT in the code to true. If you do this, then you can use the '--no-color' flag to disable the feature: + + grep-fu mig DoublePlusUnBiggened --no-color + +Thanks go out to [Joshua French](http://github.com/osake) for this feature. + +Single-Line Output +------------------ + +Sometimes you need to output all the files grep-fu finds onto a single line; for example, when piping the list into another command: + + grep-fu "# Pipe me!" --single-line + +The list of files with matches will display on a single line: + + ./test/unit/calamity_test.rb ./test/unit/havoc_test.rb ./test/unit/mayhem_test.rb... + +This is handy if you want to go ahead and load all the files it finds into a text editor. + + gvim -p `gx "#TODO"` + +Ignoring More Stuff +--------------------- + +When performing an untargeted search, Grep-fu determines which directories to skip by using the PRUNE_PATHS constant. If you have a directory you'd like to skip searching by default, simply add its path (relative to the Rails root) to PRUNE_PATHS. + +For example, if you have an unusually large set of migrations, you might want to step over them by default. Change the line + + PRUNE_PATHS = ['/.svn', '/.git', '/vendor', '/log', '/public', '/tmp', '/coverage'] + +to + + PRUNE_PATHS = ['/.svn', '/.git', '/vendor', '/log', '/public', '/tmp', '/coverage', '/db/migrations'] + +And migrations will no longer be searched. Note that targeted searches and specified directories always override the PRUNE_PATHS option. + +Version Information +------------------- + +Passing the '--version' flag to grep-fu will return the current version. + + grep-fu --version + +returns + + grep-fu 0.5.0 + +Technical mumbo-jumbo +--------------------- + +Grep-Fu speeds up the searching process by only searching the files you care about. It does this by constructing a "find" command which is piped into grep. Find "prunes" the following search directories: + + * public + * logs + * vendor + * tmp + * coverage + * .svn + * .git + +Note that all these directories are still searchable with targeted searches or a directory specification; they're simply not searched by default + +Grep-fu also ignores files larger than 100K, which tend to be unsearchable binaries or SQL dumps. + +The pruning options for find are some of the ugliest in the CLI world. Using Ruby allows us to construct a giant, hideous command that does exactly what we need. + +Note on Patches/Pull Requests +----------------------------- + +* Fork the project. +* Make your feature addition or bug fix. +* Commit, do not mess with rakefile, version, or history. + (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull) +* Send me a pull request. Bonus points for topic branches. + +Copyright +--------- + +Copyright (c) 2010 Eric Budd. See LICENSE for details. diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..e3fc80f --- /dev/null +++ b/Rakefile @@ -0,0 +1,57 @@ +require 'rubygems' +require 'rake' +require 'spec/rake/spectask' + +begin + require 'jeweler' + Jeweler::Tasks.new do |gem| + gem.name = "grep-fu" + gem.summary = %Q{Grep-Fu is a very fast, Rails-oriented command-line helper script for grep.} + gem.description = %Q{Grep-Fu is a very fast, Rails-oriented command-line helper script for grep.} + gem.email = "calamitous@calamitylane.com" + gem.homepage = "http://github.com/Calamitous/grep-fu" + gem.authors = ["Eric Budd"] + end + Jeweler::GemcutterTasks.new +rescue LoadError + puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler" +end + +require 'rake/testtask' +Rake::TestTask.new(:test) do |test| + test.libs << 'lib' << 'test' + test.pattern = 'test/**/test_*.rb' + test.verbose = true +end + +begin + require 'rcov/rcovtask' + Rcov::RcovTask.new do |test| + test.libs << 'test' + test.pattern = 'test/**/test_*.rb' + test.verbose = true + end +rescue LoadError + task :rcov do + abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov" + end +end + +desc "Run all specs" +Spec::Rake::SpecTask.new('run_specs') do |t| + t.spec_files = FileList['spec/**/*.rb'] +end + +task :test => [:check_dependencies, :run_specs] + +task :default => :test + +require 'rake/rdoctask' +Rake::RDocTask.new do |rdoc| + version = File.exist?('VERSION') ? File.read('VERSION') : "" + + rdoc.rdoc_dir = 'rdoc' + rdoc.title = "grep-fu #{version}" + rdoc.rdoc_files.include('README*') + rdoc.rdoc_files.include('lib/**/*.rb') +end diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..8f0916f --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.5.0 diff --git a/bin/grep-fu b/bin/grep-fu new file mode 100755 index 0000000..5de0491 --- /dev/null +++ b/bin/grep-fu @@ -0,0 +1,5 @@ +#!/usr/bin/env ruby +$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib') +require 'grep-fu' + +GrepFu::run!(ARGV) diff --git a/grep-fu b/grep-fu deleted file mode 100755 index 23b7352..0000000 --- a/grep-fu +++ /dev/null @@ -1,50 +0,0 @@ -#!/usr/bin/env ruby - -PATH_REPLACEMENTS = { - 'a' => 'app', - 'c' => 'app/controllers', - 'h' => 'app/helpers', - 'm' => 'app/models', - 'v' => 'app/views', - 'l' => 'lib', - 'p' => 'public', - 'css' => 'public/stylesheets', - 'js' => 'public/javascripts', - 't' => 'test', - 's' => 'spec', - 'vp' => 'vendor/plugins', - 'mig' => 'db/migrate' - } - -PRUNE_PATHS = ['/.svn', '/.git', '/vendor', '/log', '/public/yui', '/tmp', '~'] -options = '-ril' - -unless ARGV.size > 0 - puts "\nUsage: #{__FILE__} [findpath] search_string [--verbose]\n - Where findpath is one of the following: - any literal subdirectory -#{PATH_REPLACEMENTS.map { |abbr, txt| " #{abbr} - #{txt}" }.join("\n")}\n\n" - exit -end - -clargs = ARGV -verbose = (clargs -= ['--verbose'] if clargs.include?('--verbose')) -color = (clargs -= ['--color'] if clargs.include?('--color')) -options = '-rin' if verbose -options << ' --color=always' if color - -search_criteria = clargs.last -file_path = clargs.size == 2 ? PATH_REPLACEMENTS[clargs.first] || clargs.first : './' -delicious_prunes = PRUNE_PATHS ? "-path '*#{PRUNE_PATHS.join("' -prune -o -path *'")}' -prune -o" : '' - -find_command = "find #{file_path} #{delicious_prunes} \\( -size -100000c -type f \\) -print0 | xargs -0 grep #{options} \"#{search_criteria}\"" - -if verbose - `#{find_command}`.each_line do |found| - file_and_line = found.slice!(/^.*?:.*?:/) - puts "#{file_and_line}\n\t#{found.strip}" - end -else - puts `#{find_command}` -end - diff --git a/grep-fu.gemspec b/grep-fu.gemspec new file mode 100644 index 0000000..6b40be6 --- /dev/null +++ b/grep-fu.gemspec @@ -0,0 +1,59 @@ +# Generated by jeweler +# DO NOT EDIT THIS FILE DIRECTLY +# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command +# -*- encoding: utf-8 -*- + +Gem::Specification.new do |s| + s.name = %q{grep-fu} + s.version = "0.5.0" + + s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= + s.authors = ["Eric Budd"] + s.date = %q{2010-06-16} + s.default_executable = %q{grep-fu} + s.description = %q{Grep-Fu is a very fast, Rails-oriented command-line helper script for grep.} + s.email = %q{calamitous@calamitylane.com} + s.executables = ["grep-fu"] + s.extra_rdoc_files = [ + "LICENSE", + "README.md" + ] + s.files = [ + ".gitignore", + "LICENSE", + "README.md", + "Rakefile", + "VERSION", + "bin/grep-fu", + "grep-fu.gemspec", + "lib/grep-fu.rb", + "lib/grep-fu/find_builder.rb", + "lib/grep-fu/options.rb", + "spec/find_builder_spec.rb", + "spec/grep_fu_spec.rb", + "spec/options_spec.rb", + "spec/spec_helper.rb" + ] + s.homepage = %q{http://github.com/Calamitous/grep-fu} + s.rdoc_options = ["--charset=UTF-8"] + s.require_paths = ["lib"] + s.rubygems_version = %q{1.3.6} + s.summary = %q{Grep-Fu is a very fast, Rails-oriented command-line helper script for grep.} + s.test_files = [ + "spec/find_builder_spec.rb", + "spec/options_spec.rb", + "spec/spec_helper.rb", + "spec/grep_fu_spec.rb" + ] + + if s.respond_to? :specification_version then + current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION + s.specification_version = 3 + + if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then + else + end + else + end +end + diff --git a/lib/grep-fu.rb b/lib/grep-fu.rb new file mode 100644 index 0000000..51fc80b --- /dev/null +++ b/lib/grep-fu.rb @@ -0,0 +1,26 @@ +require 'grep-fu/options' +require 'grep-fu/find_builder' + +module GrepFu + def self.run!(args = []) + unless args.size > 0 + puts Options.usage(__FILE__) + return + end + + options = Options.new(args) + + find_command = FindBuilder.find_command(options) + + if options.verbose + `#{find_command}`.each_line do |found| + file_and_line = found.slice!(/^.*?:.*?:/) + puts "#{file_and_line}\n\t#{found.strip}" + end + elsif options.single_line + puts `#{find_command}`.map { |found| found.chomp }.join(' ') + else + puts `#{find_command}` + end + end +end diff --git a/lib/grep-fu/find_builder.rb b/lib/grep-fu/find_builder.rb new file mode 100644 index 0000000..d92fe98 --- /dev/null +++ b/lib/grep-fu/find_builder.rb @@ -0,0 +1,21 @@ +module GrepFu + class FindBuilder + PRUNE_PATHS = ['/.svn', '/.git', '/vendor', '/log', + '/public', '/tmp', '/coverage', '/doc'] + + def self.delicious_prunes + "-path '*#{PRUNE_PATHS.join("' -prune -o -path '*")}' -prune -o" + end + + def self.find_command(options) + [ + 'find', + options.file_path, + delicious_prunes, + "\\( -size -100000c -type f \\) -print0 | xargs -0 grep", + options.to_s, + "\"#{options.search_criteria}\"" + ].join(' ') + end + end +end diff --git a/lib/grep-fu/options.rb b/lib/grep-fu/options.rb new file mode 100644 index 0000000..40bad07 --- /dev/null +++ b/lib/grep-fu/options.rb @@ -0,0 +1,62 @@ +module GrepFu + class Options + attr_reader :verbose, :single_line, :color, :search_criteria, :file_path + + PATH_REPLACEMENTS = { + 'a' => 'app', + 'c' => 'app/controllers', + 'd' => 'doc', + 'h' => 'app/helpers', + 'm' => 'app/models', + 'v' => 'app/views', + 'l' => 'lib', + 'p' => 'public', + 'css' => 'public/stylesheets', + 'js' => 'public/javascripts', + 't' => 'test', + 's' => 'spec', + 'vp' => 'vendor/plugins', + 'mig' => 'db/migrate' + } + + COLOR_ON_BY_DEFAULT = false + + def self.usage(file) + lines = ['', + "Usage: #{file} [findpath] search_string [--verbose|--single-line]\n", + " Where findpath is one of the following:", + " any literal subdirectory", + Options::PATH_REPLACEMENTS.map { |abbr, txt| " #{abbr} - #{txt}" }.join("\n"), + "\n\n" + ] + + lines.join("\n") + end + + def initialize(args) + if args.include?('--version') + puts "grep-fu #{File.read(File.join(File.dirname(__FILE__), '..', '..', 'VERSION'))}" + exit(0) + end + + @verbose = (args -= ['--verbose'] if args.include?('--verbose')) + @single_line = (args -= ['--single-line'] if args.include?('--single-line')) + + @color = (args -= ['--color'] if (args.include?('--color')) || COLOR_ON_BY_DEFAULT) + if args.include?('--no-color') + args -= ['--no-color'] + @color = false + end + + @search_criteria = args.last + @file_path = args.size == 2 ? PATH_REPLACEMENTS[args.first] || args.first : './' + end + + def to_s + options = @verbose ? '-rin' : '-ril' + options << ' --color=always' if @color + options + end + + end +end diff --git a/spec/find_builder_spec.rb b/spec/find_builder_spec.rb new file mode 100644 index 0000000..02f0527 --- /dev/null +++ b/spec/find_builder_spec.rb @@ -0,0 +1,58 @@ +require 'grep-fu/find_builder' +require 'spec/spec_helper' + +describe GrepFu::FindBuilder do + describe "#delicious_prunes" do + it "should create command-line options for pruning path" do + silence_warnings { GrepFu::FindBuilder::PRUNE_PATHS = ['/pruney'] } + GrepFu::FindBuilder.delicious_prunes.should == "-path '*/pruney' -prune -o" + end + + it "should create command-line options for multiple pruning paths" do + silence_warnings { GrepFu::FindBuilder::PRUNE_PATHS = ['/pruney', '/apply'] } + GrepFu::FindBuilder.delicious_prunes.should == "-path '*/pruney' -prune -o -path '*/apply' -prune -o" + end + end + + describe "#find_command" do + before(:each) do + GrepFu::FindBuilder.stub!(:delicious_prunes).and_return('prune_list') + @options = mock(Object, + :file_path => 'path', + :to_s => 'str_opts', + :search_criteria => 'crits') + @find_command = GrepFu::FindBuilder.find_command(@options) + end + + it "should start with the find command" do + @find_command.should =~ /^find\s/ + end + + it "should contain the file path" do + @find_command.should =~ /\spath\s/ + end + + it "should contain all the pruned directories" do + @find_command.should =~ /\sprune_list\s/ + end + + it "should contain a blob of bash muck" do + @find_command.should =~ /\s.. -size -100000c -type f .. -print0 | xargs -0 grep\s/ + end + + it "should contain the options" do + @find_command.should =~ /\sstr_opts\s/ + end + + it "should end with the quoted search criteria" do + @find_command.should =~ /\s"crits"$/ + end + + it "should stitch together a long, long find command" do + @find_command.should =~ /find path prune_list .. -size -100000c -type f .. -print0 | xargs -0 grep str_opts \"crits\"/ + end + + end + +end + diff --git a/spec/grep_fu_spec.rb b/spec/grep_fu_spec.rb new file mode 100644 index 0000000..cb7c6b8 --- /dev/null +++ b/spec/grep_fu_spec.rb @@ -0,0 +1,76 @@ +require 'grep-fu' +require 'spec/spec_helper' + +describe GrepFu do + describe "#run!" do + before(:each) do + GrepFu.stub!(:puts) + end + + describe "with no arguments provided" do + it "should print the usage and exit if no arguments are provided" do + GrepFu::Options.should_receive(:usage).and_return('Usage: etc.') + GrepFu.should_receive(:puts).with('Usage: etc.') + GrepFu.run! + end + + it "should exit early if no arguments are provided" do + GrepFu::Options.should_not_receive(:new) + GrepFu.run! + end + end + + describe "with valid arguments" do + before(:each) do + @options = mock(GrepFu::Options, :verbose => false, :single_line => false) + @find_command = "find me" + GrepFu::Options.stub!(:new).and_return(@options) + GrepFu::FindBuilder.stub!(:find_command).and_return(@find_command) + GrepFu.stub!(:'`') + end + + it "should create options from provided arguments" do + GrepFu::Options.should_receive(:new).with('arguments!') + GrepFu.run!('arguments!') + end + + it "should build a find command from options" do + GrepFu::FindBuilder.should_receive(:find_command).with(@options) + GrepFu.run!('arguments!') + end + + it "should display the results of the find" do + GrepFu.should_receive(:'`').with(@find_command) + GrepFu.run!(['stuff_to_find']) + end + + it "should appropriately format results for single-line requests" do + @options.stub!(:single_line).and_return(true) + GrepFu.stub!(:'`').and_return("line one\nline two\nlast line\n") + GrepFu.should_receive(:puts).with('line one line two last line') + GrepFu.run!(['--single-line']) + end + + it "should appropriately format results for verbose requests" do + @options.stub!(:verbose).and_return(true) + verbose_returns = [ + "app/views/mailer/new_notification.text.plain.erb:1:A new notification has arrived!", + "README:45: although it's not clear who might have", + "app/models/funge.rb:22: Hugenot.new" + ].join("\n") + + formatted_returns = [ + "app/views/mailer/new_notification.text.plain.erb:1:\n\tA new notification has arrived!", + "README:45:\n\talthough it's not clear who might have", + "app/models/funge.rb:22:\n\tHugenot.new" + ] + + GrepFu.stub!(:'`').and_return(verbose_returns) + GrepFu.should_receive(:puts).with(formatted_returns[0]) + GrepFu.should_receive(:puts).with(formatted_returns[1]) + GrepFu.should_receive(:puts).with(formatted_returns[2]) + GrepFu.run!(['not', '--verbose']) + end + end + end +end diff --git a/spec/options_spec.rb b/spec/options_spec.rb new file mode 100644 index 0000000..483c103 --- /dev/null +++ b/spec/options_spec.rb @@ -0,0 +1,55 @@ +require 'grep-fu/options' +require 'spec/spec_helper' + +describe GrepFu::Options do + it "should instantiate correctly" do + GrepFu::Options.new([]) + end + + it "should set the verbose option to true, if provided" do + GrepFu::Options.new(['--verbose']).verbose.should be_true + end + + it "should set the single-line option to true, if provided" do + GrepFu::Options.new(['--single-line']).single_line.should be_true + end + + it "should set the color option to true, if provided" do + GrepFu::Options.new(['--color']).color.should be_true + end + + it "should default to no color" do + GrepFu::Options.new([]).color.should be_false + end + + it "should allow color to be disabled from the command line" do + GrepFu::Options.new(['--no_color']).color.should be_false + end + + it "should change color default based on constant" do + silence_warnings { GrepFu::Options::COLOR_ON_BY_DEFAULT = true } + GrepFu::Options.new([]).color.should be_true + end + + it "should allow color to be disabled from the command line when default is color=true" do + silence_warnings { GrepFu::Options::COLOR_ON_BY_DEFAULT = true } + GrepFu::Options.new(['--no-color']).color.should be_false + end + + it "should set the search criteria to the last non-option flag in the list" do + GrepFu::Options.new(%w{offer}).search_criteria.should == 'offer' + GrepFu::Options.new(%w{a help --color}).search_criteria.should == 'help' + GrepFu::Options.new(%w{lib/tasks unrake --verbose --color}).search_criteria.should == 'unrake' + end + + it "should expand the file_path to one of the replacement variables, if found" do + GrepFu::Options.new(%w{a value}).file_path.should == 'app' + GrepFu::Options.new(%w{s hoozits}).file_path.should == 'spec' + GrepFu::Options.new(%w{mig down --color}).file_path.should == 'db/migrate' + end + + it "should accept an explicit find path, if provided" do + GrepFu::Options.new(%w{lib/tasks value}).file_path.should == 'lib/tasks' + end + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..7ab6c90 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,6 @@ +def silence_warnings + old_verbose, $VERBOSE = $VERBOSE, nil + yield +ensure + $VERBOSE = old_verbose +end