From dfec254ddf0d569b405d1da426246a36efcdbcff Mon Sep 17 00:00:00 2001 From: Jacob Helwig Date: Thu, 15 Feb 2018 14:50:49 -0800 Subject: [PATCH] (PUP-8463) Check modified Ruby files in PRs against 'ruby -wc' --- .travis.yml | 11 +++++++++++ Rakefile | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/.travis.yml b/.travis.yml index 22b710fdcf0..81dfc32201f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,6 +17,7 @@ env: - "CHECK=parallel:spec\\[2\\]" - "CHECK=rubocop" - "CHECK=commits" + - "CHECK=warnings" matrix: exclude: @@ -40,3 +41,13 @@ matrix: env: "CHECK=commits" - rvm: 1.9.3 env: "CHECK=commits" + - rvm: 2.3.6 + env: "CHECK=warnings" + - rvm: 2.2.9 + env: "CHECK=warnings" + - rvm: 2.1.9 + env: "CHECK=warnings" + - rvm: 2.0.0 + env: "CHECK=warnings" + - rvm: 1.9.3 + env: "CHECK=warnings" diff --git a/Rakefile b/Rakefile index 02f0ae6154a..7d1733c13ac 100644 --- a/Rakefile +++ b/Rakefile @@ -19,6 +19,7 @@ rescue LoadError end require 'rake' +require 'open3' Dir['tasks/**/*.rake'].each { |t| load t } @@ -110,6 +111,31 @@ task(:commits) do end end +desc "verify that changed files are clean of Ruby warnings" +task(:warnings) do + # This rake task looks at all files modified in this branch. This is + # accomplished by using the TRAVIS_COMMIT_RANGE environment variable, which + # is present in travis CI and populated with the range of commits the PR + # contains. If not available, this falls back to `master..HEAD` as a next + # best bet as `master` is unlikely to ever be absent. + commit_range = ENV['TRAVIS_COMMIT_RANGE'].nil? ? 'master...HEAD' : ENV['TRAVIS_COMMIT_RANGE'] + ruby_files_ok = true + puts "Checking modified files #{commit_range}" + %x{git diff --name-only #{commit_range}}.each_line do |modified_file| + modified_file.chomp! + next unless File.extname(modified_file) == '.rb' + puts modified_file + + stdout, stderr, _ = Open3.capture3("ruby -wc \"#{modified_file}\"") + unless stderr.empty? + ruby_files_ok = false + puts stderr + end + puts stdout + end + raise "One or more ruby files contain warnings." unless ruby_files_ok +end + if Rake.application.top_level_tasks.grep(/^gettext:/).any? begin spec = Gem::Specification.find_by_name 'gettext-setup'