diff --git a/.travis.yml b/.travis.yml index 952e999999..db818fad23 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,9 +9,6 @@ addons: # Set the timezone for phantomjs with TZ # Set the timezone for karma with TIMEZONE -# -# The test cases are roughly split according to their test times. -# It would be better to use https://github.com/ArturT/knapsack. env: global: - TZ="Australia/Melbourne" @@ -21,7 +18,7 @@ env: - CI_NODE_INDEX=0 - CI_NODE_INDEX=1 - CI_NODE_INDEX=2 - - CI_NODE_INDEX=3 + - CI_NODE_INDEX=3 RSPEC_ENGINES="true" - CI_NODE_INDEX=4 KARMA="true" GITHUB_DEPLOY="true" before_script: @@ -42,6 +39,7 @@ before_script: script: - 'if [ "$KARMA" = "true" ]; then bundle exec rake karma:run; else echo "Skipping karma run"; fi' + - 'if [ "$RSPEC_ENGINES" = "true" ]; then bundle exec rake openfoodnetwork:specs:engines:rspec; else echo "Skipping RSpec run in engines"; fi' - "bundle exec rake 'knapsack:rspec[--format progress --tag ~performance]'" after_success: diff --git a/GETTING_STARTED.md b/GETTING_STARTED.md index 93cff8dad0..5a2e7eea54 100644 --- a/GETTING_STARTED.md +++ b/GETTING_STARTED.md @@ -69,10 +69,14 @@ Tests, both unit and integration, are based on RSpec. To run the test suite, fir bundle exec rake db:test:prepare -Then the tests can be run with: +Then the main application tests can be run with: bundle exec rspec spec +The tests of all custom engines can be run with: + + bundle exec rake openfoodnetwork:specs:engines:rspec + Note: If your OS is not explicitly supported in the setup guides then not all tests may pass. However, you may still be able to develop. Get in touch with the [#dev][slack-dev] channel on Slack to troubleshoot issues and determine if they will preclude you from contributing to OFN. Note: The time zone on your machine should match the one defined in `config/application.yml`. diff --git a/engines/web/spec/spec_helper.rb b/engines/web/spec/spec_helper.rb index 3306063166..9cfd0bc717 100644 --- a/engines/web/spec/spec_helper.rb +++ b/engines/web/spec/spec_helper.rb @@ -1,8 +1,3 @@ -ENV["RAILS_ENV"] = "test" - -require File.expand_path("dummy/config/environment.rb", __dir__) -require "rails/test_help" - -Rails.backtrace_cleaner.remove_silencers! +require "../../spec/spec_helper.rb" Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } diff --git a/lib/tasks/specs.rake b/lib/tasks/specs.rake new file mode 100644 index 0000000000..dddb5ffb54 --- /dev/null +++ b/lib/tasks/specs.rake @@ -0,0 +1,47 @@ +namespace :openfoodnetwork do + namespace :specs do + namespace :engines do + def detect_engine_paths + Pathname("engines/").children.select(&:directory?) + end + + def engine_name_for_engine(engine_path) + engine_path.basename.to_path + end + + def execute_rspec_for_engine(engine_path) + system "cd #{engine_path.expand_path} && DISABLE_KNAPSACK=true bundle exec rspec" + end + + engine_paths = detect_engine_paths + + engine_paths.each do |engine_path| + engine_name = engine_name_for_engine(engine_path) + + namespace engine_name do + desc "Run RSpec tests for engine \"#{engine_name}\"" + task :rspec do + success = execute_rspec_for_engine(engine_path) + abort "Failure when running tests for engine \"#{engine_name}\"" unless success + end + end + end + + namespace :all do + desc "Run RSpec tests for all engines" + task :rspec do + success = true + + engine_paths.each do |engine_path| + success = !!execute_rspec_for_engine(engine_path) && success + end + + abort "Failure encountered when running tests for engines" unless success + end + end + + desc "Alias for openfoodnetwork:specs:engines:all:rspec" + task rspec: "all:rspec" + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c198a68fbb..66924edea8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -6,9 +6,13 @@ require 'rubygems' # Require pry when we're not inside Travis-CI require 'pry' unless ENV['CI'] -require 'knapsack' -Knapsack.tracker.config({enable_time_offset_warning: false}) unless ENV['CI'] -Knapsack::Adapters::RSpecAdapter.bind +# This spec_helper.rb is being used by the custom engines in engines/. The engines are not set up to +# use Knapsack, and this provides the option to disable it when running the tests in CI services. +unless ENV['DISABLE_KNAPSACK'] + require 'knapsack' + Knapsack.tracker.config({enable_time_offset_warning: false}) unless ENV['CI'] + Knapsack::Adapters::RSpecAdapter.bind +end ENV["RAILS_ENV"] ||= 'test' require_relative "../config/environment"