diff options
Diffstat (limited to 'ci')
-rwxr-xr-x | ci/custom_cops/bin/test | 5 | ||||
-rw-r--r-- | ci/custom_cops/lib/custom_cops.rb | 4 | ||||
-rw-r--r-- | ci/custom_cops/lib/custom_cops/assert_not.rb | 40 | ||||
-rw-r--r-- | ci/custom_cops/lib/custom_cops/refute_not.rb | 71 | ||||
-rw-r--r-- | ci/custom_cops/test/custom_cops/assert_not_test.rb | 42 | ||||
-rw-r--r-- | ci/custom_cops/test/custom_cops/refute_not_test.rb | 66 | ||||
-rw-r--r-- | ci/custom_cops/test/support/cop_helper.rb | 47 | ||||
-rw-r--r-- | ci/phantomjs.js | 149 | ||||
-rw-r--r-- | ci/qunit-selenium-runner.rb | 16 | ||||
-rwxr-xr-x | ci/travis.rb | 4 |
10 files changed, 293 insertions, 151 deletions
diff --git a/ci/custom_cops/bin/test b/ci/custom_cops/bin/test new file mode 100755 index 0000000000..495ffec83a --- /dev/null +++ b/ci/custom_cops/bin/test @@ -0,0 +1,5 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +COMPONENT_ROOT = File.expand_path("..", __dir__) +require_relative "../../../tools/test" diff --git a/ci/custom_cops/lib/custom_cops.rb b/ci/custom_cops/lib/custom_cops.rb new file mode 100644 index 0000000000..157b8247e4 --- /dev/null +++ b/ci/custom_cops/lib/custom_cops.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +require_relative "custom_cops/refute_not" +require_relative "custom_cops/assert_not" diff --git a/ci/custom_cops/lib/custom_cops/assert_not.rb b/ci/custom_cops/lib/custom_cops/assert_not.rb new file mode 100644 index 0000000000..e722448e21 --- /dev/null +++ b/ci/custom_cops/lib/custom_cops/assert_not.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +module CustomCops + # Enforces the use of `assert_not` over `assert !`. + # + # @example + # # bad + # assert !x + # assert ! x + # + # # good + # assert_not x + # + class AssertNot < RuboCop::Cop::Cop + MSG = "Prefer `assert_not` over `assert !`" + + def_node_matcher :offensive?, "(send nil? :assert (send ... :!))" + + def on_send(node) + add_offense(node) if offensive?(node) + end + + def autocorrect(node) + expression = node.loc.expression + + ->(corrector) do + corrector.replace( + expression, + corrected_source(expression.source) + ) + end + end + + private + + def corrected_source(source) + source.gsub(/^assert(\(| ) *! */, "assert_not\\1") + end + end +end diff --git a/ci/custom_cops/lib/custom_cops/refute_not.rb b/ci/custom_cops/lib/custom_cops/refute_not.rb new file mode 100644 index 0000000000..3e89e0fd32 --- /dev/null +++ b/ci/custom_cops/lib/custom_cops/refute_not.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +module CustomCops + # Enforces the use of `#assert_not` methods over `#refute` methods. + # + # @example + # # bad + # refute false + # refute_empty [1, 2, 3] + # refute_equal true, false + # + # # good + # assert_not false + # assert_not_empty [1, 2, 3] + # assert_not_equal true, false + # + class RefuteNot < RuboCop::Cop::Cop + MSG = "Prefer `%<assert_method>s` over `%<refute_method>s`" + + CORRECTIONS = { + refute: "assert_not", + refute_empty: "assert_not_empty", + refute_equal: "assert_not_equal", + refute_in_delta: "assert_not_in_delta", + refute_in_epsilon: "assert_not_in_epsilon", + refute_includes: "assert_not_includes", + refute_instance_of: "assert_not_instance_of", + refute_kind_of: "assert_not_kind_of", + refute_nil: "assert_not_nil", + refute_operator: "assert_not_operator", + refute_predicate: "assert_not_predicate", + refute_respond_to: "assert_not_respond_to", + refute_same: "assert_not_same", + refute_match: "assert_no_match" + }.freeze + + OFFENSIVE_METHODS = CORRECTIONS.keys.freeze + + def_node_matcher :offensive?, "(send nil? #offensive_method? ...)" + + def on_send(node) + return unless offensive?(node) + + message = offense_message(node.method_name) + add_offense(node, location: :selector, message: message) + end + + def autocorrect(node) + ->(corrector) do + corrector.replace( + node.loc.selector, + CORRECTIONS[node.method_name] + ) + end + end + + private + + def offensive_method?(method_name) + OFFENSIVE_METHODS.include?(method_name) + end + + def offense_message(method_name) + format( + MSG, + refute_method: method_name, + assert_method: CORRECTIONS[method_name] + ) + end + end +end diff --git a/ci/custom_cops/test/custom_cops/assert_not_test.rb b/ci/custom_cops/test/custom_cops/assert_not_test.rb new file mode 100644 index 0000000000..abb151aeb4 --- /dev/null +++ b/ci/custom_cops/test/custom_cops/assert_not_test.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require "support/cop_helper" +require_relative "../../lib/custom_cops/assert_not" + +class AssertNotTest < ActiveSupport::TestCase + include CopHelper + + setup do + @cop = CustomCops::AssertNot.new + end + + test "rejects 'assert !'" do + inspect_source @cop, "assert !x" + assert_offense @cop, "^^^^^^^^^ Prefer `assert_not` over `assert !`" + end + + test "rejects 'assert !' with a complex value" do + inspect_source @cop, "assert !a.b(c)" + assert_offense @cop, "^^^^^^^^^^^^^^ Prefer `assert_not` over `assert !`" + end + + test "autocorrects `assert !`" do + corrected = autocorrect_source(@cop, "assert !false") + assert_equal "assert_not false", corrected + end + + test "autocorrects `assert !` with extra spaces" do + corrected = autocorrect_source(@cop, "assert ! false") + assert_equal "assert_not false", corrected + end + + test "autocorrects `assert !` with parentheses" do + corrected = autocorrect_source(@cop, "assert(!false)") + assert_equal "assert_not(false)", corrected + end + + test "accepts `assert_not`" do + inspect_source @cop, "assert_not x" + assert_empty @cop.offenses + end +end diff --git a/ci/custom_cops/test/custom_cops/refute_not_test.rb b/ci/custom_cops/test/custom_cops/refute_not_test.rb new file mode 100644 index 0000000000..f0f6eaeda0 --- /dev/null +++ b/ci/custom_cops/test/custom_cops/refute_not_test.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +require "support/cop_helper" +require_relative "../../lib/custom_cops/refute_not" + +class RefuteNotTest < ActiveSupport::TestCase + include CopHelper + + setup do + @cop = CustomCops::RefuteNot.new + end + + { + refute: :assert_not, + refute_empty: :assert_not_empty, + refute_equal: :assert_not_equal, + refute_in_delta: :assert_not_in_delta, + refute_in_epsilon: :assert_not_in_epsilon, + refute_includes: :assert_not_includes, + refute_instance_of: :assert_not_instance_of, + refute_kind_of: :assert_not_kind_of, + refute_nil: :assert_not_nil, + refute_operator: :assert_not_operator, + refute_predicate: :assert_not_predicate, + refute_respond_to: :assert_not_respond_to, + refute_same: :assert_not_same, + refute_match: :assert_no_match + }.each do |refute_method, assert_method| + test "rejects `#{refute_method}` with a single argument" do + inspect_source(@cop, "#{refute_method} a") + assert_offense @cop, offense_message(refute_method, assert_method) + end + + test "rejects `#{refute_method}` with multiple arguments" do + inspect_source(@cop, "#{refute_method} a, b, c") + assert_offense @cop, offense_message(refute_method, assert_method) + end + + test "autocorrects `#{refute_method}` with a single argument" do + corrected = autocorrect_source(@cop, "#{refute_method} a") + assert_equal "#{assert_method} a", corrected + end + + test "autocorrects `#{refute_method}` with multiple arguments" do + corrected = autocorrect_source(@cop, "#{refute_method} a, b, c") + assert_equal "#{assert_method} a, b, c", corrected + end + + test "accepts `#{assert_method}` with a single argument" do + inspect_source(@cop, "#{assert_method} a") + assert_empty @cop.offenses + end + + test "accepts `#{assert_method}` with multiple arguments" do + inspect_source(@cop, "#{assert_method} a, b, c") + assert_empty @cop.offenses + end + end + + private + + def offense_message(refute_method, assert_method) + carets = "^" * refute_method.to_s.length + "#{carets} Prefer `#{assert_method}` over `#{refute_method}`" + end +end diff --git a/ci/custom_cops/test/support/cop_helper.rb b/ci/custom_cops/test/support/cop_helper.rb new file mode 100644 index 0000000000..c2c6b969dd --- /dev/null +++ b/ci/custom_cops/test/support/cop_helper.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +require "rubocop" + +module CopHelper + def inspect_source(cop, source) + processed_source = parse_source(source) + raise "Error parsing example code" unless processed_source.valid_syntax? + investigate(cop, processed_source) + processed_source + end + + def autocorrect_source(cop, source) + cop.instance_variable_get(:@options)[:auto_correct] = true + processed_source = inspect_source(cop, source) + rewrite(cop, processed_source) + end + + def assert_offense(cop, expected_message) + assert_not_empty( + cop.offenses, + "Expected offense with message \"#{expected_message}\", but got no offense" + ) + + offense = cop.offenses.first + carets = "^" * offense.column_length + + assert_equal expected_message, "#{carets} #{offense.message}" + end + + private + TARGET_RUBY_VERSION = 2.4 + + def parse_source(source) + RuboCop::ProcessedSource.new(source, TARGET_RUBY_VERSION) + end + + def rewrite(cop, processed_source) + RuboCop::Cop::Corrector.new(processed_source.buffer, cop.corrections) + .rewrite + end + + def investigate(cop, processed_source) + RuboCop::Cop::Commissioner.new([cop], [], raise_error: true) + .investigate(processed_source) + end +end diff --git a/ci/phantomjs.js b/ci/phantomjs.js deleted file mode 100644 index 7a33fb14a3..0000000000 --- a/ci/phantomjs.js +++ /dev/null @@ -1,149 +0,0 @@ -/* - * PhantomJS Runner QUnit Plugin 1.2.0 - * - * PhantomJS binaries: http://phantomjs.org/download.html - * Requires PhantomJS 1.6+ (1.7+ recommended) - * - * Run with: - * phantomjs runner.js [url-of-your-qunit-testsuite] - * - * e.g. - * phantomjs runner.js http://localhost/qunit/test/index.html - */ - -/*global phantom:false, require:false, console:false, window:false, QUnit:false */ - -(function() { - 'use strict'; - - var url, page, timeout, - args = require('system').args; - - // arg[0]: scriptName, args[1...]: arguments - if (args.length < 2 || args.length > 3) { - console.error('Usage:\n phantomjs runner.js [url-of-your-qunit-testsuite] [timeout-in-seconds]'); - phantom.exit(1); - } - - url = args[1]; - page = require('webpage').create(); - if (args[2] !== undefined) { - timeout = parseInt(args[2], 10); - } - - // Route `console.log()` calls from within the Page context to the main Phantom context (i.e. current `this`) - page.onConsoleMessage = function(msg) { - console.log(msg); - }; - - page.onInitialized = function() { - page.evaluate(addLogging); - }; - - page.onCallback = function(message) { - var result, - failed; - - if (message) { - if (message.name === 'QUnit.done') { - result = message.data; - failed = !result || !result.total || result.failed; - - if (!result.total) { - console.error('No tests were executed. Are you loading tests asynchronously?'); - } - - phantom.exit(failed ? 1 : 0); - } - } - }; - - page.open(url, function(status) { - if (status !== 'success') { - console.error('Unable to access network: ' + status); - phantom.exit(1); - } else { - // Cannot do this verification with the 'DOMContentLoaded' handler because it - // will be too late to attach it if a page does not have any script tags. - var qunitMissing = page.evaluate(function() { return (typeof QUnit === 'undefined' || !QUnit); }); - if (qunitMissing) { - console.error('The `QUnit` object is not present on this page.'); - phantom.exit(1); - } - - // Set a timeout on the test running, otherwise tests with async problems will hang forever - if (typeof timeout === 'number') { - setTimeout(function() { - console.error('The specified timeout of ' + timeout + ' seconds has expired. Aborting...'); - phantom.exit(1); - }, timeout * 1000); - } - - // Do nothing... the callback mechanism will handle everything! - } - }); - - function addLogging() { - window.document.addEventListener('DOMContentLoaded', function() { - var currentTestAssertions = []; - - QUnit.log(function(details) { - var response; - - // Ignore passing assertions - if (details.result) { - return; - } - - response = details.message || ''; - - if (typeof details.expected !== 'undefined') { - if (response) { - response += ', '; - } - - response += 'expected: ' + details.expected + ', but was: ' + details.actual; - } - - if (details.source) { - response += "\n" + details.source; - } - - currentTestAssertions.push('Failed assertion: ' + response); - }); - - QUnit.testDone(function(result) { - var i, - len, - name = ''; - - if (result.module) { - name += result.module + ': '; - } - name += result.name; - - if (result.failed) { - console.log('\n' + 'Test failed: ' + name); - - for (i = 0, len = currentTestAssertions.length; i < len; i++) { - console.log(' ' + currentTestAssertions[i]); - } - } - - currentTestAssertions.length = 0; - }); - - QUnit.done(function(result) { - console.log('\n' + 'Took ' + result.runtime + 'ms to run ' + result.total + ' tests. ' + result.passed + ' passed, ' + result.failed + ' failed.'); - - if (typeof window.callPhantom === 'function') { - window.callPhantom({ - 'name': 'QUnit.done', - 'data': result - }); - } - }); - }, false); - } -})(); - diff --git a/ci/qunit-selenium-runner.rb b/ci/qunit-selenium-runner.rb new file mode 100644 index 0000000000..05bcab8cdb --- /dev/null +++ b/ci/qunit-selenium-runner.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require "qunit/selenium/test_runner" +require "chromedriver/helper" + +driver_options = Selenium::WebDriver::Chrome::Options.new +driver_options.add_argument("--headless") +driver_options.add_argument("--disable-gpu") +driver_options.add_argument("--no-sandbox") + +driver = ::Selenium::WebDriver.for(:chrome, options: driver_options) +result = QUnit::Selenium::TestRunner.new(driver).open(ARGV[0], timeout: 60) +driver.quit + +puts "Time: #{result.duration} seconds, Total: #{result.assertions[:total]}, Passed: #{result.assertions[:passed]}, Failed: #{result.assertions[:failed]}" +exit(result.tests[:failed] > 0 ? 1 : 0) diff --git a/ci/travis.rb b/ci/travis.rb index b124358789..861063afa5 100755 --- a/ci/travis.rb +++ b/ci/travis.rb @@ -135,7 +135,7 @@ class Build if activesupport? && !isolated? # There is a known issue with the listen tests that causes files to be # incorrectly GC'ed even when they are still in-use. The current solution - # is to only run them in isolation to avoid randomly failing our test suite. + # is to only run them in isolation to avoid random failures of our test suite. { "LISTEN" => "0" } else {} @@ -159,7 +159,7 @@ results = {} ENV["GEM"].split(",").each do |gem| [false, true].each do |isolated| next if ENV["TRAVIS_PULL_REQUEST"] && ENV["TRAVIS_PULL_REQUEST"] != "false" && isolated - next if RUBY_VERSION < "2.4" && isolated + next if RUBY_VERSION < "2.5" && isolated next if gem == "railties" && isolated next if gem == "ac" && isolated next if gem == "ac:integration" && isolated |