Exemplo n.º 1
0
class TestGitRepository(unittest.TestCase):
    def setUp(self):
        self.repo = GitRepository(
            url="https://github.com/opensearch-project/.github",
            ref="8ac515431bf24caf92fea9d9b0af3b8f10b88453",
        )

    def test_checkout(self):
        self.assertEqual(self.repo.url,
                         "https://github.com/opensearch-project/.github")
        self.assertEqual(self.repo.ref,
                         "8ac515431bf24caf92fea9d9b0af3b8f10b88453")
        self.assertEqual(self.repo.sha,
                         "8ac515431bf24caf92fea9d9b0af3b8f10b88453")
        self.assertIs(type(self.repo.temp_dir), TemporaryDirectory)
        self.assertEqual(self.repo.dir,
                         os.path.realpath(self.repo.temp_dir.name))
        self.assertTrue(
            os.path.isfile(os.path.join(self.repo.dir, "CODE_OF_CONDUCT.md")))
        # was added in the next commit
        self.assertFalse(
            os.path.exists(os.path.join(self.repo.dir, "CONTRIBUTING.md")))

    def test_execute(self):
        self.repo.execute("echo $PWD > created.txt")
        self.assertTrue(
            os.path.isfile(os.path.join(self.repo.dir, "created.txt")))

    def test_execute_in_dir(self):
        self.repo.execute("echo $PWD > created.txt",
                          os.path.join(self.repo.dir, "ISSUE_TEMPLATE"))
        self.assertFalse(
            os.path.isfile(os.path.join(self.repo.dir, "created.txt")))
        self.assertTrue(
            os.path.isfile(
                os.path.join(self.repo.dir, "ISSUE_TEMPLATE", "created.txt")))

    @patch("subprocess.check_call")
    def test_execute_silent(self, mock_subprocess):
        self.repo.execute_silent("echo .")
        subprocess.check_call.assert_called_with(
            "echo .",
            cwd=self.repo.dir,
            shell=True,
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
        )

    @patch("subprocess.check_output")
    def test_output(self, mock_subprocess):
        self.repo.output("echo hello")
        subprocess.check_output.assert_called_with("echo hello",
                                                   cwd=self.repo.dir,
                                                   shell=True)
class TestGitRepository(unittest.TestCase):
    @patch('subprocess.check_call', return_value=0)
    @patch('subprocess.check_output',
           return_value='8ac515431bf24caf92fea9d9b0af3b8f10b88453'.encode())
    def setUp(self, *mocks: Any) -> None:
        self.repo = GitRepository(
            url="https://github.com/opensearch-project/.github",
            ref="8ac515431bf24caf92fea9d9b0af3b8f10b88453",
        )

    def test_checkout(self) -> None:
        self.assertEqual(self.repo.url,
                         "https://github.com/opensearch-project/.github")
        self.assertEqual(self.repo.ref,
                         "8ac515431bf24caf92fea9d9b0af3b8f10b88453")
        self.assertEqual(self.repo.sha,
                         "8ac515431bf24caf92fea9d9b0af3b8f10b88453")
        self.assertIs(type(self.repo.temp_dir), TemporaryDirectory)
        self.assertEqual(self.repo.dir,
                         os.path.realpath(self.repo.temp_dir.name))

    def test_execute(self) -> None:
        self.repo.execute("echo $PWD > created.txt")
        self.assertTrue(
            os.path.isfile(os.path.join(self.repo.dir, "created.txt")))

    @patch('subprocess.check_call', return_value=0)
    def test_execute_in_subdir(self, mock_check_call: Mock) -> None:
        subdir = os.path.join(self.repo.dir, "ISSUE_TEMPLATE")
        self.repo.execute("echo $PWD > created.txt", subdir)
        mock_check_call.assert_called_with('echo $PWD > created.txt',
                                           cwd=subdir,
                                           shell=True)

    @patch("subprocess.check_call")
    def test_execute_silent(self, mock_subprocess: Mock) -> None:
        self.repo.execute_silent("echo .")
        mock_subprocess.assert_called_with(
            "echo .",
            cwd=self.repo.dir,
            shell=True,
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
        )

    @patch("subprocess.check_output")
    def test_output(self, mock_subprocess: Any) -> None:
        self.repo.output("echo hello")
        mock_subprocess.assert_called_with("echo hello",
                                           cwd=self.repo.dir,
                                           shell=True)