Class: Appraisal2::Rubocop::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/appraisal2/rubocop/runner.rb

Constant Summary collapse

DEFAULT_COMMAND =
"rubocop"
DEFAULT_FLAGS =
["--autocorrect"].freeze
RUBOCOP_BIN_PATH_LOADER =
"load Gem.activate_bin_path(\"rubocop\", \"rubocop\")"
PRESERVED_BUNDLE_VARS =
[
  "BUNDLE_GEMFILE",
  "BUNDLE_APP_CONFIG",
  "BUNDLE_PATH",
  "BUNDLE_USER_CONFIG",
  "BUNDLE_USER_CACHE",
  "BUNDLE_USER_PLUGIN",
  "BUNDLE_IGNORE_FUNDING_REQUESTS",
  "BUNDLE_DISABLE_SHARED_GEMS",
  "BUNDLE_VERSION",
  "BUNDLER_VERSION"
].freeze
PRESERVED_RUNTIME_VARS =
[
  "PATH",
  "GEM_HOME",
  "GEM_PATH"
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(path, command: ENV.fetch("APPRAISAL2_RUBOCOP_COMMAND", DEFAULT_COMMAND), flags: nil) ⇒ Runner

Returns a new instance of Runner.



31
32
33
34
35
# File 'lib/appraisal2/rubocop/runner.rb', line 31

def initialize(path, command: ENV.fetch("APPRAISAL2_RUBOCOP_COMMAND", DEFAULT_COMMAND), flags: nil)
  @path = path.to_s
  @command = command.to_s
  @flags = flags || configured_flags
end

Instance Method Details

#command_args(env = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/appraisal2/rubocop/runner.rb', line 48

def command_args(env = nil)
  return [@command, *rubocop_args] unless default_command?

  env ||= command_env
  [
    Gem.ruby,
    *ruby_require_flags(env),
    "-e",
    RUBOCOP_BIN_PATH_LOADER,
    "--",
    *rubocop_args
  ]
end

#command_envObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/appraisal2/rubocop/runner.rb', line 62

def command_env
  current_env = ENV.to_h
  clean_env = bundler_base_env(current_env)

  # Avoid leaking parent Bundler activation into the RuboCop subprocess.
  # The selected BUNDLE_GEMFILE is preserved below, and the Ruby command
  # explicitly loads bundler/setup only when a bundle has been selected.
  clean_env["BUNDLE_LOCKFILE"] = nil
  clean_env["BUNDLE_BIN_PATH"] = nil
  clean_env["BUNDLER_SETUP"] = nil

  (PRESERVED_BUNDLE_VARS + PRESERVED_RUNTIME_VARS).each do |key|
    clean_env[key] = current_env[key] if current_env[key]
  end

  clean_env["RUBYOPT"] = sanitized_rubyopt(current_env["RUBYOPT"])
  clean_env
end

#correct(content) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/appraisal2/rubocop/runner.rb', line 37

def correct(content)
  return content if disabled?

  env = command_env
  args = command_args(env)
  stdout, stderr, status = Open3.capture3(env, *args, stdin_data: content)
  raise "appraisal2-rubocop failed: #{args.join(" ")}\n#{stderr}" unless status.success?

  stdout
end