DRY up the StaticFile tests a bit. #3633.
This commit is contained in:
parent
28a1d2445e
commit
f86727c45a
|
@ -17,3 +17,4 @@ coverage
|
||||||
tmp/*
|
tmp/*
|
||||||
.jekyll-metadata
|
.jekyll-metadata
|
||||||
/vendor
|
/vendor
|
||||||
|
/test/source/file_name.txt
|
||||||
|
|
|
@ -1,95 +1,86 @@
|
||||||
require 'helper'
|
require 'helper'
|
||||||
|
|
||||||
class TestStaticFile < JekyllUnitTest
|
class TestStaticFile < JekyllUnitTest
|
||||||
def make_dummy_file(file_name)
|
def make_dummy_file(filename)
|
||||||
temp_file = File.new(file_name, "w")
|
File.write(source_dir(filename), "some content")
|
||||||
temp_file.puts("some content")
|
end
|
||||||
temp_file.close
|
|
||||||
|
def modify_dummy_file(filename)
|
||||||
|
offset = "some content".size
|
||||||
|
File.write(source_dir(filename), "more content", offset)
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_dummy_file(filename)
|
||||||
|
File.delete(source_dir(filename))
|
||||||
|
end
|
||||||
|
|
||||||
|
def setup_static_file(base, dir, name)
|
||||||
|
StaticFile.new(@site, base, dir, name)
|
||||||
|
end
|
||||||
|
|
||||||
|
context "A StaticFile" do
|
||||||
|
setup do
|
||||||
|
clear_dest
|
||||||
|
@old_pwd = Dir.pwd
|
||||||
|
Dir.chdir source_dir
|
||||||
|
@site = fixture_site
|
||||||
|
@filename = "static_file.txt"
|
||||||
|
make_dummy_file(@filename)
|
||||||
|
@static_file = setup_static_file(nil, nil, @filename)
|
||||||
end
|
end
|
||||||
|
|
||||||
def modify_dummy_file(file_name)
|
teardown do
|
||||||
temp_file = File.open(file_name, "w")
|
remove_dummy_file(@filename) if File.exist?(source_dir(@filename))
|
||||||
temp_file.puts("more content")
|
Dir.chdir @old_pwd
|
||||||
temp_file.close
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_dummy_file(file_name)
|
should "have a source file path" do
|
||||||
File.delete(file_name)
|
static_file = setup_static_file("root", "dir", @filename)
|
||||||
|
assert_equal "root/dir/#{@filename}", static_file.path
|
||||||
end
|
end
|
||||||
|
|
||||||
def setup_static_file(base, dir, name)
|
should "ignore a nil base or dir" do
|
||||||
@static_file = StaticFile.new(@site, base, dir, name)
|
assert_equal "dir/#{@filename}", setup_static_file(nil, "dir", @filename).path
|
||||||
|
assert_equal "base/#{@filename}", setup_static_file("base", nil, @filename).path
|
||||||
end
|
end
|
||||||
|
|
||||||
context "A StaticFile" do
|
should "have a destination relative directory without a collection" do
|
||||||
setup do
|
static_file = setup_static_file("root", "dir/subdir", "file.html")
|
||||||
clear_dest
|
assert "dir/subdir", static_file.destination_rel_dir
|
||||||
@site = Site.new(site_configuration)
|
|
||||||
end
|
|
||||||
|
|
||||||
should "have a source file path" do
|
|
||||||
static_file = setup_static_file("root", "dir", "file_name.html")
|
|
||||||
assert_equal "root/dir/file_name.html", static_file.path
|
|
||||||
end
|
|
||||||
|
|
||||||
should "have a destination relative directory without a collection" do
|
|
||||||
static_file = setup_static_file("root", "dir/subdir", "file.html")
|
|
||||||
assert "dir/subdir", static_file.destination_rel_dir
|
|
||||||
end
|
|
||||||
|
|
||||||
should "know its last modification time" do
|
|
||||||
file_name = "file.html"
|
|
||||||
static_file = setup_static_file(nil, nil, "file.html")
|
|
||||||
make_dummy_file(file_name)
|
|
||||||
assert_equal Time.new.to_i, static_file.mtime
|
|
||||||
remove_dummy_file(file_name)
|
|
||||||
end
|
|
||||||
|
|
||||||
should "known if the source path is modified, when it is" do
|
|
||||||
file_name = "file_name.txt"
|
|
||||||
make_dummy_file(file_name)
|
|
||||||
static_file = setup_static_file(nil, nil, file_name)
|
|
||||||
sleep 1
|
|
||||||
modify_dummy_file(file_name)
|
|
||||||
assert static_file.modified?
|
|
||||||
remove_dummy_file(file_name)
|
|
||||||
end
|
|
||||||
|
|
||||||
should "known if the source path is modified, when its not" do
|
|
||||||
file_name = "file_name.txt"
|
|
||||||
make_dummy_file(file_name)
|
|
||||||
static_file = setup_static_file(nil, nil, file_name)
|
|
||||||
static_file.write(nil)
|
|
||||||
sleep 1 # wait, else the times are still the same
|
|
||||||
assert !static_file.modified?
|
|
||||||
remove_dummy_file(file_name)
|
|
||||||
end
|
|
||||||
|
|
||||||
should "known whether to write the file to the filesystem" do
|
|
||||||
static_file = setup_static_file("root", "dir", "file_name.txt")
|
|
||||||
assert static_file.write?, "always true, with current implementation"
|
|
||||||
end
|
|
||||||
|
|
||||||
should "be able to write itself to the desitination direcotry" do
|
|
||||||
file_name = "file_name.txt"
|
|
||||||
make_dummy_file(file_name)
|
|
||||||
static_file = setup_static_file(nil, nil, file_name)
|
|
||||||
assert static_file.write(nil)
|
|
||||||
remove_dummy_file(file_name)
|
|
||||||
end
|
|
||||||
|
|
||||||
should "be able to convert to liquid" do
|
|
||||||
file_name = "file_name.txt"
|
|
||||||
make_dummy_file(file_name)
|
|
||||||
static_file = setup_static_file(nil, nil, "file_name.txt")
|
|
||||||
expected = {
|
|
||||||
"path"=>"/file_name.txt",
|
|
||||||
"modified_time"=> static_file.mtime.to_s,
|
|
||||||
"extname"=>".txt"
|
|
||||||
}
|
|
||||||
assert expected.eql?(static_file.to_liquid), "map is not the same"
|
|
||||||
remove_dummy_file(file_name)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "know its last modification time" do
|
||||||
|
assert_equal Time.new.to_i, @static_file.mtime
|
||||||
|
end
|
||||||
|
|
||||||
|
should "known if the source path is modified, when it is" do
|
||||||
|
sleep 1
|
||||||
|
modify_dummy_file(@filename)
|
||||||
|
assert @static_file.modified?
|
||||||
|
end
|
||||||
|
|
||||||
|
should "known if the source path is modified, when its not" do
|
||||||
|
@static_file.write(dest_dir)
|
||||||
|
sleep 1 # wait, else the times are still the same
|
||||||
|
assert !@static_file.modified?
|
||||||
|
end
|
||||||
|
|
||||||
|
should "known whether to write the file to the filesystem" do
|
||||||
|
assert @static_file.write?, "always true, with current implementation"
|
||||||
|
end
|
||||||
|
|
||||||
|
should "be able to write itself to the desitination direcotry" do
|
||||||
|
assert @static_file.write(dest_dir)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "be able to convert to liquid" do
|
||||||
|
expected = {
|
||||||
|
"path" => "/static_file.txt",
|
||||||
|
"modified_time" => @static_file.mtime.to_s,
|
||||||
|
"extname" => ".txt"
|
||||||
|
}
|
||||||
|
assert_equal expected, @static_file.to_liquid
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue