require File.expand_path(File.dirname(__FILE__) + '/spec_helper') describe "Manifest" do before :each do @mprod = Slinky::Manifest.new("/src", @config, :devel => false, :build_to => "/build") @mdevel = Slinky::Manifest.new("/src", @config) @md_prod = @mprod.manifest_dir @md_devel = @mdevel.manifest_dir end context "General" do it "should build manifest dir with all files in current dir" do @md_prod.files.collect{|f| f.source}.should == ["/src/test.haml"] @md_devel.files.collect{|f| f.source}.should == ["/src/test.haml"] end it "should build manifest with all files in fs" do @mprod.files.collect{|f| f.source }.sort.should == @files.collect{|x| "/src/" + x}.sort @mdevel.files.collect{|f| f.source }.sort.should == @files.collect{|x| "/src/" + x}.sort end it "should not include dot files" do File.open("/src/.index.haml.swp", "w+"){|f| f.write("x")} manifest = Slinky::Manifest.new("/src", @config) manifest.files.map{|x| x.source}.include?("/src/.index.haml.swp").should == false end it "should be able to compute a hash for the entire manifest" do m = @mdevel hash1 = m.md5 File.open("/src/hello.html", "w+") {|f| f.write("Hell!") } m.add_all_by_path(["/src/hello.html"]) m.md5.should_not == hash1 end it "should find files in the manifest by path" do @mdevel.find_by_path("test.haml").first.source.should == "/src/test.haml" @mdevel.find_by_path("asdf.haml").first.should == nil @mdevel.find_by_path("l1/l2/test.txt").first.source.should == "/src/l1/l2/test.txt" @mdevel.find_by_path("l1/test.css").first.source.should == "/src/l1/test.sass" l1 = @mdevel.manifest_dir.children.find{|c| c.dir == "/src/l1"} l1.find_by_path("../test.haml").first.source.should == "/src/test.haml" end it "should produce the correct scripts string for production" do @mprod.scripts_string.should match \ %r!! end it "should produce the correct scripts string for devel" do @mdevel.scripts_string.should == [ '', '', '', '', ''].join("\n") end it "should produce the correct styles string for production" do @mprod.styles_string.should match \ %r!! end it "should produce the correct styles string for development" do File.open("/src/l1/l2/bad.sass", "w+"){|f| f.write "require('../test.sass')\ncolor: red;" } manifest = Slinky::Manifest.new("/src", @config) @mdevel.styles_string.should == [ '', ''].join("\n") end it "should allow the creation of ManifestFiles" do mf = Slinky::ManifestFile.new("/src/test.haml", "", @mprod) end it "should correctly determine output_path" do mf = Slinky::ManifestFile.new("/src/test.haml", "/src/build", @mprod) mf.output_path.to_s.should == "/src/test.html" mf = Slinky::ManifestFile.new("/src/l1/test.js", "/src/build", @mprod) mf.output_path.to_s.should == "/src/l1/test.js" end it "should correctly determine relative_output_path" do mf = Slinky::ManifestFile.new("/src/test.haml", "/src/build", @mprod) mf.relative_output_path.to_s.should == "test.html" mf = Slinky::ManifestFile.new("/src/l1/test.js", "/src/build", @mprod) mf.relative_output_path.to_s.should == "l1/test.js" end it "should build tmp file without directives" do original = "/src/test.haml" mf = Slinky::ManifestFile.new("/src/test.haml", "/src/build", @mprod) path = mf.handle_directives mf.source File.read(original).match(/slinky_scripts|slinky_styles/).should_not == nil File.read(path).match(/slinky_scripts|slinky_styles/).should == nil end it "should build tmp file without directives for .js" do original = "/src/l1/test.js" mf = Slinky::ManifestFile.new("/src/l1/test.js", "/src/build", @mprod) path = mf.handle_directives mf.source File.read(original).match(/slinky_require/).should_not == nil File.read(path).match(/slinky_require/).should == nil end it "should compile files that need it" do $stdout.should_receive(:puts).with("Compiled /src/test.haml".foreground(:green)) mf = Slinky::ManifestFile.new("/src/test.haml", "/src/build", @mprod) FileUtils.mkdir("/src/build") build_path = mf.process mf.build_to build_path.to_s.split(".")[-1].should == "html" File.read("/src/test.haml").match("
").should == nil File.read(build_path).match("").should_not == nil $stdout.should_receive(:puts).with("Compiled /src/test.haml".foreground(:green)) mf = Slinky::ManifestFile.new("/src/test.haml", "/src/build", @mprod) build_path = mf.process "/src/build/test.html" File.read("/src/build/test.html").match("").should_not == nil FileUtils.rm_rf("/src/build") rescue nil end it "should give the proper error message for compilers with unmet dependencies" do File.open("/src/test.fake", "w+"){|f| f.write("hello, fake data")} $stderr.should_receive(:puts).with(/Missing dependency/) mf = Slinky::ManifestFile.new("/src/test.fake", "/src/build", @mprod) build_path = mf.process end it "should report errors for bad files" do File.open("/src/l1/l2/bad.sass", "w+"){|f| f.write "color: red;" } mf = Slinky::ManifestFile.new("/src/l1/l2/bad.sass", "/src/build", @mprod) expect { mf.process }.to raise_error Slinky::BuildFailedError end it "shouldn't crash on syntax errors" do File.open("/src/l1/asdf.haml", "w+"){|f| f.write("%h1{:width => 50px}") } mf = Slinky::ManifestFile.new("/src/l1/asdf.haml", "/src/build", @mprod) expect { mf.process }.to raise_error Slinky::BuildFailedError end it "should properly determine build directives" do mf = Slinky::ManifestFile.new("/src/test.haml", "/src/build", @mprod) mf.find_directives.should == {:slinky_scripts => [], :slinky_styles => []} mf = Slinky::ManifestFile.new("/src/l1/test.js", "/src/build", @mprod) mf.find_directives.should == {:slinky_require => ["test2.js", "l2/test3.js"]} end it "should properly find depends directives" do File.open("/src/depends.sass", "w+") {|f| f.write('// slinky_depends("/something.sass")') } mf = Slinky::ManifestFile.new("/src/depends.sass", "/src/build", @mprod) mf.find_directives.should == {:slinky_depends => ["/something.sass"]} end it "should properly determine build_to path" do mf = Slinky::ManifestFile.new("/src/test.haml", "/src/build", @mprod) mf.build_to.should == Pathname.new("/src/build/test.html") mf = Slinky::ManifestFile.new("/src/l1/test.js", "/src/build", @mprod) mf.build_to.should == Pathname.new("/src/build/test.js") end it "should build both compiled files and non-compiled files" do $stdout.should_receive(:puts).with("Compiled /src/test.haml".foreground(:green)) mf = Slinky::ManifestFile.new("/src/test.haml", "/src/build", @mprod) path = mf.build path.to_s.should == "/src/build/test.html" File.read("/src/test.haml").match("").should == nil File.read(path).match("").should_not == nil $stdout.should_not_receive(:puts) mf = Slinky::ManifestFile.new("/src/l1/l2/test.txt", "/src/build/l1/l2", @mprod) path = mf.build path.to_s.should == "/src/build/l1/l2/test.txt" File.read("/src/build/l1/l2/test.txt").should == "hello\n" end it "should match files" do mf = Slinky::ManifestFile.new("/src/l1/l2/test.txt", "/src/build/l1/l2", @mprod) mf.matches?("test.txt").should == true mf = Slinky::ManifestFile.new("/src/l1/test.sass", "", @mprod) mf.matches?("test.css").should == true mf.matches?("test.sass").should == true end it "should match file paths" do mf = Slinky::ManifestFile.new("/src/l1/l2/test.txt", "/src/build/l1/l2", @mprod) mf.matches_path?("test.txt").should == false mf.matches_path?("/l1/l2/test.txt").should == true mf.matches_path?("/l1/l2/*.txt").should == false mf.matches_path?("/l1/l2/*.txt", true).should == true mf = Slinky::ManifestFile.new("/src/l1/test.sass", "", @mprod) mf.matches_path?("/l1/test.css").should == true mf.matches_path?("/l1/test.sass").should == true mf.matches_path?("/l1/*.css", true).should == true mf.matches_path?("/l1/*.sass", true).should == true mf.matches_path?("l1/test.sass").should == false end it "should should properly determine if in tree" do mf = Slinky::ManifestFile.new("/src/l1/l2/test.txt", "/src/build/l1/l2", @mprod) mf.in_tree?("/l1").should == true mf.in_tree?("/l1/l2").should == true mf.in_tree?("/l1/l2/test.txt").should == true mf.in_tree?("/l1/l3").should == false mf.in_tree?("test.txt").should == false end it "should correctly build the dependency graph" do @mprod.dependency_graph.collect{|x| x.collect{|y| y.source}}.sort.should == [["/src/l1/test2.js", "/src/l1/test.js"], ["/src/l1/l2/test3.coffee", "/src/l1/test.js"], ["/src/l1/test5.js", "/src/l1/test2.js"], ["/src/l1/l2/test6.js", "/src/l1/l2/test3.coffee"]].sort end it "should fail if a required file isn't in the manifest" do FileUtils.rm("/src/l1/test2.js") manifest = Slinky::Manifest.new("/src", @config, :devel => false, :build_to => "/build") proc { manifest.dependency_graph }.should raise_error Slinky::FileNotFoundError end it "should build a correct dependency list" do @mprod.dependency_list.collect{|x| x.source}.should == ["/src/test.haml", "/src/l1/test5.js", "/src/l1/test2.js", "/src/l1/l2/test6.js", "/src/l1/l2/test3.coffee", "/src/l1/test.js", "/src/l1/test.sass", "/src/l1/l2/test.txt", "/src/l1/l2/test2.css", "/src/l1/l2/l3/test2.txt"] end it "should fail if there is a cycle in the dependency graph" do File.open("/src/l1/test5.js", "w+"){|f| f.write("slinky_require('test.js')")} manifest = Slinky::Manifest.new("/src", @config, :devel => false, :build_to => "/build") proc { manifest.dependency_list }.should raise_error Slinky::DependencyError end it "should handle depends directives" do File.open("/src/l1/test5.coffee", "w+"){|f| f.write("slinky_depends('test.sass')")} manifest = Slinky::Manifest.new("/src", @config, :devel => true) f = manifest.find_by_path("l1/test5.js").first f.should_not == nil $stdout.should_receive(:puts).with(/Compiled \/src\/l1\/test.sass/) $stdout.should_receive(:puts).with(/Compiled \/src\/l1\/test5.coffee/) f.process end it "should handle external depends directives" do Dir.mkdir("/external") File.open("/external/file.js", "w+"){|f| f.write("Hello!") } File.open("/src/l1/test5.coffee", "w+"){|f| f.write("slinky_depends_external('../external/*.js')")} manifest = Slinky::Manifest.new("/src", @config, :devel => true) f = manifest.find_by_path("l1/test5.js").first f.should_not == nil f.external_dependencies.should == ["/external/file.js"] f.external_dependencies_updated?.should == true $stdout.should_receive(:puts).with(/Compiled \/src\/l1\/test5.coffee/) f.process f.external_dependencies_updated?.should == false end it "should handle depends directives with glob patterns" do File.open("/src/l1/test5.coffee", "w+"){|f| f.write("slinky_depends('*.sass')")} File.open("/src/l1/test2.sass", "w+"){|f| f.write("body\n\tcolor: red")} manifest = Slinky::Manifest.new("/src", @config, :devel => true) f = manifest.find_by_path("l1/test5.js").first f.should_not == nil $stdout.should_receive(:puts).with(/Compiled \/src\/l1\/test.sass/) $stdout.should_receive(:puts).with(/Compiled \/src\/l1\/test2.sass/) $stdout.should_receive(:puts).with(/Compiled \/src\/l1\/test5.coffee/) f.process end it "should handle depends directives with **" do File.open("/src/l1/test5.coffee", "w+"){|f| f.write("slinky_depends('/l1/**/*.sass')")} File.open("/src/l1/l2/test5.sass", "w+"){|f| f.write("body\n\tcolor: red")} manifest = Slinky::Manifest.new("/src", @config, :devel => true) f = manifest.find_by_path("l1/test5.js").first f.should_not == nil $stdout.should_receive(:puts).with(/Compiled \/src\/l1\/test.sass/) $stdout.should_receive(:puts).with(/Compiled \/src\/l1\/l2\/test5.sass/) $stdout.should_receive(:puts).with(/Compiled \/src\/l1\/test5.coffee/) f.process end it "should handle depends directives with infinite loops" do File.open("/src/l1/test5.coffee", "w+"){|f| f.write("slinky_depends('*.sass')")} File.open("/src/l1/test2.sass", "w+"){|f| f.write("/* slinky_depends('*.coffee')")} manifest = Slinky::Manifest.new("/src", @config, :devel => true) f = manifest.find_by_path("l1/test5.js").first f.should_not == nil $stdout.should_receive(:puts).with(/Compiled \/src\/l1\/test.sass/) $stdout.should_receive(:puts).with(/Compiled \/src\/l1\/test2.sass/) $stdout.should_receive(:puts).with(/Compiled \/src\/l1\/test5.coffee/) f.process end it "should handle depends directives in config" do cf "/depend/script/vendor/backbone.js" cf "/depend/script/vendor/jquery.js" cf "/depend/script/vendor/underscore.js" config = <