Skip to content
This repository
tree: 3914452278
Fetching contributors…

Cannot retrieve contributors at this time

file 122 lines (94 sloc) 3.282 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
describe "Rake::Pipeline::FileWrapper" do
  let(:file) { Rake::Pipeline::FileWrapper.new }
  let(:root) { File.expand_path("app/assets") }
  let(:jquery) { File.join(root, "javascripts/jquery.js") }

  before do
    file.root = root
    file.path = "javascripts/jquery.js"
  end

  it "is equal to another FileWrapper if the root and path are the same" do
    other = Rake::Pipeline::FileWrapper.new
    other.root = root
    other.path = "javascripts/jquery.js"

    file.should == other
    file.hash.should == other.hash
  end

  it "is not equal to another FileWrapper if the root is the same but the path is different" do
    other = Rake::Pipeline::FileWrapper.new
    other.root = root
    other.path = "javascripts/jquery2.js"

    file.should_not == other
  end

  it "is not equal to another FileWrapper if the root is different but the path is the same" do
    other = Rake::Pipeline::FileWrapper.new
    other.root = "/"
    other.path = "javascripts/jquery.js"

    file.should_not == other
  end

  describe "#fullpath" do
    it "returns the complete path to the file" do
      file.fullpath.should == jquery
    end

    it "raises an error if its root is not a root path" do
      file.root = 'root'
      lambda { file.fullpath }.should raise_error
    end

    it "doesn't raise an error with a unix root path" do
      file.root = '/root'
      lambda { file.fullpath }.should_not raise_error
    end

    it "doesn't raise an error with a windows root path" do
      file.root = 'c:\root'
      lambda { file.fullpath }.should_not raise_error
    end
  end

  it "it knows it doesn't exist when the file doesn't exist" do
    file.exists?.should == false
  end

  it "knows it exists when the file exists" do
    touch_p jquery
    file.exists?.should == true
  end

  it "raises an exception if trying to #read a file that doesn't exist" do
    lambda { file.read }.should raise_error(Errno::ENOENT)
  end

  it "returns the file's body when invoking #read on a file that does exist" do
    touch_p jquery
    body = "This. Is. jQuery!"
    File.open(jquery, "w") { |file| file.write body }

    file.read.should == body
  end

  it "creates a file with #create" do
    new_file = file.create
    new_file.should be_kind_of File

    File.exists?(jquery).should == true

    new_file.close
  end

  it "complains if trying to close an unopened file" do
    lambda { file.close }.should raise_error(IOError)
  end

  it "closes a file created using #create with #close" do
    new_file = file.create
    new_file.closed?.should == false

    file.close
    new_file.closed?.should == true

    lambda { file.close }.should raise_error(IOError)
  end

  it "complains if trying to write to a file that was not created" do
    lambda { file.write "This. Is. jQuery" }.should raise_error(Rake::Pipeline::UnopenedFile)
  end

  it "supports a block form of create that closes the file at the end" do
    file.create do |f|
      f.write "HI"
    end

    File.exists?(jquery).should == true
    File.read(jquery).should == "HI"
    file.closed?.should == true
  end

  it "writes to the file system if the file was created" do
    new_file = file.create
    file.write "This. Is. jQuery"
    new_file.close

    file.read.should == "This. Is. jQuery"
  end
end
Something went wrong with that request. Please try again.