Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/git/tests/test_from_disk.py
- This file was copied from swh/loader/git/tests/test_loader.py.
# Copyright (C) 2018 The Software Heritage developers | # Copyright (C) 2018 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import os.path | import os.path | ||||
import subprocess | import subprocess | ||||
from swh.loader.git.loader import GitLoader, GitLoaderFromArchive | |||||
from swh.loader.git.from_disk import GitLoaderFromDisk, GitLoaderFromArchive | |||||
from swh.loader.core.tests import BaseLoaderTest | from swh.loader.core.tests import BaseLoaderTest | ||||
from . import TEST_LOADER_CONFIG | from . import TEST_LOADER_CONFIG | ||||
class GitLoaderFromArchive(GitLoaderFromArchive): | class GitLoaderFromArchive(GitLoaderFromArchive): | ||||
def project_name_from_archive(self, archive_path): | def project_name_from_archive(self, archive_path): | ||||
# We don't want the project name to be 'resources'. | # We don't want the project name to be 'resources'. | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | '79f65ac75f79dda6ff03d66e1242702ab67fb51c': | ||||
'5df34ec74d6f69072d9a0a6677d8efbed9b12e60', | '5df34ec74d6f69072d9a0a6677d8efbed9b12e60', | ||||
'b0a77609903f767a2fd3d769904ef9ef68468b87': | 'b0a77609903f767a2fd3d769904ef9ef68468b87': | ||||
'9ca0c7d6ffa3f9f0de59fd7912e08f11308a1338', | '9ca0c7d6ffa3f9f0de59fd7912e08f11308a1338', | ||||
'bd746cd1913721b269b395a56a97baf6755151c2': | 'bd746cd1913721b269b395a56a97baf6755151c2': | ||||
'e1d0d894835f91a0f887a4bc8b16f81feefdfbd5', | 'e1d0d894835f91a0f887a4bc8b16f81feefdfbd5', | ||||
} | } | ||||
class BaseGitLoaderTest(BaseLoaderTest): | class BaseGitLoaderFromDiskTest(BaseLoaderTest): | ||||
def setUp(self, archive_name, uncompress_archive, filename='testrepo'): | def setUp(self, archive_name, uncompress_archive, filename='testrepo'): | ||||
super().setUp(archive_name=archive_name, filename=filename, | super().setUp(archive_name=archive_name, filename=filename, | ||||
prefix_tmp_folder_name='swh.loader.git.', | prefix_tmp_folder_name='swh.loader.git.', | ||||
start_path=os.path.dirname(__file__), | start_path=os.path.dirname(__file__), | ||||
uncompress_archive=uncompress_archive) | uncompress_archive=uncompress_archive) | ||||
class GitLoaderTest(GitLoader): | class GitLoaderFromDiskTest(GitLoaderFromDisk): | ||||
def parse_config_file(self, *args, **kwargs): | def parse_config_file(self, *args, **kwargs): | ||||
return TEST_LOADER_CONFIG | return TEST_LOADER_CONFIG | ||||
class BaseDirGitLoaderTest(BaseGitLoaderTest): | class BaseDirGitLoaderFromDiskTest(BaseGitLoaderFromDiskTest): | ||||
"""Mixin base loader test to prepare the git | """Mixin base loader test to prepare the git | ||||
repository to uncompress, load and test the results. | repository to uncompress, load and test the results. | ||||
This sets up | This sets up | ||||
""" | """ | ||||
def setUp(self): | def setUp(self): | ||||
super().setUp('testrepo.tgz', uncompress_archive=True) | super().setUp('testrepo.tgz', uncompress_archive=True) | ||||
self.loader = GitLoaderTest() | self.loader = GitLoaderFromDiskTest() | ||||
self.storage = self.loader.storage | self.storage = self.loader.storage | ||||
def load(self): | def load(self): | ||||
return self.loader.load( | return self.loader.load( | ||||
origin_url=self.repo_url, | origin_url=self.repo_url, | ||||
visit_date='2016-05-03 15:16:32+00', | visit_date='2016-05-03 15:16:32+00', | ||||
directory=self.destination_path) | directory=self.destination_path) | ||||
class BaseGitLoaderFromArchiveTest(BaseGitLoaderTest): | class BaseGitLoaderFromArchiveTest(BaseGitLoaderFromDiskTest): | ||||
"""Mixin base loader test to prepare the git | """Mixin base loader test to prepare the git | ||||
repository to uncompress, load and test the results. | repository to uncompress, load and test the results. | ||||
This sets up | This sets up | ||||
""" | """ | ||||
def setUp(self): | def setUp(self): | ||||
super().setUp('testrepo.tgz', uncompress_archive=False) | super().setUp('testrepo.tgz', uncompress_archive=False) | ||||
self.loader = GitLoaderFromArchive() | self.loader = GitLoaderFromArchive() | ||||
self.storage = self.loader.storage | self.storage = self.loader.storage | ||||
def load(self): | def load(self): | ||||
return self.loader.load( | return self.loader.load( | ||||
origin_url=self.repo_url, | origin_url=self.repo_url, | ||||
visit_date='2016-05-03 15:16:32+00', | visit_date='2016-05-03 15:16:32+00', | ||||
archive_path=self.destination_path) | archive_path=self.destination_path) | ||||
class GitLoaderTests: | class GitLoaderFromDiskTests: | ||||
"""Common tests for all git loaders.""" | """Common tests for all git loaders.""" | ||||
def test_load(self): | def test_load(self): | ||||
"""Loads a simple repository (made available by `setUp()`), | """Loads a simple repository (made available by `setUp()`), | ||||
and checks everything was added in the storage.""" | and checks everything was added in the storage.""" | ||||
res = self.load() | res = self.load() | ||||
self.assertEqual(res['status'], 'eventful', res) | self.assertEqual(res['status'], 'eventful', res) | ||||
self.assertContentsContain(CONTENT1) | self.assertContentsContain(CONTENT1) | ||||
Show All 15 Lines | def test_load_unchanged(self): | ||||
res = self.load() | res = self.load() | ||||
self.assertEqual(res['status'], 'eventful') | self.assertEqual(res['status'], 'eventful') | ||||
res = self.load() | res = self.load() | ||||
self.assertEqual(res['status'], 'uneventful') | self.assertEqual(res['status'], 'uneventful') | ||||
self.assertCountSnapshots(1) | self.assertCountSnapshots(1) | ||||
class DirGitLoaderTest(BaseDirGitLoaderTest, GitLoaderTests): | class DirGitLoaderTest(BaseDirGitLoaderFromDiskTest, GitLoaderFromDiskTests): | ||||
"""Tests for the GitLoader. Includes the common ones, and | """Tests for the GitLoaderFromDisk. Includes the common ones, and | ||||
add others that only work with a local dir.""" | add others that only work with a local dir.""" | ||||
def _git(self, *cmd): | def _git(self, *cmd): | ||||
"""Small wrapper around subprocess to call Git.""" | """Small wrapper around subprocess to call Git.""" | ||||
try: | try: | ||||
return subprocess.check_output( | return subprocess.check_output( | ||||
['git', '-C', self.destination_path] + list(cmd)) | ['git', '-C', self.destination_path] + list(cmd)) | ||||
except subprocess.CalledProcessError as e: | except subprocess.CalledProcessError as e: | ||||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | def test_load_changed(self): | ||||
# TODO: how to check the snapshot id? | # TODO: how to check the snapshot id? | ||||
# self.assertSnapshotEqual(SNAPSHOT1) | # self.assertSnapshotEqual(SNAPSHOT1) | ||||
self.assertEqual(self.loader.load_status(), {'status': 'eventful'}) | self.assertEqual(self.loader.load_status(), {'status': 'eventful'}) | ||||
self.assertEqual(self.loader.visit_status(), 'full') | self.assertEqual(self.loader.visit_status(), 'full') | ||||
class GitLoaderFromArchiveTest(BaseGitLoaderFromArchiveTest, GitLoaderTests): | class GitLoaderFromArchiveTest(BaseGitLoaderFromArchiveTest, | ||||
GitLoaderFromDiskTests): | |||||
"""Tests for GitLoaderFromArchive. Imports the common ones | """Tests for GitLoaderFromArchive. Imports the common ones | ||||
from GitLoaderTests.""" | from GitLoaderTests.""" | ||||
pass | pass |