golden
golden
A Crystal shard for golden file testing, ported from charmbracelet/x/exp/golden.
Golden files contain the raw expected output of your tests, which can contain control codes and escape sequences. When comparing test output, Golden escapes control codes and sequences before comparing with golden files.
Installation
- Add the dependency to your
shard.yml:
dependencies:
golden:
github: dsisnero/golden
- Run
shards install
Usage
require "spec"
require "golden"
describe "MyClass" do
it "produces expected output" do
output = MyClass.new.generate_output
# Compare with testdata/MyClass/produces_expected_output.golden (default)
Golden.require_equal("MyClass/produces_expected_output", output)
# Or specify custom directory
Golden.require_equal("MyClass/produces_expected_output", output,
test_data_dir: "spec/testdata")
# Or use spec directory detection
if spec_testdata = Golden.spec_test_data_dir
Golden.require_equal("MyClass/produces_expected_output", output,
test_data_dir: spec_testdata)
end
end
end
How it works
Golden.require_equal(test_name, output, test_data_dir = nil)looks for a golden file at#{test_data_dir || Golden.dir}/#{test_name}.golden- If
Golden.update?istrue, the golden file is updated with the current output - Otherwise, the output is compared with the golden file content
- If they differ, a diff is shown and the test fails
Configuration
# Set the directory for golden files (default: "testdata")
Golden.dir = "spec/testdata"
# Enable update mode (writes golden files instead of comparing)
Golden.update = true
# Or use environment variable
# GOLDEN_UPDATE=1 crystal spec
# Initialize from environment variable (call in spec_helper)
Golden.init # Checks GOLDEN_UPDATE environment variable
Directory Detection
Golden can help locate your spec directory and test data:
# Find the spec directory (searches upward from current dir)
spec_dir = Golden.find_spec_dir
# => "path/to/your/project/spec"
# Get the test data directory within spec (spec/testdata)
spec_testdata = Golden.spec_test_data_dir
# => "path/to/your/project/spec/testdata"
# Use custom directory for a specific test
Golden.require_equal("test_name", output, test_data_dir: "custom/path")
# Or use the spec test data directory
if spec_testdata = Golden.spec_test_data_dir
Golden.require_equal("test_name", output, test_data_dir: spec_testdata)
end
The test_data_dir parameter in require_equal overrides Golden.dir for that call.
Updating golden files
There are several ways to update golden files:
- In your spec file:
describe "MyClass" do
before_each do
Golden.update = true # Update all golden files in this describe
end
it "produces output" do
Golden.require_equal("test", "output")
end
end
- Using environment variable:
GOLDEN_UPDATE=1 crystal spec
- Globally in spec helper:
# spec/spec_helper.cr
require "golden"
if ENV["GOLDEN_UPDATE"]? == "1"
Golden.update = true
end
File permissions
Golden files are created with permissions 0o600 (read/write for owner only) and directories with 0o750, matching the behavior of the Go version.
Comparison details
- Control codes and escape sequences are escaped using Crystal's
String#inspect - Windows line endings (
\r\n) are normalized to\non non-Windows systems - Both
StringandBytesoutput are supported - Diffs are generated using the similar library with unified diff format
Development
# Run tests
crystal spec
# Run tests with update mode
GOLDEN_UPDATE=1 crystal spec
Porting from Go
This shard ports the functionality from charmbracelet/x/exp/golden:
| Go Function | Crystal Equivalent |
|---|---|
golden.RequireEqual(tb, out) |
Golden.require_equal(test_name, output) |
-update flag |
GOLDEN_UPDATE=1 environment variable or Golden.update = true |
Contributing
- Fork it (https://github.com/dsisnero/golden/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Contributors
- Dominic Sisneros - creator and maintainer
golden
- 0
- 0
- 0
- 0
- 1
- 5 days ago
- January 30, 2026
MIT License
Mon, 02 Feb 2026 00:38:10 GMT