visualci 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 16852431fa9199a7504ec732ea0fe60d9bfbb9bf0819f13bc529749aac5f3bf7
4
+ data.tar.gz: 4007e662d28c8ca2fe6954ae629ccb151c983afe4aff56669ea82d05c60dad13
5
+ SHA512:
6
+ metadata.gz: c5068b31681955fa041bd2c14cd6dabba894ef03fc6012a503a86523b2a313a8bcecef6f1335fd40f9ffc2968b8c5d99e67283b8fa5004e61e37881bf871f1b4
7
+ data.tar.gz: a272e29412e19c527fe7e87df32eb0031edce4c2785c39b2e1488bd02a8b6dcad189b01c99d99e34b1d365ef72573697254d0d93a9a746488745d629e30578a4
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # visualci (Ruby)
2
+
3
+ RSpec helpers for [VisualCI](https://visualci.com): queue PDFs, images, JSON, HTML and text as snapshots during a run; they're uploaded
4
+ with the `visualci` CLI when the run ends. Rendering, diffing and review happen on the server.
5
+
6
+ ```ruby
7
+ # spec_helper.rb
8
+ require 'visualci/rspec'
9
+ RSpec.configure { |c| c.include VisualCI::RSpec }
10
+
11
+ # in a spec
12
+ expect_pdf_to_match_visual('invoice_typical', pdf_path) # the project's renderer
13
+ expect_pdf_to_match_visual('editable_form', pdf_path, engine: :mutool)
14
+ expect_image_to_match_visual('signup_form/dark/1280', screenshot_path, threshold: 0.1)
15
+
16
+ # snapshot tests, reviewed on VisualCI instead of failing the example
17
+ expect_json_to_match_snapshot('api/users/show', JSON.parse(response.body)) # key order doesn't matter
18
+ expect_html_to_match_snapshot('emails/welcome', mail.html_part.body.to_s) # nor attribute order or whitespace
19
+ expect_text_to_match_snapshot('reports/monthly', report.to_s)
20
+ ```
21
+
22
+ Projects that already use rspec-snapshot can send their existing files with the CLI instead:
23
+ `visualci upload --from rspec spec`.
24
+
25
+ In CI, create the build once before the shards run and export its id:
26
+
27
+ ```sh
28
+ export VISUALCI_BUILD_ID=$(visualci build create --shards $CIRCLE_NODE_TOTAL)
29
+ bin/rspec # each shard uploads and finalizes its own snapshots at exit
30
+ visualci build finalize --wait # optional: wait for the result (strict and wait modes)
31
+ ```
32
+
33
+ Set `VISUALCI_TOKEN` (a project token) to enable it. Without `VISUALCI_BUILD_ID`, a local build is created, so a
34
+ laptop's run is compared with the baseline using the server's renderer.
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../visualci'
4
+
5
+ module VisualCI
6
+ # RSpec helpers. `require 'visualci/rspec'` in spec_helper and include VisualCI::RSpec.
7
+ #
8
+ # The helpers don't fail the example: changes are reviewed on VisualCI, where a GitHub check waits for approval.
9
+ # Projects in `strict` mode fail the CI step instead (`visualci build finalize --wait`).
10
+ module RSpec
11
+ def expect_pdf_to_match_visual(name, pdf_path, engine: nil, threshold: nil, platform: '')
12
+ VisualCI.snapshot(name, pdf_path, engine: engine&.to_s, threshold: threshold, platform: platform)
13
+ end
14
+
15
+ def expect_image_to_match_visual(name, image_path, threshold: nil, platform: '')
16
+ VisualCI.snapshot(name, image_path, threshold: threshold, platform: platform)
17
+ end
18
+
19
+ # Snapshot tests reviewed on VisualCI: a changed snapshot doesn't fail the example.
20
+ def expect_text_to_match_snapshot(name, text, platform: '')
21
+ VisualCI.text_snapshot(name, text.to_s, format: 'text', platform: platform)
22
+ end
23
+
24
+ # A Hash, an Array or a JSON string. Key order and formatting don't matter.
25
+ def expect_json_to_match_snapshot(name, value, platform: '')
26
+ value = JSON.parse(value) if value.is_a?(String)
27
+ VisualCI.text_snapshot(name, value, format: 'json', platform: platform)
28
+ end
29
+
30
+ # Attribute order and whitespace between tags don't matter.
31
+ def expect_html_to_match_snapshot(name, html, platform: '')
32
+ VisualCI.text_snapshot(name, html.to_s, format: 'html', platform: platform)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VisualCI
4
+ VERSION = '0.1.0'
5
+ end
data/lib/visualci.rb ADDED
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'open3'
5
+ require 'tempfile'
6
+ require 'fileutils'
7
+ require_relative 'visualci/version'
8
+
9
+ # VisualCI client: queue snapshots during a test run, upload them in one go at the end.
10
+ #
11
+ # VisualCI.snapshot('invoice_typical', 'spec/artifacts/invoice_typical.pdf', engine: 'pdfbox')
12
+ #
13
+ # At exit (or when VisualCI.flush is called) the queued snapshots are uploaded with `visualci upload --manifest`
14
+ # and this process's shard is finalized. Enabled when VISUALCI_TOKEN is set. The build comes from
15
+ # VISUALCI_BUILD_ID (a CI step creates it with `visualci build create --shards N` and exports it); without one, a
16
+ # local build is created on first use, so laptops compare against the baseline too.
17
+ module VisualCI
18
+ class Error < StandardError; end
19
+
20
+ Snapshot = Struct.new(:name, :path, :platform, :threshold, :engine, :format, keyword_init: true)
21
+ FORMATS = %w[text json html].freeze
22
+
23
+ class << self
24
+ attr_writer :cli
25
+
26
+ def cli
27
+ @cli ||= ENV.fetch('VISUALCI_CLI', 'visualci')
28
+ end
29
+
30
+ def enabled?
31
+ !ENV['VISUALCI_TOKEN'].to_s.empty? && ENV['VISUALCI_DISABLED'].to_s.empty?
32
+ end
33
+
34
+ # Queue a file (a PDF, PNG, JPEG, WebP or text file). The file is copied, so tests may delete theirs.
35
+ def snapshot(name, path, platform: '', threshold: nil, engine: nil)
36
+ return false unless enabled?
37
+ raise Error, "#{path} does not exist" unless File.exist?(path)
38
+
39
+ mutex.synchronize do
40
+ copy = File.join(staging_dir, "#{queue.size}-#{File.basename(path)}")
41
+ FileUtils.cp(path, copy)
42
+ queue << Snapshot.new(name: name.to_s, path: copy, platform: platform.to_s, threshold: threshold, engine: engine)
43
+ end
44
+ true
45
+ end
46
+
47
+ # Queue a value as a text snapshot: a String as it is, anything else as JSON (a Hash or Array from an API
48
+ # response, a serialized component tree). `format:` is `text`, `json` or `html`; JSON is compared with its keys
49
+ # sorted and HTML with its attributes sorted, so only real changes count.
50
+ def text_snapshot(name, value, format: nil, platform: '')
51
+ return false unless enabled?
52
+
53
+ format = (format || (value.is_a?(String) ? 'text' : 'json')).to_s
54
+ raise Error, "format must be one of #{FORMATS.join(', ')}" unless FORMATS.include?(format)
55
+
56
+ content = value.is_a?(String) ? value : JSON.pretty_generate(value)
57
+ mutex.synchronize do
58
+ copy = File.join(staging_dir, "#{queue.size}-text.#{format == 'text' ? 'txt' : format}")
59
+ File.write(copy, content)
60
+ queue << Snapshot.new(name: name.to_s, path: copy, platform: platform.to_s, format: format)
61
+ end
62
+ true
63
+ end
64
+
65
+ # Upload the queued snapshots and finalize this shard. Returns the build id, or nil when nothing was queued.
66
+ def flush
67
+ snapshots = mutex.synchronize { queue.dup.tap { queue.clear } }
68
+ return nil if snapshots.empty?
69
+
70
+ build = build_id
71
+ manifest = Tempfile.new(['visualci-manifest', '.json'])
72
+ manifest.write(JSON.generate(snapshots.map { |s| s.to_h.compact }))
73
+ manifest.close
74
+ run!(cli, 'upload', '--manifest', manifest.path, '--build', build)
75
+ finalize_args = [cli, 'build', 'finalize', '--build', build]
76
+ finalize_args += ['--shard', shard_index.to_s] if shard_index
77
+ run!(*finalize_args)
78
+ build
79
+ ensure
80
+ manifest&.unlink
81
+ end
82
+
83
+ def build_id
84
+ @build_id ||= ENV['VISUALCI_BUILD_ID'].to_s.empty? ? create_local_build : ENV.fetch('VISUALCI_BUILD_ID')
85
+ end
86
+
87
+ def reset!
88
+ @queue = []
89
+ @build_id = nil
90
+ end
91
+
92
+ private
93
+
94
+ def mutex
95
+ @mutex ||= Mutex.new
96
+ end
97
+
98
+ def queue
99
+ @queue ||= []
100
+ end
101
+
102
+ def staging_dir
103
+ @staging_dir ||= Dir.mktmpdir('visualci')
104
+ end
105
+
106
+ def shard_index
107
+ index = ENV['VISUALCI_SHARD'] || ENV.fetch('CIRCLE_NODE_INDEX', nil)
108
+ index&.to_i
109
+ end
110
+
111
+ def create_local_build
112
+ out = run!(cli, 'build', 'create', '--mode', 'local', '--subset')
113
+ out.strip.lines.last.to_s.strip
114
+ end
115
+
116
+ def run!(*cmd)
117
+ out, err, status = Open3.capture3(*cmd)
118
+ raise Error, "#{cmd.first(3).join(' ')} failed: #{err.strip}" unless status.success?
119
+
120
+ warn(err) unless err.strip.empty?
121
+ out
122
+ end
123
+ end
124
+ end
125
+
126
+ at_exit { VisualCI.flush if VisualCI.enabled? }
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: visualci
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - DocSpring
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-11-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Queues PDFs and images as VisualCI snapshots during a test run and uploads
14
+ them at the end with the visualci CLI. Rendering, diffing and review happen on the
15
+ VisualCI server.
16
+ email:
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - README.md
22
+ - lib/visualci.rb
23
+ - lib/visualci/rspec.rb
24
+ - lib/visualci/version.rb
25
+ homepage: https://visualci.com
26
+ licenses:
27
+ - Apache-2.0
28
+ metadata:
29
+ source_code_uri: https://github.com/DocSpring/visualci
30
+ rubygems_mfa_required: 'true'
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '3.1'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubygems_version: 3.5.22
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: 'VisualCI client for RSpec: visual regression tests for PDFs, screenshots
50
+ and images'
51
+ test_files: []