Skip to content

Commit

Permalink
do not deduplicate log warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jaimergp committed Nov 6, 2023
1 parent 76dbc1e commit 00b2544
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 58 deletions.
4 changes: 2 additions & 2 deletions conda_build/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -1683,7 +1683,7 @@ def check_menuinst_json(files, prefix):
return

print("Validating Menu/*.json files")
log = utils.get_logger(__name__)
log = utils.get_logger(__name__, dedupe=False)
try:
import jsonschema
from menuinst.utils import data_path
Expand Down Expand Up @@ -1715,7 +1715,7 @@ def check_menuinst_json(files, prefix):
)
continue
validator.validate(json.loads(text))
except (jsonschema.ValidationError, json.JSONDecodeError) as exc:
except (jsonschema.ValidationError, json.JSONDecodeError, OSError) as exc:
log.warning(
"'%s' is not a valid menuinst JSON document!",
json_file,
Expand Down
106 changes: 50 additions & 56 deletions tests/test_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import shutil
import sys
from pathlib import Path

import pytest

Expand Down Expand Up @@ -95,64 +96,57 @@ def test_pypi_installer_metadata(testing_config):
assert "conda" == (package_has_file(pkg, expected_installer, refresh_mode="forced"))


def test_menuinst_validation_passes(testing_config, caplog):
recipe = os.path.join(metadata_dir, "menu_json_validation")
def test_menuinst_validation_ok(testing_config, caplog, tmp_path):
# 1st check - validation passes with recipe as is
recipe = Path(metadata_dir, "menu_json_validation")
recipe_tmp = tmp_path / "menu_json_validation"
shutil.copytree(recipe, recipe_tmp)

with caplog.at_level(logging.INFO):
pkg = api.build(recipe, config=testing_config, notest=True)[0]
pkg = api.build(str(recipe_tmp), config=testing_config, notest=True)[0]

print(caplog.text)
assert "Found 'Menu/*.json' files but couldn't validate:" not in caplog.text
assert "not a valid menuinst JSON file" not in caplog.text
assert "is a valid menuinst JSON document" in caplog.text
captured_text = caplog.text
print(captured_text)
assert "Found 'Menu/*.json' files but couldn't validate:" not in captured_text
assert "not a valid menuinst JSON file" not in captured_text
assert "is a valid menuinst JSON document" in captured_text
assert package_has_file(pkg, "Menu/menu_json_validation.json")


def test_menuinst_validation_fails_bad_json(testing_config, caplog):
"1st check - non-parsable JSON"
recipe = os.path.join(metadata_dir, "menu_json_validation")
original_content = None

try:
with open(os.path.join(recipe, "menu.json"), "r+") as f:
original_content = f.read()
f.write("Make this an invalid JSON")

with caplog.at_level(logging.WARNING):
api.build(recipe, config=testing_config, notest=True)

print(caplog.text)
assert "Found 'Menu/*.json' files but couldn't validate:" not in caplog.text
assert "not a valid menuinst JSON document" in caplog.text
assert "JSONDecodeError" in caplog.text
finally:
if original_content is not None:
with open(os.path.join(recipe, "menu.json"), "w") as f:
f.write(original_content)


def test_menuinst_validation_fails_bad_schema(testing_config, caplog):
"2nd check - valid JSON, but invalid content fails schema validation"
recipe = os.path.join(metadata_dir, "menu_json_validation")
original_content = None

try:
with open(os.path.join(recipe, "menu.json")) as f:
original_content = f.read()

bad_data = json.loads(original_content)
bad_data["menu_items"][0]["icon"] = None
with open(os.path.join(recipe, "menu.json"), "w") as f:
json.dump(bad_data, f)

with caplog.at_level(logging.WARNING):
api.build(recipe, config=testing_config, notest=True)

print(caplog.text)
assert "Found 'Menu/*.json' files but couldn't validate:" not in caplog.text
assert "not a valid menuinst JSON document" in caplog.text
assert "ValidationError" in caplog.text
assert "is a valid menuinst JSON document" not in caplog.text
finally:
if original_content is not None:
with open(os.path.join(recipe, "menu.json"), "w") as f:
f.write(original_content)
def test_menuinst_validation_fails_bad_schema(testing_config, caplog, tmp_path):
# 1st check - valid JSON but invalid content fails validation
recipe = Path(metadata_dir, "menu_json_validation")
recipe_tmp = tmp_path / "menu_json_validation"
shutil.copytree(recipe, recipe_tmp)
menu_json = recipe_tmp / "menu.json"
menu_json_contents = menu_json.read_text()

bad_data = json.loads(menu_json_contents)
bad_data["menu_items"][0]["osx"] = ["bad", "schema"]
menu_json.write_text(json.dumps(bad_data, indent=2))
with caplog.at_level(logging.WARNING):
api.build(str(recipe_tmp), config=testing_config, notest=True)

captured_text = caplog.text
assert "Found 'Menu/*.json' files but couldn't validate:" not in captured_text
assert "not a valid menuinst JSON document" in captured_text
assert "ValidationError" in captured_text
caplog.clear()


def test_menuinst_validation_fails_bad_json(testing_config, caplog, tmp_path):
# 2nd check - non-parsable JSON fails validation
recipe = Path(metadata_dir, "menu_json_validation")
recipe_tmp = tmp_path / "menu_json_validation"
shutil.copytree(recipe, recipe_tmp)
menu_json = recipe_tmp / "menu.json"
menu_json_contents = menu_json.read_text()
menu_json.write_text(menu_json_contents + "Make this an invalid JSON")

with caplog.at_level(logging.WARNING):
api.build(str(recipe_tmp), config=testing_config, notest=True)

captured_text = caplog.text
assert "Found 'Menu/*.json' files but couldn't validate:" not in captured_text
assert "not a valid menuinst JSON document" in captured_text
assert "JSONDecodeError" in captured_text

0 comments on commit 00b2544

Please sign in to comment.