Skip to content

Commit

Permalink
Fix Guardfile matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Oct 17, 2023
1 parent ea0e6fd commit 349e750
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 12 deletions.
11 changes: 5 additions & 6 deletions lib/hanami/reloader/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def self.path(value)

# Generate hanami-reloader configuration
class Install < Hanami::CLI::Command
# @api private
# @since 2.1.0
MATCHER = %r{^(app|config|lib|slices)([\\/][^\\/]+)*\.(rb|erb|haml|slim)$}i

desc "Generate configuration for code reloading"

def initialize(fs: Dry::Files.new, bundler: CLI::Bundler.new(fs: fs), **args)
Expand All @@ -44,12 +48,7 @@ def generate_configuration(path)
group :#{Guardfile.group} do
guard "puma", port: ENV.fetch("#{Hanami::Port::ENV_VAR}", #{Hanami::Port::DEFAULT}) do
watch(%r{config/*})
watch(%r{lib/*})
watch(%r{app/[^/]+.rb})
watch(%r{app/templates/.+..+..+})
watch(%r{slices/.+/.+.rb})
watch(%r{slices/.+/templates/.+..+..+})
watch(%r{^(app|config|lib|slices)([\\/][^\\/]+)*.(rb|erb|haml|slim)$}i)
end
end
CODE
Expand Down
9 changes: 3 additions & 6 deletions spec/unit/hanami/reloader/commands/install_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,14 @@
expect(fs.read("Gemfile")).to include(gemfile)

# Guardfile
matcher = Hanami::Reloader::Commands::Install::MATCHER.inspect.gsub("/^", "^").gsub("$/i", "$}i").gsub('*\\.', "*.").gsub("\\\\\\", %(\\))
matcher = %(%r{#{matcher})
guardfile = <<~EOF
# frozen_string_literal: true
group :server do
guard "puma", port: ENV.fetch("HANAMI_PORT", 2300) do
watch(%r{config/*})
watch(%r{lib/*})
watch(%r{app/[^/]+.rb})
watch(%r{app/templates/.+..+..+})
watch(%r{slices/.+/.+.rb})
watch(%r{slices/.+/templates/.+..+..+})
watch(#{matcher})
end
end
EOF
Expand Down
105 changes: 105 additions & 0 deletions spec/unit/hanami/reloader/matcher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# frozen_string_literal: true

require "hanami/reloader/commands"

RSpec.describe "Hanami::Reloader::Commands::Install::MATCHER" do
subject { Hanami::Reloader::Commands::Install::MATCHER }

let(:matching_paths) do
[
"app/action.rb",
"app/view.rb",
"app/actions/books/index.rb",
"app/actions/books/discounted/index.rb",
"app/views/helpers.rb",
"app/views/books/index.rb",
"app/views/books/discounted/index.rb",
"app/templates/books/index.html.erb",
"app/templates/books/discounted/index.html.erb",
"app/templates/layouts/app.html.erb",
"config/app.rb",
"config/puma.rb",
"config/routes.rb",
"config/settings.rb",
"lib/bookshelf/types.rb",
"slices/admin/action.rb",
"slices/admin/view.rb",
"slices/admin/actions/users/index.rb",
"slices/admin/actions/users/deactivated/index.rb",
"slices/admin/views/helpers.rb",
"slices/admin/views/users/index.rb",
"slices/admin/views/users/deactivated/index.rb",
"slices/admin/templates/users/index.html.slim",
"slices/admin/templates/users/deactivated/index.html.slim",
"slices/api/templates/authors/index.json.haml",
"slices/api/templates/authors/uprising/index.json.haml"
]
end

let(:non_matching_paths) do
[
"Gemfile",
"Gemfile.lock",
"Guardfile",
"Procfile.dev",
"README.md",
"Rakefile",
"config.ru",
"app/assets/css/app.css",
"app/assets/js/app.js",
"app/assets/images/favicon.ico",
"slices/admin/assets/css/app.css",
"slices/admin/assets/js/app.js",
"slices/admin/assets/images/favicon.ico",
"log/test.log",
"node_modules/hanami-assets/dist/hanami-assets.js",
"package.json",
"package-lock.json",
"package.json",
"public/assets.json",
"public/assets/favicon.ico",
"spec/spec_helper.rb",
"spec/requests/root_spec.rb",
"spec/support/requests.rb",
"spec/support/rspec.rb",
"spec/actions/books/index_spec.rb",
"spec/actions/books/discounted/index_spec.rb",
"spec/views/books/index_spec.rb",
"spec/views/books/discounted/index_spec.rb"
]
end

context "UNIX" do
it "matches the given patterns" do
matching_paths.each do |path|
expect(path).to match(subject)
end
end

it "does not matches the given patterns" do
non_matching_paths.each do |path|
expect(path).to_not match(subject)
end
end
end

context "Windows" do
it "matches the given patterns" do
matching_paths.each do |path|
expect(windows_path(path)).to match(subject)
end
end

it "does not matches the given patterns" do
non_matching_paths.each do |path|
expect(windows_path(path)).to_not match(subject)
end
end

private

def windows_path(path)
path.gsub("/", "\\")
end
end
end

0 comments on commit 349e750

Please sign in to comment.