Skip to content

Commit

Permalink
Use upcoming Bridgetown 2 test setup
Browse files Browse the repository at this point in the history
It supports testing static pages and Roda routes with
the same setup.

I extracted it from bridgetownrb/bridgetown#954
With Jared's advice he gave me in Discord.
  • Loading branch information
radanskoric committed Dec 13, 2024
1 parent 8470438 commit 8cc2ba1
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 6 deletions.
8 changes: 4 additions & 4 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
FROM ruby:3.3.0
FROM ruby:3.3.6

# Install all of the preqeusities to actually building the app.
RUN apt-get update \
&& apt-get install -y \
vim \
curl \
cmake \
&& curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - \
&& apt-get install -y nodejs\
&& npm install --global yarn\
&& rm -rf /var/lib/apt/lists/* \
&& gem install bundler -v 2.5.22
&& gem install bundler -v 2.5.23

# Add some custom setup to the container shell for easier usage.
COPY .devcontainer/load-profile.sh /etc/profile.d/
COPY .devcontainer/load-profile.sh /etc/profile.d/load-profile.sh
RUN chmod +x /etc/profile.d/load-profile.sh

# Do not work as a root user.
Expand All @@ -38,4 +39,3 @@ EXPOSE 4000

# Run the application when the container starts
CMD ["bin/bridgetown", "start"]

1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ bower_components
.env

# Dev and build only files
.devcontainer
.git
Dockerfile
.dockerignore
Expand Down
10 changes: 9 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ gem "puma", "< 7"
# gem "nokogiri", "~> 1.13"

# Or for faster parsing of HTML-only resources via Inspectors, use Nokolexbor:
# gem "nokolexbor", "~> 0.4"
gem "nokolexbor", "~> 0.4"

gem "debug"

group :test do
gem "minitest"
gem "minitest-reporters"
gem "rack-test"
end
35 changes: 35 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ GEM
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
amazing_print (1.6.0)
ansi (1.5.0)
base64 (0.2.0)
bigdecimal (3.1.8)
bridgetown (1.3.4)
Expand Down Expand Up @@ -50,10 +51,15 @@ GEM
zeitwerk (~> 2.5)
bridgetown-paginate (1.3.4)
bridgetown-core (= 1.3.4)
builder (3.3.0)
colorator (1.1.0)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
csv (3.3.0)
date (3.4.1)
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
drb (2.2.1)
erubi (1.13.0)
faraday (2.12.0)
Expand All @@ -79,6 +85,10 @@ GEM
activesupport (>= 5.0.0, < 8.0)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
io-console (0.8.0)
irb (1.14.1)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.7.2)
kramdown (2.4.0)
rexml
Expand All @@ -90,26 +100,46 @@ GEM
rb-inotify (~> 0.9, >= 0.9.10)
logger (1.6.1)
minitest (5.25.1)
minitest-reporters (1.7.1)
ansi
builder
minitest (>= 5.0)
ruby-progressbar
net-http (0.4.1)
uri
nio4r (2.7.3)
nokolexbor (0.5.4)
nokolexbor (0.5.4-arm64-darwin)
nokolexbor (0.5.4-x86_64-darwin)
nokolexbor (0.5.4-x86_64-linux)
psych (5.2.1)
date
stringio
public_suffix (6.0.1)
puma (6.4.3)
nio4r (~> 2.0)
rack (3.1.8)
rack-test (2.1.0)
rack (>= 1.3)
rake (13.2.1)
rb-fsevent (0.11.2)
rb-inotify (0.11.1)
ffi (~> 1.0)
rdoc (6.8.1)
psych (>= 4.0.0)
reline (0.5.12)
io-console (~> 0.5)
rexml (3.3.8)
roda (3.85.0)
rack
rouge (4.4.0)
ruby-progressbar (1.13.0)
securerandom (0.3.1)
serbea (1.0.1)
activesupport (>= 6.0)
erubi (>= 1.10)
tilt (~> 2.0)
stringio (3.1.2)
thor (1.3.2)
tilt (2.4.0)
tzinfo (2.0.6)
Expand All @@ -133,7 +163,12 @@ PLATFORMS

DEPENDENCIES
bridgetown (~> 1.3.4)
debug
minitest
minitest-reporters
nokolexbor (~> 0.4)
puma (< 7)
rack-test

BUNDLED WITH
2.5.22
5 changes: 5 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ task :test do
Bridgetown::Commands::Build.start
end

require "minitest/test_task"
Minitest::TestTask.create(:test) do |t| # add on to the test task
t.warning = false
end

desc "Runs the clean command"
task :clean do
Bridgetown::Commands::Clean.start
Expand Down
11 changes: 11 additions & 0 deletions plugins/test_output.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module TestOutput
unless Bridgetown.env.development?
Bridgetown::Hooks.register_one :site, :post_write do
# Load test suite to run on exit
require "nokogiri"
Dir["test/**/*.rb"].each { |file| require_relative("../#{file}") }
rescue LoadError
Bridgetown.logger.warn "Testing:", "To run tests, you must first run `bundle install --with test`"
end
end
end
47 changes: 47 additions & 0 deletions test/bridgetown_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

# This file can be required by project test suites to set up the Minitest environment

require "bridgetown"

ENV["BRIDGETOWN_NO_BUNDLER_REQUIRE"] = nil
Bridgetown.begin!

Bridgetown::Builders::PluginBuilder.then do
Bridgetown::Builders::DSL::Inspectors.setup_nokolexbor
end

require "bridgetown-core/rack/boot"
class Bridgetown::Rack::Logger
def add(*)
super if ENV["SERVER_LOGS"] == "true"
end
end

Bridgetown::Current.preloaded_configuration = Bridgetown.configuration
Bridgetown::Rack.boot

require "rack/test"

class Bridgetown::Test < Minitest::Test
include Rack::Test::Methods

attr_reader :document

def roda_app_class = RodaApp

def app = roda_app_class.app

def site
roda_app_class.opts[:bridgetown_site]
end

def html(request) = @document = Nokolexbor::Document.parse(request.body)

def json(request) = @document = JSON.parse(request.body)

def routes = JSON.parse(File.read(
File.join(Bridgetown::Current.preloaded_configuration.root_dir,
".routes.json")
))
end
5 changes: 5 additions & 0 deletions test/minitest_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "minitest/autorun"
require "minitest/reporters"
Minitest::Reporters.use! [Minitest::Reporters::ProgressReporter.new]

require_relative "bridgetown_test"
9 changes: 9 additions & 0 deletions test/test_homepage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "minitest_helper"

class TestHomepage < Bridgetown::Test
def test_homepage
html get "/"

assert document.query_selector("body")
end
end

0 comments on commit 8cc2ba1

Please sign in to comment.