Exemplo n.º 1
0
    def test_list_github_source(self):
        """
        Listing a github source should create a `DirectoryListEntry` for each item returned by
        `GitHubFacade.list_directory`.
        """
        file_edit_date = datetime.datetime(2019, 12, 25, 10, 5, 4)
        directory_edit_date = datetime.datetime(2018, 10, 4, 6, 2, 1)

        facade = mock.MagicMock()
        facade.list_directory.return_value = [
            ("file", False, file_edit_date),
            ("directory", True, directory_edit_date),
        ]

        source = GithubSource()
        source.subpath = "subpath"
        source.path = "mapped"

        entries = list(list_linked_source_directory(source, "any/path", facade))

        facade.list_directory.assert_called_with("subpath/any/path")

        self.assertEqual(2, len(entries))

        self.assertIsInstance(entries[0], DirectoryListEntry)
        self.assertEqual(DirectoryEntryType.FILE, entries[0].type)
        self.assertEqual("mapped/any/path/file", entries[0].path)
        self.assertEqual(file_edit_date, entries[0].modification_date)
        self.assertEqual(source, entries[0].source)

        self.assertIsInstance(entries[1], DirectoryListEntry)
        self.assertEqual(DirectoryEntryType.DIRECTORY, entries[1].type)
        self.assertEqual("mapped/any/path/directory", entries[1].path)
        self.assertEqual(directory_edit_date, entries[1].modification_date)
        self.assertEqual(source, entries[1].source)
Exemplo n.º 2
0
def test_to_address():
    s = GithubSource(repo="org/repo", subpath="a/folder")
    a = s.to_address()
    assert a.type_name == "Github"
    assert list(a.keys()) == ["repo", "subpath", "type"]
    assert a.repo == "org/repo"
    assert a.subpath == "a/folder"
Exemplo n.º 3
0
def test_githubsource_parse_address():
    for url in [
        "github://django/django",
        "http://github.com/django/django",
        "https://github.com/django/django/",
    ]:
        sa = GithubSource.parse_address(url)
        assert sa.type == GithubSource
        assert sa["repo"] == "django/django"
        assert sa["subpath"] is None

    for url in [
        "github://django/django/django/db/models",
        "http://github.com/django/django/tree/master/django/db/models",
        "https://github.com/django/django/tree/master/django/db/models",
    ]:
        sa = GithubSource.parse_address(url)
        assert sa.type == GithubSource
        assert sa["repo"] == "django/django"
        assert sa["subpath"] == "django/db/models"

    for url in [
        "github://django/django/django/db/models/query_utils.py",
        "https://github.com/django/django/blob/master/django/db/models/query_utils.py",
    ]:
        sa = GithubSource.parse_address(url)
        assert sa.type == GithubSource
        assert sa["repo"] == "django/django"
        assert sa["subpath"] == "django/db/models/query_utils.py"
Exemplo n.º 4
0
    def test_iterate_github_source_incorrect_directory(self):
        """
        If `iterate_github_source` is called with a source that is not inside the directory passed in, an
        `IncorrectDirectoryException` should be raised.
        """
        source = GithubSource()
        source.path = "some/bad/path"

        with self.assertRaises(IncorrectDirectoryException):
            list(iterate_github_source("a/different/path", source, mock.MagicMock()))
Exemplo n.º 5
0
 def test_determine_linked(self):
     """An entry should be considered a linked source if its source is not FileSource."""
     self.assertEqual(
         DirectoryEntryType.LINKED_SOURCE,
         determine_entry_type(GithubSource(), "long/path"),
     )
     self.assertEqual(
         DirectoryEntryType.LINKED_SOURCE,
         determine_entry_type(GithubSource(), "name"),
     )
Exemplo n.º 6
0
    def test_sources_in_directory_happy_path(self, mock_igs, mock_ghf_init):
        """
        Extensive test using mocks to make sure that:
        a) only sources inside the current directory are processed
        b) `GithubSource`s are handled by github functions
        c) Github authorization tokens are passed in
        d) `FileSource`s are handled by generating a DirectoryEntry
        """
        github_source_1 = GithubSource(path="good/path", repo="user/goodrepo")
        github_source_2 = GithubSource(path="bad/path", repo="user/badrepo")
        github_source_3 = GithubSource(path="good/path/three", repo="user/goodrepo")

        file_source_1 = FileSource(path="good/path/file.py")
        file_source_2 = FileSource(path="bad/path/file2.py")
        file_source_3 = FileSource(path="good/path/three/file2.py")

        sources = [
            github_source_1,
            file_source_1,
            github_source_2,
            file_source_2,
            github_source_3,
            file_source_3,
        ]

        dir_list = list(
            sources_in_directory(
                "good/path", sources, LinkedSourceAuthentication("abc123")
            )
        )

        mock_igs.assert_any_call(
            "good/path", github_source_1, mock_ghf_init.return_value
        )
        mock_igs.assert_any_call(
            "good/path", github_source_2, mock_ghf_init.return_value
        )
        mock_ghf_init.assert_any_call("user/goodrepo", "abc123")
        mock_ghf_init.assert_any_call("user/badrepo", "abc123")

        # Github sources show as directories
        self.assertEqual("ghs_iterator_good_return_good/path", dir_list[0].ghs_id)
        self.assertEqual("path", dir_list[0].name)
        self.assertEqual(DirectoryEntryType.DIRECTORY, dir_list[0].type)

        self.assertEqual("file.py", dir_list[1].name)
        self.assertEqual(DirectoryEntryType.FILE, dir_list[1].type)

        # Directories inside github sources show as directories (although it's kind of nebulous as they could be
        # virtually generated)
        self.assertEqual("three", dir_list[2].name)
        self.assertEqual("ghs_iterator_good_return_good/path/three", dir_list[2].ghs_id)
        self.assertEqual(DirectoryEntryType.DIRECTORY, dir_list[2].type)
Exemplo n.º 7
0
    def test_iterate_github_source_directory(self, mock_llsd):
        """
        If `iterate_github_source` is called with a source inside the current directory, then the relative directory
        should be listed.
        """
        source = GithubSource()
        source.path = "path/in/project"
        facade = mock.MagicMock()

        mock_llsd.return_value = [("directory2", True), ("file2", False)]

        entries = list(iterate_github_source("path/in/project", source, facade))

        mock_llsd.assert_called_with(source, "", facade)

        self.assertEqual(2, len(entries))

        self.assertEqual("directory2", entries[0][0])
        self.assertTrue(entries[0][1])

        self.assertEqual("file2", entries[1][0])
        self.assertFalse(entries[1][1])
Exemplo n.º 8
0
    def test_iterate_github_source_current_directory(self, mock_llsd):
        """
        If `iterate_github_source` is called with a source that matches the current directory, then the root repository
        directory should be listed.
        """
        source = GithubSource()
        source.path = "."
        facade = mock.MagicMock()

        mock_llsd.return_value = [("file", False), ("directory", True)]

        entries = list(iterate_github_source("", source, facade))

        mock_llsd.assert_called_with(source, "", facade)

        self.assertEqual(2, len(entries))

        self.assertEqual("file", entries[0][0])
        self.assertFalse(entries[0][1])

        self.assertEqual("directory", entries[1][0])
        self.assertTrue(entries[1][1])
Exemplo n.º 9
0
 def test_list_github_source_no_facade(self):
     """Listing a github source should fail if no facade is passed in."""
     with self.assertRaises(TypeError):
         # wrap with list otherwise the function won't be called as it is an iterator
         list(list_linked_source_directory(GithubSource(), "any/path"))
Exemplo n.º 10
0
def test_githubsource_url():
    assert GithubSource(repo="user/repo").url == "https://github.com/user/repo"
    assert (
        GithubSource(repo="user/repo", subpath="a/file.txt").url
        == "https://github.com/user/repo/blob/master/a/file.txt"
    )
Exemplo n.º 11
0
def test_githubsource_str():
    assert str(GithubSource(repo="user/repo")) == "github://user/repo"
    assert (
        str(GithubSource(repo="user/repo", subpath="a/file.txt"))
        == "github://user/repo/a/file.txt"
    )
Exemplo n.º 12
0
def test_githubsource_provider_name():
    assert GithubSource().provider_name == "Github"