Reqord.Tasks.Helpers (reqord v0.4.0)
View SourceShared helper functions for Reqord Mix tasks.
Provides common utilities for:
- Resolving cassette paths (relative, absolute, short names)
- Loading and parsing cassette entries
- Finding cassettes in directories
Summary
Functions
Decompresses a response body if it has gzip content-encoding.
Gets the default cassette directory.
Validates that a cassette file exists.
Validates that a directory exists.
Finds all cassette files in a directory recursively.
Loads and parses entries from a cassette file.
Resolves a cassette path, handling both short names and full paths.
Writes entries back to a cassette file.
Functions
Decompresses a response body if it has gzip content-encoding.
Returns the decompressed body if gzipped, otherwise returns the original body. If decompression fails, returns the original body.
Examples
decompress_body(gzipped_binary, %{"content-encoding" => "gzip"})
#=> decompressed_binary
decompress_body(plain_binary, %{"content-type" => "application/json"})
#=> plain_binary
@spec default_cassette_dir() :: String.t()
Gets the default cassette directory.
Examples
default_cassette_dir()
#=> "test/support/cassettes"
@spec ensure_cassette_exists!(String.t()) :: :ok
Validates that a cassette file exists.
Exits with error message if file doesn't exist.
Examples
ensure_cassette_exists!("test/support/cassettes/my_test.jsonl")
#=> :ok
ensure_cassette_exists!("nonexistent.jsonl")
#=> exits with error
@spec ensure_directory_exists!(String.t()) :: :ok
Validates that a directory exists.
Exits with error message if directory doesn't exist.
Examples
ensure_directory_exists!("test/support/cassettes")
#=> :ok
ensure_directory_exists!("nonexistent")
#=> exits with error
Finds all cassette files in a directory recursively.
Returns a list of absolute paths to .jsonl files.
Examples
find_cassettes("test/support/cassettes")
#=> [
"test/support/cassettes/my_test.jsonl",
"test/support/cassettes/api/users_test.jsonl"
]
Loads and parses entries from a cassette file.
Returns a list of decoded JSON entries.
Examples
load_entries("test/support/cassettes/my_test.jsonl")
#=> [%{"req" => %{...}, "resp" => %{...}}]
Resolves a cassette path, handling both short names and full paths.
Examples
# Short name (relative to cassette dir)
resolve_cassette_path("my_test.jsonl", [])
#=> "test/support/cassettes/my_test.jsonl"
# Relative path from project root
resolve_cassette_path("test/support/cassettes/my_test.jsonl", [])
#=> "test/support/cassettes/my_test.jsonl"
# Absolute path
resolve_cassette_path("/full/path/to/cassette.jsonl", [])
#=> "/full/path/to/cassette.jsonl"
# Custom cassette directory
resolve_cassette_path("my_test.jsonl", dir: "test/fixtures")
#=> "test/fixtures/my_test.jsonl"
Writes entries back to a cassette file.
Each entry is encoded as JSON and written on a single line.
Examples
write_entries("cassette.jsonl", [
%{"req" => %{...}, "resp" => %{...}}
])
#=> :ok