A platform for community discussion. Free, open, simple.
DEV: Stop `bin/lint` skipping files outside the project (#43321)
Régis Hanolc7b7a0b12517
diff --git a/bin/lint b/bin/lintindex 820944afc18..ea7c0b8bd19 100755--- a/bin/lint+++ b/bin/lint@@ -11,6 +11,8 @@ PROJECT_ROOT_PATH = Pathname.new(PROJECT_ROOT)PROJECT_ROOT_REALPATH = PROJECT_ROOT_PATH.realpathINVOCATION_PWD = Dir.pwd+LintAborted = Class.new(StandardError)+module Loggingdef log(message)puts "[bin/lint] #{message}"@@ -185,7 +187,10 @@ class LefthookLinterlint(determine_files)print_summary- exit 1 if @results.any? { |_, ok| !ok }+ @results.all? { |_, ok| ok }+ rescue LintAborted => e+ warn "[bin/lint] #{e.message}"+ falseendprivate@@ -234,7 +239,7 @@ class LefthookLinterif @selector.nil?resolved.filter_map(&:last).each { |problem| warn "[bin/lint] Skipping #{problem}" }- abort "[bin/lint] Nothing to lint" if files.empty?+ raise LintAborted, "Nothing to lint" if files.empty?endfiles@@ -275,7 +280,15 @@ class LefthookLinterdef expand_directory(path)files =- git_lines("ls-files", "--cached", "--others", "--exclude-standard", "--", path, quiet: true)+ git_lines(+ "ls-files",+ "--cached",+ "--others",+ "--exclude-standard",+ "--",+ path,+ on_failure: :ignore,+ )files = [path] if files.empty?files@@ -283,12 +296,12 @@ class LefthookLinter.select { |file| File.file?(file) && lintable_file?(file) }end- def git_lines(*args, quiet: false)+ def git_lines(*args, on_failure: :abort)output, error, status = Open3.capture3("git", *args)return output.lines.map(&:strip).reject(&:empty?) if status.success?+ return [] if on_failure == :ignore- warn "[bin/lint] git #{args.first} failed: #{error.lines.first&.strip}" unless quiet- []+ raise LintAborted, "git #{args.first} failed: #{error.lines.first&.strip}"enddef recent_files@@ -309,7 +322,7 @@ class LefthookLinterenddef core_lintable_file?(file)- return false if file.start_with?("../")+ return false if outside_project?(file)return true if file == "Gemfile"extension = File.extname(file)[1..]@@ -329,8 +342,7 @@ class LefthookLinterenddef lintable_file?(file)- return false if SKIPPED_PATHS.include?(file)- return false if file.split("/").any? { |segment| SKIPPED_SEGMENTS.include?(segment) }+ return false if skipped_project_path?(file)return true if File.basename(file) == "Gemfile"extension = File.extname(file)[1..]@@ -339,6 +351,17 @@ class LefthookLinterLINTABLE_EXTENSIONS.include?(extension)end+ def outside_project?(file)+ file.start_with?("../")+ end++ def skipped_project_path?(file)+ return false if outside_project?(file)+ return true if SKIPPED_PATHS.include?(file)++ file.split("/").any? { |segment| SKIPPED_SEGMENTS.include?(segment) }+ end+def ruby_script?(file)return false unless file.start_with?("bin/") && File.file?(file)@@ -354,7 +377,7 @@ class LefthookLinterenddef external_plugin_root(file)- return standalone_plugin_root(file) if file.start_with?("..")+ return standalone_plugin_root(file) if outside_project?(file)return nil unless file.start_with?("plugins/")parts = file.split("/")@@ -377,7 +400,7 @@ class LefthookLinter@bundled_plugins ||=beginoutput, status = Open3.capture2(File.join(PROJECT_ROOT, "script", "list_bundled_plugins"))- abort "Failed to list bundled plugins" unless status.success?+ raise LintAborted, "Failed to list bundled plugins" unless status.success?Set.new(output.lines.map(&:strip).reject(&:empty?))end@@ -468,5 +491,5 @@ endif __FILE__ == $0options = parse_optionsDir.chdir(PROJECT_ROOT) # rubocop:disable Discourse/NoChdir- LefthookLinter.new(options).run+ exit 1 unless LefthookLinter.new(options).runenddiff --git a/spec/tasks/lint_cli_spec.rb b/spec/tasks/lint_cli_spec.rbindex a6598d8f88a..be8aee39370 100644--- a/spec/tasks/lint_cli_spec.rb+++ b/spec/tasks/lint_cli_spec.rb@@ -5,7 +5,36 @@ require "tmpdir"load Rails.root.join("bin/lint")+module FakeExecutable+ INVOCATIONS_ENV = "LINT_SPEC_INVOCATIONS"+ RECORD_ARGUMENTS = "printf '%s\\n' \"$*\" >> \"$#{INVOCATIONS_ENV}\""++ def with_fake_executable(name, script = nil)+ Dir.mktmpdir("lint") do |directory|+ original_path = ENV.fetch("PATH")+ original_invocations = ENV[INVOCATIONS_ENV]+ fake_bin = File.join(directory, "bin")+ executable = File.join(fake_bin, name)+ invocations = File.join(directory, "invocations")++ FileUtils.mkdir_p(fake_bin)+ File.write(executable, ["#!/bin/sh", RECORD_ARGUMENTS, script].compact.join("\n") + "\n")+ FileUtils.chmod(0o755, executable)++ ENV["PATH"] = "#{fake_bin}:#{original_path}"+ ENV[INVOCATIONS_ENV] = invocations++ yield directory, invocations+ ensure+ ENV["PATH"] = original_path+ ENV[INVOCATIONS_ENV] = original_invocations+ end+ end+end+RSpec.describe LefthookLinter do+ include FakeExecutable+it "reports when a core file has no configured linter" doexpect { described_class.new(files: ["README.md"]).run }.to output(<<~OUTPUT).to_stdout[bin/lint] No core linter for: README.md@@ -14,63 +43,76 @@ RSpec.describe LefthookLinter doendit "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_no_core_linter_outside_project("example.rb")+ end- 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+ it "applies the skipped path list only inside the project" do+ expect_no_core_linter_outside_project("tmp/example.rb")+ end++ it "reports a failing linter without exiting the process" do+ with_fake_executable("pnpm", "exit 1") do+ expect { expect(described_class.new(files: ["Gemfile"]).run).to eq(false) }.to output(+ %r{\[bin/lint\] core linters failed},+ ).to_stdout+ end+ end++ it "fails on the first git error instead of linting nothing" do+ failing_git = "printf 'fatal: detected dubious ownership\\n' >&2\nexit 128"++ with_fake_executable("git", failing_git) do |_directory, invocations|+ expect { expect(described_class.new(recent: true).run).to eq(false) }.to output(+ %r{\[bin/lint\] git log failed: fatal: detected dubious ownership},+ ).to_stderr+ expect(File.readlines(invocations).size).to eq(1)endendit "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"]+ symlink = Rails.root.join("themes", "lint-spec-#{Process.pid}")++ with_fake_executable("pnpm") do |directory, 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])+ FileUtils.mkdir_p(target)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_logexpect { 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(+ expect(File.read(invocations)).to include("lefthook run lint-files --file themes/#{symlink.basename}/example.rb",)ensure- ENV["PATH"] = original_path- ENV["LINT_SPEC_INVOCATIONS"] = original_invocation_logFileUtils.rm_f(symlink)endend++ def expect_no_core_linter_outside_project(relative_path)+ Dir.mktmpdir("lint") do |directory|+ file = File.join(directory, relative_path)+ FileUtils.mkdir_p(File.dirname(file))+ 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+ endendRSpec.describe ExternalLinter do+ include FakeExecutable+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"]+ with_fake_executable("bundle") do |directory, 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])+ FileUtils.mkdir_p(source_directory)files = []argument_bytes = 0index = 0@@ -83,25 +125,13 @@ RSpec.describe ExternalLinter doindex += 1end- 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)+ recorded = File.readlines(invocations, chomp: true)+ expect(recorded.count { |invocation| invocation.start_with?("exec stree check") }).to eq(2)+ expect(recorded.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_logendendend