Module: RSpecTracer::CLI::CacheClear Private

Defined in:
lib/rspec_tracer/cli/cache_clear.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

rspec-tracer cache:clear — remove cache, coverage, and report directories. Prompts for confirmation unless --yes is passed. The next rspec run is a cold run.

Constant Summary collapse

SKIP_CONFIRMATION_FLAGS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns true when any of the documented skip-confirmation flags is present. --yes / -y is the canonical form; --force / -f is the Unix-conventional synonym accepted so users' muscle memory works. Either form skips the interactive Proceed? [y/N] prompt.

%w[--yes -y --force -f].freeze

Class Method Summary collapse

Class Method Details

.aborted(stdout) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Internal helper for the tracer pipeline.



83
84
85
86
# File 'lib/rspec_tracer/cli/cache_clear.rb', line 83

def self.aborted(stdout)
  stdout.puts 'cache:clear: aborted'
  0
end

.announce(stdout, existing) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Internal helper for the tracer pipeline.



67
68
69
70
# File 'lib/rspec_tracer/cli/cache_clear.rb', line 67

def self.announce(stdout, existing)
  stdout.puts 'cache:clear: will remove:'
  existing.each { |path| stdout.puts "  - #{path}" }
end

.confirm?(stdout) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Internal helper for the tracer pipeline.

Returns:

  • (Boolean)


74
75
76
77
78
79
# File 'lib/rspec_tracer/cli/cache_clear.rb', line 74

def self.confirm?(stdout)
  stdout.print 'Proceed? [y/N] '
  stdout.flush
  response = $stdin.gets&.chomp&.downcase
  %w[y yes].include?(response)
end

.existing_targetsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Internal helper for the tracer pipeline.



53
54
55
56
# File 'lib/rspec_tracer/cli/cache_clear.rb', line 53

def self.existing_targets
  [RSpecTracer.cache_path, RSpecTracer.coverage_path, RSpecTracer.report_path]
    .select { |path| File.directory?(path) }
end

.nothing_to_remove(stdout) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Internal helper for the tracer pipeline.



60
61
62
63
# File 'lib/rspec_tracer/cli/cache_clear.rb', line 60

def self.nothing_to_remove(stdout)
  stdout.puts 'cache:clear: nothing to remove (cache directories do not exist)'
  0
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Internal helper for the tracer pipeline.



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rspec_tracer/cli/cache_clear.rb', line 99

def self.print_help(stdout)
  stdout.puts <<~HELP
    Usage: rspec-tracer cache:clear [--yes | --force]

    Remove cache, coverage, and report directories. The next rspec
    run will be a cold run (full re-execution + cache rebuild).

    Options:
      -y, --yes     Skip the confirmation prompt.
      -f, --force   Synonym for --yes.
  HELP
  0
end

.remove_each(stdout, existing) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Internal helper for the tracer pipeline.



90
91
92
93
94
95
# File 'lib/rspec_tracer/cli/cache_clear.rb', line 90

def self.remove_each(stdout, existing)
  existing.each do |path|
    FileUtils.rm_rf(path)
    stdout.puts "  removed #{path}"
  end
end

.run(args, stdout: $stdout, stderr: $stderr) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns exit status (0 = success / aborted, 1 = error).

Parameters:

  • args (Array<String>)

    sub-command args (-y / --yes skips confirmation; -h / --help prints help).

  • stdout (IO) (defaults to: $stdout)
  • stderr (IO) (defaults to: $stderr)

Returns:

  • (Integer)

    exit status (0 = success / aborted, 1 = error).



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rspec_tracer/cli/cache_clear.rb', line 18

def self.run(args, stdout: $stdout, stderr: $stderr)
  return print_help(stdout) if args.include?('-h') || args.include?('--help')

  existing = existing_targets
  return nothing_to_remove(stdout) if existing.empty?

  announce(stdout, existing)
  return aborted(stdout) unless skip_confirmation?(args) || confirm?(stdout)

  remove_each(stdout, existing)
  0
rescue Errno::EPIPE
  # Downstream pipe (`... | head`) closed early -- routine in
  # shell pipelines, not a failure. Exit 0 silently.
  0
rescue StandardError => e
  stderr.puts "cache:clear: #{e.class}: #{e.message}"
  1
end

.skip_confirmation?(args) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


47
48
49
# File 'lib/rspec_tracer/cli/cache_clear.rb', line 47

def self.skip_confirmation?(args)
  args.intersect?(SKIP_CONFIRMATION_FLAGS)
end