-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Carmen Bianca BAKKER <[email protected]>
- Loading branch information
1 parent
03414ca
commit 54c2a92
Showing
2 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,50 @@ | |
|
||
"""Tests for some _config.""" | ||
|
||
import os | ||
from inspect import cleandoc | ||
from textwrap import indent | ||
from unittest import mock | ||
|
||
from reuse._config import Config | ||
from reuse._config import AnnotateOptions, Config | ||
|
||
# REUSE-IgnoreStart | ||
|
||
|
||
def test_annotate_options_merge_one(): | ||
"""Replace one attribute.""" | ||
first = AnnotateOptions( | ||
name="Jane Doe", contact="[email protected]", license="MIT" | ||
) | ||
second = AnnotateOptions(name="John Doe") | ||
result = first.merge(second) | ||
assert result.name == second.name | ||
assert result.contact == first.contact | ||
assert result.license == first.license | ||
|
||
|
||
def test_annotate_options_merge_nothing(): | ||
"""When merging with an empty AnnotateOptions, do nothing.""" | ||
first = AnnotateOptions( | ||
name="Jane Doe", contact="[email protected]", license="MIT" | ||
) | ||
second = AnnotateOptions() | ||
result = first.merge(second) | ||
assert result == first | ||
|
||
|
||
def test_annotate_options_merge_all(): | ||
"""When merging with a full AnnotateOptions, replace all attributes.""" | ||
first = AnnotateOptions( | ||
name="Jane Doe", contact="[email protected]", license="MIT" | ||
) | ||
second = AnnotateOptions( | ||
name="John Doe", contact="[email protected]", license="0BSD" | ||
) | ||
result = first.merge(second) | ||
assert result == second | ||
|
||
|
||
def test_config_from_dict_global_simple(): | ||
"""A simple test case for Config.from_dict.""" | ||
value = { | ||
|
@@ -87,4 +124,91 @@ def test_config_from_yaml_simple(): | |
) | ||
|
||
|
||
def test_config_from_yaml_ordered(): | ||
"""The override options are ordered by appearance in the yaml file.""" | ||
overrides = [] | ||
for i in range(100): | ||
overrides.append( | ||
indent( | ||
cleandoc( | ||
f""" | ||
- path: "{i}" | ||
default_name: Jane Doe | ||
""" | ||
), | ||
prefix=" " * 4, | ||
) | ||
) | ||
text = cleandoc( | ||
""" | ||
annotate: | ||
overrides: | ||
{} | ||
""" | ||
).format("\n".join(overrides)) | ||
result = Config.from_yaml(text) | ||
for i, path in enumerate(result.override_annotate_options): | ||
assert str(i) == path | ||
|
||
|
||
def test_annotations_for_path_global(): | ||
"""When there are no overrides, the annotate options for a given path are | ||
always the global options. | ||
""" | ||
options = AnnotateOptions(name="Jane Doe") | ||
config = Config(global_annotate_options=options) | ||
result = config.annotations_for_path("foo") | ||
assert result == options == config.global_annotate_options | ||
|
||
|
||
def test_annotations_for_path_no_match(): | ||
"""When the given path doesn't match any overrides, return the global | ||
options. | ||
""" | ||
global_options = AnnotateOptions(name="Jane Doe") | ||
override_options = AnnotateOptions(name="John Doe") | ||
config = Config( | ||
global_annotate_options=global_options, | ||
override_annotate_options={"~/Projects": override_options}, | ||
) | ||
result = config.annotations_for_path("/etc/foo") | ||
assert result == global_options | ||
|
||
|
||
def test_annotations_for_path_one_match(): | ||
"""If one override matches, return the global options merged with the | ||
override options. | ||
""" | ||
global_options = AnnotateOptions(name="Jane Doe") | ||
override_options = AnnotateOptions(contact="[email protected]") | ||
config = Config( | ||
global_annotate_options=global_options, | ||
override_annotate_options={"/home/jane/Projects": override_options}, | ||
) | ||
result = config.annotations_for_path( | ||
"/home/jane/Projects/reuse-tool/README.md" | ||
) | ||
assert result.name == "Jane Doe" | ||
assert result.contact == "[email protected]" | ||
assert not result.license | ||
|
||
|
||
def test_annotations_for_path_expand_home(): | ||
"""When the key path of an override starts with '~', expand it when | ||
checking. | ||
""" | ||
with mock.patch.dict(os.environ, {"HOME": "/home/jane"}): | ||
global_options = AnnotateOptions(name="Jane Doe") | ||
override_options = AnnotateOptions(contact="[email protected]") | ||
config = Config( | ||
global_annotate_options=global_options, | ||
override_annotate_options={"~/Projects": override_options}, | ||
) | ||
result = config.annotations_for_path( | ||
# This path must be manually expanded and cannot start with a '~'. | ||
"/home/jane/Projects/reuse-tool/README.md" | ||
) | ||
assert result.contact == "[email protected]" | ||
|
||
|
||
# REUSE-IgnoreEnd |