Skip to content

Commit

Permalink
Changed find_recipe_by names, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nmcspadden committed Sep 21, 2023
1 parent 37c8f37 commit dd2b22c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
11 changes: 6 additions & 5 deletions Code/autopkg
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ from autopkglib import (
core_processor_names,
extract_processor_name_with_recipe_identifier,
find_binary,
find_recipe_by_identifier,
find_recipe_by_name,
find_recipe_by_id_in_map,
find_recipe_by_name_in_map,
get_all_prefs,
get_autopkg_version,
get_identifier,
Expand Down Expand Up @@ -135,12 +135,13 @@ def find_recipe(id_or_name: str, skip_overrides: bool = False):
# search in identifiers
# oh noez we can't find it
log(f"Looking for {id_or_name}...")
recipe = find_recipe_by_name(
recipe = find_recipe_by_name_in_map(
id_or_name, skip_overrides
) or find_recipe_by_identifier(id_or_name, skip_overrides)
) or find_recipe_by_id_in_map(id_or_name, skip_overrides)
if not recipe:
# We didn't find the recipe in the map
log(f"Did not find {id_or_name} in recipe map")
# Look in the repo search directories
return recipe


Expand Down Expand Up @@ -1301,7 +1302,7 @@ def find_processor_path(processor_name, recipe, env=None):
processor_recipe_id,
) = extract_processor_name_with_recipe_identifier(processor_name)
if processor_recipe_id:
shared_processor_recipe_path = find_recipe_by_identifier(
shared_processor_recipe_path = find_recipe_by_id_in_map(
processor_recipe_id
)
if shared_processor_recipe_path:
Expand Down
6 changes: 3 additions & 3 deletions Code/autopkglib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def get_identifier_from_recipe_file(filename) -> Optional[str]:
return None


def find_recipe_by_identifier(
def find_recipe_by_id_in_map(
identifier: str, skip_overrides: bool = False
) -> Optional[str]:
"""Search recipe map for an identifier"""
Expand All @@ -246,7 +246,7 @@ def find_recipe_by_identifier(
return None


def find_recipe_by_name(name: str, skip_overrides: bool = False) -> Optional[str]:
def find_recipe_by_name_in_map(name: str, skip_overrides: bool = False) -> Optional[str]:
"""Search recipe map for a shortname"""
# Check the overrides first, unless skipping them
if not skip_overrides and name in globalRecipeMap["overrides"]:
Expand Down Expand Up @@ -1061,7 +1061,7 @@ def get_processor(processor_name, verbose=None, recipe=None, env=None):
processor_recipe_id,
) = extract_processor_name_with_recipe_identifier(processor_name)
if processor_recipe_id:
shared_processor_recipe_path = find_recipe_by_identifier(
shared_processor_recipe_path = find_recipe_by_id_in_map(
processor_recipe_id
)
if shared_processor_recipe_path:
Expand Down
67 changes: 67 additions & 0 deletions Code/tests/test_autopkglib.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,26 @@ class TestAutoPkg(unittest.TestCase):
"""
)
munki_struct = plistlib.loads(munki_recipe.encode("utf-8"))
recipe_file_struct = {
"identifiers": {
"com.github.autopkg.download.googlechrome": "/Users/test/Library/AutoPkg/RecipeRepos/com.github.autopkg.recipes/GoogleChrome/GoogleChrome.download.recipe",
},
"overrides": {
"GoogleChrome.download": "/Users/test/Library/AutoPkg/RecipeOverrides/GoogleChrome.download.recipe",
},
"overrides-identifiers": {
"local.download.GoogleChrome": "/Users/test/Library/AutoPkg/RecipeOverrides/GoogleChrome.download.recipe",
},
"shortnames": {
"GoogleChrome.download": "/Users/test/Library/AutoPkg/RecipeRepos/com.github.autopkg.recipes/GoogleChrome/GoogleChrome.download.recipe",
},
}

def setUp(self):
# This forces autopkglib to accept our patching of memoize
imp.reload(autopkglib)
autopkglib.globalPreferences
self.mock_recipemap = patch.object(autopkglib, "globalRecipeMap", self.recipe_file_struct)

def tearDown(self):
pass
Expand Down Expand Up @@ -225,6 +240,58 @@ def test_get_identifier_from_recipe_file_returns_none(self, mock_load, mock_read
id = autopkglib.get_identifier_from_recipe_file("fake")
self.assertIsNone(id)

@patch("autopkglib.valid_recipe_file")
def test_find_recipe_by_name_in_map_override(self, mock_valid):
"""find_recipe_by_name_in_map should return identifier from overrides in map"""
mock_valid.return_value = True
with self.mock_recipemap:
id = autopkglib.find_recipe_by_name_in_map("GoogleChrome.download", skip_overrides=False)
self.assertEqual(id, '/Users/test/Library/AutoPkg/RecipeOverrides/GoogleChrome.download.recipe')

@patch("autopkglib.valid_recipe_file")
def test_find_recipe_by_name_in_map_no_override(self, mock_valid):
"""find_recipe_by_name_in_map should return identifier from recipe repos in map."""
mock_valid.return_value = True
with self.mock_recipemap:
id = autopkglib.find_recipe_by_name_in_map("GoogleChrome.download", skip_overrides=True)
self.assertEqual(id, "/Users/test/Library/AutoPkg/RecipeRepos/com.github.autopkg.recipes/GoogleChrome/GoogleChrome.download.recipe")

@patch("autopkglib.valid_recipe_file")
def test_find_recipe_by_name_in_map_returns_none_if_missing(self, mock_valid):
"""find_recipe_by_name_in_map should return None if an id not in the map is given."""
mock_valid.return_value = True
with self.mock_recipemap:
id = autopkglib.find_recipe_by_name_in_map("fake_id", skip_overrides=True)
self.assertIsNone(id)
id = autopkglib.find_recipe_by_name_in_map("fake_id", skip_overrides=False)
self.assertIsNone(id)

@patch("autopkglib.valid_recipe_file")
def test_find_recipe_by_id_in_map_override(self, mock_valid):
"""find_recipe_by_id_in_map should return identifier from overrides in map."""
mock_valid.return_value = True
with self.mock_recipemap:
id = autopkglib.find_recipe_by_id_in_map("local.download.GoogleChrome", skip_overrides=False)
self.assertEqual(id, '/Users/test/Library/AutoPkg/RecipeOverrides/GoogleChrome.download.recipe')

@patch("autopkglib.valid_recipe_file")
def test_find_recipe_by_id_in_map_no_override(self, mock_valid):
"""find_recipe_by_id_in_map should return identifier from recipe repos in map."""
mock_valid.return_value = True
with self.mock_recipemap:
id = autopkglib.find_recipe_by_id_in_map("com.github.autopkg.download.googlechrome", skip_overrides=True)
self.assertEqual(id, "/Users/test/Library/AutoPkg/RecipeRepos/com.github.autopkg.recipes/GoogleChrome/GoogleChrome.download.recipe")

@patch("autopkglib.valid_recipe_file")
def test_find_recipe_by_id_in_map_returns_none_if_missing(self, mock_valid):
"""find_recipe_by_id_in_map should return None if an id not in the map is given."""
mock_valid.return_value = True
with self.mock_recipemap:
id = autopkglib.find_recipe_by_id_in_map("fake_id", skip_overrides=True)
self.assertIsNone(id)
id = autopkglib.find_recipe_by_id_in_map("fake_id", skip_overrides=False)
self.assertIsNone(id)


if __name__ == "__main__":
unittest.main()

0 comments on commit dd2b22c

Please sign in to comment.