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.

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.



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

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.



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

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)


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

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.



38
39
40
41
# File 'lib/rspec_tracer/cli/cache_clear.rb', line 38

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.



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

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.



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rspec_tracer/cli/cache_clear.rb', line 84

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

    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.
  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.



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

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
# 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')

  require 'rspec_tracer/load_config'
  existing = existing_targets
  return nothing_to_remove(stdout) if existing.empty?

  announce(stdout, existing)
  force = args.include?('--yes') || args.include?('-y')
  return aborted(stdout) unless force || confirm?(stdout)

  remove_each(stdout, existing)
  0
rescue StandardError => e
  stderr.puts "cache:clear: #{e.class}: #{e.message}"
  1
end