def test_third_level_of_import_name(requests_mock, path_finder_mock,
                                    sys_modules_mock, repo_mock, open_mock):
    sys_modules = {}

    def getitem(name):
        return sys_modules[name]

    def setitem(name, val):
        sys_modules[name] = val

    sys_modules_mock.__getitem__.side_effect = getitem
    sys_modules_mock.__setitem__.side_effect = setitem
    importer = GithubMetaPathFinder()

    mocked_response = Mock()
    mocked_response.status_code = 200
    requests_mock.return_value = mocked_response

    importer.load_module('packyou.github.github_username.test_repo')
    assert 'packyou/github/github_username/test_repo/__init__.py' in open_mock.mock_calls[
        0][1][0]
    assert repo_mock.mock_calls[0][1][
        0] == 'https://github.com/github_username/test_repo.git'
    assert 'packyou/github/github_username/test_repo' in repo_mock.mock_calls[
        0][1][1]
    assert path_finder_mock.mock_calls[0][1] == (
        'packyou.github.github_username.test_repo', )
    assert requests_mock.mock_calls[0][1][
        0] == 'https://github.com/github_username/test_repo.git'
示例#2
0
def test_sys_path_populate_only_add_root_path(mock_isdir, mocked_listdir):
    installed_projects = [
        'reverse_shell', 'sqlmapproject', '__pycache__', '__init__.py',
        'karpathy'
    ]
    mock_isdir.return_value = True
    mocked_listdir.return_value = installed_projects
    importer = GithubMetaPathFinder()

    importer.update_sys_path()
    for installed_project in installed_projects:
        assert installed_project in sys.path
def test_repo_does_not_exists(repo_mock, requests_mock, path_finder_mock,
                              open_mock):
    urls = []

    def get_side_effect(url):
        urls.append(url)
        mocked_response = Mock()
        mocked_response.status_code = 404
        return mocked_response

    requests_mock.get = get_side_effect

    importer = GithubMetaPathFinder()
    with raises(ImportError):
        importer.load_module('packyou.github.github_username.test_repo')
示例#4
0
def test_find_module():
    importer = GithubFinder()
    with raises(ImportError):
        importer.find_module('pepe.pepe', 'path')

    importer = GithubMetaPathFinder()
    module_loader = importer.find_module('pepe.pepe', 'path')
    assert module_loader == importer
    assert module_loader.path == 'path'
def test_second_level_of_import_name(path_finder_mock, sys_modules_mock,
                                     mkdir_mock, open_mock):
    sys_modules = {}

    def getitem(name):
        return sys_modules[name]

    def setitem(name, val):
        sys_modules[name] = val

    sys_modules_mock.__getitem__.side_effect = getitem
    sys_modules_mock.__setitem__.side_effect = setitem
    importer = GithubMetaPathFinder()
    importer.load_module('packyou.github.github_username')

    assert 'packyou/github/github_username/__init__.py' in open_mock.mock_calls[
        0][1][0]
    assert 'packyou/github/github_username' in mkdir_mock.mock_calls[0][1][0]
    assert path_finder_mock.mock_calls[0][1] == (
        'packyou.github.github_username', )
def test_check_repo_exists_username_with_dash(repo_mock, requests_mock,
                                              path_finder_mock, open_mock):
    urls = []

    def get_side_effect(url):
        urls.append(url)
        mocked_response = Mock()
        mocked_response.status_code = 404
        if 'github-username' in url:
            mocked_response.status_code = 200
        return mocked_response

    requests_mock.get = get_side_effect

    importer = GithubMetaPathFinder()
    importer.load_module('packyou.github.github_username.test_repo')
    assert set(urls) == set([
        'https://github.com/github_username/test_repo.git',
        'https://github.com/github-username/test_repo.git'
    ])
示例#7
0
def test_second_level_of_import_name(sys_modules_mock, mkdir_mock, exists_mock,
                                     isdir_mock):
    sys_modules = {}

    def getitem(name):
        return sys_modules[name]

    def setitem(name, val):
        sys_modules[name] = val

    sys_modules_mock.__getitem__.side_effect = getitem
    sys_modules_mock.__setitem__.side_effect = setitem
    importer = GithubMetaPathFinder()
    importer.load_module('packyou.github.github_username')

    exists_mock.return_value = False
    isdir_mock.return_value = True

    importer = GithubLoader()

    fake_file = StringIO()
    fake_file.write('print("ok!")')
    fake_file.seek(0)

    fake_file_2 = StringIO()
    fake_file_2.write('print("ok!")')
    fake_file_2.seek(0)

    with patch('packyou.utils.open', return_value=fake_file,
               create=True) as open_utils_mock:
        with patch('packyou.open', return_value=fake_file_2,
                   create=True) as open_mock:
            importer.load_module('packyou.github.github_username')
            assert 'packyou/github/github_username/__init__.py' in open_mock.mock_calls[
                0][1][0]
    assert 'packyou/github/github_username' in mkdir_mock.mock_calls[0][1][0]
def test_first_level_of_import_name(requests_mock, path_finder_mock,
                                    sys_modules_mock):
    sys_modules = {}

    def getitem(name):
        return sys_modules[name]

    def setitem(name, val):
        sys_modules[name] = val

    sys_modules_mock.__getitem__.side_effect = getitem
    sys_modules_mock.__setitem__.side_effect = setitem
    importer = GithubMetaPathFinder()

    mocked_response = Mock()
    mocked_response.status_code = 200
    requests_mock.return_value = mocked_response
    assert 'packyou.github' not in sys_modules
    importer.load_module('packyou.github')
    assert 'packyou.github' in sys_modules
    # check cached module in sys.modules
    importer.load_module('packyou.github')
    assert path_finder_mock.mock_calls[0][1] == ('packyou.github', )
def test_valid_imports_can_be_imported():
    importer = GithubMetaPathFinder()
    module = importer.load_module('os.path')
    'split' in dir(module)
def test_invalid_import():
    importer = GithubMetaPathFinder()
    with raises(ImportError):
        importer.load_module('os.mkdir')