Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 59 additions & 4 deletions Percy/Percy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,59 @@ public static List<Dictionary<string, object>> CaptureResponsiveDom(WebDriver dr
return domSnapshots;
}

private static Dictionary<string, object> MergeSnapshotOptions(Dictionary<string, object>? options)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Medium] No tests for the new merge/array-typing logic

MergeSnapshotOptions and ConvertJsonArray (including the widths-typing fix that was the prior FAIL) ship with no unit tests, though the repo has a Percy.Test project. Regressions in array typing or merge precedence would go uncaught.

Suggestion: Add tests covering: config widths array converting to List<int> consumable by CaptureResponsiveDom; per-call options overriding config; empty and mixed-type arrays; and absent/non-object snapshot config falling back cleanly.

Reviewer: stack:code-review

{
var merged = new Dictionary<string, object>();
if (cliConfig != null)
{
try
{
JsonElement config = (JsonElement)cliConfig;
if (config.TryGetProperty("snapshot", out JsonElement snapshotElement) &&
snapshotElement.ValueKind == JsonValueKind.Object)
{
foreach (JsonProperty prop in snapshotElement.EnumerateObject())
{
merged[prop.Name] = prop.Value.ValueKind switch
{
JsonValueKind.True => true,
JsonValueKind.False => false,
JsonValueKind.Number => prop.Value.TryGetInt32(out int intVal) ? intVal : (object)prop.Value.GetDouble(),
JsonValueKind.String => prop.Value.GetString(),
JsonValueKind.Array => ConvertJsonArray(prop.Value),
_ => prop.Value
Comment thread
rishigupta1599 marked this conversation as resolved.
Outdated
};
}
}
}
catch (Exception) { }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Low] Empty catch swallows config-merge errors

catch (Exception) { } discards every error during config extraction with no log, so a malformed cliConfig silently contributes nothing and the user gets no signal.

Suggestion: Log at debug, matching the existing pattern: Log($"Error reading snapshot config: {e.Message}", "debug");

Reviewer: stack:code-review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Low] Silently swallowed merge error

catch (Exception) { } discards any failure while reading cliConfig.snapshot, so a misconfigured .percy.yml silently drops all config-seeded options with no diagnostic.

Suggestion: Log at debug level inside the catch (the file already has a Log(..., "debug") helper) so misconfiguration is diagnosable while still degrading gracefully.

Reviewer: stack:code-review

}
if (options != null)
{
foreach (var kvp in options)
merged[kvp.Key] = kvp.Value;
}
return merged;
}

private static object ConvertJsonArray(JsonElement array)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Low] ConvertJsonArray is untested

The added test only covers scalar (bool/string) merge precedence; the widths-style array conversion path has no direct coverage.

Suggestion: Add a case where cliConfig.snapshot.widths is a JSON number array and assert it converts to a typed List<int> and survives the merge.

Reviewer: stack:code-review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Low] Dead code: ConvertJsonArray is never called

ConvertJsonArray is a private static method with no call site. JsonElementToObjectDeep (used by MergeSnapshotOptions) already implements the all-integer→List<int> logic, so this method is unused and may trigger unused-member analyzer warnings.

Suggestion: Delete ConvertJsonArray, or route the array branch of JsonElementToObjectDeep through it to remove the duplication.

Reviewer: stack:code-review

{
var items = array.EnumerateArray().ToList();
if (items.Count > 0 && items.All(item => item.ValueKind == JsonValueKind.Number && item.TryGetInt32(out _)))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Low] Empty config widths: [] re-triggers the InvalidCastException

ConvertJsonArray only returns List<int> when items.Count > 0. An empty config array falls through to the second branch returning an empty List<object>. Since MergeSnapshotOptions still adds the widths key, CaptureResponsiveDom (line 774, ContainsKey("widths") true) then runs (List<int>)options["widths"] on a List<object> and throws — the same failure this PR fixes, for the empty-widths edge case.

Suggestion: Return List<int> for empty numeric arrays too, e.g. if (items.Count == 0 || items.All(item => item.ValueKind == JsonValueKind.Number && item.TryGetInt32(out _))), or make line 774 defensive.

Reviewer: stack:code-review

{
return items.Select(item => item.GetInt32()).ToList();
}

return items.Select(item => item.ValueKind switch
{
JsonValueKind.True => true,
JsonValueKind.False => false,
JsonValueKind.Number => item.TryGetInt32(out int intVal) ? intVal : (object)item.GetDouble(),
JsonValueKind.String => item.GetString(),
_ => (object)item
}).ToList();
}

private static bool isResponsiveSnapshotCapture(Dictionary<string, object>? options)
{
if (cliConfig == null) return false;
Expand Down Expand Up @@ -882,14 +935,16 @@ public class Options : Dictionary<string, object> {}
if (_dom == null)
_dom = GetPercyDOM();

// Merge .percy.yml config options with snapshot options (snapshot options take priority)
var mergedOptions = MergeSnapshotOptions(options);

var cookies = driver.Manage().Cookies.AllCookies;
string opts = JsonSerializer.Serialize(options);
dynamic domSnapshot = null;

if (isResponsiveSnapshotCapture(options)) {
domSnapshot = CaptureResponsiveDom(driver, cookies, options);
if (isResponsiveSnapshotCapture(mergedOptions)) {
domSnapshot = CaptureResponsiveDom(driver, cookies, mergedOptions);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[High] Config-seeded widths crashes responsive capture

This now passes mergedOptions into CaptureResponsiveDom. When widths comes from .percy.yml, MergeSnapshotOptions/JsonElementToObjectDeep convert it to List<object>, but CaptureResponsiveDom (line 774) hard-casts options["widths"] to List<int> and throws InvalidCastException. It's swallowed by Snapshot's outer catch, so a config-driven responsive snapshot that previously fell back to default widths now produces no snapshot at all.

Suggestion: Coerce widths at the consumption site regardless of element type:

List<int> widths = new List<int>();
if (options != null && options.TryGetValue("widths", out var raw)) {
    if (raw is List<int> ints) widths = ints;
    else if (raw is List<object> objs) widths = objs.Select(Convert.ToInt32).ToList();
}

or route array conversion through the (currently unused) ConvertJsonArray, which already yields List<int> for homogeneous int arrays.

Reviewer: stack:code-review

} else {
domSnapshot = getSerializedDom(driver, cookies, options, _dom);
domSnapshot = getSerializedDom(driver, cookies, mergedOptions, _dom);
}

Options snapshotOptions = new Options {
Expand Down
Loading