Finish of moving the unit tests over to Minitest

This commit is contained in:
Parker Moore 2015-02-21 00:31:07 -08:00
parent 4b59eb4175
commit 52c4ce2a5a
12 changed files with 130 additions and 137 deletions

View File

@ -26,6 +26,7 @@ gem 'jekyll_test_plugin'
gem 'jekyll_test_plugin_malicious' gem 'jekyll_test_plugin_malicious'
gem 'liquid-c', '~> 0.0.3' gem 'liquid-c', '~> 0.0.3'
gem 'minitest' gem 'minitest'
gem 'minitest-reporters'
gem 'test-unit' if RUBY_PLATFORM =~ /cygwin/ || RUBY_VERSION.start_with?("2.2") gem 'test-unit' if RUBY_PLATFORM =~ /cygwin/ || RUBY_VERSION.start_with?("2.2")
if ENV['BENCHMARK'] if ENV['BENCHMARK']

View File

@ -1,103 +1,101 @@
require 'set' require 'set'
module Jekyll module Jekyll
class Site # Handles the cleanup of a site's destination before it is built.
# Handles the cleanup of a site's destination before it is built. class Cleaner
class Cleaner attr_reader :site
attr_reader :site
def initialize(site) def initialize(site)
@site = site @site = site
end
# Cleans up the site's destination directory
def cleanup!
FileUtils.rm_rf(obsolete_files)
FileUtils.rm_rf(metadata_file) if @site.full_rebuild?
end
private
# Private: The list of files and directories to be deleted during cleanup process
#
# Returns an Array of the file and directory paths
def obsolete_files
(existing_files - new_files - new_dirs + replaced_files).to_a
end
# Private: The metadata file storing dependency tree and build history
#
# Returns an Array with the metdata file as the only item
def metadata_file
[site.regenerator.metadata_file]
end
# Private: The list of existing files, apart from those included in keep_files and hidden files.
#
# Returns a Set with the file paths
def existing_files
files = Set.new
Dir.glob(site.in_dest_dir("**", "*"), File::FNM_DOTMATCH) do |file|
files << file unless file =~ /\/\.{1,2}$/ || file =~ keep_file_regex || keep_dirs.include?(file)
end end
files
end
# Cleans up the site's destination directory # Private: The list of files to be created when site is built.
def cleanup! #
FileUtils.rm_rf(obsolete_files) # Returns a Set with the file paths
FileUtils.rm_rf(metadata_file) if @site.full_rebuild? def new_files
files = Set.new
site.each_site_file { |item| files << item.destination(site.dest) }
files
end
# Private: The list of directories to be created when site is built.
# These are the parent directories of the files in #new_files.
#
# Returns a Set with the directory paths
def new_dirs
new_files.map { |file| parent_dirs(file) }.flatten.to_set
end
# Private: The list of parent directories of a given file
#
# Returns an Array with the directory paths
def parent_dirs(file)
parent_dir = File.dirname(file)
if parent_dir == site.dest
[]
else
[parent_dir] + parent_dirs(parent_dir)
end end
end
private # Private: The list of existing files that will be replaced by a directory during build
#
# Returns a Set with the file paths
def replaced_files
new_dirs.select { |dir| File.file?(dir) }.to_set
end
# Private: The list of files and directories to be deleted during cleanup process # Private: The list of directories that need to be kept because they are parent directories
# # of files specified in keep_files
# Returns an Array of the file and directory paths #
def obsolete_files # Returns a Set with the directory paths
(existing_files - new_files - new_dirs + replaced_files).to_a def keep_dirs
end site.keep_files.map { |file| parent_dirs(site.in_dest_dir(file)) }.flatten.to_set
end
# Private: The metadata file storing dependency tree and build history # Private: Creates a regular expression from the config's keep_files array
# #
# Returns an Array with the metdata file as the only item # Examples
def metadata_file # ['.git','.svn'] creates the following regex: /\/(\.git|\/.svn)/
[site.regenerator.metadata_file] #
end # Returns the regular expression
def keep_file_regex
# Private: The list of existing files, apart from those included in keep_files and hidden files. or_list = site.keep_files.join("|")
# pattern = "\/(#{or_list.gsub(".", "\.")})"
# Returns a Set with the file paths Regexp.new pattern
def existing_files
files = Set.new
Dir.glob(site.in_dest_dir("**", "*"), File::FNM_DOTMATCH) do |file|
files << file unless file =~ /\/\.{1,2}$/ || file =~ keep_file_regex || keep_dirs.include?(file)
end
files
end
# Private: The list of files to be created when site is built.
#
# Returns a Set with the file paths
def new_files
files = Set.new
site.each_site_file { |item| files << item.destination(site.dest) }
files
end
# Private: The list of directories to be created when site is built.
# These are the parent directories of the files in #new_files.
#
# Returns a Set with the directory paths
def new_dirs
new_files.map { |file| parent_dirs(file) }.flatten.to_set
end
# Private: The list of parent directories of a given file
#
# Returns an Array with the directory paths
def parent_dirs(file)
parent_dir = File.dirname(file)
if parent_dir == site.dest
[]
else
[parent_dir] + parent_dirs(parent_dir)
end
end
# Private: The list of existing files that will be replaced by a directory during build
#
# Returns a Set with the file paths
def replaced_files
new_dirs.select { |dir| File.file?(dir) }.to_set
end
# Private: The list of directories that need to be kept because they are parent directories
# of files specified in keep_files
#
# Returns a Set with the directory paths
def keep_dirs
site.keep_files.map { |file| parent_dirs(site.in_dest_dir(file)) }.flatten.to_set
end
# Private: Creates a regular expression from the config's keep_files array
#
# Examples
# ['.git','.svn'] creates the following regex: /\/(\.git|\/.svn)/
#
# Returns the regular expression
def keep_file_regex
or_list = site.keep_files.join("|")
pattern = "\/(#{or_list.gsub(".", "\.")})"
Regexp.new pattern
end
end end
end end
end end

View File

@ -8,6 +8,10 @@ if [ ! -d tmp ]; then
mkdir tmp mkdir tmp
fi fi
if [ -d test/dest ]; then
rm -r test/dest
fi
if [ -z "$1" ]; then if [ -z "$1" ]; then
TEST_FILES=$(ruby -e "puts Dir.glob('test/test_*.rb')") TEST_FILES=$(ruby -e "puts Dir.glob('test/test_*.rb')")
else else

View File

@ -8,6 +8,7 @@ end
require 'rubygems' require 'rubygems'
require 'ostruct' require 'ostruct'
require 'minitest/autorun' require 'minitest/autorun'
require 'minitest/reporters'
require 'jekyll' require 'jekyll'
@ -23,6 +24,9 @@ include Jekyll
# Send STDERR into the void to suppress program output messages # Send STDERR into the void to suppress program output messages
STDERR.reopen(test(?e, '/dev/null') ? '/dev/null' : 'NUL:') STDERR.reopen(test(?e, '/dev/null') ? '/dev/null' : 'NUL:')
# Report with color.
Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(:color => true)]
class Minitest::Test class Minitest::Test
def fixture_site(overrides = {}) def fixture_site(overrides = {})
Jekyll::Site.new(site_configuration(overrides)) Jekyll::Site.new(site_configuration(overrides))
@ -33,7 +37,10 @@ class Minitest::Test
end end
def site_configuration(overrides = {}) def site_configuration(overrides = {})
full_overrides = build_configs(overrides, build_configs({"destination" => dest_dir})) full_overrides = build_configs(overrides, build_configs({
"destination" => dest_dir,
"full_rebuild" => true
}))
build_configs({ build_configs({
"source" => source_dir "source" => source_dir
}, full_overrides) }, full_overrides)

View File

@ -1,11 +0,0 @@
require 'rubygems'
gem 'test-unit'
require 'test/unit'
# for some reason these tests fail when run via TextMate
# but succeed when run on the command line.
tests = Dir[File.expand_path("#{File.dirname(__FILE__)}/test_*.rb")]
tests.each do |file|
require file
end

View File

@ -12,7 +12,7 @@ class TestCleaner < Minitest::Test
@site = fixture_site @site = fixture_site
@site.keep_files = ['to_keep/child_dir'] @site.keep_files = ['to_keep/child_dir']
@cleaner = Site::Cleaner.new(@site) @cleaner = Cleaner.new(@site)
@cleaner.cleanup! @cleaner.cleanup!
end end
@ -47,7 +47,7 @@ class TestCleaner < Minitest::Test
@site = fixture_site @site = fixture_site
@site.process @site.process
@cleaner = Site::Cleaner.new(@site) @cleaner = Cleaner.new(@site)
@cleaner.cleanup! @cleaner.cleanup!
end end

View File

@ -19,16 +19,16 @@ class TestCoffeeScript < Minitest::Test
return square(x) * x; return square(x) * x;
}; };
cubes = (function() { cubes = (function() {
var _i, _len, _results; var i, len, results;
_results = []; results = [];
for (_i = 0, _len = list.length; _i < _len; _i++) { for (i = 0, len = list.length; i < len; i++) {
num = list[_i]; num = list[i];
_results.push(math.cube(num)); results.push(math.cube(num));
} }
return _results; return results;
})(); })();
if (typeof elvis !== \"undefined\" && elvis !== null) { if (typeof elvis !== "undefined" && elvis !== null) {
return alert(\"I knew it!\"); return alert("I knew it!");
} }
}); });

View File

@ -122,7 +122,7 @@ class TestCollections < Minitest::Test
assert @site.collections["methods"].docs.is_a? Array assert @site.collections["methods"].docs.is_a? Array
@site.collections["methods"].docs.each do |doc| @site.collections["methods"].docs.each do |doc|
assert doc.is_a? Jekyll::Document assert doc.is_a? Jekyll::Document
assert_include %w[ assert_includes %w[
_methods/configuration.md _methods/configuration.md
_methods/sanitized_path.md _methods/sanitized_path.md
_methods/site/generate.md _methods/site/generate.md
@ -135,16 +135,16 @@ class TestCollections < Minitest::Test
end end
should "not include files which start with an underscore in the base collection directory" do should "not include files which start with an underscore in the base collection directory" do
refute_include @collection.filtered_entries, "_do_not_read_me.md" refute_includes @collection.filtered_entries, "_do_not_read_me.md"
end end
should "not include files which start with an underscore in a subdirectory" do should "not include files which start with an underscore in a subdirectory" do
refute_include @collection.filtered_entries, "site/_dont_include_me_either.md" refute_includes @collection.filtered_entries, "site/_dont_include_me_either.md"
end end
should "not include the underscored files in the list of docs" do should "not include the underscored files in the list of docs" do
refute_include @collection.docs.map(&:relative_path), "_methods/_do_not_read_me.md" refute_includes @collection.docs.map(&:relative_path), "_methods/_do_not_read_me.md"
refute_include @collection.docs.map(&:relative_path), "_methods/site/_dont_include_me_either.md" refute_includes @collection.docs.map(&:relative_path), "_methods/site/_dont_include_me_either.md"
end end
end end
@ -178,12 +178,12 @@ class TestCollections < Minitest::Test
end end
should "not allow symlinks" do should "not allow symlinks" do
refute_include @collection.filtered_entries, "um_hi.md" refute_includes @collection.filtered_entries, "um_hi.md"
refute_include @collection.filtered_entries, "/um_hi.md" refute_includes @collection.filtered_entries, "/um_hi.md"
end end
should "not include the symlinked file in the list of docs" do should "not include the symlinked file in the list of docs" do
refute_include @collection.docs.map(&:relative_path), "_methods/um_hi.md" refute_includes @collection.docs.map(&:relative_path), "_methods/um_hi.md"
end end
end end

View File

@ -84,14 +84,12 @@ OUTPUT
end end
should "acceptable limit post is 0" do should "acceptable limit post is 0" do
refute_raises ArgumentError do clear_dest
clear_dest stub(Jekyll).configuration do
stub(Jekyll).configuration do Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'limit_posts' => 0})
Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'limit_posts' => 0})
end
@site = Site.new(Jekyll.configuration)
end end
assert Site.new(Jekyll.configuration), "Couldn't create a site with the given limit_posts."
end end
end end
end end

View File

@ -79,9 +79,8 @@ class TestNewCommand < Minitest::Test
should 'force created folder' do should 'force created folder' do
capture_stdout { Jekyll::Commands::New.process(@args) } capture_stdout { Jekyll::Commands::New.process(@args) }
refute_raises(SystemExit) do output = capture_stdout { Jekyll::Commands::New.process(@args, '--force') }
capture_stdout {Jekyll::Commands::New.process(@args, '--force') } assert_match /New jekyll site installed in/, output
end
end end
end end

View File

@ -10,7 +10,8 @@ class TestRegenerator < Minitest::Test
"methods" => { "methods" => {
"output" => true "output" => true
} }
} },
"full_rebuild" => false
}) })
@site.read @site.read

View File

@ -309,9 +309,7 @@ class TestSite < Minitest::Test
custom_processor = "CustomMarkdown" custom_processor = "CustomMarkdown"
s = Site.new(site_configuration('markdown' => custom_processor)) s = Site.new(site_configuration('markdown' => custom_processor))
refute_raises do assert !!s.process
s.process
end
# Do some cleanup, we don't like straggling stuff's. # Do some cleanup, we don't like straggling stuff's.
Jekyll::Converters::Markdown.send(:remove_const, :CustomMarkdown) Jekyll::Converters::Markdown.send(:remove_const, :CustomMarkdown)
@ -344,9 +342,7 @@ class TestSite < Minitest::Test
context 'with an invalid markdown processor in the configuration' do context 'with an invalid markdown processor in the configuration' do
should 'not throw an error at initialization time' do should 'not throw an error at initialization time' do
bad_processor = 'not a processor name' bad_processor = 'not a processor name'
refute_raises do assert Site.new(site_configuration('markdown' => bad_processor))
Site.new(site_configuration('markdown' => bad_processor))
end
end end
should 'throw FatalException at process time' do should 'throw FatalException at process time' do