From c72def9c79478ca17724c59bf3e55536eea4bca8 Mon Sep 17 00:00:00 2001 From: Vladimir Dementyev Date: Tue, 28 May 2024 09:30:15 -0700 Subject: [PATCH] let's thrust --- .gem_release.yml | 4 ++ .github/ISSUE_TEMPLATE/bug_report.md | 25 ++++++++++++ .github/ISSUE_TEMPLATE/feature_request.md | 25 ++++++++++++ .github/PULL_REQUEST_TEMPLATE.md | 24 +++++++++++ .github/workflows/release.yml | 27 +++++++++++++ .github/workflows/rubocop.yml | 23 +++++++++++ .github/workflows/test.yml | 35 ++++++++++++++++ .gitignore | 44 ++++++++++++++++++++ .rubocop.yml | 25 ++++++++++++ CHANGELOG.md | 3 ++ Gemfile | 16 ++++++++ LICENSE.txt | 23 +++++++++++ README.md | 49 +++++++++++++++++++++++ RELEASING.md | 43 ++++++++++++++++++++ Rakefile | 25 ++++++++++++ capybara-thruster.gemspec | 34 ++++++++++++++++ gemfiles/rubocop.gemfile | 6 +++ lib/capybara-thruster.rb | 4 ++ lib/capybara/thruster.rb | 25 ++++++++++++ lib/capybara/thruster/version.rb | 7 ++++ test/test_helper.rb | 13 ++++++ 21 files changed, 480 insertions(+) create mode 100644 .gem_release.yml create mode 100644 .github/ISSUE_TEMPLATE/bug_report.md create mode 100644 .github/ISSUE_TEMPLATE/feature_request.md create mode 100644 .github/PULL_REQUEST_TEMPLATE.md create mode 100644 .github/workflows/release.yml create mode 100644 .github/workflows/rubocop.yml create mode 100644 .github/workflows/test.yml create mode 100644 .gitignore create mode 100644 .rubocop.yml create mode 100644 CHANGELOG.md create mode 100644 Gemfile create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 RELEASING.md create mode 100644 Rakefile create mode 100644 capybara-thruster.gemspec create mode 100644 gemfiles/rubocop.gemfile create mode 100644 lib/capybara-thruster.rb create mode 100644 lib/capybara/thruster.rb create mode 100644 lib/capybara/thruster/version.rb create mode 100644 test/test_helper.rb diff --git a/.gem_release.yml b/.gem_release.yml new file mode 100644 index 0000000..c6abbb5 --- /dev/null +++ b/.gem_release.yml @@ -0,0 +1,4 @@ +bump: + file: lib/capybara/thruster/version.rb + skip_ci: true + diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..887eb40 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,25 @@ +--- +name: Bug report +about: Create a report to help us improve +title: '' +labels: '' +assignees: palkan + +--- + +## What did you do? + +## What did you expect to happen? + +## What actually happened? + +## Additional context + +## Environment + +**Ruby Version:** + +**Framework Version (Rails, whatever):** + +**Capybara Thruster Version:** + diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..f5aabca --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,25 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: enhancement +assignees: palkan + +--- + +## Is your feature request related to a problem? Please describe. + +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +## Describe the solution you'd like + +A clear and concise description of what you want to happen. + +## Describe alternatives you've considered + +A clear and concise description of any alternative solutions or features you've considered. + +## Additional context + +Add any other context or screenshots about the feature request here. + diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..34d2c15 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,24 @@ + + +## What is the purpose of this pull request? + + + +## What changes did you make? (overview) + +## Is there anything you'd like reviewers to focus on? + +## Checklist + +- [ ] I've added tests for this change +- [ ] I've added a Changelog entry +- [ ] I've updated a documentation + diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..f1d5b7b --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,27 @@ +name: Release gems +on: + workflow_dispatch: + push: + tags: + - v* + +jobs: + release: + runs-on: ubuntu-latest + permissions: + contents: write + id-token: write + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 # Fetch current tag as annotated. See https://github.com/actions/checkout/issues/290 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: 3.2 + - name: Configure RubyGems Credentials + uses: rubygems/configure-rubygems-credentials@main + - name: Publish to RubyGems + run: | + gem install gem-release + gem release diff --git a/.github/workflows/rubocop.yml b/.github/workflows/rubocop.yml new file mode 100644 index 0000000..8d01085 --- /dev/null +++ b/.github/workflows/rubocop.yml @@ -0,0 +1,23 @@ +name: Lint Ruby + +on: + push: + branches: + - master + pull_request: + +jobs: + rubocop: + runs-on: ubuntu-latest + env: + BUNDLE_GEMFILE: gemfiles/rubocop.gemfile + steps: + - uses: actions/checkout@v4 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: 3.3 + bundler-cache: true + - name: Lint Ruby code with RuboCop + run: | + bundle exec rubocop + diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..76e8745 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,35 @@ +name: Build + +on: + push: + branches: + - master + pull_request: + +jobs: + rspec: + runs-on: ubuntu-latest + env: + BUNDLE_JOBS: 4 + BUNDLE_RETRY: 3 + + CI: true + strategy: + fail-fast: false + matrix: + + ruby: ["2.7", "3.0", "3.1", "3.2", "3.3"] + + steps: + - uses: actions/checkout@v4 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + + + - name: Run tests + run: | + bundle exec rake test + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6499097 --- /dev/null +++ b/.gitignore @@ -0,0 +1,44 @@ +# Numerous always-ignore extensions +*.diff +*.err +*.orig +*.log +*.rej +*.swo +*.swp +*.vi +*~ +*.sass-cache +*.iml +.idea/ + +# Sublime +*.sublime-project +*.sublime-workspace + +# OS or Editor folders +.DS_Store +.cache +.project +.settings +.tmproj +Thumbs.db + +.bundle/ +log/*.log +pkg/ +spec/dummy/db/*.sqlite3 +spec/dummy/db/*.sqlite3-journal +spec/dummy/tmp/ + +Gemfile.lock +Gemfile.local +.rspec-local +.ruby-version +*.gem + +tmp/ +.rbnext/ + +gemfiles/*.lock + diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..f22646b --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,25 @@ +require: + - standard + +inherit_gem: + standard: config/base.yml + + +AllCops: + Exclude: + - 'bin/*' + - 'tmp/**/*' + - 'Gemfile' + - 'vendor/**/*' + - 'gemfiles/**/*' + - 'lib/.rbnext/**/*' + - 'lib/generators/**/templates/*.rb' + - '.github/**/*' + DisplayCopNames: true + SuggestExtensions: false + NewCops: disable + TargetRubyVersion: 2.7 + +Style/FrozenStringLiteralComment: + Enabled: true + diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..f870b20 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +# Change log + +## master diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..9cf87f4 --- /dev/null +++ b/Gemfile @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +gem "debug", platform: :mri + +gemspec + +eval_gemfile "gemfiles/rubocop.gemfile" + +local_gemfile = "#{File.dirname(__FILE__)}/Gemfile.local" + +if File.exist?(local_gemfile) + eval(File.read(local_gemfile)) # rubocop:disable Security/Eval +end + diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..3d13776 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,23 @@ +Copyright (c) 2024 Vladimir Dementyev + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..44924fe --- /dev/null +++ b/README.md @@ -0,0 +1,49 @@ +[![Gem Version](https://badge.fury.io/rb/capybara-thruster.svg)](https://rubygems.org/gems/capybara-thruster) +[![Build](https://github.com/evilmartians/capybara-thruster/workflows/Build/badge.svg)](https://github.com/palkan/capybara-thruster/actions) + +# Capybara Thruster server + +This gem makes it possible to use [Thruster][] as a Capybara server. Run your browser test with HTTP/2 enabled and static assets served via Thruster for faster load times! + +> [TIP!] +> Using AnyCable? This gem works with [AnyCable-d Thruster][anycable-thruster], so you can run your system tests against a real AnyCable server with all its features! + +## Getting started + +Adding the gem to your project: + +```ruby +# Gemfile +gem "capybara-thruster", group: :test +``` + +Then, configure Capybara to use Thruster as a server: + +```ruby +Capybara.server = :thruster + +# You can also specify some options. + +# For example, if you want to see the server output, +# pass the `debug: true` option +Capybara.server = :thruster, {debug: true} + +# To customize the server settings, you can pass arbitrary environment +# variables via the `env` option +Capybara.server = :thruster, {env: {"DEBUG" => "true"}} +``` + +## Contributing + +Bug reports and pull requests are welcome on GitHub at [https://github.com/evilmartians/capybara-thruster](https://github.com/evilmartians/capybara-thruster). + +## Credits + +This gem is generated via [`newgem` template](https://github.com/palkan/newgem) by [@palkan](https://github.com/palkan). + +## License + +The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). + +[Thruster]: https://github.com/basecamp/thruster +[anycable-thruster]: https://github.com/anycable/thruster diff --git a/RELEASING.md b/RELEASING.md new file mode 100644 index 0000000..cb6e382 --- /dev/null +++ b/RELEASING.md @@ -0,0 +1,43 @@ +# How to release a gem + +This document describes a process of releasing a new version of a gem. + +1. Bump version. + +```sh +git commit -m "Bump 1.." +``` + +We're (kinda) using semantic versioning: + +- Bugfixes should be released as fast as possible as patch versions. +- New features could be combined and released as minor or patch version upgrades (depending on the _size of the feature_—it's up to maintainers to decide). +- Breaking API changes should be avoided in minor and patch releases. +- Breaking dependencies changes (e.g., dropping older Ruby support) could be released in minor versions. + +How to bump a version: + +- Change the version number in `lib/capybara/thruster/version.rb` file. +- Update the changelog (add new heading with the version name and date). +- Update the installation documentation if necessary (e.g., during minor and major updates). + +2. Push code to GitHub and make sure CI passes. + +```sh +git push +``` + +3. Release a gem. + +```sh +gem release -t +git push --tags +``` + +We use [gem-release](https://github.com/svenfuchs/gem-release) for publishing gems with a single command: + +```sh +gem release -t +``` + +Don't forget to push tags and write release notes on GitHub (if necessary). diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..0504839 --- /dev/null +++ b/Rakefile @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require "bundler/gem_tasks" +require "rake/testtask" + +Rake::TestTask.new(:test) do |t| + t.libs << "test" + t.libs << "lib" + t.test_files = FileList["test/**/*_test.rb"] + t.warning = false +end + +begin + require "rubocop/rake_task" + RuboCop::RakeTask.new + + RuboCop::RakeTask.new("rubocop:md") do |task| + task.options << %w[-c .rubocop-md.yml] + end +rescue LoadError + task(:rubocop) {} + task("rubocop:md") {} +end + +task default: %w[rubocop test] diff --git a/capybara-thruster.gemspec b/capybara-thruster.gemspec new file mode 100644 index 0000000..c17be1f --- /dev/null +++ b/capybara-thruster.gemspec @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require_relative "lib/capybara/thruster/version" + +Gem::Specification.new do |s| + s.name = "capybara-thruster" + s.version = Capybara::Thruster::VERSION + s.authors = ["Vladimir Dementyev"] + s.email = ["Vladimir Dementyev"] + s.homepage = "https://github.com/evilmartians/capybara-thruster" + s.summary = "Example description" + s.description = "Example description" + + s.metadata = { + "bug_tracker_uri" => "https://github.com/evilmartians/capybara-thruster/issues", + "changelog_uri" => "https://github.com/evilmartians/capybara-thruster/blob/master/CHANGELOG.md", + "documentation_uri" => "https://github.com/evilmartians/capybara-thruster", + "homepage_uri" => "https://github.com/evilmartians/capybara-thruster", + "source_code_uri" => "https://github.com/evilmartians/capybara-thruster" + } + + s.license = "MIT" + + s.files = Dir.glob("lib/**/*") + Dir.glob("bin/**/*") + %w[README.md LICENSE.txt CHANGELOG.md] + s.require_paths = ["lib"] + s.required_ruby_version = ">= 2.7" + + s.add_dependency "childprocess", ">= 4.0" + s.add_dependency "puma" + + s.add_development_dependency "bundler", ">= 1.15" + s.add_development_dependency "rake", ">= 13.0" + s.add_development_dependency "minitest", "~> 5.0" +end diff --git a/gemfiles/rubocop.gemfile b/gemfiles/rubocop.gemfile new file mode 100644 index 0000000..1cf3da8 --- /dev/null +++ b/gemfiles/rubocop.gemfile @@ -0,0 +1,6 @@ +source "https://rubygems.org" do + + + gem "standard", "~> 1.0" +end + diff --git a/lib/capybara-thruster.rb b/lib/capybara-thruster.rb new file mode 100644 index 0000000..c43d044 --- /dev/null +++ b/lib/capybara-thruster.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +require "capybara/thruster/version" +require "capybara/thruster" diff --git a/lib/capybara/thruster.rb b/lib/capybara/thruster.rb new file mode 100644 index 0000000..30c2aea --- /dev/null +++ b/lib/capybara/thruster.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require "childprocess" +require "capybara" + +# Replaces the default Puma server on CAPYBARA_SERVER_PORT port with Thruster server. +# Requests are proxied to the Puma server running on the internal port (CAPYBARA_SERVER_PORT + 1). +Capybara.register_server :thruster do |app, port, host, **options| + puma_port = port + 1 + + # TODO: Figure out how to avoid sleep here + process = ChildProcess.build("bundle", "exec", "thrust", "ruby", "-e", "sleep") + process.environment["TARGET_PORT"] = puma_port.to_s + process.environment["HTTP_PORT"] = port.to_s + + # Additional environment variables + options.fetch(:env, {}).each { |k, v| process.environment[k.to_s] = v.to_s } + + process.io.inherit! if options.delete(:debug) == true + process.start + + at_exit { process.stop } + + Capybara.servers[:puma].call(app, puma_port, host) +end diff --git a/lib/capybara/thruster/version.rb b/lib/capybara/thruster/version.rb new file mode 100644 index 0000000..a3ba000 --- /dev/null +++ b/lib/capybara/thruster/version.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module Capybara + module Thruster # :nodoc: + VERSION = "0.0.1" + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..d019cfa --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +begin + require "debug" unless ENV["CI"] +rescue LoadError +end + +$LOAD_PATH.unshift File.expand_path("../lib", __dir__) +require "capybara-thruster" + +Dir["#{__dir__}/support/**/*.rb"].sort.each { |f| require f } + +require "minitest/autorun"