Skip to content

Commit

Permalink
Updated some type information.
Browse files Browse the repository at this point in the history
  • Loading branch information
hiker committed Jun 24, 2024
1 parent 523e876 commit 538e4da
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
10 changes: 5 additions & 5 deletions source/fab/artefacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from collections import defaultdict
from enum import auto, Enum
from pathlib import Path
from typing import Dict, Iterable, List, Optional, Set, Union
from typing import Dict, Iterable, List, Optional, Union

from fab.dep_tree import filter_source_tree, AnalysedDependent
from fab.util import suffix_filter
Expand Down Expand Up @@ -66,7 +66,7 @@ def reset(self):
self[artefact] = set()

def add(self, collection: Union[str, ArtefactSet],
files: Union[str, List[str], Set[str]]):
files: Union[Path, str, Iterable[Path], Iterable[str]]):
'''Adds the specified artefacts to a collection. The artefact
can be specified as a simple string, a list of string or a set, in
which case all individual entries of the list/set will be added.
Expand All @@ -75,7 +75,7 @@ def add(self, collection: Union[str, ArtefactSet],
'''
if isinstance(files, list):
files = set(files)
elif not isinstance(files, set):
elif not isinstance(files, Iterable):
# We need to use a list, otherwise each character is added
files = set([files])

Expand Down Expand Up @@ -110,8 +110,8 @@ def copy_artefacts(self, source: Union[str, ArtefactSet],
self.add(dest, self[source])

def replace(self, artefact: Union[str, ArtefactSet],
remove_files: List[str],
add_files: Union[List[str], dict]):
remove_files: List[Union[str, Path]],
add_files: Union[List[Union[str, Path]], dict]):
'''Replaces artefacts in one artefact set with other artefacts. This
can be used e.g to replace files that have been preprocessed
and renamed. There is no requirement for these lists to have the
Expand Down
42 changes: 23 additions & 19 deletions tests/unit_tests/test_artefacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
FilterBuildTrees, SuffixFilter)


def test_artefact_store():
def test_artefact_store() -> None:
'''Tests the ArtefactStore class.'''
artefact_store = ArtefactStore()
assert len(artefact_store) == len(ArtefactSet)
Expand All @@ -25,7 +25,7 @@ def test_artefact_store():
assert isinstance(artefact_store[artefact], set)


def test_artefact_store_copy():
def test_artefact_store_copy() -> None:
'''Tests the add and copy operations.'''
artefact_store = ArtefactStore()
# We need paths for suffix filtering, so create some
Expand Down Expand Up @@ -63,28 +63,32 @@ def test_artefact_store_copy():
assert artefact_store[ArtefactSet.C_BUILD_FILES] == set([a, c])


def test_artefact_store_update_dict():
def test_artefact_store_update_dict() -> None:
'''Tests the update_dict function.'''
artefact_store = ArtefactStore()
artefact_store.update_dict(ArtefactSet.OBJECT_FILES, "a", ["AA"])
assert artefact_store[ArtefactSet.OBJECT_FILES] == {"a": {"AA"}}
artefact_store.update_dict(ArtefactSet.OBJECT_FILES, "b", set(["BB"]))
assert (artefact_store[ArtefactSet.OBJECT_FILES] == {"a": {"AA"},
"b": {"BB"}})
artefact_store.update_dict(ArtefactSet.OBJECT_FILES, "a", [Path("AA")])
assert artefact_store[ArtefactSet.OBJECT_FILES] == {"a": {Path("AA")}}
artefact_store.update_dict(ArtefactSet.OBJECT_FILES,
"b", set([Path("BB")]))
assert (artefact_store[ArtefactSet.OBJECT_FILES] == {"a": {Path("AA")},
"b": {Path("BB")}})


def test_artefact_store_replace():
def test_artefact_store_replace() -> None:
'''Tests the replace function.'''
artefact_store = ArtefactStore()
artefact_store.add(ArtefactSet.ALL_SOURCE, ["a", "b", "c"])
artefact_store.replace(ArtefactSet.ALL_SOURCE, remove_files=["a", "b"],
add_files=["B"])
assert artefact_store[ArtefactSet.ALL_SOURCE] == set(["B", "c"])
artefact_store.add(ArtefactSet.ALL_SOURCE, [Path("a"), Path("b"),
Path("c")])
artefact_store.replace(ArtefactSet.ALL_SOURCE,
remove_files=[Path("a"), Path("b")],
add_files=[Path("B")])
assert artefact_store[ArtefactSet.ALL_SOURCE] == set([Path("B"),
Path("c")])

# Test the behaviour for dictionaries
with pytest.raises(RuntimeError) as err:
artefact_store.replace(ArtefactSet.OBJECT_FILES, remove_files=["a"],
add_files=["c"])
artefact_store.replace(ArtefactSet.OBJECT_FILES,
remove_files=[Path("a")], add_files=["c"])
assert ("Replacing artefacts in dictionary 'ArtefactSet.OBJECT_FILES' "
"is not supported" in str(err.value))

Expand Down Expand Up @@ -128,7 +132,7 @@ class TestFilterBuildTrees():
'''Tests for FilterBuildTrees.'''

@pytest.fixture
def artefact_store(self):
def artefact_store(self) -> ArtefactStore:
'''A fixture that returns an ArtefactStore with
some elements.'''
artefact_store = ArtefactStore()
Expand All @@ -142,7 +146,7 @@ def artefact_store(self):
}
return artefact_store

def test_single_suffix(self, artefact_store):
def test_single_suffix(self, artefact_store) -> None:
'''Ensure the artefact getter passes through the trees properly to
the filter func.'''

Expand All @@ -159,7 +163,7 @@ def test_single_suffix(self, artefact_store):
suffixes=['.foo']),
])

def test_multiple_suffixes(self, artefact_store):
def test_multiple_suffixes(self, artefact_store) -> None:
'''Test it works with multiple suffixes provided.'''
filter_build_trees = FilterBuildTrees(['.foo', '.bar'])
with mock.patch('fab.artefacts.filter_source_tree') as mock_filter:
Expand All @@ -174,7 +178,7 @@ def test_multiple_suffixes(self, artefact_store):
])


def test_collection_getter():
def test_collection_getter() -> None:
'''Test CollectionGetter.'''
artefact_store = ArtefactStore()
artefact_store.add(ArtefactSet.ALL_SOURCE, ["a", "b", "c"])
Expand Down

0 comments on commit 538e4da

Please sign in to comment.