Skip to content

Commit

Permalink
Add style_check job to CI workflow (#178)
Browse files Browse the repository at this point in the history
Merge pull request 178
  • Loading branch information
ashmaroli authored Dec 5, 2024
1 parent d35231e commit 7ff0bb9
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 48 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 11 additions & 12 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
9 changes: 1 addition & 8 deletions jekyll-archives.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/jekyll-archives.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/jekyll-archives/archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@ 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.
#
# 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
Expand Down
4 changes: 2 additions & 2 deletions script/fmt
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
12 changes: 4 additions & 8 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/test_jekyll_archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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" }
Expand All @@ -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" }
Expand All @@ -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" }
Expand All @@ -87,6 +91,7 @@ class TestJekyllArchive < Minitest::Test
"url" => "/2013/08/16/",
"permalink" => nil,
}

assert_equal expected, archive.to_liquid.to_h
end
end
Expand Down
40 changes: 26 additions & 14 deletions test/test_jekyll_archives.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,42 @@ 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"
end

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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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."

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 7ff0bb9

Please sign in to comment.