cc-os/plugins/cc-architect/scripts/plugin_config/file_verifier.rb

66 lines
2.2 KiB
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
require 'json'
module PluginConfig
class FileVerifier
attr_reader :path
def initialize(path)
@path = path
end
def verify_written(expected_content)
return { verified: false, details: "File not found: #{path}" } unless File.exist?(path)
actual = File.read(path)
if actual == expected_content
{ verified: true, details: 'Write verified: content matches' }
else
{ verified: false, details: 'Write verification failed: content mismatch' }
end
rescue StandardError => e
{ verified: false, details: "Verification error: #{e.message}" }
end
def verify_key(section, key, expected_value)
return { verified: false, details: "File not found: #{path}" } unless File.exist?(path)
data = JSON.parse(File.read(path))
container = data.dig(section)
if container.nil?
return { verified: false, details: "Section '#{section}' not found" }
end
if container.key?(key) && container[key] == expected_value
{ verified: true, details: "Key '#{key}' has expected value #{expected_value.inspect}" }
else
actual = container.key?(key) ? container[key].inspect : '(absent)'
{ verified: false, details: "Key '#{key}' expected #{expected_value.inspect}, got #{actual}" }
end
rescue JSON::ParserError => e
{ verified: false, details: "JSON parse error: #{e.message}" }
rescue StandardError => e
{ verified: false, details: "Verification error: #{e.message}" }
end
def verify_key_absent(section, key)
return { verified: false, details: "File not found: #{path}" } unless File.exist?(path)
data = JSON.parse(File.read(path))
container = data.dig(section)
if container.nil? || !container.key?(key)
{ verified: true, details: "Key '#{key}' is absent from '#{section}'" }
else
{ verified: false, details: "Key '#{key}' still present in '#{section}'" }
end
rescue JSON::ParserError => e
{ verified: false, details: "JSON parse error: #{e.message}" }
rescue StandardError => e
{ verified: false, details: "Verification error: #{e.message}" }
end
end
end