Skip to content

Commit

Permalink
Add basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Envek committed May 30, 2022
1 parent f7d5595 commit e327dc4
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 16 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Lint

on:
pull_request:
push:
branches:
- '**'
tags-ignore:
- 'v*'

jobs:
rubocop:
# Skip running tests for local pull requests (use push event instead), run only for foreign ones
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.owner.login != github.event.pull_request.base.repo.owner.login
name: RuboCop
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
with:
ruby-version: "3.1"
bundler-cache: true
- name: Lint Ruby code with RuboCop
run: |
bundle exec rubocop
24 changes: 15 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Run tests
name: Tests

on:
pull_request:
Expand All @@ -10,21 +10,29 @@ on:

jobs:
test:
name: "Run tests"
if: "! contains(toJSON(github.event.commits.latest.message), '[ci skip]')"
name: 'Rails ${{ matrix.rails }} × Ruby ${{ matrix.ruby }}'
# Skip running tests for local pull requests (use push event instead), run only for foreign ones
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.owner.login != github.event.pull_request.base.repo.owner.login
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- ruby: 3.0
- ruby: 2.7
- ruby: 2.6
- ruby: 2.5
- ruby: "3.1"
rails: "HEAD"
- ruby: "3.0"
rails: "7.0"
- ruby: "2.7"
rails: "6.1"
- ruby: "2.6"
rails: "6.0"
- ruby: "2.5"
rails: "5.2"
container:
image: ruby:${{ matrix.ruby }}
env:
CI: true
RAILS_VERSION: ${{ matrix.rails }}
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
Expand All @@ -41,7 +49,5 @@ jobs:
bundle config path vendor/bundle
bundle install
bundle update
- name: Run Rubocop
run: bundle exec rubocop
- name: Run RSpec
run: bundle exec rspec
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ Style/TrailingCommaInHashLiteral:
Enabled: true
EnforcedStyleForMultiline: consistent_comma

Bundler/DuplicatedGem:
Enabled: false
21 changes: 19 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,26 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
# Specify your gem's dependencies in yabeda-rails.gemspec
gemspec

rails_version = ENV.fetch("RAILS_VERSION", "~> 7.0")
case rails_version
when "HEAD"
git "https://github.com/rails/rails.git" do
gem "rails"
gem "activesupport"
gem "railties"
end
else
rails_version = "~> #{rails_version}.0" if rails_version.match?(/^\d+\.\d+$/)
gem "rails", rails_version
gem "activesupport", rails_version
gem "railties", rails_version
end

group :development, :test do
gem "pry"
gem "pry-byebug", platform: :mri
gem "yabeda", "~> 0.11" # Test helpers
gem "rspec-rails"

gem "debug"

gem "rubocop", "~> 1.8"
gem "rubocop-rspec"
Expand Down
15 changes: 15 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# frozen_string_literal: true

ENV["RAILS_ENV"] = "test"

require "bundler/setup"
require "debug"
require "yabeda/rails"
require "yabeda/rspec"

require_relative "support/rails_app"

require "rspec/rails"

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand All @@ -13,4 +21,11 @@
config.expect_with :rspec do |c|
c.syntax = :expect
end

Kernel.srand config.seed
config.order = :random

config.before(:suite) do
Yabeda::Rails.install!
end
end
32 changes: 32 additions & 0 deletions spec/support/rails_app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require "rails"
require "action_controller/railtie"
require "active_support/railtie"

class TestApplication < Rails::Application
config.logger = Logger.new($stdout)
config.log_level = :fatal
config.consider_all_requests_local = true
config.eager_load = true

routes.append do
get "/hello/world" => "hello#world"
get "/hello/long" => "hello#long"
end
end

class HelloController < ActionController::API
def world
render json: { hello: :world }
end

def long
sleep(0.01)
render json: { good: :morning }
end
end

Rails.application = TestApplication

TestApplication.initialize!
25 changes: 20 additions & 5 deletions spec/yabeda/rails_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
# frozen_string_literal: true

RSpec.describe Yabeda::Rails do
it "has a version number" do
expect(Yabeda::Rails::VERSION).not_to be nil
require "action_controller/test_case"

RSpec.describe Yabeda::Rails, type: :integration do
include ActionDispatch::Integration::Runner
include ActionDispatch::IntegrationTest::Behavior

def app
TestApplication
end

it "increments counters for every request" do
expect { get "/hello/world" }.to \
increment_yabeda_counter(Yabeda.rails.requests_total)
.with_tags(controller: "hello", action: "world", status: 200, method: "get", format: :html)
.by(1)
end

xit "does something useful" do
# Nothing here
it "measure action runtime for every request" do
expect { get "/hello/long" }.to \
measure_yabeda_histogram(Yabeda.rails.request_duration)
.with_tags(controller: "hello", action: "long", status: 200, method: "get", format: :html)
.with(be_between(0.005, 0.05))
end
end

0 comments on commit e327dc4

Please sign in to comment.