From d8c66c91ad791a1de25e24575f6a80dbb437827b Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Thu, 5 Dec 2024 19:33:41 +0530 Subject: [PATCH 01/11] Add style-check workflow --- .github/workflows/ci.yml | 18 ++++++++++++++++++ .rubocop.yml | 4 +--- jekyll-archives.gemspec | 2 +- 3 files changed, 20 insertions(+), 4 deletions(-) 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..a4d0978 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -4,9 +4,7 @@ inherit_gem: rubocop-jekyll: .rubocop.yml AllCops: - TargetRubyVersion: 2.3 - Exclude: - - vendor/**/* + TargetRubyVersion: 2.7 Lint/ShadowingOuterLocalVariable: Exclude: diff --git a/jekyll-archives.gemspec b/jekyll-archives.gemspec index 6afb827..d8338ec 100644 --- a/jekyll-archives.gemspec +++ b/jekyll-archives.gemspec @@ -15,7 +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" From acf0399470cd6aab48ae7496f1d27ae5e5b88f85 Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Thu, 5 Dec 2024 19:36:02 +0530 Subject: [PATCH 02/11] Move dev-dependencies to Gemfile --- Gemfile | 5 +++++ jekyll-archives.gemspec | 7 ------- test/helper.rb | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index cbec53b..20ec76a 100644 --- a/Gemfile +++ b/Gemfile @@ -5,3 +5,8 @@ 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 "shoulda-context" diff --git a/jekyll-archives.gemspec b/jekyll-archives.gemspec index d8338ec..5afeaf7 100644 --- a/jekyll-archives.gemspec +++ b/jekyll-archives.gemspec @@ -18,11 +18,4 @@ Gem::Specification.new do |s| 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/test/helper.rb b/test/helper.rb index c4eda43..65eec47 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -5,7 +5,7 @@ require "rubygems" require "minitest/autorun" -require "shoulda" +require "shoulda/context" $LOAD_PATH.unshift(File.join(__dir__, "..", "lib")) $LOAD_PATH.unshift(__dir__) From f056782710735ae480550bdbffe42df7a9ed19ac Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Thu, 5 Dec 2024 19:42:43 +0530 Subject: [PATCH 03/11] Enable some `rubocop-minitest` cops --- .rubocop.yml | 10 ++++++++- Gemfile | 1 + test/test_jekyll_archive.rb | 5 +++++ test/test_jekyll_archives.rb | 40 +++++++++++++++++++++++------------- 4 files changed, 41 insertions(+), 15 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index a4d0978..4c82613 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,10 +1,13 @@ -require: rubocop-jekyll +require: + - rubocop-jekyll + - rubocop-minitest inherit_gem: rubocop-jekyll: .rubocop.yml AllCops: TargetRubyVersion: 2.7 + SuggestExtensions: false Lint/ShadowingOuterLocalVariable: Exclude: @@ -24,3 +27,8 @@ Metrics/LineLength: - 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 20ec76a..4fad357 100644 --- a/Gemfile +++ b/Gemfile @@ -9,4 +9,5 @@ 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/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 From 8217b2b9acf158c8a4160619d9b07ee48015e0a3 Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Thu, 5 Dec 2024 19:48:31 +0530 Subject: [PATCH 04/11] Fix style offenses --- .rubocop.yml | 13 ++----------- lib/jekyll-archives.rb | 2 +- lib/jekyll-archives/archive.rb | 6 +++--- script/fmt | 4 ++-- 4 files changed, 8 insertions(+), 17 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 4c82613..654f622 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -9,23 +9,14 @@ AllCops: TargetRubyVersion: 2.7 SuggestExtensions: false -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 - -Metrics/BlockLength: - Exclude: - test/**/*.rb -Metrics/LineLength: +Metrics/BlockLength: Exclude: - - lib/jekyll-archives.rb - - lib/jekyll-archives/archive.rb - test/**/*.rb Minitest/AssertKindOf: 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" From a515299b4a14caacf9712eed33ce54621f107bf9 Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Thu, 5 Dec 2024 19:49:10 +0530 Subject: [PATCH 05/11] Clean up test-helper --- test/helper.rb | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/test/helper.rb b/test/helper.rb index 65eec47..4ec0b8b 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -7,14 +7,10 @@ require "minitest/autorun" 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 From 878abc9604fb07d31cfaf831b4c7277561823f59 Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Thu, 5 Dec 2024 20:37:35 +0530 Subject: [PATCH 06/11] Test running `style_check` on multiple Ruby versions --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e255f47..7a46bd2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,6 +39,8 @@ jobs: fail-fast: false matrix: ruby_version: + - "2.7" + - "3.2" - "3.3" steps: - uses: actions/checkout@v4 From 9c7cd22bcf3f24834785e7927a619424bf6e6fea Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Thu, 5 Dec 2024 20:40:44 +0530 Subject: [PATCH 07/11] Remove short-flag from `script/fmt` --- script/fmt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/fmt b/script/fmt index ec087cd..6f3cb7b 100755 --- a/script/fmt +++ b/script/fmt @@ -2,7 +2,7 @@ set -e echo "RuboCop $(bundle exec rubocop --version)" -bundle exec rubocop -E --disable-pending-cops $@ +bundle exec rubocop --disable-pending-cops $@ success=$? if ((success != 0)); then echo -e "\nTry running \`script/fmt -a\` to automatically fix errors" From b726fee3c24ceed6f7820e6e4219ba6734b080ce Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Thu, 5 Dec 2024 20:46:02 +0530 Subject: [PATCH 08/11] Add `rubocop-rake` to bundle --- Gemfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Gemfile b/Gemfile index 4fad357..0a6e653 100644 --- a/Gemfile +++ b/Gemfile @@ -10,4 +10,5 @@ gem "minitest" gem "rake" gem "rubocop-jekyll", "~> 0.14.0" gem "rubocop-minitest" +gem "rubocop-rake" gem "shoulda-context" From 00567919da5db2f101000be1245528ff6f549495 Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Thu, 5 Dec 2024 20:48:51 +0530 Subject: [PATCH 09/11] Debug running `script/fmt` --- script/fmt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/fmt b/script/fmt index 6f3cb7b..c114566 100755 --- a/script/fmt +++ b/script/fmt @@ -2,7 +2,7 @@ set -e echo "RuboCop $(bundle exec rubocop --version)" -bundle exec rubocop --disable-pending-cops $@ +bundle exec rubocop -d --disable-pending-cops $@ success=$? if ((success != 0)); then echo -e "\nTry running \`script/fmt -a\` to automatically fix errors" From be06d7dacef8e31c625912020da0554b73b62fa5 Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Thu, 5 Dec 2024 20:52:18 +0530 Subject: [PATCH 10/11] Exclude `vendor/**/*` in RuboCop config --- .rubocop.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 654f622..bcebed9 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -8,6 +8,8 @@ inherit_gem: AllCops: TargetRubyVersion: 2.7 SuggestExtensions: false + Exclude: + - vendor/**/* Layout/LineLength: Exclude: From a7fa54666a2fd9135c58143520e076f7aa14f373 Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Thu, 5 Dec 2024 20:57:25 +0530 Subject: [PATCH 11/11] Revert superfluous changes to branch --- .github/workflows/ci.yml | 2 -- Gemfile | 1 - script/fmt | 2 +- 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7a46bd2..e255f47 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,8 +39,6 @@ jobs: fail-fast: false matrix: ruby_version: - - "2.7" - - "3.2" - "3.3" steps: - uses: actions/checkout@v4 diff --git a/Gemfile b/Gemfile index 0a6e653..4fad357 100644 --- a/Gemfile +++ b/Gemfile @@ -10,5 +10,4 @@ gem "minitest" gem "rake" gem "rubocop-jekyll", "~> 0.14.0" gem "rubocop-minitest" -gem "rubocop-rake" gem "shoulda-context" diff --git a/script/fmt b/script/fmt index c114566..ec087cd 100755 --- a/script/fmt +++ b/script/fmt @@ -2,7 +2,7 @@ set -e echo "RuboCop $(bundle exec rubocop --version)" -bundle exec rubocop -d --disable-pending-cops $@ +bundle exec rubocop -E --disable-pending-cops $@ success=$? if ((success != 0)); then echo -e "\nTry running \`script/fmt -a\` to automatically fix errors"