1import os
2import shutil
3
4from tests.download.gitremote import GitRemote
5
6import infra
7
8
9class GitTestBase(infra.basetest.BRConfigTest):
10    config = \
11        """
12        BR2_BACKUP_SITE=""
13        """
14    gitremotedir = infra.filepath("tests/download/git-remote")
15    gitremote = None
16
17    def setUp(self):
18        super(GitTestBase, self).setUp()
19        self.gitremote = GitRemote(self.builddir, self.gitremotedir, self.logtofile)
20
21    def tearDown(self):
22        self.show_msg("Cleaning up")
23        if self.gitremote:
24            self.gitremote.stop()
25        if self.b and not self.keepbuilds:
26            self.b.delete()
27
28    def check_hash(self, package):
29        # store downloaded tarball inside the output dir so the test infra
30        # cleans it up at the end
31        env = {"BR2_DL_DIR": os.path.join(self.builddir, "dl"),
32               "GITREMOTE_PORT_NUMBER": str(self.gitremote.port)}
33        self.b.build(["{}-dirclean".format(package),
34                      "{}-source".format(package)],
35                     env)
36
37    def check_download(self, package):
38        # store downloaded tarball inside the output dir so the test infra
39        # cleans it up at the end
40        dl_dir = os.path.join(self.builddir, "dl")
41        # enforce we test the download
42        if os.path.exists(dl_dir):
43            shutil.rmtree(dl_dir)
44        env = {"BR2_DL_DIR": dl_dir,
45               "GITREMOTE_PORT_NUMBER": str(self.gitremote.port)}
46        self.b.build(["{}-dirclean".format(package),
47                      "{}-legal-info".format(package)],
48                     env)
49
50
51class TestGitHash(GitTestBase):
52    br2_external = [infra.filepath("tests/download/br2-external/git-hash")]
53
54    def test_run(self):
55        with self.assertRaises(SystemError):
56            self.check_hash("bad")
57        self.check_hash("good")
58        self.check_hash("nohash")
59        self.check_hash("export-subst")
60        with open(os.path.join(self.builddir, "dl", "export-subst", "git", "file2"), "r") as f:
61            blob = f.read()
62        self.assertEqual(blob, "0fdb95cf4f3c5ed4003287649cabb33c5f843e26\n")
63
64
65class TestGitRefs(GitTestBase):
66    br2_external = [infra.filepath("tests/download/br2-external/git-refs")]
67
68    def test_run(self):
69        with self.assertRaises(SystemError):
70            self.check_download("git-wrong-content")
71        with self.assertRaises(SystemError):
72            self.check_download("git-wrong-sha1")
73        self.check_download("git-partial-sha1-branch-head")
74        self.check_download("git-partial-sha1-reachable-by-branch")
75        self.check_download("git-partial-sha1-reachable-by-tag")
76        self.check_download("git-partial-sha1-tag-itself")
77        self.check_download("git-partial-sha1-tag-points-to")
78        self.check_download("git-sha1-branch-head")
79        self.check_download("git-sha1-reachable-by-branch")
80        self.check_download("git-sha1-reachable-by-tag")
81        self.check_download("git-sha1-tag-itself")
82        self.check_download("git-sha1-tag-points-to")
83        self.check_download("git-submodule-disabled")
84        self.check_download("git-submodule-enabled")
85        self.check_download("git-tag")
86