Navigation Menu

Skip to content

Commit

Permalink
All specs now pass.
Browse files Browse the repository at this point in the history
* Pathname from Ruby stdlib is an instant solution to the huge mess of File.blahs/Dir.blehs
* Using supposedly correct way of finding Ruby binary (supports ruby1.9, jruby, ruby.exe etc.).
* All specs standarized on behaviour that a group like:
  file:
  1| line
  2| line

  is always followed by empty line.
  I don't think the last one should - newline should be separator not terminator,
  but this is another issue.
* Reason danlucraft#1 why Ruby is awesome for Unix scripting is that it makes it so easy
  to totally avoid manipulating global state. Changing directory in any
  way other than Dir.chdir(dir){ ... } or exactly once on start/exit is just so shell...
  Current directory is global state of the worst kind - affecting every
  almost every i/o operation. Never do that.
* ENV isn't as bad as chdir, but it's far better to encapsulate it.
  Here oddly Perl pwns Ruby with its local $ENV{A}='B' that restores
  either old value or its absence on exit.
  Ruby code for that would be something horrible like:

  def with_changed_env(key, *args)
    existed, old = ENV.has_key?(key), ENV[key]
    begin
      if args.empty?
        ENV.delete(key)
      else
        ENV[key] = args[0]
      end
      yield
    ensure
      if existed
        ENV[key] = old
      else
        ENV.delete(old)
      end
    end
  end

  But know RAK_TEST doesn't exist when started, so it's simplified.

Anyway there is no global mutable state now left,
and all specs pass.
  • Loading branch information
taw committed Jul 22, 2010
1 parent cf806b5 commit 7fbc3e3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 72 deletions.
2 changes: 1 addition & 1 deletion spec/help_spec.rb
Expand Up @@ -45,6 +45,6 @@
t=<<END
rak: see rak --help for usage.
END
%x{rak foo --asfdasfd}.include?(t).should be_true
%x{rak foo --asfdasfd 2>/dev/null}.include?(t).should be_true
end
end
65 changes: 10 additions & 55 deletions spec/rak_spec.rb
Expand Up @@ -5,18 +5,6 @@
require File.dirname(__FILE__) + "/spec_helpers"

describe "Rak", "with no options" do
before(:all) do
ENV['RAK_TEST'] = "true"
end

before(:each) do
FileUtils.cd(HERE+"/example/")
end

after(:all) do
ENV['RAK_TEST'] = "false"
end

it "prints all matches from files in the current directory" do
asterize_ansi(rak "Cap.ic").should == t=<<END
*foo.rb*
Expand Down Expand Up @@ -45,6 +33,7 @@
*dir1/bar.rb*
2|bar bar bar bar *Pikon* bar
9|bar bar *Pikon* bar bar bar
*foo.rb*
6|foo foo foo foo foo *Pikon* foo foo
8|foo *Pikon* foo foo foo foo foo foo
Expand All @@ -70,12 +59,10 @@
end

it "changes defaults when redirected" do
ENV['RAK_TEST'] = "false"
asterize_ansi(rak "Six | cat").should == t=<<END
asterize_ansi(rak("Six | cat", :test_mode => false)).should == t=<<END
foo.rb:10:foo foo Six foo foo foo Six foo
foo.rb:11:foo foo foo foo Six foo foo foo
END
ENV['RAK_TEST'] = "true"
end

it "searches shebangs for valid inputs" do
Expand All @@ -90,19 +77,12 @@
asterize_ansi(rak "Canceron").should == t=<<END
*Rakefile*
1|rakefile rakefile *Canceron* rakefile
END
end
end

describe "Rak", "with FILE or STDIN inputs" do
before(:all) do
ENV['RAK_TEST'] = "true"
end

after(:all) do
ENV['RAK_TEST'] = "false"
end

it "should only search in given files or directories" do
asterize_ansi(rak "Pikon foo.rb").should == t=<<END
6|foo foo foo foo foo *Pikon* foo foo
Expand All @@ -112,31 +92,24 @@
dir1/bar.rb
2|bar bar bar bar Pikon bar
9|bar bar Pikon bar bar bar
END
end

it "should search in STDIN by default if no files are specified" do
asterize_ansi(%x{cat _darcs/baz.rb | #{rak_bin} Aere}).should == t=<<END
asterize_ansi(rak "Aere", :pipe => "cat _darcs/baz.rb").should == t=<<END
2|baz baz baz *Aere*lon baz baz baz
END
end

it "only searches STDIN when piped to" do
asterize_ansi(%x{echo asdfCapasdf | #{rak_bin} Cap}).should == t=<<END
asterize_ansi(rak "Cap", :pipe => "echo asdfCapasdf").should == t=<<END
1|asdf*Cap*asdf
END
end
end

describe "Rak", "options" do
before(:all) do
ENV['RAK_TEST'] = "true"
end

after(:all) do
ENV['RAK_TEST'] = "false"
end

it "prints only files with --files" do
t=<<END
Rakefile
Expand Down Expand Up @@ -325,6 +298,7 @@
asterize_ansi(rak "Libris -a").should == t=<<END
*qux*
1|qux qux qux *Libris* qux qux qux
END

end
Expand Down Expand Up @@ -463,6 +437,7 @@
*dir1/bar.rb*
2|bar bar bar bar *Pikon* bar
9|bar bar *Pikon* bar bar bar
END
end

Expand Down Expand Up @@ -493,25 +468,12 @@
end

it "should not recurse down '..' when used with . " do
FileUtils.cd(HERE+"/example/dir1/")
asterize_ansi(rak "foo .").should == t=<<END
asterize_ansi(rak("foo .", :dir=>HERE+"example/dir1")).should == t=<<END
END
end
end

describe "Rak", "with combinations of options" do
before(:all) do
ENV['RAK_TEST'] = "true"
end

before(:each)do
FileUtils.cd(HERE+"/example/")
end

after(:all) do
ENV['RAK_TEST'] = "false"
end

it "should process -c -v " do
t1=<<END
quux.py:1
Expand All @@ -524,18 +486,11 @@
end

it "-h and redirection" do
ENV['RAK_TEST'] = "false"
(rak "Pik -h | cat").should == t=<<END
(rak("Pik -h | cat", :test_mode => false)).should == t=<<END
bar bar bar bar Pikon bar
bar bar Pikon bar bar bar
foo foo foo foo foo Pikon foo foo
foo Pikon foo foo foo foo foo foo
END
ENV['RAK_TEST'] = "true"
end
end





36 changes: 20 additions & 16 deletions spec/spec_helpers.rb
@@ -1,7 +1,7 @@

require 'fileutils'
require "pathname"

HERE = File.expand_path(File.dirname(__FILE__))
HERE = Pathname(__FILE__).parent.expand_path

def strip_ansi(str)
str.gsub /\033\[(\d;)?\d+m/, ""
Expand All @@ -11,24 +11,28 @@ def asterize_ansi(str)
str.gsub /(\033\[(\d;)?\d+m)+/, "*"
end

def exe(str)
sys(str+">tmp")
File.read("tmp")
end

def rak_bin
Config::CONFIG["bindir"] + "/ruby #{File.dirname(__FILE__) + "/../bin/rak"}"
def ruby_bin
File.join(
Config::CONFIG["bindir"],
Config::CONFIG["ruby_install_name"] + Config::CONFIG["EXEEXT"]
)
end

def rak(argstring)
cmd = "#{rak_bin} #{argstring}"
result = %x{#{cmd}}
# p [:result, result]
result
def rak(argstring, opts={})
begin
bin_rak = HERE.parent.join("bin/rak")
cmd = "#{ruby_bin} #{bin_rak} #{argstring}"
cmd = "#{opts[:pipe]} | #{cmd}" if opts[:pipe]
ENV['RAK_TEST'] = "true" unless opts[:test_mode] == false
dir = opts[:dir] || HERE+"example"
Dir.chdir(dir) do
%x{#{cmd}}
end
ensure
ENV.delete('RAK_TEST')
end
end

FileUtils.cd("spec/example")

def sort_lines(str)
str.split("\n").sort.join("\n")
end

0 comments on commit 7fbc3e3

Please sign in to comment.