Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix falsy values not being properly compared #490

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/nitpick/blender.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import json
import re
import shlex
from collections.abc import Iterable
from functools import lru_cache, partial
from pathlib import Path
from typing import Any, Callable, TypeVar, cast
Expand Down Expand Up @@ -160,7 +161,7 @@ def find_by_key(self, desired: ElementDetail) -> ElementDetail | None:

def set_key_if_not_empty(dict_: JsonDict, key: str, value: Any) -> None:
"""Update the dict if the value is valid."""
if not value:
if isinstance(value, Iterable) and not value:
return
dict_[key] = value

Expand Down
25 changes: 25 additions & 0 deletions tests/test_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,28 @@ def test_maximum_two_level_nesting_on_lists_using_jmes_expression_as_list_key_fa
),
).assert_file_contents(filename, datadir / "jmes-list-key-expected.yaml")
project.api_check().assert_violations()


def test_falsy_values_properly_reported(tmp_path, datadir):
"""Test that falsy and truthy values are included in the report."""
filename = "foo/file.yaml"
project = ProjectMock(tmp_path).save_file(filename, datadir / "dict-falsy-values-actual.yaml")
project.style(datadir / "dict-falsy-values-desired.toml").api_check_then_fix(
Fuss(
True,
filename,
369,
" has different values. Use this:",
"""
boolean_true_unmatch: true
boolean_false_unmatch: false
string_a_unmatch: string_a
string_b_unmatch: string_b
truthy_int_unmatch: 1
falsy_int_unmatch: 0
truthy_float_unmatch: 1.0
falsy_float_unmatch: 0.0
""",
),
).assert_file_contents(filename, datadir / "dict-falsy-values-expected.yaml")
project.api_check().assert_violations()
16 changes: 16 additions & 0 deletions tests/test_yaml/dict-falsy-values-actual.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
boolean_true_match: true
boolean_true_unmatch: false
boolean_false_match: false
boolean_false_unmatch: true
string_a_match: 'string_a'
string_a_unmatch: 'string_b'
string_b_match: 'string_b'
string_b_unmatch: 'string_a'
truthy_int_match: 1
truthy_int_unmatch: 0
falsy_int_match: 0
falsy_int_unmatch: 1
truthy_float_match: 1.0
truthy_float_unmatch: 0.0
falsy_float_match: 0.0
falsy_float_unmatch: 1.0
20 changes: 20 additions & 0 deletions tests/test_yaml/dict-falsy-values-desired.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
["foo/file.yaml"]
boolean_true_match = true
boolean_true_unmatch = true
boolean_false_match = false
boolean_false_unmatch = false

string_a_match = "string_a"
string_a_unmatch = "string_a"
string_b_match = "string_b"
string_b_unmatch = "string_b"

truthy_int_match = 1
truthy_int_unmatch = 1
falsy_int_match = 0
falsy_int_unmatch = 0

truthy_float_match = 1.0
truthy_float_unmatch = 1.0
falsy_float_match = 0.0
falsy_float_unmatch = 0.0
16 changes: 16 additions & 0 deletions tests/test_yaml/dict-falsy-values-expected.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
boolean_true_match: true
boolean_true_unmatch: true
boolean_false_match: false
boolean_false_unmatch: false
string_a_match: 'string_a'
string_a_unmatch: 'string_a'
string_b_match: 'string_b'
string_b_unmatch: 'string_b'
truthy_int_match: 1
truthy_int_unmatch: 1
falsy_int_match: 0
falsy_int_unmatch: 0
truthy_float_match: 1.0
truthy_float_unmatch: 1.0
falsy_float_match: 0.0
falsy_float_unmatch: 0.0