From 86a7b60f2c7cc3102a2338d56d7f54695bc6ddc8 Mon Sep 17 00:00:00 2001 From: Jan Grodowski Date: Sun, 30 Jul 2023 11:32:28 +0200 Subject: [PATCH 1/2] Add some demo code (incl unteseted ERB) --- .github/workflows/test.yml | 10 +++++----- Gemfile | 3 +++ Gemfile.lock | 12 ++++++++++++ app/controllers/application_controller.rb | 10 +++++++++- app/views/home/index.haml | 8 ++++++++ app/views/home/index.html.erb | 2 -- app/views/layouts/application.html.erb | 3 +++ test/controllers/home_controller_test.rb | 2 +- test/test_helper.rb | 4 ++-- 9 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 app/views/home/index.haml delete mode 100644 app/views/home/index.html.erb diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c316742..7f549e1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,10 +1,10 @@ name: Rails Unit Tests -on: [push, pull_request] +on: [push] jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up Ruby uses: ruby/setup-ruby@v1 with: @@ -20,6 +20,6 @@ jobs: bundle exec rails db:migrate bundle exec rails test ruby -e "$(curl -s https://undercover-ci.com/uploader.rb)" -- \ - --repo twitchy-tortoise/undercover-test \ - --commit $GITHUB_SHA \ - --lcov coverage/lcov/undercover-test.lcov + --repo twitchy-tortoise/undercover-test \ + --commit $GITHUB_SHA \ + --lcov coverage/lcov/undercover-test.lcov diff --git a/Gemfile b/Gemfile index ab10196..16db30c 100644 --- a/Gemfile +++ b/Gemfile @@ -20,6 +20,9 @@ gem 'jbuilder' # Use Active Model has_secure_password # gem 'bcrypt', '~> 3.1.7' +# Try out haml coverage +gem 'haml-rails' + # Use Active Storage variant # gem 'image_processing', '~> 1.2' diff --git a/Gemfile.lock b/Gemfile.lock index a0a1793..b4fb576 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -90,6 +90,15 @@ GEM ffi (1.15.5) globalid (1.1.0) activesupport (>= 5.0) + haml (6.1.1) + temple (>= 0.8.2) + thor + tilt + haml-rails (2.1.0) + actionpack (>= 5.1) + activesupport (>= 5.1) + haml (>= 4.0.6) + railties (>= 5.1) i18n (1.14.1) concurrent-ruby (~> 1.0) jbuilder (2.11.5) @@ -195,7 +204,9 @@ GEM sprockets (>= 3.0.0) sqlite3 (1.6.3) mini_portile2 (~> 2.8.0) + temple (0.10.2) thor (1.2.2) + tilt (2.2.0) timeout (0.4.0) turbolinks (5.2.1) turbolinks-source (~> 5.2) @@ -226,6 +237,7 @@ DEPENDENCIES bootsnap byebug capybara (>= 2.15) + haml-rails jbuilder listen mocha diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 62fb35b..a6aa874 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,8 +1,16 @@ class ApplicationController < ActionController::Base private + def check_cookie + if params[:x] != ENV.fetch("CODE", "beta") + redirect_to(bounce_home_index_url) and return + end + + cookies[:beta_sign_in] = { value: 1, expires: 1.year } + end + def check_beta_cookie - redirect_to(bounce_home_index_url) and return if params[:x] != ENV.fetch("BETA_CODE", "beta") + redirect_to(bounce_home_index_url) and return if params[:x] != ENV.fetch("CODE", "beta") cookies[:beta_sign_in] = { value: 1, expires: 1.year } end diff --git a/app/views/home/index.haml b/app/views/home/index.haml new file mode 100644 index 0000000..36b6772 --- /dev/null +++ b/app/views/home/index.haml @@ -0,0 +1,8 @@ +%p + Hi! ✅ + From: + =controller.class.name +- if controller.class.name == 'WelcomeController' + %p Unreachable element +- else + %p Unreachable element \ No newline at end of file diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb deleted file mode 100644 index 4bfe424..0000000 --- a/app/views/home/index.html.erb +++ /dev/null @@ -1,2 +0,0 @@ -Hi! ✅ -From: <%= controller.class.name %> \ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index c8034e2..135f9c8 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -10,6 +10,9 @@ + <% if controller.class.name == "FancyController" %> +

Undercover Agent 🕵️‍♂️

+ <% end %> <%= yield %> diff --git a/test/controllers/home_controller_test.rb b/test/controllers/home_controller_test.rb index 929e84f..bb849e3 100644 --- a/test/controllers/home_controller_test.rb +++ b/test/controllers/home_controller_test.rb @@ -3,7 +3,7 @@ class HomeControllerTest < ActionDispatch::IntegrationTest test "the truth" do ApplicationController.any_instance.stubs(:current_user).returns(nil) - ENV['BETA_CODE'] = 'false' + ENV['CODE'] = 'false' get "/home?x=falsey" assert_equal 302, status diff --git a/test/test_helper.rb b/test/test_helper.rb index 431094d..b090144 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,3 +1,5 @@ +require_relative '../config/environment' + require 'simplecov' require 'simplecov-lcov' SimpleCov::Formatter::LcovFormatter.config.report_with_single_file = true @@ -5,12 +7,10 @@ SimpleCov.start do add_filter(/^\/test\//) enable_coverage(:branch) - enable_coverage_for_eval end ENV['RAILS_ENV'] ||= 'test' require 'mocha/minitest' -require_relative '../config/environment' require 'rails/test_help' class ActiveSupport::TestCase # Run tests in parallel with specified workers From aea765a9b29092a034bf58d19e70f785b42291ee Mon Sep 17 00:00:00 2001 From: Jan Grodowski Date: Fri, 2 Feb 2024 17:23:31 +0100 Subject: [PATCH 2/2] Demo correct commit SHA incl. github action merge commit GitHub will create multiple SHAs for pull request workflows: https://www.kenmuse.com/blog/the-many-shas-of-a-github-pull-request/ --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7f549e1..008feb3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -21,5 +21,5 @@ jobs: bundle exec rails test ruby -e "$(curl -s https://undercover-ci.com/uploader.rb)" -- \ --repo twitchy-tortoise/undercover-test \ - --commit $GITHUB_SHA \ + --commit {{ github.event.pull_request.head.sha || github.sha }} \ --lcov coverage/lcov/undercover-test.lcov