Files
Brian Smith d87492d54e Create autopkgcmd module to house subcommands
The main `autopkg` module is quite substantial in size. This makes it                                                                                                          harder to learn how the tool fits together. It also impacts confidence
that changes have not broken or introduced implicit coupling, increases
code duplication, makes code review more challenging, and complicates                                                                                                          incrementally adding typing.

In support of moving the `search` command to a dedicated module, a few
utility functions for github access have been consolidated, and a case
of duplicated github search queries has been consolidated. The logic for
initializing or fetching a github token has been refactored to reduce
code duplication as well. Lastly, more type annotations have been added.
as well. More typing was added.
2020-06-24 18:44:52 -07:00

56 lines
1.9 KiB
Python

#!/usr/local/autopkg/python
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
from unittest.mock import patch
from autopkgcmd import search_recipes
class TestSearchCmd(unittest.TestCase):
def setUp(self):
# Disable preference reading for consistency
patch("autopkgcmd.opts.globalPreferences").start()
pass
def test_no_term(self):
self.assertEqual(1, search_recipes(["TestSearchCmd", "search"]))
@patch("autopkgcmd.searchcmd.GitHubSession.code_search")
def test_empty_results(self, gh_mock):
gh_mock.return_value = []
self.assertEqual(
2, search_recipes(["TestSearchCmd", "search", "#test-search#"])
)
@patch("autopkgcmd.searchcmd.print_gh_search_results")
@patch("autopkgcmd.searchcmd.GitHubSession.search_for_name")
def test_too_many_results(self, search_mock, _print_results_mock):
search_mock.return_value = list(range(101))
self.assertEqual(
3, search_recipes(["TestSearchCmd", "search", "#test-search#"])
)
@patch("autopkgcmd.searchcmd.print_gh_search_results")
@patch("autopkgcmd.searchcmd.GitHubSession.search_for_name")
def test_got_results(self, search_mock, _print_results_mock):
search_mock.return_value = list(range(10))
self.assertEqual(
0, search_recipes(["TestSearchCmd", "search", "#test-search#"])
)
if __name__ == "__main__":
unittest.main()