diff options
Diffstat (limited to 'spec/mspec/tool')
| -rwxr-xr-x | spec/mspec/tool/check_require_spec_helper.rb | 34 | ||||
| -rwxr-xr-x | spec/mspec/tool/pull-latest-mspec-spec | 26 | ||||
| -rwxr-xr-x[-rw-r--r--] | spec/mspec/tool/remove_old_guards.rb | 126 | ||||
| -rw-r--r-- | spec/mspec/tool/sync/sync-rubyspec.rb | 120 | ||||
| -rwxr-xr-x | spec/mspec/tool/tag_from_output.rb | 65 | ||||
| -rwxr-xr-x | spec/mspec/tool/wrap_with_guard.rb | 28 |
6 files changed, 335 insertions, 64 deletions
diff --git a/spec/mspec/tool/check_require_spec_helper.rb b/spec/mspec/tool/check_require_spec_helper.rb new file mode 100755 index 0000000000..07126e68dc --- /dev/null +++ b/spec/mspec/tool/check_require_spec_helper.rb @@ -0,0 +1,34 @@ +#!/usr/bin/env ruby + +# This script is used to check that each *_spec.rb file has +# a relative_require for spec_helper which should live higher +# up in the ruby/spec repo directory tree. +# +# Prints errors to $stderr and returns a non-zero exit code when +# errors are found. +# +# Related to https://github.com/ruby/spec/pull/992 + +def check_file(fn) + File.foreach(fn) do |line| + return $1 if line =~ /^\s*require_relative\s*['"](.*spec_helper)['"]/ + end + nil +end + +rootdir = ARGV[0] || "." +fglob = File.join(rootdir, "**", "*_spec.rb") +specfiles = Dir.glob(fglob) +raise "No spec files found in #{fglob.inspect}. Give an argument to specify the root-directory of ruby/spec" if specfiles.empty? + +errors = 0 +specfiles.sort.each do |fn| + result = check_file(fn) + if result.nil? + warn "Missing require_relative for *spec_helper for file: #{fn}" + errors += 1 + end +end + +puts "# Found #{errors} files with require_relative spec_helper issues." +exit 1 if errors > 0 diff --git a/spec/mspec/tool/pull-latest-mspec-spec b/spec/mspec/tool/pull-latest-mspec-spec new file mode 100755 index 0000000000..154a353e64 --- /dev/null +++ b/spec/mspec/tool/pull-latest-mspec-spec @@ -0,0 +1,26 @@ +#!/bin/bash + +# Assumes all commits have been synchronized to https://github.com/ruby/spec +# See spec/mspec/tool/sync/sync-rubyspec.rb + +function sync { + dir="$1" + repo="$2" + short_repo_name="ruby/$(basename "$repo" .git)" + + rm -rf "$dir" + git clone --depth 1 "$repo" "$dir" + commit=$(git -C "$dir" log -n 1 --format='%h') + rm -rf "$dir/.git" + + # Remove CI files to avoid confusion + rm -f "$dir/appveyor.yml" + rm -f "$dir/.travis.yml" + rm -rf "$dir/.github" + + git add "$dir" + git commit -m "Update to ${short_repo_name}@${commit}" +} + +sync spec/mspec https://github.com/ruby/mspec.git +sync spec/ruby https://github.com/ruby/spec.git diff --git a/spec/mspec/tool/remove_old_guards.rb b/spec/mspec/tool/remove_old_guards.rb index d0920344eb..bc5612c78d 100644..100755 --- a/spec/mspec/tool/remove_old_guards.rb +++ b/spec/mspec/tool/remove_old_guards.rb @@ -1,4 +1,23 @@ -# Remove old version guards in ruby/spec +#!/usr/bin/env ruby + +# Removes old version guards in ruby/spec. +# Run it from the ruby/spec repository root. +# The argument is the new minimum supported version. +# +# cd spec +# ../mspec/tool/remove_old_guards.rb <ruby-version> +# +# where <ruby-version> is a version guard with which should be removed +# +# Example: +# tool/remove_old_guards.rb 3.1 +# +# As a result guards like +# ruby_version_is "3.1" do +# # ... +# end +# +# will be removed. def dedent(line) if line.start_with?(" ") @@ -8,34 +27,119 @@ def dedent(line) end end +def each_spec_file(&block) + Dir["*/**/*.rb"].each(&block) +end + +def each_file(&block) + Dir["**/*"].each { |path| + yield path if File.file?(path) + } +end + def remove_guards(guard, keep) - Dir["*/**/*.rb"].each do |file| - contents = File.read(file) + each_spec_file do |file| + contents = File.binread(file) if contents =~ guard puts file lines = contents.lines.to_a while first = lines.find_index { |line| line =~ guard } + comment = first + while comment > 0 and lines[comment-1] =~ /^(\s*)#/ + comment -= 1 + end indent = lines[first][/^(\s*)/, 1].length last = (first+1...lines.size).find { |i| space = lines[i][/^(\s*)end$/, 1] and space.length == indent } raise file unless last if keep - lines[first..last] = lines[first+1..last-1].map { |l| dedent(l) } + lines[comment..last] = lines[first+1..last-1].map { |l| dedent(l) } else - if first > 0 and lines[first-1] == "\n" - first -= 1 + if comment > 0 and lines[comment-1] == "\n" + comment -= 1 elsif lines[last+1] == "\n" last += 1 end - lines[first..last] = [] + lines[comment..last] = [] end end - File.write file, lines.join + File.binwrite file, lines.join + end + end +end + +def remove_empty_files + each_spec_file do |file| + unless file.include?("fixtures/") + lines = File.readlines(file) + if lines.all? { |line| line.chomp.empty? or line.start_with?('require', '#') } + puts "Removing empty file #{file}" + File.delete(file) + end end end end -version = "2.2" -remove_guards(/ruby_version_is ["']#{version}["'] do/, true) -remove_guards(/ruby_version_is ["'][0-9.]*["']...["']#{version}["'] do/, false) +def remove_unused_shared_specs + shared_groups = {} + # Dir["**/shared/**/*.rb"].each do |shared| + each_spec_file do |shared| + next if File.basename(shared) == 'constants.rb' + contents = File.binread(shared) + found = false + contents.scan(/^\s*describe (:[\w_?]+), shared: true do$/) { + shared_groups[$1] = 0 + found = true + } + if !found and shared.include?('shared/') and !shared.include?('fixtures/') and !shared.end_with?('/constants.rb') + puts "no shared describe in #{shared} ?" + end + end + + each_spec_file do |file| + contents = File.binread(file) + contents.scan(/(?:it_behaves_like|it_should_behave_like) (:[\w_?]+)[,\s]/) do + puts $1 unless shared_groups.key?($1) + shared_groups[$1] += 1 + end + end + + shared_groups.each_pair do |group, value| + if value == 0 + puts "Shared describe #{group} seems unused" + elsif value == 1 + puts "Shared describe #{group} seems used only once" if $VERBOSE + end + end +end + +def search(regexp) + each_file do |file| + contents = File.binread(file) + if contents =~ regexp + puts file + contents.each_line do |line| + if line =~ regexp + puts line + end + end + end + end +end + +abort "usage: #{$0} <ruby-version>" if ARGV.empty? + +version = Regexp.escape(ARGV.fetch(0)) +version += "(?:\\.0)?" if version.count(".") < 2 +remove_guards(/ruby_version_is (["'])#{version}\1 do/, true) +remove_guards(/ruby_version_is (["'])[0-9.]*\1 *... *(["'])#{version}\2 do/, false) +remove_guards(/ruby_bug ["']#\d+["'], (["'])[0-9.]*\1 *... *(["'])#{version}\2 do/, true) + +remove_empty_files +remove_unused_shared_specs + +puts "Search:" +search(/(["'])#{version}\1/) +search(/^\s*#.+#{version}/) +search(/RUBY_VERSION_IS_#{version.tr('.', '_')}/) diff --git a/spec/mspec/tool/sync/sync-rubyspec.rb b/spec/mspec/tool/sync/sync-rubyspec.rb index fbd37fe95b..86c43d0dc8 100644 --- a/spec/mspec/tool/sync/sync-rubyspec.rb +++ b/spec/mspec/tool/sync/sync-rubyspec.rb @@ -1,6 +1,9 @@ +# This script is based on commands from the wiki: +# https://github.com/ruby/spec/wiki/Merging-specs-from-JRuby-and-other-sources + IMPLS = { truffleruby: { - git: "https://github.com/graalvm/truffleruby.git", + git: "https://github.com/truffleruby/truffleruby.git", from_commit: "f10ab6988d", }, jruby: { @@ -12,13 +15,16 @@ IMPLS = { }, mri: { git: "https://github.com/ruby/ruby.git", - master: "trunk", - merge_message: "Update to ruby/spec@", }, } MSPEC = ARGV.delete('--mspec') +CHECK_LAST_MERGE = !MSPEC && ENV['CHECK_LAST_MERGE'] != 'false' +TEST_MASTER = ENV['TEST_MASTER'] != 'false' + +ONLY_FILTER = ENV['ONLY_FILTER'] == 'true' + MSPEC_REPO = File.expand_path("../../..", __FILE__) raise MSPEC_REPO if !Dir.exist?(MSPEC_REPO) or !Dir.exist?("#{MSPEC_REPO}/.git") @@ -28,12 +34,22 @@ raise RUBYSPEC_REPO unless Dir.exist?(RUBYSPEC_REPO) SOURCE_REPO = MSPEC ? MSPEC_REPO : RUBYSPEC_REPO +# LAST_MERGE is a commit of ruby/spec or ruby/mspec +# which is the spec/mspec commit that was last imported in the Ruby implementation +# (i.e. the commit in "Update to ruby/spec@commit"). +# It is normally automatically computed, but can be manually set when +# e.g. the last update of specs wasn't merged in the Ruby implementation. +LAST_MERGE = ENV["LAST_MERGE"] + NOW = Time.now BRIGHT_RED = "\e[31;1m" BRIGHT_YELLOW = "\e[33;1m" RESET = "\e[0m" +# git filter-branch --subdirectory-filter works fine for our use case +ENV['FILTER_BRANCH_SQUELCH_WARNING'] = '1' + class RubyImplementation attr_reader :name @@ -46,14 +62,14 @@ class RubyImplementation @data[:git] end - def default_branch - @data[:master] || "master" - end - def repo_name File.basename(git_url, ".git") end + def repo_path + "#{__dir__}/#{repo_name}" + end + def repo_org File.basename(File.dirname(git_url)) end @@ -64,7 +80,7 @@ class RubyImplementation end def last_merge_message - message = @data[:merge_message] || "Merge ruby/spec commit" + message = @data[:merge_message] || "Update to ruby/spec@" message.gsub!("ruby/spec", "ruby/mspec") if MSPEC message end @@ -97,7 +113,7 @@ def update_repo(impl) Dir.chdir(impl.repo_name) do puts Dir.pwd - sh "git", "checkout", impl.default_branch + sh "git", "checkout", "master" sh "git", "pull" end end @@ -133,22 +149,27 @@ def rebase_commits(impl) else sh "git", "checkout", impl.name - if ENV["LAST_MERGE"] - last_merge = `git log -n 1 --format='%H %ct' #{ENV["LAST_MERGE"]}` + if LAST_MERGE + last_merge = `git log -n 1 --format='%H %ct' #{LAST_MERGE}` else last_merge = `git log --grep='^#{impl.last_merge_message}' -n 1 --format='%H %ct'` end - last_merge, commit_timestamp = last_merge.chomp.split(' ') + last_merge, commit_timestamp = last_merge.split(' ') raise "Could not find last merge" unless last_merge puts "Last merge is #{last_merge}" commit_date = Time.at(Integer(commit_timestamp)) days_since_last_merge = (NOW-commit_date) / 86400 - if days_since_last_merge > 60 + if CHECK_LAST_MERGE and days_since_last_merge > 60 raise "#{days_since_last_merge.floor} days since last merge, probably wrong commit" end + puts "Checking if the last merge is consistent with upstream files" + rubyspec_commit = `git log -n 1 --format='%s' #{last_merge}`.chomp.split('@', 2)[-1] + sh "git", "checkout", last_merge + sh "git", "diff", "--exit-code", rubyspec_commit, "--", ":!.github" + puts "Rebasing..." sh "git", "branch", "-D", rebased if branch?(rebased) sh "git", "checkout", "-b", rebased, impl.name @@ -157,53 +178,40 @@ def rebase_commits(impl) end end -def test_new_specs - require "yaml" +def new_commits?(impl) Dir.chdir(SOURCE_REPO) do - if MSPEC - sh "bundle", "exec", "rspec" - else - versions = YAML.load_file(".travis.yml") - versions = versions["matrix"]["include"].map { |job| job["rvm"] } - versions.delete "ruby-head" - min_version, max_version = versions.minmax - - run_rubyspec = -> version { - command = "chruby #{version} && ../mspec/bin/mspec -j" - sh ENV["SHELL"], "-c", command - } - run_rubyspec[min_version] - run_rubyspec[max_version] - run_rubyspec["trunk"] - end + diff = `git diff master #{impl.rebased_branch}` + !diff.empty? end end -def verify_commits(impl) - puts +def test_new_specs + require "yaml" Dir.chdir(SOURCE_REPO) do - history = `git log master...` - history.lines.slice_before(/^commit \h{40}$/).each do |commit, *message| - commit = commit.chomp.split.last - message = message.join - if /\W(#\d+)/ === message - puts "Commit #{commit} contains an unqualified issue number: #{$1}" - puts "Replace it with #{impl.repo_org}/#{impl.repo_name}#{$1}" - sh "git", "rebase", "-i", "#{commit}^" - end - end + workflow = YAML.load_file(".github/workflows/ci.yml") + job_name = MSPEC ? "test" : "specs" + versions = workflow.dig("jobs", job_name, "strategy", "matrix", "ruby").map(&:to_s) + versions = versions.grep(/^\d+\./) # Test on MRI + min_version, max_version = versions.minmax + + test_command = MSPEC ? "bundle install && bundle exec rspec" : "../mspec/bin/mspec -j" + + run_test = -> version { + command = "chruby ruby-#{version} && #{test_command}" + sh ENV["SHELL"], "-c", command + } - puts "Manually check commit messages:" - print "Press enter >" - STDIN.gets - sh "git", "log", "master..." + run_test[min_version] + run_test[max_version] + run_test["master"] if TEST_MASTER end end def fast_forward_master(impl) Dir.chdir(SOURCE_REPO) do sh "git", "checkout", "master" - sh "git", "merge", "--ff-only", "#{impl.name}-rebased" + sh "git", "merge", "--ff-only", impl.rebased_branch + sh "git", "branch", "--delete", impl.rebased_branch end end @@ -221,11 +229,17 @@ def main(impls) impl = RubyImplementation.new(impl, data) update_repo(impl) filter_commits(impl) - rebase_commits(impl) - test_new_specs - verify_commits(impl) - fast_forward_master(impl) - check_ci + unless ONLY_FILTER + rebase_commits(impl) + if new_commits?(impl) + test_new_specs + fast_forward_master(impl) + check_ci + else + STDERR.puts "#{BRIGHT_YELLOW}No new commits#{RESET}" + fast_forward_master(impl) + end + end end end diff --git a/spec/mspec/tool/tag_from_output.rb b/spec/mspec/tool/tag_from_output.rb new file mode 100755 index 0000000000..41aa70f932 --- /dev/null +++ b/spec/mspec/tool/tag_from_output.rb @@ -0,0 +1,65 @@ +#!/usr/bin/env ruby + +# Adds tags based on error and failures output (e.g., from a CI log), +# without running any spec code. + +tag = ENV["TAG"] || "fails" + +tags_dir = %w[ + spec/tags + spec/tags/ruby +].find { |dir| Dir.exist?("#{dir}/language") } +abort 'Could not find tags directory' unless tags_dir + +output = ARGF.readlines + +# Automatically strip datetime of GitHub Actions +if output.first =~ /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d+Z / + output = output.map { |line| line.split(' ', 2).last } +end + +NUMBER = /^\d+\)$/ +ERROR_OR_FAILED = / (ERROR|FAILED)$/ +SPEC_FILE = /^((?:\/|[CD]:\/).+_spec\.rb)\:\d+/ + +output.slice_before(NUMBER).select { |number, *rest| + number =~ NUMBER and rest.any? { |line| line =~ ERROR_OR_FAILED } +}.each { |number, *rest| + error_line = rest.find { |line| line =~ ERROR_OR_FAILED } + description = error_line.match(ERROR_OR_FAILED).pre_match + + spec_file = rest.find { |line| line =~ SPEC_FILE } + if spec_file + spec_file = spec_file[SPEC_FILE, 1] or raise + else + if error_line =~ /^([\w:]+)[#\.](\w+) / + mod, method = $1, $2 + file = "#{mod.downcase.gsub('::', '/')}/#{method}_spec.rb" + spec_file = ['spec/ruby/core', 'spec/ruby/library', *Dir.glob('spec/ruby/library/*')].find { |dir| + path = "#{dir}/#{file}" + break path if File.exist?(path) + } + end + + unless spec_file + warn "Could not find file for:\n#{error_line}" + next + end + end + + prefix = spec_file.index('spec/ruby/') || spec_file.index('spec/truffle/') + spec_file = spec_file[prefix..-1] + + tags_file = spec_file.sub('spec/ruby/', "#{tags_dir}/").sub('spec/truffle/', "#{tags_dir}/truffle/") + tags_file = tags_file.sub(/_spec\.rb$/, '_tags.txt') + + dir = File.dirname(tags_file) + Dir.mkdir(dir) unless Dir.exist?(dir) + + tag_line = "#{tag}:#{description}" + lines = File.exist?(tags_file) ? File.readlines(tags_file, chomp: true) : [] + unless lines.include?(tag_line) + puts tags_file + File.write(tags_file, (lines + [tag_line]).join("\n") + "\n") + end +} diff --git a/spec/mspec/tool/wrap_with_guard.rb b/spec/mspec/tool/wrap_with_guard.rb new file mode 100755 index 0000000000..5b1bf4d7f7 --- /dev/null +++ b/spec/mspec/tool/wrap_with_guard.rb @@ -0,0 +1,28 @@ +#!/usr/bin/env ruby +# Wrap the passed the files with a guard (e.g., `ruby_version_is ""..."3.0"`). +# Notably if some methods are removed, this is a convenient way to skip such file from a given version. +# Example usage: +# $ spec/mspec/tool/wrap_with_guard.rb 'ruby_version_is ""..."3.0"' spec/ruby/library/set/sortedset/**/*_spec.rb + +guard, *files = ARGV +abort "Usage: #{$0} GUARD FILES..." if files.empty? + +files.each do |file| + contents = File.binread(file) + lines = contents.lines.to_a + + lines = lines.map { |line| line.chomp.empty? ? line : " #{line}" } + + version_line = "#{guard} do\n" + if lines[0] =~ /^\s*require.+spec_helper/ + lines[0] = lines[0].sub(/^ /, '') + lines.insert 1, "\n", version_line + else + warn "Could not find 'require spec_helper' line in #{file}" + lines.insert 0, version_line + end + + lines << "end\n" + + File.binwrite file, lines.join +end |
