Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def add_files(struct, opts):
struct = helpers.ensure(struct, "proj/tests/extra.file", "content")
struct = helpers.merge(struct, {"proj": {"tests": {"another.file": "content"}}})
return struct, opts
def test_merge_basics():
# Given an existing structure,
structure = {"a": {"b": {"c": "1", "d": "2"}}}
# when it is merged to another structure with some common folder
extra_files = {"a": {"b": {"c": "0"}, "e": "2"}, "f": {"g": {"h": "0"}}}
structure = helpers.merge(structure, extra_files)
# then the result, should contain both files from the original and the
# merged structure,
assert structure["a"]["b"]["d"] == "2"
assert structure["f"]["g"]["h"] == "0"
assert structure["a"]["e"] == "2"
# the common leaves should be overridden and a tuple (content, rule)
assert structure["a"]["b"]["c"] == "0"
def add_files(struct, opts):
nov, ncr = helpers.NO_OVERWRITE, helpers.NO_CREATE
struct = helpers.ensure(struct, "proj/tests/file0", "new")
struct = helpers.ensure(struct, "proj/tests/file1", "new", nov)
struct = helpers.ensure(struct, "proj/tests/file2", "new", ncr)
struct = helpers.merge(
struct,
{
"proj": {
"tests": {
"file3": ("new", nov),
"file4": ("new", ncr),
"file5": ("new", None),
"file6": "new",
}
}
},
)
return struct, opts
def test_merge_rules_just_in_original():
# When an update rule exists in the original structure,
structure = {"a": {"b": ("0", helpers.NO_CREATE)}}
# but not in the merged,
extra_files = {"a": {"b": "3"}}
structure = helpers.merge(structure, extra_files)
# then just the content should be updated
# and the rule should be kept identical
assert structure["a"]["b"] == ("3", helpers.NO_CREATE)
def test_merge_rules_just_in_merged():
# When an update rule does not exist in the original structure,
structure = {"a": {"b": "0"}}
# but exists in the merged,
extra_files = {"a": {"b": (None, helpers.NO_CREATE)}}
structure = helpers.merge(structure, extra_files)
# then just the rule should be updated
# and the content should be kept identical
assert structure["a"]["b"] == ("0", helpers.NO_CREATE)
def test_empty_string_leads_to_empty_file_during_merge():
# When the original structure contains a leaf,
structure = {"a": {"b": "0"}}
# and the merged structure overrides it with an empty content
extra_files = {"a": {"b": ""}}
structure = helpers.merge(structure, extra_files)
# then the resulting content should exist and be empty
assert structure["a"]["b"] == ""