Module: Plushie::Test::Snapshot
- Defined in:
- lib/plushie/test/snapshot.rb
Overview
Snapshot and screenshot assertion helpers.
Tree hash snapshots compare a SHA-256 hash captured by the renderer against a golden file in test/snapshots/.sha256. Screenshot assertions do the same with pixel data hashes in test/screenshots/.sha256.
First run creates the golden file. Set PLUSHIE_UPDATE_SNAPSHOTS=1 or PLUSHIE_UPDATE_SCREENSHOTS=1 to update existing golden files.
Constant Summary collapse
- SNAPSHOTS_DIR =
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.
Default directory for tree snapshot files.
"test/snapshots"- SCREENSHOTS_DIR =
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.
Default directory for screenshot files.
"test/screenshots"
Instance Method Summary collapse
-
#assert_screenshot(name, **opts) ⇒ Object
Compare a screenshot pixel hash against a stored golden value.
-
#assert_tree_hash(name) ⇒ Object
Compare a tree hash from the renderer against a stored golden value.
-
#assert_tree_snapshot(tree, path) ⇒ Object
Compare a tree (as JSON) against a stored golden file.
Instance Method Details
#assert_screenshot(name, **opts) ⇒ Object
Compare a screenshot pixel hash against a stored golden value. No-op on mock backend (screenshots are meaningless without rendering).
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/plushie/test/snapshot.rb', line 53 def assert_screenshot(name, **opts) return if Plushie::Test.backend == :mock response = session.screenshot(name, **opts) hash = response[:hash] || response["hash"] raise "screenshot returned no hash for #{name.inspect}" unless hash path = File.join(SCREENSHOTS_DIR, "#{name}.sha256") _assert_golden_hash(path, hash, update_env: "PLUSHIE_UPDATE_SCREENSHOTS") end |
#assert_tree_hash(name) ⇒ Object
Compare a tree hash from the renderer against a stored golden value. On first run, writes the golden file. Set PLUSHIE_UPDATE_SNAPSHOTS=1 to overwrite.
40 41 42 43 44 45 46 47 |
# File 'lib/plushie/test/snapshot.rb', line 40 def assert_tree_hash(name) response = session.tree_hash(name) hash = response[:hash] || response["hash"] raise "tree_hash returned no hash for #{name.inspect}" unless hash path = File.join(SNAPSHOTS_DIR, "#{name}.sha256") _assert_golden_hash(path, hash, update_env: "PLUSHIE_UPDATE_SNAPSHOTS") end |
#assert_tree_snapshot(tree, path) ⇒ Object
Compare a tree (as JSON) against a stored golden file. On first run, writes the golden file. Set PLUSHIE_UPDATE_SNAPSHOTS=1 to overwrite.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/plushie/test/snapshot.rb', line 70 def assert_tree_snapshot(tree, path) tree = (tree) unless tree.is_a?(String) json = tree.is_a?(String) ? tree : JSON.pretty_generate(tree) hash = Digest::SHA256.hexdigest(json) if !File.exist?(path) || ENV["PLUSHIE_UPDATE_SNAPSHOTS"] FileUtils.mkdir_p(File.dirname(path)) File.write(path, json) else stored = File.read(path) stored_hash = Digest::SHA256.hexdigest(stored) assert_equal stored_hash, hash, "Tree snapshot mismatch for #{path}. Run with PLUSHIE_UPDATE_SNAPSHOTS=1 to update." end end |