def test_get_file_content_from_master(self):
    commit_hash = '0000000000000000000000000000000123456789'
    file_phid = 'PHID-FILE-somefile'
    path = 'some/file'
    self.phab.get_latest_master_commit_hash = mock.Mock()
    self.phab.get_latest_master_commit_hash.return_value = commit_hash
    self.phab.diffusion.browsequery = mock.Mock()

    def configure_browsequery(file_path=path, hash='abcdef'):
        self.phab.diffusion.browsequery.return_value = {'paths': [{'fullPath': 'some/file/1', 'hash': '1234'}, {'fullPath': 'some/file/2', 'hash': '5678'}, {'fullPath': file_path, 'hash': hash}]}

    def assert_diffusion_browsequery_called():
        self.phab.get_latest_master_commit_hash.assert_called()
        self.phab.diffusion.browsequery.assert_called_with(path=(os.path.dirname(path) or None), commit=commit_hash, repository=BITCOIN_ABC_REPO, branch='master')

    def configure_file_content_query(file_phid=file_phid, too_slow=False, too_huge=False):
        output = {'tooSlow': too_slow, 'tooHuge': too_huge}
        if (file_phid is not None):
            output['filePHID'] = file_phid
        self.phab.diffusion.filecontentquery.return_value = output

    def assert_file_commit_and_file_searched():
        self.phab.get_latest_master_commit_hash.assert_called()
        self.phab.diffusion.filecontentquery.assert_called_with(path=path, commit=commit_hash, timeout=5, byteLimit=(1024 * 1024), repository=BITCOIN_ABC_REPO, branch='master')
    self.phab.diffusion.browsequery.return_value = {}
    with self.assertRaisesRegex(AssertionError, 'File .+ not found in master'):
        self.phab.get_file_content_from_master(path)
        assert_diffusion_browsequery_called()
    self.phab.diffusion.browsequery.return_value = {'paths': []}
    with self.assertRaisesRegex(AssertionError, 'File .+ not found in master'):
        self.phab.get_file_content_from_master(path)
        assert_diffusion_browsequery_called()
    configure_browsequery(file_path='something/else')
    with self.assertRaisesRegex(AssertionError, 'File .+ not found in master'):
        self.phab.get_file_content_from_master(path)
        assert_diffusion_browsequery_called()
    configure_browsequery()
    configure_file_content_query(file_phid=None)
    with self.assertRaisesRegex(AssertionError, 'File .+ not found in master'):
        self.phab.get_file_content_from_master(path)
        assert_file_commit_and_file_searched()
    configure_file_content_query(too_slow=True)
    with self.assertRaisesRegex(AssertionError, 'is oversized or took too long to download'):
        self.phab.get_file_content_from_master(path)
        assert_file_commit_and_file_searched()
    configure_file_content_query(too_huge=True)
    with self.assertRaisesRegex(AssertionError, 'is oversized or took too long to download'):
        self.phab.get_file_content_from_master(path)
        assert_file_commit_and_file_searched()
    expected_content = b'Some nice content'
    result = test.mocks.phabricator.Result([])
    result.response = b64encode(expected_content)
    self.phab.file.download.return_value = result
    configure_file_content_query()
    file_content = self.phab.get_file_content_from_master(path)
    assert_file_commit_and_file_searched()
    self.phab.file.download.assert_called_with(phid=file_phid)
    self.assertEqual(file_content, expected_content)
    self.phab.diffusion.filecontentquery.reset_mock()
    self.phab.file.download.reset_mock()
    for i in range(10):
        file_content = self.phab.get_file_content_from_master(path)
        self.assertEqual(file_content, expected_content)
        self.phab.diffusion.filecontentquery.assert_not_called()
        self.phab.file.download.assert_not_called()
    for i in range(10):
        commit_hash = str((int(commit_hash) + 1))
        self.phab.get_latest_master_commit_hash.return_value = commit_hash
        file_content = self.phab.get_file_content_from_master(path)
        self.assertEqual(file_content, expected_content)
        self.phab.diffusion.filecontentquery.assert_not_called()
        self.phab.file.download.assert_not_called()
    configure_browsequery(hash='defghi')
    file_content = self.phab.get_file_content_from_master(path)
    assert_file_commit_and_file_searched()
    self.phab.file.download.assert_called_with(phid=file_phid)
    self.assertEqual(file_content, expected_content)
    self.phab.diffusion.filecontentquery.reset_mock()
    self.phab.file.download.reset_mock()
    for i in range(10):
        file_content = self.phab.get_file_content_from_master(path)
        self.assertEqual(file_content, expected_content)
        self.phab.diffusion.filecontentquery.assert_not_called()
        self.phab.file.download.assert_not_called()
def test_get_file_content_from_master(self):
    commit_hash = '0000000000000000000000000000000123456789'
    file_phid = 'PHID-FILE-somefile'
    path = 'some/file'
    self.phab.get_latest_master_commit_hash = mock.Mock()
    self.phab.get_latest_master_commit_hash.return_value = commit_hash
    self.phab.diffusion.browsequery = mock.Mock()

    def configure_browsequery(file_path=path, hash='abcdef'):
        self.phab.diffusion.browsequery.return_value = {'paths': [{'fullPath': 'some/file/1', 'hash': '1234'}, {'fullPath': 'some/file/2', 'hash': '5678'}, {'fullPath': file_path, 'hash': hash}]}

    def assert_diffusion_browsequery_called():
        self.phab.get_latest_master_commit_hash.assert_called()
        self.phab.diffusion.browsequery.assert_called_with(path=(os.path.dirname(path) or None), commit=commit_hash, repository=BITCOIN_ABC_REPO, branch='master')

    def configure_file_content_query(file_phid=file_phid, too_slow=False, too_huge=False):
        output = {'tooSlow': too_slow, 'tooHuge': too_huge}
        if (file_phid is not None):
            output['filePHID'] = file_phid
        self.phab.diffusion.filecontentquery.return_value = output

    def assert_file_commit_and_file_searched():
        self.phab.get_latest_master_commit_hash.assert_called()
        self.phab.diffusion.filecontentquery.assert_called_with(path=path, commit=commit_hash, timeout=5, byteLimit=(1024 * 1024), repository=BITCOIN_ABC_REPO, branch='master')
    self.phab.diffusion.browsequery.return_value = {}
    with self.assertRaisesRegex(AssertionError, 'File .+ not found in master'):
        self.phab.get_file_content_from_master(path)
        assert_diffusion_browsequery_called()
    self.phab.diffusion.browsequery.return_value = {'paths': []}
    with self.assertRaisesRegex(AssertionError, 'File .+ not found in master'):
        self.phab.get_file_content_from_master(path)
        assert_diffusion_browsequery_called()
    configure_browsequery(file_path='something/else')
    with self.assertRaisesRegex(AssertionError, 'File .+ not found in master'):
        self.phab.get_file_content_from_master(path)
        assert_diffusion_browsequery_called()
    configure_browsequery()
    configure_file_content_query(file_phid=None)
    with self.assertRaisesRegex(AssertionError, 'File .+ not found in master'):
        self.phab.get_file_content_from_master(path)
        assert_file_commit_and_file_searched()
    configure_file_content_query(too_slow=True)
    with self.assertRaisesRegex(AssertionError, 'is oversized or took too long to download'):
        self.phab.get_file_content_from_master(path)
        assert_file_commit_and_file_searched()
    configure_file_content_query(too_huge=True)
    with self.assertRaisesRegex(AssertionError, 'is oversized or took too long to download'):
        self.phab.get_file_content_from_master(path)
        assert_file_commit_and_file_searched()
    expected_content = b'Some nice content'
    result = test.mocks.phabricator.Result([])
    result.response = b64encode(expected_content)
    self.phab.file.download.return_value = result
    configure_file_content_query()
    file_content = self.phab.get_file_content_from_master(path)
    assert_file_commit_and_file_searched()
    self.phab.file.download.assert_called_with(phid=file_phid)
    self.assertEqual(file_content, expected_content)
    self.phab.diffusion.filecontentquery.reset_mock()
    self.phab.file.download.reset_mock()
    for i in range(10):
        file_content = self.phab.get_file_content_from_master(path)
        self.assertEqual(file_content, expected_content)
        self.phab.diffusion.filecontentquery.assert_not_called()
        self.phab.file.download.assert_not_called()
    for i in range(10):
        commit_hash = str((int(commit_hash) + 1))
        self.phab.get_latest_master_commit_hash.return_value = commit_hash
        file_content = self.phab.get_file_content_from_master(path)
        self.assertEqual(file_content, expected_content)
        self.phab.diffusion.filecontentquery.assert_not_called()
        self.phab.file.download.assert_not_called()
    configure_browsequery(hash='defghi')
    file_content = self.phab.get_file_content_from_master(path)
    assert_file_commit_and_file_searched()
    self.phab.file.download.assert_called_with(phid=file_phid)
    self.assertEqual(file_content, expected_content)
    self.phab.diffusion.filecontentquery.reset_mock()
    self.phab.file.download.reset_mock()
    for i in range(10):
        file_content = self.phab.get_file_content_from_master(path)
        self.assertEqual(file_content, expected_content)
        self.phab.diffusion.filecontentquery.assert_not_called()
        self.phab.file.download.assert_not_called()