A platform for community discussion. Free, open, simple.
DEV: Fix `bin/lint` silently passing when it lints nothing (#43068)
Régis Hanol1390bcf33af9
diff --git a/bin/lint b/bin/lintindex 974d42c6a5d..820944afc18 100755--- a/bin/lint+++ b/bin/lint@@ -3,36 +3,81 @@require "optparse"require "open3"-require "shellwords"require "pathname"+require "shellwords"PROJECT_ROOT = File.expand_path("..", __dir__)+PROJECT_ROOT_PATH = Pathname.new(PROJECT_ROOT)+PROJECT_ROOT_REALPATH = PROJECT_ROOT_PATH.realpath+INVOCATION_PWD = Dir.pwd++module Logging+ def log(message)+ puts "[bin/lint] #{message}"+ end++ def debug(message)+ puts message if @verbose+ end+end++module ArgumentBatcher+ MAX_ARGV_BYTES = 100_000++ def self.batches(files, per_file_overhead: 1)+ batches = [[]]+ size = 0++ files.each do |file|+ entry = file.bytesize + per_file_overhead++ if size + entry > MAX_ARGV_BYTES && batches.last.any?+ batches << []+ size = 0+ end++ batches.last << file+ size += entry+ end++ batches+ end+end-# Runs linters directly (bundle exec rubocop/stree, pnpm lint) for files that-# live outside the core repo (e.g. plugins/*) where lefthook is not available.class ExternalLinter+ include Logging+RUBY_EXTENSIONS = %w[rb rake thor].freezePRETTIER_EXTENSIONS = %w[css scss js gjs cjs mjs ts gts mts cts].freezeESLINT_EXTENSIONS = %w[js gjs cjs mjs ts gts mts cts].freezeSTYLELINT_EXTENSIONS = %w[scss].freeze-JS_EXTENSIONS = (PRETTIER_EXTENSIONS + ESLINT_EXTENSIONS + STYLELINT_EXTENSIONS).uniq.freeze+ PNPM = %w[pnpm --ignore-workspace].freeze+ JS_LINTERS = [+ ["prettier", PRETTIER_EXTENSIONS, %w[--write], %w[--list-different]],+ ["eslint", ESLINT_EXTENSIONS, %w[--fix], %w[--quiet]],+ ["stylelint", STYLELINT_EXTENSIONS, %w[--fix], []],+ ].freeze+attr_reader :resultsdef initialize(root, files, fix:, verbose: false)+ root_path = Pathname.new(root).realpath+@root = root- root_path = Pathname.new(@root).realpath- # Keep paths relative to the plugin root so linter exclusions work for symlinked plugins.- @files = files.map { |f| Pathname.new(f).realpath.relative_path_from(root_path).to_s }+ @files = files.map { |file| Pathname.new(file).realpath.relative_path_from(root_path).to_s }@fix = fix@verbose = verbose@results = []enddef run- ruby_files = @files.select { |f| ruby_file?(f) || File.basename(f) == "Gemfile" }- js_files = @files.select { |f| js_file?(f) }+ ruby_files = @files.select { |file| ruby_file?(file) }+ js_files = @files.select { |file| extension_in?(file, JS_EXTENSIONS) }++ skipped = @files - ruby_files - js_files+ log "No external-plugin linter for: #{skipped.join(", ")}" if skipped.any?run_ruby_linters(ruby_files) if ruby_files.any?run_js_linters(js_files) if js_files.any?@@ -40,350 +85,335 @@ class ExternalLinterprivate- def ruby_file?(f)- RUBY_EXTENSIONS.include?(File.extname(f)[1..])+ def extension_in?(file, extensions)+ extensions.include?(File.extname(file)[1..])end- def js_file?(f)- JS_EXTENSIONS.include?(File.extname(f)[1..])+ def ruby_file?(file)+ extension_in?(file, RUBY_EXTENSIONS) || File.basename(file) == "Gemfile"end- def run_cmd(*cmd)- puts "Running: #{cmd.shelljoin} (cwd: #{@root})" if @verbose- system(*cmd, chdir: @root)+ def run_cmd(*cmd, env: {})+ debug "Running: #{cmd.shelljoin} (cwd: #{@root})"+ system(env, *cmd, chdir: @root)enddef run_linter(name, *cmd)- puts "[bin/lint] Running #{name}..."+ log "Running #{name}..."@results << [name, run_cmd(*cmd)]enddef run_ruby_linters(files)- puts "[bin/lint] Installing bundler dependencies in #{@root}..."- run_cmd("bundle", "install") or abort "bundle install failed in #{@root}"-- stree_subcmd = @fix ? "write" : "check"- run_linter("stree", "bundle", "exec", "stree", stree_subcmd, *files)-- rubocop_args = @fix ? ["--autocorrect"] : []- run_linter("rubocop", "bundle", "exec", "rubocop", *rubocop_args, *files)+ log "Installing bundler dependencies in #{@root}..."+ frozen = @fix ? {} : { "BUNDLE_FROZEN" => "true" }+ return @results << ["bundle install", false] unless run_cmd("bundle", "install", env: frozen)++ ArgumentBatcher+ .batches(files)+ .each do |batch|+ run_linter("stree", "bundle", "exec", "stree", @fix ? "write" : "check", *batch)+ end+ ArgumentBatcher+ .batches(files)+ .each do |batch|+ run_linter("rubocop", "bundle", "exec", "rubocop", *(@fix ? ["--autocorrect"] : []), *batch)+ endenddef run_js_linters(files)- by_ext = ->(exts) { files.select { |f| exts.include?(File.extname(f)[1..]) } }-- prettier_files = by_ext.call(PRETTIER_EXTENSIONS)- eslint_files = by_ext.call(ESLINT_EXTENSIONS)- stylelint_files = by_ext.call(STYLELINT_EXTENSIONS)+ log "Installing pnpm dependencies in #{@root}..."+ frozen = @fix ? [] : ["--frozen-lockfile"]+ return @results << ["pnpm install", false] unless run_cmd(*PNPM, "i", *frozen)- pnpm = %w[pnpm --ignore-workspace]- puts "[bin/lint] Installing pnpm dependencies in #{@root}..."- run_cmd(*pnpm, "i") or abort "pnpm i failed in #{@root}"+ JS_LINTERS.each do |name, extensions, fix_args, check_args|+ matching = files.select { |file| extension_in?(file, extensions) }+ next if matching.empty?- if prettier_files.any?- args = @fix ? ["--write"] : ["--list-different"]- run_linter("prettier", *pnpm, "prettier", *args, *prettier_files)- end-- if eslint_files.any?- args = @fix ? ["--fix"] : ["--quiet"]- run_linter("eslint", *pnpm, "eslint", *args, *eslint_files)- end-- if stylelint_files.any?- args = @fix ? ["--fix"] : []- run_linter("stylelint", *pnpm, "stylelint", *args, *stylelint_files)+ ArgumentBatcher+ .batches(matching)+ .each { |batch| run_linter(name, *PNPM, name, *(@fix ? fix_args : check_args), *batch) }endendendclass LefthookLinter+ include Logging++ EVERYTHING = ["ALL FILES"].freeze+ SELECTORS = %i[recent staged unstaged wip].freeze++ SKIPPED_PATHS = %w[config/database.yml].freeze+ SKIPPED_SEGMENTS = %w[node_modules vendor tmp .git].freeze+ LINTABLE_EXTENSIONS = %w[+ rb+ rake+ thor+ js+ gjs+ cjs+ mjs+ ts+ gts+ mts+ cts+ hbs+ scss+ css+ yml+ yaml+ md+ json+ ].freeze+ RUBY_SHEBANG = "#!/usr/bin/env ruby"++ CORE_FRONTEND_PATHS = %w[frontend/ plugins/ themes/].freeze+ CORE_STYLESHEET_PATHS = %w[app/assets/stylesheets/].freeze+ DEVELOPER_DOCS_PATH = "docs/developer-guides/"+ DEVELOPER_DOCS_EXTENSIONS = %w[md json mjs yml].freeze++ FILE_FLAG_BYTES = "--file".bytesize + 2+def initialize(options = {})@fix = options[:fix]- @recent = options[:recent]- @staged = options[:staged]- @unstaged = options[:unstaged]- @wip = options[:wip]- @files = options[:files] || []@verbose = options[:verbose]+ @files = options[:files] || []+ @selector = SELECTORS.find { |name| options[name] }@results = []end- EVERYTHING = ["ALL FILES"]+ def run+ lint(determine_files)+ print_summary- # In --staged mode we want to lint exactly what is in the index, so we use the- # pre-commit/fix-staged hooks (which read the staged blobs). Every other mode- # operates on what is currently on disk, so it routes to the lint-files/fix-files- # hooks: those are not named pre-commit/pre-push, so lefthook does not hide- # unstaged changes and the linters see the working-tree content.- def check_hook- @staged ? "pre-commit" : "lint-files"+ exit 1 if @results.any? { |_, ok| !ok }end- def fix_hook- @staged ? "fix-staged" : "fix-files"- end+ private- def run- files = determine_files+ def lint(files)+ return run_lefthook(@fix ? "fix-all" : "lints", []) if files.equal?(EVERYTHING)+ return if files.empty?- if @fix- run_fix_mode(files)+ core, external = partition_files(files)+ core, skipped = core.partition { |file| core_lintable_file?(file) }++ log "No core linter for: #{skipped.join(", ")}" if skipped.any?+ run_lefthook(hook, core) if core.any?+ external.each { |root, paths| run_external(root, paths) }+ end++ def hook+ if @selector == :staged+ @fix ? "fix-staged" : "pre-commit"else- run_check_mode(files)+ @fix ? "fix-files" : "lint-files"end-- print_summary- exit 1 if @results.any? { |_, ok| !ok }enddef print_summaryfailed = @results.reject { |_, ok| ok }- if failed.empty?- puts "[bin/lint] All lints passed"++ if failed.any?+ failed.each { |name, _| log "#{name} failed" }+ elsif @results.empty?+ log "Nothing was linted"else- failed.each { |name, _| puts "[bin/lint] #{name} failed" }+ log "All lints passed"endend- private-def determine_files- files =- if @recent- recent_files- elsif @staged- staged_files- elsif @unstaged- unstaged_files- elsif @wip- wip_files- elsif [email protected]?- @files- else- return EVERYTHING- end+ return EVERYTHING if @selector.nil? && @files.empty?++ if @selector && @files.any?+ warn "[bin/lint] Ignoring file arguments: a selector flag takes precedence"+ end++ resolved = source_paths.map { |path| resolve_path(path) }+ files = resolved.flat_map(&:first).uniq++ if @selector.nil?+ resolved.filter_map(&:last).each { |problem| warn "[bin/lint] Skipping #{problem}" }+ abort "[bin/lint] Nothing to lint" if files.empty?+ endfiles- .flat_map do |f|- path = normalize_relative_path(f)- if File.directory?(path)- expanded =- Dir.glob(File.join(path, "**", "*")).select { |g| File.file?(g) && lintable_file?(g) }- abort "Error: No lintable files found in directory: #{path}" if expanded.empty?- expanded- else- [path]- end- end- .select { |f| File.file?(f) && lintable_file?(f) }- .uniqend- def recent_files- log_output, status = Open3.capture2("git", "log", "-50", "--name-only", "--pretty=format:")- return [] unless status.success?+ def source_paths+ return source_files if @selector++ @files.map { |file| normalize_relative_path(file) }+ end- log_files = log_output.lines.map(&:strip).reject(&:empty?)+ def source_files+ case @selector+ when :recent+ recent_files+ when :staged+ staged_files+ when :unstaged+ unstaged_files+ when :wip+ wip_files+ else+ @files+ end+ end- tracked_out, _ = Open3.capture2("git", "ls-files")- untracked_out, _ = Open3.capture2("git", "ls-files", "--others", "--exclude-standard")+ def resolve_path(path)+ if File.directory?(path)+ expanded = expand_directory(path)+ return expanded, expanded.empty? ? "#{path}: no lintable files" : nil+ end- tracked = Set.new(tracked_out.lines.map(&:strip))- untracked = Set.new(untracked_out.lines.map(&:strip))+ return [], "#{path}: does not exist" unless File.file?(path)+ return [], "#{path}: not a lintable file type" unless lintable_file?(path)- candidates = []- log_files.each { |f| candidates << f if tracked.include?(f) }- candidates + untracked.to_a+ [[path], nil]+ end++ def expand_directory(path)+ files =+ git_lines("ls-files", "--cached", "--others", "--exclude-standard", "--", path, quiet: true)+ files = [path] if files.empty?++ files+ .flat_map { |file| File.directory?(file) ? Dir.glob(File.join(file, "**", "*")) : file }+ .select { |file| File.file?(file) && lintable_file?(file) }+ end++ def git_lines(*args, quiet: false)+ output, error, status = Open3.capture3("git", *args)+ return output.lines.map(&:strip).reject(&:empty?) if status.success?++ warn "[bin/lint] git #{args.first} failed: #{error.lines.first&.strip}" unless quiet+ []+ end++ def recent_files+ git_lines("log", "-50", "--name-only", "--pretty=format:") ++ git_lines("ls-files", "--others", "--exclude-standard") + staged_files + unstaged_filesenddef staged_files- git_output, status = Open3.capture2("git", "diff", "--cached", "--name-only")- return [] unless status.success?- git_output.lines.map(&:strip).reject(&:empty?)+ git_lines("diff", "--cached", "--name-only")enddef unstaged_files- git_output, status = Open3.capture2("git", "diff", "--name-only")- return [] unless status.success?- git_output.lines.map(&:strip).reject(&:empty?)+ git_lines("diff", "--name-only")enddef wip_files- main_diff_output, _ = Open3.capture2("git", "diff", "main...HEAD", "--name-only")- main_files = main_diff_output.lines.map(&:strip).reject(&:empty?)-- main_files + staged_files + unstaged_files+ git_lines("diff", "main...HEAD", "--name-only") + staged_files + unstaged_filesend- def lintable_file?(file)- # Skip certain directories and files- if file.include?("node_modules") || file.include?("vendor") || file.include?("tmp") ||- file.include?(".git") || file == "config/database.yml"- return false- end-+ def core_lintable_file?(file)+ return false if file.start_with?("../")return true if file == "Gemfile"- ext = File.extname(file)[1..]+ extension = File.extname(file)[1..]+ return ruby_script?(file) if extension.nil? || extension.empty?+ return true if ExternalLinter::RUBY_EXTENSIONS.include?(extension)+ return true if %w[yml yaml].include?(extension)- # Check for Ruby files in /bin/ directory without extensions- if ext.nil? || ext.empty?- if file.start_with?("bin/") && File.file?(file)- begin- first_line = File.open(file, &:readline)- return true if first_line.strip == "#!/usr/bin/env ruby"- rescue StandardError- return false- end- end- return false+ if file.start_with?(DEVELOPER_DOCS_PATH)+ return true if DEVELOPER_DOCS_EXTENSIONS.include?(extension)end- # Check if file extension is lintable- lintable_extensions = %w[- rb- rake- js- gjs- cjs- mjs- ts- gts- mts- cts- hbs- scss- css- yml- yaml- thor- md- json- ]- lintable_extensions.include?(ext)- end-- def run_fix_mode(files)- if files == EVERYTHING- puts "Running linters in fix mode on all files" if @verbose- run_lefthook_command("fix-all", [])- return- end+ return false unless ExternalLinter::JS_EXTENSIONS.include?(extension)- if files.empty?- puts "No files to fix, exiting."- return- end-- core, external = partition_files(files)- run_lefthook_command(fix_hook, core) if core.any?- external.each do |root, fs|- linter = ExternalLinter.new(root, fs, fix: true, verbose: @verbose)- linter.run- collect_external_results(root, linter)- end+ paths = CORE_FRONTEND_PATHS+ paths += CORE_STYLESHEET_PATHS if %w[css scss].include?(extension)+ paths.any? { |path| file.start_with?(path) }end- def run_check_mode(files)- if files == EVERYTHING- puts "Running linters in check mode on all files" if @verbose- run_lefthook_command("lints", [])- return- end+ def lintable_file?(file)+ return false if SKIPPED_PATHS.include?(file)+ return false if file.split("/").any? { |segment| SKIPPED_SEGMENTS.include?(segment) }+ return true if File.basename(file) == "Gemfile"- if files.empty?- puts "No files to lint, exiting."- return- end+ extension = File.extname(file)[1..]+ return ruby_script?(file) if extension.nil? || extension.empty?- core, external = partition_files(files)- run_lefthook_command(check_hook, core) if core.any?- external.each do |root, fs|- linter = ExternalLinter.new(root, fs, fix: false, verbose: @verbose)- linter.run- collect_external_results(root, linter)- end+ LINTABLE_EXTENSIONS.include?(extension)end- def collect_external_results(root, linter)- rel = Pathname.new(root).relative_path_from(Pathname.new(PROJECT_ROOT)).to_s- linter.results.each { |name, ok| @results << ["#{name} (#{rel})", ok] }+ def ruby_script?(file)+ return false unless file.start_with?("bin/") && File.file?(file)++ File.open(file, &:readline).strip == RUBY_SHEBANG+ rescue StandardError+ falseenddef partition_files(files)- core_files = []- external_files = Hash.new { |h, k| h[k] = [] }-- files.each do |f|- if (root = external_plugin_root(f))- external_files[root] << f- else- core_files << f- end- end+ grouped = files.group_by { |file| external_plugin_root(file) }- [core_files, external_files]+ [grouped.delete(nil) || [], grouped]end- # Returns the plugin root if the file is in an unbundled plugin directory, else nil.def external_plugin_root(file)- path = normalize_relative_path(file)- return standalone_plugin_root(file) unless path.start_with?("plugins/")+ return standalone_plugin_root(file) if file.start_with?("..")+ return nil unless file.start_with?("plugins/")- parts = path.split("/")+ parts = file.split("/")return nil if parts.length < 2plugin_dir = "plugins/#{parts[1]}"- File.join(PROJECT_ROOT, plugin_dir) unless bundled_plugins.include?(plugin_dir)+ return nil if bundled_plugins.include?(plugin_dir)++ File.join(PROJECT_ROOT, plugin_dir)enddef standalone_plugin_root(file)- file_path = Pathname.new(file).realpath- plugin_root = file_path.ascend.find { |path| path.join("plugin.rb").file? }- plugin_root&.to_s+ path = Pathname.new(file).realpath+ return nil if path.to_s.start_with?("#{PROJECT_ROOT_REALPATH}#{File::SEPARATOR}")++ path.ascend.find { |dir| dir.join("plugin.rb").file? }&.to_senddef bundled_plugins@bundled_plugins ||=begin- out, status = Open3.capture2(File.join(PROJECT_ROOT, "script", "list_bundled_plugins"))+ output, status = Open3.capture2(File.join(PROJECT_ROOT, "script", "list_bundled_plugins"))abort "Failed to list bundled plugins" unless status.success?- Set.new(out.lines.map(&:strip).reject(&:empty?))++ Set.new(output.lines.map(&:strip).reject(&:empty?))endend- def run_lefthook_command(hook, files)- if !files.empty?- normalized = files.map { |f| normalize_relative_path(f) }- exec_lefthook(hook, nil, normalized)- else- exec_lefthook(hook, nil, files)- end+ def run_external(root, files)+ linter = ExternalLinter.new(root, files, fix: @fix, verbose: @verbose)+ linter.run++ relative = Pathname.new(root).relative_path_from(PROJECT_ROOT_PATH)+ linter.results.each { |name, ok| @results << ["#{name} (#{relative})", ok] }end- def exec_lefthook(hook, command, files)- cmd = ["pnpm", "lefthook", "run", hook]- cmd << "--command" << command if command- files.each { |f| cmd << "--file" << f }- cmd << "--verbose" if @verbose+ def run_lefthook(hook, files)+ log "Running core linters via lefthook..."- puts "[bin/lint] Running core linters via lefthook..."- puts "Running: #{cmd.shelljoin}" if @verbose- @results << ["core linters", system({ "LEFTHOOK" => "1" }, *cmd)]+ outcomes =+ ArgumentBatcher+ .batches(files, per_file_overhead: FILE_FLAG_BYTES)+ .map do |batch|+ cmd = ["pnpm", "lefthook", "run", hook]+ batch.each { |file| cmd << "--file" << file }+ cmd << "--verbose" if @verbose++ debug "Running: #{cmd.shelljoin}"+ system({ "LEFTHOOK" => "1" }, *cmd)+ end++ @results << ["core linters", outcomes.all?]enddef normalize_relative_path(file)- cleaned = file.start_with?("./") ? file[2..] : file- path = Pathname.new(cleaned)- if path.absolute?- begin- path = path.relative_path_from(Pathname.new(PROJECT_ROOT))- rescue ArgumentError- # leave as is if it's outside the repo- end- end- path.to_s+ absolute = File.expand_path(file, INVOCATION_PWD)+ Pathname.new(absolute).relative_path_from(PROJECT_ROOT_PATH).to_s+ rescue ArgumentError+ fileendend@@ -407,8 +437,7 @@ def parse_optionsputs " bin/lint app/models/*.rb # Lint multiple files"puts " bin/lint frontend/discourse/app/ # Lint all lintable files in directory"puts- puts "Note: This script now uses lefthook to run linters."- puts "Check lefthook.yml for linting configuration."+ puts "Linters are configured in lefthook.yml."exitend@@ -432,10 +461,12 @@ def parse_optionsoptions[:files] = ARGV unless ARGV.empty?options+rescue OptionParser::ParseError => e+ abort "[bin/lint] #{e.message}\nRun `bin/lint --help` for usage."endif __FILE__ == $0options = parse_options- linter = LefthookLinter.new(options)- linter.run+ Dir.chdir(PROJECT_ROOT) # rubocop:disable Discourse/NoChdir+ LefthookLinter.new(options).runenddiff --git a/lefthook.yml b/lefthook.ymlindex 648f4e1d0cb..66fd7d3a6d4 100644--- a/lefthook.yml+++ b/lefthook.yml@@ -17,7 +17,9 @@ pre-commit:commands:rubocop:exclude: &ruby_exclude+ - "bin/dev"- "bin/notify_file_change"+ - "bin/system_rspec"- "bin/docker/**/*"glob: &ruby_glob- "**/*.{rb,rake,thor}"diff --git a/spec/tasks/lint_cli_spec.rb b/spec/tasks/lint_cli_spec.rbnew file mode 100644index 00000000000..a6598d8f88a--- /dev/null+++ b/spec/tasks/lint_cli_spec.rb@@ -0,0 +1,107 @@+# frozen_string_literal: true++require "fileutils"+require "tmpdir"++load Rails.root.join("bin/lint")++RSpec.describe LefthookLinter do+ it "reports when a core file has no configured linter" do+ expect { described_class.new(files: ["README.md"]).run }.to output(<<~OUTPUT).to_stdout+ [bin/lint] No core linter for: README.md+ [bin/lint] Nothing was linted+ OUTPUT+ end++ it "does not send non-plugin files outside the project to core linters" do+ Dir.mktmpdir("lint-outside", Rails.root.parent) do |directory|+ file = File.join(directory, "example.rb")+ File.write(file, "puts :example\n")+ relative_file = Pathname.new(file).relative_path_from(PROJECT_ROOT_PATH)++ expect { described_class.new(files: [file]).run }.to output(<<~OUTPUT).to_stdout+ [bin/lint] No core linter for: #{relative_file}+ [bin/lint] Nothing was linted+ OUTPUT+ end+ end++ it "expands symlinked directories returned by git" do+ Dir.mktmpdir("lint-symlink") do |directory|+ original_path = ENV.fetch("PATH")+ original_invocation_log = ENV["LINT_SPEC_INVOCATIONS"]+ target = File.join(directory, "target")+ fake_bin = File.join(directory, "bin")+ invocation_log = File.join(directory, "invocations")+ symlink = Rails.root.join("themes", "lint-spec-#{Process.pid}")++ FileUtils.mkdir_p([target, fake_bin])+ File.write(File.join(target, "example.rb"), "puts :example\n")+ File.symlink(target, symlink)+ File.write(+ File.join(fake_bin, "pnpm"),+ "#!/bin/sh\nprintf '%s\\n' \"$*\" >> \"$LINT_SPEC_INVOCATIONS\"\n",+ )+ FileUtils.chmod(0o755, File.join(fake_bin, "pnpm"))++ ENV["PATH"] = "#{fake_bin}:#{original_path}"+ ENV["LINT_SPEC_INVOCATIONS"] = invocation_log++ expect { described_class.new(files: [symlink.to_s]).run }.to output(+ %r{\[bin/lint\] All lints passed},+ ).to_stdout+ expect(File.read(invocation_log)).to include(+ "lefthook run lint-files --file themes/#{symlink.basename}/example.rb",+ )+ ensure+ ENV["PATH"] = original_path+ ENV["LINT_SPEC_INVOCATIONS"] = original_invocation_log+ FileUtils.rm_f(symlink)+ end+ end+end++RSpec.describe ExternalLinter do+ it "batches large file sets for each external linter" do+ Dir.mktmpdir("external-lint") do |directory|+ original_path = ENV.fetch("PATH")+ original_invocation_log = ENV["LINT_SPEC_INVOCATIONS"]+ source_directory = File.join(directory, "lib")+ fake_bin = File.join(directory, "bin")+ invocation_log = File.join(directory, "invocations")++ FileUtils.mkdir_p([source_directory, fake_bin])+ files = []+ argument_bytes = 0+ index = 0+ while argument_bytes <= ArgumentBatcher::MAX_ARGV_BYTES + 1_000+ relative_path = File.join("lib", "#{index.to_s.rjust(3, "0")}-#{"x" * 220}.rb")+ path = File.join(directory, relative_path)+ File.write(path, "puts :example\n")+ files << path+ argument_bytes += relative_path.bytesize + 1+ index += 1+ end++ File.write(File.join(fake_bin, "bundle"), <<~RUBY)+ #!/usr/bin/env ruby+ File.open(ENV.fetch("LINT_SPEC_INVOCATIONS"), "a") { |file| file.puts(ARGV.join(" ")) }+ RUBY+ FileUtils.chmod(0o755, File.join(fake_bin, "bundle"))++ ENV["PATH"] = "#{fake_bin}:#{original_path}"+ ENV["LINT_SPEC_INVOCATIONS"] = invocation_log++ linter = described_class.new(directory, files, fix: false)+ expect { linter.run }.to output(%r{\[bin/lint\] Running rubocop}).to_stdout++ invocations = File.readlines(invocation_log, chomp: true)+ expect(invocations.count { |invocation| invocation.start_with?("exec stree check") }).to eq(2)+ expect(invocations.count { |invocation| invocation.start_with?("exec rubocop") }).to eq(2)+ expect(linter.results).to all(satisfy { |_, result| result })+ ensure+ ENV["PATH"] = original_path+ ENV["LINT_SPEC_INVOCATIONS"] = original_invocation_log+ end+ end+end