11#!/usr/bin/env python3
2+ """Module used to test resolve_dependency.py script."""
23
34import os
45import string
56
7+ from pathlib import PosixPath
68from random import choice
79from unittest .mock import MagicMock
810from unittest .mock import patch
3840 ("Depends-On: https://github.com/my_org/my_collection/pull" , False ),
3941 ],
4042)
41- def test_resolve_ref (pr_body , match ):
43+ def test_resolve_ref (pr_body : str , match : bool ) -> None :
44+ """Test resolve_ref function.
45+
46+ :param pr_body: pull request body
47+ :param match: whether a depends-on should be found or not
48+ """
4249 expected = 12345 if match else 0
4350 assert resolve_ref (pr_body , "my_org/my_collection" ) == expected
4451
4552
4653class FakePullRequest :
47- def __init__ (self , mergeable ):
54+ # pylint: disable=too-few-public-methods
55+ """Class to simulate PullRequest Object."""
56+
57+ def __init__ (self , mergeable : bool ) -> None :
58+ """Class constructor.
59+
60+ :param mergeable: whether the pull request is mergeable or not
61+ """
4862 self .mergeable = mergeable
4963 self .merge_commit_sha = self .generate_commit_sha ()
5064
5165 @staticmethod
52- def generate_commit_sha (length = 16 ):
66+ def generate_commit_sha (length : int = 16 ) -> str :
67+ """Generate random commit sha.
68+
69+ :param length: The length of the generated string
70+ :returns: The generated commit sha
71+ """
5372 data = string .ascii_letters + string .digits
5473 return "" .join ([choice (data ) for _ in range (length )])
5574
5675
5776@pytest .mark .parametrize ("mergeable" , [True , False ])
5877@patch ("resolve_dependency.Github" )
59- def test_get_pr_merge_commit_sha (m_Github , mergeable ):
60- m_github_obj = MagicMock ()
61- m_Github .return_value = m_github_obj
78+ def test_get_pr_merge_commit_sha (m_github : MagicMock , mergeable : bool ) -> None :
79+ """Test get_pr_merge_commit_sha function.
80+
81+ :param m_github: The github module
82+ :param mergeable: whether the pull request is mergeable or not
83+ """
84+ github_obj = MagicMock ()
85+ m_github .return_value = github_obj
6286
6387 os .environ ["GITHUB_TOKEN" ] = "unittest_github_token"
6488
6589 m_github_repo = MagicMock ()
66- m_github_obj .get_repo = MagicMock ()
67- m_github_obj .get_repo .return_value = m_github_repo
90+ github_obj .get_repo = MagicMock ()
91+ github_obj .get_repo .return_value = m_github_repo
6892
6993 local_pr = FakePullRequest (mergeable = mergeable )
7094 m_github_repo .get_pull = MagicMock ()
@@ -79,16 +103,30 @@ def test_get_pr_merge_commit_sha(m_Github, mergeable):
79103 with pytest .raises (ValueError ):
80104 get_pr_merge_commit_sha (repository , pr_number )
81105
82- m_Github .assert_called_once_with ("unittest_github_token" )
83- m_github_obj .get_repo .assert_called_once_with (repository )
106+ m_github .assert_called_once_with ("unittest_github_token" )
107+ github_obj .get_repo .assert_called_once_with (repository )
84108 m_github_repo .get_pull .assert_called_once_with (pr_number )
85109
86110
87111@pytest .mark .parametrize ("repository" , [True , False ])
88112@pytest .mark .parametrize ("resolve_ref_pr" , [0 , 1 ])
89113@patch ("resolve_dependency.get_pr_merge_commit_sha" )
90114@patch ("resolve_dependency.resolve_ref" )
91- def test_main (m_resolve_ref , m_get_pr_merge_commit_sha , repository , resolve_ref_pr , tmp_path ):
115+ def test_main (
116+ m_resolve_ref : MagicMock ,
117+ m_get_pr_merge_commit_sha : MagicMock ,
118+ repository : bool ,
119+ resolve_ref_pr : int ,
120+ tmp_path : PosixPath ,
121+ ) -> None :
122+ """Test main function.
123+
124+ :param m_resolve_ref: The resolve_ref mock function
125+ :param m_get_pr_merge_commit_sha: The get_pr_merge_commit_sha mock function
126+ :param repository: whether the repository is defined on environment variable or not
127+ :param resolve_ref_pr: The pull request number
128+ :param tmp_path: The temporary path for file to create for test
129+ """
92130 pr_body = "My pull request body - this is a sample for unit tests"
93131 repository_name = "my_test_repository"
94132 os .environ ["RESOLVE_REF_PR_BODY" ] = pr_body
0 commit comments