Class: SnapDiff::SnapManager

Inherits:
Object
  • Object
show all
Defined in:
lib/snap_diff/snap_manager.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ SnapManager

Returns a new instance of SnapManager.



13
14
15
16
# File 'lib/snap_diff/snap_manager.rb', line 13

def initialize(root)
  @root = Pathname.new(root)
  @snapshots = Set.new
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



11
12
13
# File 'lib/snap_diff/snap_manager.rb', line 11

def root
  @root
end

#snapshotsObject (readonly)

Returns the value of attribute snapshots.



85
86
87
# File 'lib/snap_diff/snap_manager.rb', line 85

def snapshots
  @snapshots
end

Class Method Details

.cleanup!Object



69
70
71
# File 'lib/snap_diff/snap_manager.rb', line 69

def self.cleanup!
  instance.cleanup!
end

.instanceObject

Thread-local memoized manager (D7 fix): snapshots tracked via the class-method path and class-method cleanup! now share one instance, so cleanup! actually deletes what was tracked. Rebuilt when the configured manager class or screenshot root changes (tests re-root per example).

Thread.current is fiber-local — same construct as SnapDiff.session; both split per fiber and must migrate together if async support ever lands.

Scope of the fix: default-root unit tests and before_setup re-rooters get working cleanup. System tests do NOT — system_test_case.rb re-roots in setup do, and LIFO teardown restores the root BEFORE test_helper's cleanup! runs, so the root-mismatch check below rebuilds an empty manager there. That miss is protective: firing cleanup on the restored instance would delete committed baselines under test/fixtures/app/doc/screenshots/ that VCS rollback just restored. Do not reorder those teardowns without revisiting this.



112
113
114
115
116
117
118
119
120
121
# File 'lib/snap_diff/snap_manager.rb', line 112

def self.instance
  manager_class = SnapDiff.config.manager
  root = Pathname.new(SnapDiff.config.screenshot_area_abs)

  current = Thread.current[:snap_diff_manager]
  unless current&.instance_of?(manager_class) && current.root == root
    current = Thread.current[:snap_diff_manager] = manager_class.new(root)
  end
  current
end

.path_for(screenshot_full_name, screenshot_format = "png") ⇒ Object



35
36
37
# File 'lib/snap_diff/snap_manager.rb', line 35

def self.path_for(screenshot_full_name, screenshot_format = "png")
  instance.path_for(screenshot_full_name, screenshot_format)
end

.rootObject



91
92
93
# File 'lib/snap_diff/snap_manager.rb', line 91

def self.root
  instance.root
end

.screenshotsObject



87
88
89
# File 'lib/snap_diff/snap_manager.rb', line 87

def self.screenshots
  instance.screenshots
end

.snapshot(screenshot_full_name, screenshot_format = "png") ⇒ Object



24
25
26
# File 'lib/snap_diff/snap_manager.rb', line 24

def self.snapshot(screenshot_full_name, screenshot_format = "png")
  instance.snapshot(screenshot_full_name, screenshot_format)
end

Instance Method Details

#abs_path_for(relative_path) ⇒ Object



39
40
41
# File 'lib/snap_diff/snap_manager.rb', line 39

def abs_path_for(relative_path)
  @root / relative_path
end

#checkout_file(path, as_path) ⇒ Object



43
44
45
46
# File 'lib/snap_diff/snap_manager.rb', line 43

def checkout_file(path, as_path)
  create_output_directory_for(as_path) unless as_path.exist?
  SnapDiff::Vcs.checkout_vcs(root, path, as_path)
end

#cleanup!Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/snap_diff/snap_manager.rb', line 58

def cleanup!
  snapshots.each do |snapshot|
    cleanup_attempts!(snapshot)
    snapshot.delete!
  end
  # With the manager memoized per thread the set would otherwise grow for
  # the whole run, and a later cleanup! could delete a path an earlier
  # test tracked but the current test re-provisioned.
  snapshots.clear
end

#cleanup_attempts!(snapshot) ⇒ Object



73
74
75
# File 'lib/snap_diff/snap_manager.rb', line 73

def cleanup_attempts!(snapshot)
  FileUtils.rm_rf snapshot.find_attempts_paths, secure: true
end

#create_output_directory_for(path = nil) ⇒ Object



54
55
56
# File 'lib/snap_diff/snap_manager.rb', line 54

def create_output_directory_for(path = nil)
  path ? path.dirname.mkpath : root.mkpath
end

#move(new_screenshot_path, screenshot_path) ⇒ Object



77
78
79
# File 'lib/snap_diff/snap_manager.rb', line 77

def move(new_screenshot_path, screenshot_path)
  FileUtils.mv(new_screenshot_path, screenshot_path, force: true)
end

#path_for(screenshot_full_name, screenshot_format = "png") ⇒ Object

Pure path lookup: builds the Snap (path bundle) for a name WITHOUT registering it for cleanup. Use this when you only need to know where a screenshot lives; use #snapshot when taking one that cleanup! owns.



31
32
33
# File 'lib/snap_diff/snap_manager.rb', line 31

def path_for(screenshot_full_name, screenshot_format = "png")
  Snap.new(screenshot_full_name, screenshot_format, manager: self)
end

#provision_snap_with(snap, path, version: :actual) ⇒ Object



48
49
50
51
52
# File 'lib/snap_diff/snap_manager.rb', line 48

def provision_snap_with(snap, path, version: :actual)
  managed_path = snap.path_for(version)
  create_output_directory_for(managed_path) unless managed_path.exist?
  FileUtils.cp(path, managed_path)
end

#screenshotsObject



81
82
83
# File 'lib/snap_diff/snap_manager.rb', line 81

def screenshots
  root.children.map { |f| f.basename.to_s }
end

#snapshot(screenshot_full_name, screenshot_format = "png") ⇒ Object



18
19
20
21
22
# File 'lib/snap_diff/snap_manager.rb', line 18

def snapshot(screenshot_full_name, screenshot_format = "png")
  Snap.new(screenshot_full_name, screenshot_format, manager: self).tap do |snapshot|
    @snapshots << snapshot
  end
end