From 47a55d5feb9bdd6dc14ee34410ba8e5a44351ac7 Mon Sep 17 00:00:00 2001 From: "Ian Heggie (OAF)" Date: Fri, 6 Nov 2026 00:05:17 +1100 Subject: [PATCH] Make import-comments.rb start its own class import-comments.rb started ExportComments instead of ImportComments, a slip from March 2026 that was also made in three other scripts and fixed there. Checking the syntax didn't catch any of them, so add a spec that checks every entry script starts a class it defines or loads. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Ian Heggie (OAF) --- .rubocop_todo.yml | 1 + import-comments.rb | 2 +- spec/scripts/entry_points_spec.rb | 35 +++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 spec/scripts/entry_points_spec.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 5a30c360..bd3316b6 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -149,6 +149,7 @@ RSpec/DescribeClass: - '**/spec/system/**/*' - '**/spec/views/**/*' - 'spec/scripts/create_patch_spec.rb' + - 'spec/scripts/entry_points_spec.rb' - 'spec/scripts/export_comments_spec.rb' - 'spec/scripts/import_comments_spec.rb' - 'spec/scripts/member_images_spec.rb' diff --git a/import-comments.rb b/import-comments.rb index f8d37202..70b2d859 100755 --- a/import-comments.rb +++ b/import-comments.rb @@ -55,4 +55,4 @@ def run end end -exit ExportComments.new(ARGV).run if File.expand_path($PROGRAM_NAME) == File.expand_path(__FILE__) +exit ImportComments.new(ARGV).run if File.expand_path($PROGRAM_NAME) == File.expand_path(__FILE__) diff --git a/spec/scripts/entry_points_spec.rb b/spec/scripts/entry_points_spec.rb new file mode 100644 index 00000000..be1d6201 --- /dev/null +++ b/spec/scripts/entry_points_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require_relative "../spec_helper" + +# A script that starts the wrong class does the wrong job when run, yet still +# passes a syntax check, so compare the two by reading the source. +RSpec.describe "entry scripts" do + # The line that runs the script when it is started directly + def self.entry_line(path) + File.readlines(path).find { |line| line.match?(/\$PROGRAM_NAME.*__FILE__/) } + end + + def self.entry_scripts + Dir[File.expand_path("../../*.rb", __dir__)].select { |path| entry_line(path) } + end + + # Classes the script defines itself or in the files it loads with require_relative + def classes_defined_for(path) + source = File.read(path) + loaded = source.scan(/^require_relative "(.+)"/).flatten.map { |name| File.expand_path("#{name}.rb", File.dirname(path)) } + ([source] + loaded.select { |file| File.exist?(file) }.map { |file| File.read(file) }) + .flat_map { |text| text.scan(/^class (\w+)/).flatten } + end + + it "finds the entry scripts" do + expect(self.class.entry_scripts.size).to be > 5 + end + + entry_scripts.each do |path| + it "#{File.basename(path)} starts a class it defines or loads" do + started_class = self.class.entry_line(path)[/(\w+)\.new\b/, 1] + expect(classes_defined_for(path)).to include(started_class) + end + end +end