diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 57d1dfd..e255f47 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,3 +31,21 @@ jobs: bundler-cache: true - name: Run Unit Tests run: bundle exec rake test + + style_check: + name: "Style Check (Ruby ${{ matrix.ruby_version }})" + runs-on: "ubuntu-latest" + strategy: + fail-fast: false + matrix: + ruby_version: + - "3.3" + steps: + - uses: actions/checkout@v4 + - name: "Set up Ruby ${{ matrix.ruby_version }}" + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby_version }} + bundler-cache: true + - name: Run RuboCop + run: bash script/fmt diff --git a/.rubocop.yml b/.rubocop.yml index 245b600..bcebed9 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,28 +1,27 @@ -require: rubocop-jekyll +require: + - rubocop-jekyll + - rubocop-minitest inherit_gem: rubocop-jekyll: .rubocop.yml AllCops: - TargetRubyVersion: 2.3 + TargetRubyVersion: 2.7 + SuggestExtensions: false Exclude: - vendor/**/* -Lint/ShadowingOuterLocalVariable: +Layout/LineLength: Exclude: - lib/jekyll-archives.rb - -# Remove once Jekyll core has dropped explicit support for Ruby 2.2 -Lint/SafeNavigationConsistency: - Exclude: - lib/jekyll-archives/archive.rb + - test/**/*.rb Metrics/BlockLength: Exclude: - test/**/*.rb -Metrics/LineLength: - Exclude: - - lib/jekyll-archives.rb - - lib/jekyll-archives/archive.rb - - test/**/*.rb +Minitest/AssertKindOf: + Enabled: true +Minitest/EmptyLineBeforeAssertionMethods: + Enabled: true diff --git a/Gemfile b/Gemfile index cbec53b..4fad357 100644 --- a/Gemfile +++ b/Gemfile @@ -5,3 +5,9 @@ gemspec gem "jekyll", ENV["JEKYLL_VERSION"] if ENV["JEKYLL_VERSION"] gem "kramdown-parser-gfm" if ENV["JEKYLL_VERSION"] == "~> 3.9" + +gem "minitest" +gem "rake" +gem "rubocop-jekyll", "~> 0.14.0" +gem "rubocop-minitest" +gem "shoulda-context" diff --git a/jekyll-archives.gemspec b/jekyll-archives.gemspec index 6afb827..5afeaf7 100644 --- a/jekyll-archives.gemspec +++ b/jekyll-archives.gemspec @@ -15,14 +15,7 @@ Gem::Specification.new do |s| all_files = `git ls-files -z`.split("\x0") s.files = all_files.grep(%r!^(lib)/!) - s.required_ruby_version = ">= 2.3.0" + s.required_ruby_version = ">= 2.7.0" s.add_dependency "jekyll", ">= 3.6", "< 5.0" - - s.add_development_dependency "bundler" - s.add_development_dependency "minitest" - s.add_development_dependency "rake" - s.add_development_dependency "rdoc" - s.add_development_dependency "rubocop-jekyll", "~> 0.9" - s.add_development_dependency "shoulda" end diff --git a/lib/jekyll-archives.rb b/lib/jekyll-archives.rb index d0a2a26..a84bd11 100644 --- a/lib/jekyll-archives.rb +++ b/lib/jekyll-archives.rb @@ -133,7 +133,7 @@ def append_enabled_date_type(meta, type, posts) def date_attr_hash(posts, id) hash = Hash.new { |hsh, key| hsh[key] = [] } posts.each { |post| hash[post.date.strftime(id)] << post } - hash.each_value { |posts| posts.sort!.reverse! } + hash.each_value { |posts_in_hsh| posts_in_hsh.sort!.reverse! } hash end end diff --git a/lib/jekyll-archives/archive.rb b/lib/jekyll-archives/archive.rb index ac4a45a..da75b47 100644 --- a/lib/jekyll-archives/archive.rb +++ b/lib/jekyll-archives/archive.rb @@ -77,11 +77,11 @@ def url :permalink => nil ).to_s rescue ArgumentError - raise ArgumentError, "Template \"#{template}\" provided is invalid." + raise ArgumentError, "Template #{template.inspect} provided is invalid." end def permalink - data&.is_a?(Hash) && data["permalink"] + data.is_a?(Hash) && data["permalink"] end # Produce a title object suitable for Liquid based on type of archive. @@ -89,7 +89,7 @@ def permalink # Returns a String (for tag and category archives) and nil for # date-based archives. def title - @title if @title.is_a? String + @title if @title.is_a?(String) end # Produce a date object if a date-based archive diff --git a/script/fmt b/script/fmt index 872d0ee..ec087cd 100755 --- a/script/fmt +++ b/script/fmt @@ -1,8 +1,8 @@ #!/bin/bash set -e -echo "Rubocop $(bundle exec rubocop --version)" -bundle exec rubocop -S -D -E $@ +echo "RuboCop $(bundle exec rubocop --version)" +bundle exec rubocop -E --disable-pending-cops $@ success=$? if ((success != 0)); then echo -e "\nTry running \`script/fmt -a\` to automatically fix errors" diff --git a/test/helper.rb b/test/helper.rb index c4eda43..4ec0b8b 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -5,16 +5,12 @@ require "rubygems" require "minitest/autorun" -require "shoulda" +require "shoulda/context" -$LOAD_PATH.unshift(File.join(__dir__, "..", "lib")) -$LOAD_PATH.unshift(__dir__) +require_relative "../lib/jekyll-archives" -require "jekyll-archives" - -TEST_DIR = __dir__ -SOURCE_DIR = File.expand_path("source", TEST_DIR) -DEST_DIR = File.expand_path("destination", TEST_DIR) +SOURCE_DIR = File.expand_path("source", __dir__) +DEST_DIR = File.expand_path("destination", __dir__) module Minitest class Test diff --git a/test/test_jekyll_archive.rb b/test/test_jekyll_archive.rb index 9232389..e38b51d 100644 --- a/test/test_jekyll_archive.rb +++ b/test/test_jekyll_archive.rb @@ -27,6 +27,7 @@ class TestJekyllArchive < Minitest::Test "url" => "/tag/test-tag/", "permalink" => nil, } + assert_equal expected, archive.to_liquid.to_h archive = @archives.find { |a| a.type == "category" } @@ -42,6 +43,7 @@ class TestJekyllArchive < Minitest::Test "url" => "/category/plugins/", "permalink" => nil, } + assert_equal expected, archive.to_liquid.to_h archive = @archives.find { |a| a.type == "year" } @@ -57,6 +59,7 @@ class TestJekyllArchive < Minitest::Test "url" => "/2013/", "permalink" => nil, } + assert_equal expected, archive.to_liquid.to_h archive = @archives.find { |a| a.type == "month" } @@ -72,6 +75,7 @@ class TestJekyllArchive < Minitest::Test "url" => "/2013/08/", "permalink" => nil, } + assert_equal expected, archive.to_liquid.to_h archive = @archives.find { |a| a.type == "day" } @@ -87,6 +91,7 @@ class TestJekyllArchive < Minitest::Test "url" => "/2013/08/16/", "permalink" => nil, } + assert_equal expected, archive.to_liquid.to_h end end diff --git a/test/test_jekyll_archives.rb b/test/test_jekyll_archives.rb index 8ccbd84..2ac1ea1 100644 --- a/test/test_jekyll_archives.rb +++ b/test/test_jekyll_archives.rb @@ -14,24 +14,28 @@ class TestJekyllArchives < Minitest::Test should "generate archive pages by year" do @archives.generate(@site) + assert archive_exists? @site, "2014/index.html" assert archive_exists? @site, "2013/index.html" end should "generate archive pages by month" do @archives.generate(@site) + assert archive_exists? @site, "2014/08/index.html" assert archive_exists? @site, "2014/03/index.html" end should "generate archive pages by day" do @archives.generate(@site) + assert archive_exists? @site, "2014/08/17/index.html" assert archive_exists? @site, "2013/08/16/index.html" end should "generate archive pages by tag" do @archives.generate(@site) + assert archive_exists? @site, "tag/test-tag/index.html" assert archive_exists? @site, "tag/tagged/index.html" assert archive_exists? @site, "tag/new/index.html" @@ -39,11 +43,13 @@ class TestJekyllArchives < Minitest::Test should "generate archive pages by category" do @archives.generate(@site) + assert archive_exists? @site, "category/plugins/index.html" end should "generate archive pages with a layout" do @site.process + assert_equal "Test", read_file("tag/test-tag/index.html") end end @@ -62,6 +68,7 @@ class TestJekyllArchives < Minitest::Test should "generate slugs using the mode specified" do @archives.generate(@site) + assert archive_exists? @site, "category/💎/index.html" end end @@ -77,6 +84,7 @@ class TestJekyllArchives < Minitest::Test should "use custom layout" do @site.process + assert_equal "Test too", read_file("tag/test-tag/index.html") end end @@ -164,10 +172,10 @@ class TestJekyllArchives < Minitest::Test end should "not generate the disabled archives" do - assert !archive_exists?(@site, "2014/index.html") - assert !archive_exists?(@site, "2014/08/index.html") - assert !archive_exists?(@site, "2013/08/16/index.html") - assert !archive_exists?(@site, "category/plugins/index.html") + refute archive_exists?(@site, "2014/index.html") + refute archive_exists?(@site, "2014/08/index.html") + refute archive_exists?(@site, "2013/08/16/index.html") + refute archive_exists?(@site, "category/plugins/index.html") end end @@ -186,25 +194,25 @@ class TestJekyllArchives < Minitest::Test end should "populate the title field in case of category or tag" do - assert @tag_archive.title.is_a? String - assert @category_archive.title.is_a? String + assert_kind_of String, @tag_archive.title + assert_kind_of String, @category_archive.title end should "use nil for the title field in case of dates" do - assert @year_archive.title.nil? - assert @month_archive.title.nil? - assert @day_archive.title.nil? + assert_nil @year_archive.title + assert_nil @month_archive.title + assert_nil @day_archive.title end should "use nil for the date field in case of category or tag" do - assert @tag_archive.date.nil? - assert @category_archive.date.nil? + assert_nil @tag_archive.date + assert_nil @category_archive.date end should "populate the date field with a Date in case of dates" do - assert @year_archive.date.is_a? Date - assert @month_archive.date.is_a? Date - assert @day_archive.date.is_a? Date + assert_kind_of Date, @year_archive.date + assert_kind_of Date, @month_archive.date + assert_kind_of Date, @day_archive.date end end @@ -215,6 +223,7 @@ class TestJekyllArchives < Minitest::Test site.read site.generate end + assert_includes output, "Archives: Expected a hash but got [\"apples\", \"oranges\"]" assert_includes output, "Archives will not be generated for this site." @@ -223,6 +232,7 @@ class TestJekyllArchives < Minitest::Test site.read site.generate end + assert_includes output, "Archives: Expected a hash but got nil" assert_includes output, "Archives will not be generated for this site." end @@ -232,6 +242,7 @@ class TestJekyllArchives < Minitest::Test site = fixture_site("jekyll-archives" => nil) site.read site.generate + assert_nil(site.pages.find { |p| p.is_a?(Jekyll::Archives::Archive) }) end end @@ -242,6 +253,7 @@ class TestJekyllArchives < Minitest::Test @site.read @site.generate end + refute_includes output, "Archives: Expected a hash but got nil" assert_nil(@site.pages.find { |p| p.is_a?(Jekyll::Archives::Archive) }) end