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/find.rb | 10 | ||||
| -rwxr-xr-x | spec/mspec/tool/pull-latest-mspec-spec | 26 | ||||
| -rwxr-xr-x | spec/mspec/tool/remove_old_guards.rb | 145 | ||||
| -rw-r--r-- | spec/mspec/tool/sync/.gitignore | 4 | ||||
| -rw-r--r-- | spec/mspec/tool/sync/sync-rubyspec.rb | 254 | ||||
| -rwxr-xr-x | spec/mspec/tool/tag_from_output.rb | 65 | ||||
| -rwxr-xr-x | spec/mspec/tool/wrap_with_guard.rb | 28 |
8 files changed, 566 insertions, 0 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/find.rb b/spec/mspec/tool/find.rb new file mode 100755 index 0000000000..322b023f15 --- /dev/null +++ b/spec/mspec/tool/find.rb @@ -0,0 +1,10 @@ +#!/usr/bin/env ruby +Dir.chdir('../rubyspec') do + regexp = Regexp.new(ARGV[0]) + Dir.glob('**/*.rb') do |file| + contents = File.read(file) + if regexp =~ contents + puts file + end + end +end 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 new file mode 100755 index 0000000000..bc5612c78d --- /dev/null +++ b/spec/mspec/tool/remove_old_guards.rb @@ -0,0 +1,145 @@ +#!/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?(" ") + line[2..-1] + else + 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) + 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[comment..last] = lines[first+1..last-1].map { |l| dedent(l) } + else + if comment > 0 and lines[comment-1] == "\n" + comment -= 1 + elsif lines[last+1] == "\n" + last += 1 + end + lines[comment..last] = [] + end + end + 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 + +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/.gitignore b/spec/mspec/tool/sync/.gitignore new file mode 100644 index 0000000000..e64f1e8542 --- /dev/null +++ b/spec/mspec/tool/sync/.gitignore @@ -0,0 +1,4 @@ +/jruby +/rubinius +/ruby +/truffleruby diff --git a/spec/mspec/tool/sync/sync-rubyspec.rb b/spec/mspec/tool/sync/sync-rubyspec.rb new file mode 100644 index 0000000000..86c43d0dc8 --- /dev/null +++ b/spec/mspec/tool/sync/sync-rubyspec.rb @@ -0,0 +1,254 @@ +# 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/truffleruby/truffleruby.git", + from_commit: "f10ab6988d", + }, + jruby: { + git: "https://github.com/jruby/jruby.git", + from_commit: "f10ab6988d", + }, + rbx: { + git: "https://github.com/rubinius/rubinius.git", + }, + mri: { + git: "https://github.com/ruby/ruby.git", + }, +} + +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") + +# Assuming the rubyspec repo is a sibling of the mspec repo +RUBYSPEC_REPO = File.expand_path("../rubyspec", MSPEC_REPO) +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 + + def initialize(name, data) + @name = name.to_s + @data = data + end + + def git_url + @data[:git] + 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 + + def from_commit + from = @data[:from_commit] + "#{from}..." if from + end + + def last_merge_message + message = @data[:merge_message] || "Update to ruby/spec@" + message.gsub!("ruby/spec", "ruby/mspec") if MSPEC + message + end + + def prefix + MSPEC ? "spec/mspec" : "spec/ruby" + end + + def rebased_branch + "#{@name}-rebased" + end +end + +def sh(*args) + puts args.join(' ') + system(*args) + raise unless $?.success? +end + +def branch?(name) + branches = `git branch`.sub('*', '').lines.map(&:strip) + branches.include?(name) +end + +def update_repo(impl) + unless File.directory? impl.repo_name + sh "git", "clone", impl.git_url + end + + Dir.chdir(impl.repo_name) do + puts Dir.pwd + + sh "git", "checkout", "master" + sh "git", "pull" + end +end + +def filter_commits(impl) + Dir.chdir(impl.repo_name) do + date = NOW.strftime("%F") + branch = "#{MSPEC ? :mspec : :specs}-#{date}" + + unless branch?(branch) + sh "git", "checkout", "-b", branch + sh "git", "filter-branch", "-f", "--subdirectory-filter", impl.prefix, *impl.from_commit + sh "git", "push", "-f", SOURCE_REPO, "#{branch}:#{impl.name}" + end + end +end + +def rebase_commits(impl) + Dir.chdir(SOURCE_REPO) do + sh "git", "checkout", "master" + sh "git", "pull" + + rebased = impl.rebased_branch + if branch?(rebased) + last_commit = Time.at(Integer(`git log -n 1 --format='%ct' #{rebased}`)) + days_since_last_commit = (NOW-last_commit) / 86400 + if days_since_last_commit > 7 + abort "#{BRIGHT_RED}#{rebased} exists but last commit is old (#{last_commit}), delete the branch if it was merged#{RESET}" + else + puts "#{BRIGHT_YELLOW}#{rebased} already exists, last commit on #{last_commit}, assuming it correct#{RESET}" + sh "git", "checkout", rebased + end + else + sh "git", "checkout", impl.name + + 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.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 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 + sh "git", "rebase", "--onto", "master", last_merge + end + end +end + +def new_commits?(impl) + Dir.chdir(SOURCE_REPO) do + diff = `git diff master #{impl.rebased_branch}` + !diff.empty? + end +end + +def test_new_specs + require "yaml" + Dir.chdir(SOURCE_REPO) do + 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 + } + + 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.rebased_branch + sh "git", "branch", "--delete", impl.rebased_branch + end +end + +def check_ci + puts + puts <<-EOS + Push to master, and check that the CI passes: + https://github.com/ruby/#{:m if MSPEC}spec/commits/master + + EOS +end + +def main(impls) + impls.each_pair do |impl, data| + impl = RubyImplementation.new(impl, data) + update_repo(impl) + filter_commits(impl) + 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 + +if ARGV == ["all"] + impls = IMPLS +else + args = ARGV.map { |arg| arg.to_sym } + raise ARGV.to_s unless (args - IMPLS.keys).empty? + impls = IMPLS.select { |impl| args.include?(impl) } +end + +main(impls) 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 |
