1. Purpose
Canon provides environment variable support that works uniformly across all three interfaces: CLI, Ruby API, and RSpec. This allows you to configure Canon behavior through your environment without changing code or command-line arguments.
Environment variables are particularly useful for:
-
Setting defaults across multiple test runs
-
Configuring CI/CD pipelines
-
Temporary overrides without code changes
-
System-wide configuration preferences
2. Environment Variable Naming
Canon uses a hierarchical naming scheme for environment variables:
2.1. Global Variables
Global variables apply to all formats (XML, HTML, JSON, YAML):
CANON_{ATTRIBUTE_NAME}
export CANON_USE_COLOR=false
export CANON_ALGORITHM=semantic
export CANON_MAX_FILE_SIZE=10485760
2.2. Format-Specific Variables
Format-specific variables apply only to a particular format and override global variables:
CANON_{FORMAT}_{CONFIG_TYPE}_{ATTRIBUTE_NAME}
Where:
-
{FORMAT}is one of:XML,HTML,JSON,YAML -
{CONFIG_TYPE}is one of:DIFF,MATCH,FORMAT -
{ATTRIBUTE_NAME}is the configuration attribute
# XML-specific diff configuration
export CANON_XML_DIFF_ALGORITHM=semantic
export CANON_XML_DIFF_USE_COLOR=true
# HTML-specific match configuration
export CANON_HTML_MATCH_PROFILE=rendered
# JSON-specific preprocessing
export CANON_JSON_FORMAT_PREPROCESSING=normalize
3. Complete Environment Variables Reference
3.1. Diff Configuration Variables
| Variable Name | Type | Default | Description | Applies To |
|---|---|---|---|---|
|
symbol |
|
Diff output mode: |
All formats |
|
symbol |
|
How documents are normalized for display before the line diff: |
All formats |
|
integer |
|
Indentation depth used by the pretty-printer when |
All formats (display only) |
|
symbol |
|
Indentation character for the pretty-printer: |
All formats (display only) |
|
string array |
|
Comma-separated list of XML element names whose inter-element whitespace is presence-significant but form-insensitive: both |
XML (display only) |
|
string array |
|
Comma-separated list of XML element names where every whitespace character is significant and visualized verbatim ( |
XML (display only) |
|
string array |
|
Comma-separated list of XML element names where whitespace-only text nodes are stripped entirely. Only applies when |
XML (display only) |
|
boolean |
|
When |
XML (display only) |
|
boolean |
|
Same as |
XML (display only) |
|
boolean |
|
When |
XML (display only) |
|
boolean |
|
Show the raw (un-preprocessed) file contents of both sides before the diff output. Equivalent to enabling both |
All formats (display only) |
|
boolean |
|
Show only the EXPECTED (fixture) block in the raw-inputs section. Has no effect unless |
All formats (display only) |
|
boolean |
|
Show only the RECEIVED (actual) block in the raw-inputs section. Format-specific: |
All formats (display only) |
|
boolean |
|
Show the preprocessed (post-comparison-preprocessing) contents of both sides before the diff output. Equivalent to enabling both |
All formats (display only) |
|
boolean |
|
Show only the EXPECTED (fixture) block in the preprocessed-inputs section. Has no effect unless |
All formats (display only) |
|
boolean |
|
Show only the RECEIVED (actual) block in the preprocessed-inputs section. Format-specific: |
All formats (display only) |
|
boolean |
|
Show a fixture-ready pretty-printed form of both original input sides before the diff output. The output is formatted with one XML/HTML tag per line and proper indentation (using the configured |
All formats (display only) |
|
boolean |
|
Show only the EXPECTED (fixture) block in the fixture-ready pretty-printed section. Use this to see the current fixture re-formatted for copy-pasting when the fixture is the side that needs updating. Format-specific: |
All formats (display only) |
|
boolean |
|
Show only the RECEIVED (actual) block in the fixture-ready pretty-printed section. This is the most common fixture-update workflow: enable this option to get a copy-pasteable pretty-printed form of the generated output that can replace the old fixture heredoc. Format-specific: For HTML / HTML4 / HTML5 inputs, the pretty-printed output is XHTML-shaped: void elements are self-closed ( |
All formats (display only) |
|
boolean |
|
Render element nodes in the Semantic Diff Report as compact inline XML (e.g. |
All formats (display only) |
|
symbol |
|
Replace invisible characters with visible Unicode symbols in diff output: |
All formats (display only) |
|
boolean |
|
Enable/disable ANSI color codes in output |
All formats |
|
integer |
|
Number of context lines around changes |
All formats |
|
integer |
|
Maximum lines between changes to group them |
All formats |
|
symbol |
|
Which diffs to show: |
All formats |
|
boolean |
|
Enable verbose diff output with detailed analysis |
All formats |
|
symbol |
|
Diff algorithm: |
All formats |
|
integer |
|
Maximum file size in bytes (default 5MB) |
All formats |
|
integer |
|
Maximum number of nodes in parsed tree |
All formats |
|
integer |
|
Maximum number of diff output lines |
All formats |
|
symbol |
|
Diff display theme: |
All formats |
4. Examples: Same Configuration, Different Interfaces
This section demonstrates how the same configuration is expressed across all three Canon interfaces.
4.1. Example 1: Basic Configuration
Enable semantic diff algorithm with color output.
export CANON_ALGORITHM=semantic
export CANON_USE_COLOR=true
canon diff file1.xml file2.xml \
--diff-algorithm semantic \
--color
Canon.compare(file1, file2, format: :xml,
diff_algorithm: :semantic,
color: true
)
RSpec.configure do |config|
config.canon.xml.diff.algorithm = :semantic
config.canon.xml.diff.use_color = true
end
4.2. Example 2: Format-Specific Configuration
Configure XML and HTML differently.
# XML uses semantic diff
export CANON_XML_DIFF_ALGORITHM=semantic
export CANON_XML_MATCH_PROFILE=strict
# HTML uses DOM diff and rendered profile
export CANON_HTML_DIFF_ALGORITHM=dom
export CANON_HTML_MATCH_PROFILE=rendered
# For XML files
canon diff file1.xml file2.xml \
--diff-algorithm semantic \
--match-profile strict
# For HTML files
canon diff file1.html file2.html \
--diff-algorithm dom \
--match-profile rendered
# For XML
Canon.compare(file1_xml, file2_xml, format: :xml,
diff_algorithm: :semantic,
match_profile: :strict
)
# For HTML
Canon.compare(file1_html, file2_html, format: :html,
diff_algorithm: :dom,
match_profile: :rendered
)
RSpec.configure do |config|
# XML configuration
config.canon.xml.diff.algorithm = :semantic
config.canon.xml.match.profile = :strict
# HTML configuration
config.canon.html.diff.algorithm = :dom
config.canon.html.match.profile = :rendered
end
4.3. Example 3: Size Limits
Configure file size and node count limits.
export CANON_MAX_FILE_SIZE=10485760 # 10MB
export CANON_MAX_NODE_COUNT=50000 # 50k nodes
export CANON_MAX_DIFF_LINES=20000 # 20k lines
Canon.configure do |config|
config.xml.diff.max_file_size = 10_485_760
config.xml.diff.max_node_count = 50_000
config.xml.diff.max_diff_lines = 20_000
end
RSpec.configure do |config|
config.canon.xml.diff.max_file_size = 10_485_760
config.canon.xml.diff.max_node_count = 50_000
config.canon.xml.diff.max_diff_lines = 20_000
end
| Size limits are not available as CLI options - they must be configured through environment variables or programmatic configuration. |
5. Priority Chain
When multiple configuration sources are present, Canon uses the following priority order (highest to lowest):
-
Explicit arguments - Passed directly to the comparison method or CLI command
-
Environment variables - Set via
CANON_*variables -
Default values - Built-in Canon defaults
# Environment variable sets default
export CANON_ALGORITHM=semantic
# CLI argument overrides environment variable
canon diff file1.xml file2.xml --diff-algorithm dom
# Uses: dom (explicit CLI argument wins)
# No CLI argument, environment variable applies
canon diff file1.xml file2.xml
# Uses: semantic (from environment variable)
6. Type Conversion
Canon automatically converts environment variable string values to the appropriate types:
| Type | Conversion Rules |
|---|---|
Boolean |
|
Integer |
String parsed as decimal integer |
Symbol |
String converted to symbol (e.g., |