From 52c4ce2a5a4b4bf9db03af646bc041a62e89f617 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Sat, 21 Feb 2015 00:31:07 -0800 Subject: [PATCH] Finish of moving the unit tests over to Minitest --- Gemfile | 1 + lib/jekyll/cleaner.rb | 178 ++++++++++++++++++------------------ script/test | 4 + test/helper.rb | 9 +- test/suite.rb | 11 --- test/test_cleaner.rb | 4 +- test/test_coffeescript.rb | 16 ++-- test/test_collections.rb | 16 ++-- test/test_generated_site.rb | 12 +-- test/test_new_command.rb | 5 +- test/test_regenerator.rb | 3 +- test/test_site.rb | 8 +- 12 files changed, 130 insertions(+), 137 deletions(-) delete mode 100644 test/suite.rb diff --git a/Gemfile b/Gemfile index 17ecb7dd..d695524b 100644 --- a/Gemfile +++ b/Gemfile @@ -26,6 +26,7 @@ gem 'jekyll_test_plugin' gem 'jekyll_test_plugin_malicious' gem 'liquid-c', '~> 0.0.3' gem 'minitest' +gem 'minitest-reporters' gem 'test-unit' if RUBY_PLATFORM =~ /cygwin/ || RUBY_VERSION.start_with?("2.2") if ENV['BENCHMARK'] diff --git a/lib/jekyll/cleaner.rb b/lib/jekyll/cleaner.rb index 0295fb92..a8b269f4 100644 --- a/lib/jekyll/cleaner.rb +++ b/lib/jekyll/cleaner.rb @@ -1,103 +1,101 @@ require 'set' module Jekyll - class Site - # Handles the cleanup of a site's destination before it is built. - class Cleaner - attr_reader :site + # Handles the cleanup of a site's destination before it is built. + class Cleaner + attr_reader :site - def initialize(site) - @site = site + def initialize(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 + files + end - # Cleans up the site's destination directory - def cleanup! - FileUtils.rm_rf(obsolete_files) - FileUtils.rm_rf(metadata_file) if @site.full_rebuild? + # 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 + # 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 - # - # 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 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: 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 - 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 + # 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 diff --git a/script/test b/script/test index 87df1f60..38ae46b6 100755 --- a/script/test +++ b/script/test @@ -8,6 +8,10 @@ if [ ! -d tmp ]; then mkdir tmp fi +if [ -d test/dest ]; then + rm -r test/dest +fi + if [ -z "$1" ]; then TEST_FILES=$(ruby -e "puts Dir.glob('test/test_*.rb')") else diff --git a/test/helper.rb b/test/helper.rb index 6d86db2a..71133559 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -8,6 +8,7 @@ end require 'rubygems' require 'ostruct' require 'minitest/autorun' +require 'minitest/reporters' require 'jekyll' @@ -23,6 +24,9 @@ include Jekyll # Send STDERR into the void to suppress program output messages STDERR.reopen(test(?e, '/dev/null') ? '/dev/null' : 'NUL:') +# Report with color. +Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(:color => true)] + class Minitest::Test def fixture_site(overrides = {}) Jekyll::Site.new(site_configuration(overrides)) @@ -33,7 +37,10 @@ class Minitest::Test end 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({ "source" => source_dir }, full_overrides) diff --git a/test/suite.rb b/test/suite.rb deleted file mode 100644 index 81b61719..00000000 --- a/test/suite.rb +++ /dev/null @@ -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 diff --git a/test/test_cleaner.rb b/test/test_cleaner.rb index fb36f59f..3b45fad7 100644 --- a/test/test_cleaner.rb +++ b/test/test_cleaner.rb @@ -12,7 +12,7 @@ class TestCleaner < Minitest::Test @site = fixture_site @site.keep_files = ['to_keep/child_dir'] - @cleaner = Site::Cleaner.new(@site) + @cleaner = Cleaner.new(@site) @cleaner.cleanup! end @@ -47,7 +47,7 @@ class TestCleaner < Minitest::Test @site = fixture_site @site.process - @cleaner = Site::Cleaner.new(@site) + @cleaner = Cleaner.new(@site) @cleaner.cleanup! end diff --git a/test/test_coffeescript.rb b/test/test_coffeescript.rb index e62fbc0c..edf6c7d0 100644 --- a/test/test_coffeescript.rb +++ b/test/test_coffeescript.rb @@ -19,16 +19,16 @@ class TestCoffeeScript < Minitest::Test return square(x) * x; }; cubes = (function() { - var _i, _len, _results; - _results = []; - for (_i = 0, _len = list.length; _i < _len; _i++) { - num = list[_i]; - _results.push(math.cube(num)); + var i, len, results; + results = []; + for (i = 0, len = list.length; i < len; i++) { + num = list[i]; + results.push(math.cube(num)); } - return _results; + return results; })(); - if (typeof elvis !== \"undefined\" && elvis !== null) { - return alert(\"I knew it!\"); + if (typeof elvis !== "undefined" && elvis !== null) { + return alert("I knew it!"); } }); diff --git a/test/test_collections.rb b/test/test_collections.rb index cdfc8ad4..3e64022a 100644 --- a/test/test_collections.rb +++ b/test/test_collections.rb @@ -122,7 +122,7 @@ class TestCollections < Minitest::Test assert @site.collections["methods"].docs.is_a? Array @site.collections["methods"].docs.each do |doc| assert doc.is_a? Jekyll::Document - assert_include %w[ + assert_includes %w[ _methods/configuration.md _methods/sanitized_path.md _methods/site/generate.md @@ -135,16 +135,16 @@ class TestCollections < Minitest::Test end 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 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 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_include @collection.docs.map(&:relative_path), "_methods/site/_dont_include_me_either.md" + refute_includes @collection.docs.map(&:relative_path), "_methods/_do_not_read_me.md" + refute_includes @collection.docs.map(&:relative_path), "_methods/site/_dont_include_me_either.md" end end @@ -178,12 +178,12 @@ class TestCollections < Minitest::Test end should "not allow symlinks" do - refute_include @collection.filtered_entries, "um_hi.md" - refute_include @collection.filtered_entries, "/um_hi.md" + refute_includes @collection.filtered_entries, "um_hi.md" + refute_includes @collection.filtered_entries, "/um_hi.md" end 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 diff --git a/test/test_generated_site.rb b/test/test_generated_site.rb index 40bcd8c1..e9c1efbd 100644 --- a/test/test_generated_site.rb +++ b/test/test_generated_site.rb @@ -84,14 +84,12 @@ OUTPUT end should "acceptable limit post is 0" do - refute_raises ArgumentError do - clear_dest - stub(Jekyll).configuration do - Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'limit_posts' => 0}) - end - - @site = Site.new(Jekyll.configuration) + clear_dest + stub(Jekyll).configuration do + Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'limit_posts' => 0}) end + + assert Site.new(Jekyll.configuration), "Couldn't create a site with the given limit_posts." end end end diff --git a/test/test_new_command.rb b/test/test_new_command.rb index 6fffb49f..09332cf9 100644 --- a/test/test_new_command.rb +++ b/test/test_new_command.rb @@ -79,9 +79,8 @@ class TestNewCommand < Minitest::Test should 'force created folder' do capture_stdout { Jekyll::Commands::New.process(@args) } - refute_raises(SystemExit) do - capture_stdout {Jekyll::Commands::New.process(@args, '--force') } - end + output = capture_stdout { Jekyll::Commands::New.process(@args, '--force') } + assert_match /New jekyll site installed in/, output end end diff --git a/test/test_regenerator.rb b/test/test_regenerator.rb index feb5c647..371e711d 100644 --- a/test/test_regenerator.rb +++ b/test/test_regenerator.rb @@ -10,7 +10,8 @@ class TestRegenerator < Minitest::Test "methods" => { "output" => true } - } + }, + "full_rebuild" => false }) @site.read diff --git a/test/test_site.rb b/test/test_site.rb index a1a24e60..6cbfe5be 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -309,9 +309,7 @@ class TestSite < Minitest::Test custom_processor = "CustomMarkdown" s = Site.new(site_configuration('markdown' => custom_processor)) - refute_raises do - s.process - end + assert !!s.process # Do some cleanup, we don't like straggling stuff's. 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 should 'not throw an error at initialization time' do bad_processor = 'not a processor name' - refute_raises do - Site.new(site_configuration('markdown' => bad_processor)) - end + assert Site.new(site_configuration('markdown' => bad_processor)) end should 'throw FatalException at process time' do