Module: RSpecTracer::CLI::CacheInfo Private

Defined in:
lib/rspec_tracer/cli/cache_info.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:info — show cache size, last run, and invalidation stats. Backend-agnostic: dispatches through Storage::Backend.build so storage_backend :sqlite reports the populated cache instead of the false "no cache yet" the JsonBackend-only path used to emit.

Class Method Summary collapse

Class Method Details

.directory_size(path) ⇒ 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.



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rspec_tracer/cli/cache_info.rb', line 76

def self.directory_size(path)
  return 0 unless File.directory?(path)

  total = 0
  Dir.glob(File.join(path, '**', '*'), File::FNM_DOTMATCH).each do |entry|
    next unless File.file?(entry)

    total += File.size(entry)
  rescue SystemCallError
    next
  end
  total
end

.format_bytes(bytes) ⇒ 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.



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rspec_tracer/cli/cache_info.rb', line 92

def self.format_bytes(bytes)
  return '0 B' if bytes <= 0

  units = %w[B KB MB GB]
  scale = bytes
  unit_index = 0
  while scale >= 1024 && unit_index < units.length - 1
    scale /= 1024.0
    unit_index += 1
  end
  format('%<scale>.1f %<unit>s', scale: scale, unit: units[unit_index])
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.



64
65
66
67
68
69
70
71
72
# File 'lib/rspec_tracer/cli/cache_info.rb', line 64

def self.print_example_count(stdout, backend)
  snapshot = backend.load_graph(schema_version: Storage::Schema::CURRENT)
  if snapshot.nil?
    stdout.puts 'examples:   <unknown> (schema mismatch; next rspec run will be cold)'
    return
  end

  stdout.puts "examples:   #{snapshot.all_examples.size} tracked"
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.



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rspec_tracer/cli/cache_info.rb', line 50

def self.print_help(stdout)
  stdout.puts <<~HELP
    Usage: rspec-tracer cache:info

    Show the on-disk cache size, the last run id, and example counts
    for the most recent run. Backend-aware: works under
    `storage_backend :json` (default) and `storage_backend :sqlite`.
    Read-only; does not modify the cache.
  HELP
  0
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).

Parameters:

  • args (Array<String>)

    sub-command args (-h / --help).

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

Returns:

  • (Integer)

    exit status (0 = success).



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rspec_tracer/cli/cache_info.rb', line 22

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

  cache_path = RSpecTracer.cache_path
  stdout.puts "cache_path: #{cache_path}"
  stdout.puts "size:       #{format_bytes(directory_size(cache_path))}"

  backend = Storage::Backend.build(cache_path: cache_path, configuration: RSpecTracer)
  run_id = backend.last_run_id
  if run_id.nil? || run_id.to_s.empty?
    stdout.puts 'last_run:   no cache yet (run rspec first)'
    return 0
  end

  stdout.puts "last_run:   #{run_id}"
  print_example_count(stdout, backend)
  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:info: #{e.class}: #{e.message}"
  1
end