Skip to content

Commit

Permalink
Remove hardcoded roda_app.rb file path, other serveer Zeitwerk opti…
Browse files Browse the repository at this point in the history
…mizations

Fixes #688
  • Loading branch information
jaredcwhite committed Dec 15, 2024
1 parent 83d045c commit 7b3d2f8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
52 changes: 33 additions & 19 deletions bridgetown-core/lib/bridgetown-core/rack/boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def self.boot(*)
end

# @param root [String] root of Bridgetown site, defaults to config value
def self.autoload_server_folder( # rubocop:todo Metrics
def self.autoload_server_folder( # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
root: Bridgetown::Current.preloaded_configuration.root_dir
)
server_folder = File.join(root, "server")
Expand All @@ -46,26 +46,18 @@ def self.autoload_server_folder( # rubocop:todo Metrics
next unless load_path == server_folder

loader.eager_load
loader.do_not_eager_load(File.join(server_folder, "roda_app.rb"))
subclass_names = Roda.subclasses.map(&:name)
subclass_paths = Set.new

loader.all_expected_cpaths.each do |cpath, cname|
if subclass_names.include?(cname) && cpath.start_with?(server_folder)
subclass_paths << cpath
loader.do_not_eager_load cpath
end
end

unless ENV["BRIDGETOWN_ENV"] == "production"
Listen.to(server_folder) do |modified, added, removed|
c = modified + added + removed
n = c.length

Bridgetown.logger.info(
"Reloading…",
"#{n} file#{"s" if n > 1} changed at #{Time.now.strftime("%Y-%m-%d %H:%M:%S")}"
)
c.each do |path|
Bridgetown.logger.info "", "- #{path["#{File.dirname(server_folder)}/".length..]}"
end

loader.reload
loader.eager_load
rescue SyntaxError => e
Bridgetown::Errors.print_build_error(e)
end.start
setup_autoload_listener loader, server_folder, subclass_paths
end
end

Expand All @@ -79,5 +71,27 @@ def self.autoload_server_folder( # rubocop:todo Metrics

loaders_manager.setup_loaders([server_folder])
end

def self.setup_autoload_listener(loader, server_folder, subclass_paths)
Listen.to(server_folder) do |modified, added, removed|
c = modified + added + removed
n = c.length

unless n == 1 && subclass_paths.include?(c.first)
Bridgetown.logger.info(
"Reloading…",
"#{n} file#{"s" if n > 1} changed at #{Time.now.strftime("%Y-%m-%d %H:%M:%S")}"
)
c.each do |path|
Bridgetown.logger.info "", "- #{path["#{File.dirname(server_folder)}/".length..]}"
end
end

loader.reload
Bridgetown::Hooks.trigger :loader, :post_reload, loader, server_folder
rescue SyntaxError => e
Bridgetown::Errors.print_build_error(e)
end.start
end
end
end
5 changes: 5 additions & 0 deletions bridgetown-website/src/_docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ There are `get`, `post`, and `delete` methods available for testing various serv

You can add additional tests via `test_*` methods, and you can create as many test files as you want to handle various parts of the site. Be advised that these tests are run via the `server` initialization context, so it's possible something may not have run as you would expect under a `static` initialization context. But since the static site is already built prior to your tests being executed, it's probably best for you to test static use cases via the output HTML.

Methods you can override in a `Bridgetown::Test` subclass:

* `roda_app_class` - default return value is `RodaApp`
* `roda_log_level` - default return value is `Logger::WARN` (if you want to see all server logs when running tests, return `Logger::INFO` or `Logger::DEBUG` instead)

## Headless Browser Testing with Cypress

You can install Cypress using a [bundled configuration](/docs/bundled-configurations):
Expand Down

0 comments on commit 7b3d2f8

Please sign in to comment.