Skip to content

Instantly share code, notes, and snippets.

View ArturT's full-sized avatar

Artur Trzop ArturT

View GitHub Profile
class GildedRose
def initialize(items)
@items = items
end
def update_quality()
@items.each do |item|
if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert"
if item.quality > 0
if item.name != "Sulfuras, Hand of Ragnaros"
@ArturT
ArturT / .circleci-config.yml
Last active December 5, 2023 12:01
CircleCI rerun failed tests in RSpec with Knapsack Pro Queue Mode. Thanks to that you can rerun only failed tests instead of rerunning the whole CI build. https://docs.knapsackpro.com/ruby/circleci/ & https://knapsackpro.com
- run:
name: Run tests
command: |
export CIRCLE_TEST_REPORTS=/tmp/test-results
mkdir -p $CIRCLE_TEST_REPORTS
# split slow spec files by test examples
# https://docs.knapsackpro.com/ruby/split-by-test-examples/
export KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES=true
@ArturT
ArturT / spec_helper.rb
Created September 29, 2023 12:15
Knapsack Pro Queue Mode hooks examples
before_suite_block = proc {
puts '+'*100
puts 'RSpec before suite hook called only once'
}
unless KnapsackPro::Config::Env.queue_recording_enabled?
RSpec.configure do |config|
config.before(:suite) do
before_suite_block.call
puts 'Run this only when not using Knapsack Pro Queue Mode'
@ArturT
ArturT / redis_latency.rb
Created September 22, 2023 14:20
Measure Redis latency from inside of Heroku Dyno
# repalce REDIS_CONNECTION with your Redis instance
#
# run this script in Rails console.
# For example start Heroku Dyno with Rails console to measure the Redis latency from inside of the Heroku Dyno
#
# Do you like it? Check what we do: https://knapsackpro.com
rtts = []
100.times do
a = b = 0; a = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC, :microsecond); REDIS_CONNECTION.ping; b = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC, :microsecond); rtt = b - a;
@ArturT
ArturT / api_controller.rb
Last active July 20, 2023 06:44
How to rescue ActionDispatch::Http::MimeNegotiation::InvalidType in API controller for Rails 6.1+ and render nice JSON error. Learn more how you could run RSpec/Minitest tests faster in Rails app https://docs.knapsackpro.com/2020/how-to-speed-up-ruby-and-javascript-tests-with-ci-parallelisation
# This is example how to rescue from exception ActionDispatch::Http::MimeNegotiation::InvalidType
# and show nice JSON error in your API
module API
class BaseController < ActionController::API
def process_action(*args)
super
rescue ActionDispatch::Http::MimeNegotiation::InvalidType => exception
# set valid Content-Type to be able to call render method below
request.headers['Content-Type'] = 'application/json'
render status: 400, json: { errors: [exception.message] }
@ArturT
ArturT / spec_helper.rb
Created March 20, 2023 18:56
Preview executed tests in Knapsack Pro Queue Mode for RSpec
KnapsackPro::Hooks::Queue.after_subset_queue do |queue_id, subset_queue_id|
puts '\\'*100
puts 'Executed tests for a batch of tests fetched from Queue API'
Dir.glob(".knapsack_pro/queue/#{queue_id}/*.json").each do |file|
report = JSON.parse(File.read(file))
puts report.inspect
end
puts '/'*100
end
@ArturT
ArturT / run_flaky_test_until_fails.sh
Last active February 23, 2023 17:07
How to reproduce a flaky test. Run a flaky test until it fails.
# bash
while [ "$?" == 0 ]; do rspec spec/flaky_spec.rb:123; done
# zsh
while rspec spec/flaky_spec.rb:123; do :; done
@ArturT
ArturT / Jest-GitHub-Actions-JavaScript.md
Last active December 1, 2022 07:31
GitHub Actions config for NodeJS and Jest tests. Running parallel jobs for Jest test suite with https://knapsackpro.com/?utm_source=github&utm_medium=gist&utm_campaign=jest-on-github-actions&utm_content=sign_up

GitHub Actions config for running Jest tests with parallel jobs

@knapsack-pro/jest supports environment variables provided by GitHub Actions to run your tests. You have to define a few things in .github/workflows/main.yaml config file.

  • You need to set KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST environment variable in GitHub repository Settings -> Secrets. See creating and using secrets in GitHub Actions.
  • You should create as many parallel jobs as you need with matrix.ci_node_total and matrix.ci_node_index properties. If your test suite is slow you should use more parallel jobs.

Below you can find config for GitHub Actions.

Video demo Jest and GitHub Actions

@ArturT
ArturT / main.yaml
Last active November 9, 2022 06:10
GitHub Actions - how to run parallel tests in RSpec for Ruby on Rails project. See article how to run RSpec on GitHub Actions for Ruby on Rails app using parallel jobs https://docs.knapsackpro.com/2019/how-to-run-rspec-on-github-actions-for-ruby-on-rails-app-using-parallel-jobs or sign up at https://knapsackpro.com/?utm_source=github&utm_medium=…
# .github/workflows/main.yaml
name: Main
on: [push]
jobs:
test:
runs-on: ubuntu-latest
# If you need DB like PostgreSQL, Redis then define service below.
@ArturT
ArturT / openssl-certificate-verify-failed.md
Last active February 24, 2022 17:24
OpenSSL::SSL::SSLError SSL_connect returned=1 errno=0 state=error: certificate verify failed - https://knapsackpro.com/?utm_source=github&utm_medium=gist&utm_campaign=github-gist-openssl-certificate-verify-failed