|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "tmpdir" |
| 4 | +require "fileutils" |
| 5 | +require "stringio" |
| 6 | +require "guardrails/audit" |
| 7 | +require "guardrails/audit/auto_fixer" |
| 8 | +require "guardrails/tokens" |
| 9 | + |
| 10 | +RSpec.describe Guardrails::Audit::AutoFixer do |
| 11 | + let(:root) { Pathname(Dir.mktmpdir) } |
| 12 | + after { FileUtils.rm_rf(root) } |
| 13 | + |
| 14 | + def write_view(relative, content) |
| 15 | + full = root.join(relative) |
| 16 | + full.dirname.mkpath |
| 17 | + full.write(content) |
| 18 | + end |
| 19 | + |
| 20 | + def view_content(relative) |
| 21 | + root.join(relative).read(encoding: Encoding::UTF_8) |
| 22 | + end |
| 23 | + |
| 24 | + def violation(type:, file:, line:, column:, value:, snippet: "") |
| 25 | + Guardrails::Audit::Violation.new( |
| 26 | + type: type, file: file, line: line, column: column, snippet: snippet, value: value |
| 27 | + ) |
| 28 | + end |
| 29 | + |
| 30 | + def token(name:, value:, syntax: :css_var) |
| 31 | + Guardrails::Tokens::Token.new( |
| 32 | + name: name, value: value, syntax: syntax, |
| 33 | + file: "tokens.css", line: 1 |
| 34 | + ) |
| 35 | + end |
| 36 | + |
| 37 | + describe "#apply" do |
| 38 | + it "rewrites a raw_color hex with var(--token) when an exact CSS-var match exists" do |
| 39 | + write_view "app/views/x.html.erb", '<svg fill="#0066ff"></svg>' |
| 40 | + v = violation(type: :raw_color, file: "app/views/x.html.erb", line: 1, column: 12, value: "#0066ff") |
| 41 | + tokens = [token(name: "primary-500", value: "#0066ff")] |
| 42 | + |
| 43 | + described_class.new(root, output: StringIO.new, tokens: tokens).apply([v]) |
| 44 | + |
| 45 | + expect(view_content("app/views/x.html.erb")).to eq('<svg fill="var(--primary-500)"></svg>') |
| 46 | + end |
| 47 | + |
| 48 | + it "matches normalized hex (case + short form)" do |
| 49 | + write_view "app/views/x.html.erb", '<svg fill="#fa3"></svg>' |
| 50 | + v = violation(type: :raw_color, file: "app/views/x.html.erb", line: 1, column: 12, value: "#fa3") |
| 51 | + tokens = [token(name: "secondary", value: "#FFAA33")] |
| 52 | + |
| 53 | + described_class.new(root, output: StringIO.new, tokens: tokens).apply([v]) |
| 54 | + |
| 55 | + expect(view_content("app/views/x.html.erb")).to include("var(--secondary)") |
| 56 | + end |
| 57 | + |
| 58 | + it "applies multiple replacements on the same line right-to-left" do |
| 59 | + write_view "app/views/x.html.erb", '<svg fill="#0066ff" stroke="#fa3"></svg>' |
| 60 | + vs = [ |
| 61 | + violation(type: :raw_color, file: "app/views/x.html.erb", line: 1, column: 12, value: "#0066ff"), |
| 62 | + violation(type: :raw_color, file: "app/views/x.html.erb", line: 1, column: 29, value: "#fa3") |
| 63 | + ] |
| 64 | + tokens = [ |
| 65 | + token(name: "primary", value: "#0066ff"), |
| 66 | + token(name: "secondary", value: "#fa3") |
| 67 | + ] |
| 68 | + |
| 69 | + described_class.new(root, output: StringIO.new, tokens: tokens).apply(vs) |
| 70 | + |
| 71 | + content = view_content("app/views/x.html.erb") |
| 72 | + expect(content).to include("var(--primary)") |
| 73 | + expect(content).to include("var(--secondary)") |
| 74 | + end |
| 75 | + |
| 76 | + it "does not apply for SCSS variable tokens (not valid in views)" do |
| 77 | + write_view "app/views/x.html.erb", '<svg fill="#0066ff"></svg>' |
| 78 | + v = violation(type: :raw_color, file: "app/views/x.html.erb", line: 1, column: 12, value: "#0066ff") |
| 79 | + tokens = [token(name: "primary", value: "#0066ff", syntax: :scss_var)] |
| 80 | + |
| 81 | + result = described_class.new(root, output: StringIO.new, tokens: tokens).apply([v]) |
| 82 | + |
| 83 | + expect(result).to be_empty |
| 84 | + expect(view_content("app/views/x.html.erb")).to eq('<svg fill="#0066ff"></svg>') |
| 85 | + end |
| 86 | + |
| 87 | + it "does not apply when no token matches" do |
| 88 | + write_view "app/views/x.html.erb", '<svg fill="#abcdef"></svg>' |
| 89 | + v = violation(type: :raw_color, file: "app/views/x.html.erb", line: 1, column: 12, value: "#abcdef") |
| 90 | + tokens = [token(name: "primary", value: "#0066ff")] |
| 91 | + |
| 92 | + result = described_class.new(root, output: StringIO.new, tokens: tokens).apply([v]) |
| 93 | + |
| 94 | + expect(result).to be_empty |
| 95 | + expect(view_content("app/views/x.html.erb")).to eq('<svg fill="#abcdef"></svg>') |
| 96 | + end |
| 97 | + |
| 98 | + it "skips inline_style violations" do |
| 99 | + v = violation(type: :inline_style, file: "app/views/x.html.erb", line: 1, column: 1, value: 'style="color: red"') |
| 100 | + tokens = [token(name: "primary", value: "#0066ff")] |
| 101 | + |
| 102 | + fixer = described_class.new(root, output: StringIO.new, tokens: tokens) |
| 103 | + expect(fixer.applicable?(v)).to be(false) |
| 104 | + end |
| 105 | + |
| 106 | + it "skips tailwind_arbitrary violations" do |
| 107 | + v = violation(type: :tailwind_arbitrary, file: "app/views/x.html.erb", line: 1, column: 1, value: "#0066ff") |
| 108 | + tokens = [token(name: "primary", value: "#0066ff")] |
| 109 | + |
| 110 | + fixer = described_class.new(root, output: StringIO.new, tokens: tokens) |
| 111 | + expect(fixer.applicable?(v)).to be(false) |
| 112 | + end |
| 113 | + |
| 114 | + it "verifies the value is at the expected column before replacing" do |
| 115 | + write_view "app/views/x.html.erb", "<p>different content</p>" |
| 116 | + v = violation(type: :raw_color, file: "app/views/x.html.erb", line: 1, column: 12, value: "#0066ff") |
| 117 | + tokens = [token(name: "primary", value: "#0066ff")] |
| 118 | + |
| 119 | + result = described_class.new(root, output: StringIO.new, tokens: tokens).apply([v]) |
| 120 | + |
| 121 | + expect(result).to be_empty |
| 122 | + expect(view_content("app/views/x.html.erb")).to eq("<p>different content</p>") |
| 123 | + end |
| 124 | + |
| 125 | + it "returns Result entries with the violation, token, and replacement string" do |
| 126 | + write_view "app/views/x.html.erb", '<svg fill="#0066ff"></svg>' |
| 127 | + v = violation(type: :raw_color, file: "app/views/x.html.erb", line: 1, column: 12, value: "#0066ff") |
| 128 | + tokens = [token(name: "primary-500", value: "#0066ff")] |
| 129 | + |
| 130 | + result = described_class.new(root, output: StringIO.new, tokens: tokens).apply([v]) |
| 131 | + |
| 132 | + expect(result.length).to eq(1) |
| 133 | + expect(result.first.token.name).to eq("primary-500") |
| 134 | + expect(result.first.replacement).to eq("var(--primary-500)") |
| 135 | + end |
| 136 | + |
| 137 | + it "applies fixes across multiple files" do |
| 138 | + write_view "app/views/a.html.erb", '<svg fill="#0066ff"></svg>' |
| 139 | + write_view "app/views/b.html.erb", '<svg fill="#0066ff"></svg>' |
| 140 | + vs = [ |
| 141 | + violation(type: :raw_color, file: "app/views/a.html.erb", line: 1, column: 12, value: "#0066ff"), |
| 142 | + violation(type: :raw_color, file: "app/views/b.html.erb", line: 1, column: 12, value: "#0066ff") |
| 143 | + ] |
| 144 | + tokens = [token(name: "primary", value: "#0066ff")] |
| 145 | + |
| 146 | + described_class.new(root, output: StringIO.new, tokens: tokens).apply(vs) |
| 147 | + |
| 148 | + expect(view_content("app/views/a.html.erb")).to include("var(--primary)") |
| 149 | + expect(view_content("app/views/b.html.erb")).to include("var(--primary)") |
| 150 | + end |
| 151 | + end |
| 152 | +end |
0 commit comments