Ejemplo n.º 1
0
def test_gather_plugin_files_from_dist():
    repository = dummy_repository_plugin()
    repository.content.path.remote = "dist"
    repository.data.file_name = "test.js"
    repository.tree = [
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "test.js", "type": "blob"}, "test/test", "master"
        ),
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "dist/image.png", "type": "blob"}, "test/test", "master"
        ),
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "dist/test.js", "type": "blob"}, "test/test", "master"
        ),
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "dist/subdir", "type": "tree"}, "test/test", "master"
        ),
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "dist/subdir/file.file", "type": "blob"}, "test/test", "master"
        ),
    ]
    files = [x.path for x in gather_files_to_download(repository)]
    assert "test.js" not in files
    assert "dist/image.png" in files
    assert "dist/subdir/file.file" in files
    assert "dist/subdir" not in files
    assert "dist/test.js" in files
Ejemplo n.º 2
0
def test_gather_plugin_multiple_files_in_root():
    repository = dummy_repository_plugin()
    repository.content.path.remote = ""
    repository.data.file_name = "test.js"
    repository.tree = [
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "test.js", "type": "blob"}, "test/test", "master"
        ),
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "dep1.js", "type": "blob"}, "test/test", "master"
        ),
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "dep2.js", "type": "blob"}, "test/test", "master"
        ),
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "dep3.js", "type": "blob"}, "test/test", "master"
        ),
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "info.md", "type": "blob"}, "test/test", "master"
        ),
    ]
    files = [x.path for x in gather_files_to_download(repository)]
    assert "test.js" in files
    assert "dep1.js" in files
    assert "dep2.js" in files
    assert "dep3.js" in files
    assert "info.md" not in files
Ejemplo n.º 3
0
def test_gather_plugin_files_from_root():
    repository = dummy_repository_plugin()
    repository.content.path.remote = ""
    repository.tree = [
        AIOGitHubAPIRepositoryTreeContent({
            "path": "test.js",
            "type": "blob"
        }, "test/test", "master"),
        AIOGitHubAPIRepositoryTreeContent({
            "path": "dir",
            "type": "tree"
        }, "test/test", "master"),
        AIOGitHubAPIRepositoryTreeContent({
            "path": "aaaa.js",
            "type": "blob"
        }, "test/test", "master"),
        AIOGitHubAPIRepositoryTreeContent(
            {
                "path": "dist/test.js",
                "type": "blob"
            }, "test/test", "master"),
    ]
    find_file_name(repository)
    files = [x.path for x in gather_files_to_download(repository)]
    assert "test.js" in files
    assert "dir" not in files
    assert "aaaa.js" in files
    assert "dist/test.js" not in files
Ejemplo n.º 4
0
def test_gather_netdaemon_files_base():
    repository = dummy_repository_netdaemon()
    repository.tree = [
        AIOGitHubAPIRepositoryTreeContent({
            "path": "test.cs",
            "type": "blob"
        }, "test/test", "master"),
        AIOGitHubAPIRepositoryTreeContent(
            {
                "path": "apps/test/test.cs",
                "type": "blob"
            }, "test/test", "master"),
        AIOGitHubAPIRepositoryTreeContent(
            {
                "path": "apps/test/test.yaml",
                "type": "blob"
            }, "test/test", "master"),
        AIOGitHubAPIRepositoryTreeContent(
            {
                "path": ".github/file.file",
                "type": "blob"
            }, "test/test", "master"),
    ]
    files = [x.path for x in gather_files_to_download(repository)]
    assert ".github/file.file" not in files
    assert "test.cs" not in files
    assert "apps/test/test.cs" in files
    assert "apps/test/test.yaml" in files
Ejemplo n.º 5
0
def test_single_file_repo():
    repository = dummy_repository_base()
    repository.content.single = True
    repository.data.file_name = "test.file"
    repository.tree = [
        AIOGitHubAPIRepositoryTreeContent({
            "path": "test.file",
            "type": "blob"
        }, "test/test", "master"),
        AIOGitHubAPIRepositoryTreeContent({
            "path": "dir",
            "type": "tree"
        }, "test/test", "master"),
        AIOGitHubAPIRepositoryTreeContent({
            "path": "test.yaml",
            "type": "blob"
        }, "test/test", "master"),
        AIOGitHubAPIRepositoryTreeContent({
            "path": "readme.md",
            "type": "blob"
        }, "test/test", "master"),
    ]
    files = [x.path for x in gather_files_to_download(repository)]
    assert "readme.md" not in files
    assert "test.yaml" not in files
    assert "test.file" in files
Ejemplo n.º 6
0
def test_valid_objects():
    tree = [
        AIOGitHubAPIRepositoryTreeContent(
            {
                "path": "test/file.file",
                "type": "blob"
            }, "test/test", "master"),
        AIOGitHubAPIRepositoryTreeContent(
            {
                "path": "test/newfile.file",
                "type": "blob"
            }, "test/test", "master"),
        AIOGitHubAPIRepositoryTreeContent(
            {
                "path": "test/file.png",
                "type": "blob"
            }, "test/test", "master"),
    ]
    files = [
        x.filename for x in filter_content_return_one_of_type(
            tree, "test", "file", "full_path")
    ]
    assert "file.file" in files
    assert "newfile.file" not in files
    assert "file.png" in files
def test_valid_objects():
    tree = [
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "test/path/file.file", "type": "blob"}, "test/test", "master"
        ),
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "test/path/sub", "type": "blob"}, "test/test", "master"
        ),
    ]
    assert find_first_of_filetype(tree, "file", "filename") == "file.file"
def test_not_valid():
    tree = [
        AIOGitHubAPIRepositoryTreeContent(
            {"path": ".github/path/file.yaml", "type": "blob"}, "test/test", "master"
        ),
        AIOGitHubAPIRepositoryTreeContent(
            {"path": ".github/path/file.js", "type": "blob"}, "test/test", "master"
        ),
    ]
    assert not find_first_of_filetype(tree, "file", "filename")
Ejemplo n.º 9
0
async def test_download_content(aresponses, tmp_path, event_loop):
    aresponses.add(
        "raw.githubusercontent.com",
        "/test/test/master/test/path/file.file",
        "get",
        aresponses.Response(body="test", headers=response_rate_limit_header),
    )

    repository = dummy_repository_base()
    repository.content.path.remote = ""
    repository.content.path.local = tmp_path
    repository.tree = [
        AIOGitHubAPIRepositoryTreeContent(
            {
                "path": "test/path/file.file",
                "type": "blob"
            }, "test/test", "master")
    ]
    async with aiohttp.ClientSession(loop=event_loop) as session:
        hacs = get_hacs()
        hacs.hass.loop = event_loop
        hacs.session = session
        await download_content(repository)
        assert os.path.exists(
            f"{repository.content.path.local}/test/path/file.file")
Ejemplo n.º 10
0
def test_gather_plugin_different_card_name():
    repository = dummy_repository_plugin()
    repository.content.path.remote = ""
    repository.data.file_name = "card.js"
    repository.tree = [
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "card.js", "type": "blob"}, "test/test", "master"
        ),
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "info.md", "type": "blob"}, "test/test", "master"
        ),
    ]
    find_file_name(repository)
    files = [x.path for x in gather_files_to_download(repository)]
    assert "card.js" in files
    assert "info.md" not in files
Ejemplo n.º 11
0
def test_valid():
    tree = [
        AIOGitHubAPIRepositoryTreeContent({
            "path": "test",
            "type": "tree"
        }, "test/test", "master"),
        AIOGitHubAPIRepositoryTreeContent({
            "path": "test/path",
            "type": "tree"
        }, "test/test", "master"),
        AIOGitHubAPIRepositoryTreeContent(
            {
                "path": "test/path/sub",
                "type": "tree"
            }, "test/test", "master"),
    ]
    assert get_first_directory_in_directory(tree, "test") == "path"
Ejemplo n.º 12
0
def test_not_valid():
    tree = [
        AIOGitHubAPIRepositoryTreeContent(
            {
                "path": ".github/path/file.file",
                "type": "tree"
            }, "test/test", "master")
    ]
    assert get_first_directory_in_directory(tree, "test") is None
Ejemplo n.º 13
0
def test_gather_content_in_root_theme():
    repository = dummy_repository_theme()
    repository.data.content_in_root = True
    repository.content.path.remote = ""
    repository.data.file_name = "test.yaml"
    repository.tree = [
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "test.yaml", "type": "blob"}, "test/test", "master"
        ),
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "dir", "type": "tree"}, "test/test", "master"
        ),
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "test2.yaml", "type": "blob"}, "test/test", "master"
        ),
    ]
    files = [x.path for x in gather_files_to_download(repository)]
    assert "test2.yaml" not in files
    assert "test.yaml" in files
Ejemplo n.º 14
0
def test_gather_files_to_download():
    repository = dummy_repository_base()
    repository.content.path.remote = ""
    repository.tree = [
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "test/path/file.file", "type": "blob"}, "test/test", "master"
        )
    ]
    files = [x.path for x in gather_files_to_download(repository)]
    assert "test/path/file.file" in files
Ejemplo n.º 15
0
async def test_download_content_integration(aresponses, tmp_path, event_loop):
    aresponses.add(
        "raw.githubusercontent.com",
        aresponses.ANY,
        "get",
        aresponses.Response(body="", headers=response_rate_limit_header),
    )
    aresponses.add(
        "raw.githubusercontent.com",
        aresponses.ANY,
        "get",
        aresponses.Response(body="", headers=response_rate_limit_header),
    )
    aresponses.add(
        "raw.githubusercontent.com",
        aresponses.ANY,
        "get",
        aresponses.Response(body="", headers=response_rate_limit_header),
    )
    aresponses.add(
        "raw.githubusercontent.com",
        aresponses.ANY,
        "get",
        aresponses.Response(body="", headers=response_rate_limit_header),
    )
    hacs = get_hacs()
    hacs.system.config_path = tmp_path
    repository = dummy_repository_integration()
    repository.data.domain = "test"
    repository.content.path.local = repository.localpath
    repository.content.path.remote = "custom_components/test"
    integration_files = [
        "__init__.py",
        "sensor.py",
        ".translations/en.json",
        "manifest.json",
    ]
    for integration_file in integration_files:
        repository.tree.append(
            AIOGitHubAPIRepositoryTreeContent(
                {
                    "path": f"custom_components/test/{integration_file}",
                    "type": "blob"
                },
                "test/test",
                "master",
            ))

    async with aiohttp.ClientSession(loop=event_loop) as session:
        hacs.hass.loop = event_loop
        hacs.session = session
        await download_content(repository)
        for path in repository.tree:
            assert os.path.exists(
                f"{hacs.system.config_path}/{path.full_path}")
Ejemplo n.º 16
0
def test_find_file_name_base():
    repository = dummy_repository_plugin()
    repository.tree = [
        AIOGitHubAPIRepositoryTreeContent({
            "path": "test.js",
            "type": "blob"
        }, "test/test", "master")
    ]
    find_file_name(repository)
    assert repository.data.file_name == "test.js"
    assert repository.content.path.remote == ""
async def test_has_info_md_file():
    repository = dummy_repository_base()
    repository.tree = [
        AIOGitHubAPIRepositoryTreeContent({
            "path": "info.md",
            "type": "file"
        }, "test/test", "master")
    ]
    check = RepositoryInformationFile(repository)
    await check._async_run_check()
    assert not check.failed
Ejemplo n.º 18
0
async def test_hacs_manifest_with_manifest():
    repository = dummy_repository_base()
    repository.tree = [
        AIOGitHubAPIRepositoryTreeContent({
            "path": "hacs.json",
            "type": "file"
        }, "test/test", "master")
    ]
    check = HacsManifest(repository)
    await check._async_run_check()
    assert not check.failed
Ejemplo n.º 19
0
def test_find_file_name_base_python_script():
    repository = dummy_repository_python_script()
    repository.tree = [
        AIOGitHubAPIRepositoryTreeContent(
            {
                "path": "python_scripts/test.py",
                "type": "blob"
            }, "test/test", "master")
    ]
    find_file_name(repository)
    assert repository.data.file_name == "test.py"
    assert repository.data.name == "test"
Ejemplo n.º 20
0
def test_find_file_name_base_theme():
    repository = dummy_repository_theme()
    repository.tree = [
        AIOGitHubAPIRepositoryTreeContent(
            {
                "path": "themes/test.yaml",
                "type": "blob"
            }, "test/test", "master")
    ]
    find_file_name(repository)
    assert repository.data.file_name == "test.yaml"
    assert repository.data.name == "test"
async def test_has_readme_file():
    repository = dummy_repository_base()
    repository.data.render_readme = True
    repository.tree = [
        AIOGitHubAPIRepositoryTreeContent({
            "path": "readme",
            "type": "file"
        }, "test/test", "master")
    ]
    check = RepositoryInformationFile(repository)
    await check._async_run_check()
    assert not check.failed
Ejemplo n.º 22
0
def test_gather_appdaemon_files_with_subdir():
    repository = dummy_repository_appdaemon()
    repository.data.file_name = "test.py"
    repository.tree = [
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "test.py", "type": "blob"}, "test/test", "master"
        ),
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "apps/test/test.py", "type": "blob"}, "test/test", "master"
        ),
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "apps/test/core/test.py", "type": "blob"}, "test/test", "master"
        ),
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "apps/test/devices/test.py", "type": "blob"}, "test/test", "master"
        ),
        AIOGitHubAPIRepositoryTreeContent(
            {"path": "apps/test/test/test.py", "type": "blob"}, "test/test", "master"
        ),
        AIOGitHubAPIRepositoryTreeContent(
            {"path": ".github/file.file", "type": "blob"}, "test/test", "master"
        ),
    ]
    files = [x.path for x in gather_files_to_download(repository)]
    assert ".github/file.file" not in files
    assert "test.py" not in files
    assert "apps/test/test.py" in files
    assert "apps/test/devices/test.py" in files
    assert "apps/test/test/test.py" in files
    assert "apps/test/core/test.py" in files
Ejemplo n.º 23
0
    async def get_tree(
        self,
        ref: str
        or None = None) -> ["AIOGitHubAPIRepositoryTreeContent"] or list:
        """Retrun a list of repository tree objects."""
        if ref is None:
            raise AIOGitHubAPIException("Missing ref")
        _endpoint = f"/repos/{self.full_name}/git/trees/{ref}"
        _params = {"recursive": "1"}

        response = await self.client.get(endpoint=_endpoint, params=_params)

        return [
            AIOGitHubAPIRepositoryTreeContent(x, self.full_name, ref)
            for x in response.get("tree", [])
        ]
async def test_get_integration_manifest_missing_required_key(aresponses, event_loop):
    aresponses.add(
        "api.github.com",
        "/rate_limit",
        "get",
        aresponses.Response(body=b"{}", headers=response_rate_limit_header),
    )
    aresponses.add(
        "api.github.com",
        "/repos/test/test",
        "get",
        aresponses.Response(
            body=json.dumps(repository_data), headers=response_rate_limit_header
        ),
    )
    aresponses.add(
        "api.github.com",
        "/rate_limit",
        "get",
        aresponses.Response(body=b"{}", headers=response_rate_limit_header),
    )
    del integration_manifest["domain"]
    content = base64.b64encode(json.dumps(integration_manifest).encode("utf-8"))
    aresponses.add(
        "api.github.com",
        "/repos/test/test/contents/custom_components/test/manifest.json",
        "get",
        aresponses.Response(
            body=json.dumps({"content": content.decode("utf-8")}),
            headers=response_rate_limit_header,
        ),
    )

    async with aiohttp.ClientSession(loop=event_loop) as session:
        repository = dummy_repository_integration()
        repository.repository_object = await get_repository(session, TOKEN, "test/test")
        repository.content.path.remote = "custom_components/test"
        repository.tree = [
            AIOGitHubAPIRepositoryTreeContent(
                {"path": "custom_components/test/manifest.json", "type": "blob"},
                "test/test",
                "master",
            )
        ]
        with pytest.raises(HacsException):
            await get_integration_manifest(repository)
Ejemplo n.º 25
0
def test_find_file_release_no_asset():
    repository = dummy_repository_plugin()
    repository.releases.objects = [
        AIOGitHubAPIRepositoryRelease({
            "tag_name": "3",
            "assets": []
        })
    ]
    repository.tree = [
        AIOGitHubAPIRepositoryTreeContent({
            "path": "test.js",
            "type": "blob"
        }, "test/test", "master")
    ]
    find_file_name(repository)
    assert repository.data.file_name == "test.js"
    assert repository.content.path.remote == ""
async def test_get_integration_manifest_format_issue(aresponses, event_loop):
    aresponses.add(
        "api.github.com",
        "/rate_limit",
        "get",
        aresponses.Response(body=b"{}", headers=response_rate_limit_header),
    )
    aresponses.add(
        "api.github.com",
        "/repos/test/test",
        "get",
        aresponses.Response(
            body=json.dumps(repository_data), headers=response_rate_limit_header
        ),
    )
    aresponses.add(
        "api.github.com",
        "/rate_limit",
        "get",
        aresponses.Response(body=b"{}", headers=response_rate_limit_header),
    )
    aresponses.add(
        "api.github.com",
        "/repos/test/test/contents/custom_components/test/manifest.json",
        "get",
        aresponses.Response(
            body=json.dumps({"content": {"wrong": "format"}}),
            headers=response_rate_limit_header,
        ),
    )

    async with aiohttp.ClientSession(loop=event_loop) as session:
        repository = dummy_repository_integration()
        repository.repository_object = await get_repository(session, TOKEN, "test/test")
        repository.content.path.remote = "custom_components/test"
        repository.tree = [
            AIOGitHubAPIRepositoryTreeContent(
                {"path": "custom_components/test/manifest.json", "type": "blob"},
                "test/test",
                "master",
            )
        ]
        with pytest.raises(HacsException):
            await get_integration_manifest(repository)