~rootmos/fetch

18666f779b2842d3c173069bd31601eb30c1f67f — Gustav Behm 4 months ago b336ed6
Rework example.json handling
1 files changed, 18 insertions(+), 20 deletions(-)

M tests/test_basic.py
M tests/test_basic.py => tests/test_basic.py +18 -20
@@ 4,45 4,43 @@ import shutil
from . import package_data, fresh
from .common import *

import importlib.resources
example_json_path = package_data("data", "example.json")
with open(example_json_path) as f:
    example_json = json.load(f)

example_url = example_json[0]["url"]
example_sha256 = example_json[0]["sha256"]

class BasicTests(TestCase):
    def test_add(self):
        with self.tempdir("root") as root:
            url = "https://git.sr.ht/~rootmos/fetch/blob/439a01fc257bb5ffdcebf50d1fa95344bd7b4590/fetch"
            sha256 = "88832934c42c9ff0f0066da44bec48b31e45149ce02f6f3bd11b60cfb54af183"
            target = fresh.alphanum(prefix="t")
            stdout = self.run_exe("add", url, target, cwd=root)
            stdout = self.run_exe("add", example_url, target, cwd=root)
            self.assertEqual(stdout.splitlines(), [target.encode("UTF-8")])
            self.assertEqual(fresh.Bytes.read(os.path.join(root, target)).sha256, sha256)
            self.assertSHA256(os.path.join(root, target), sha256)
            self.assertEqual(fresh.Bytes.read(os.path.join(root, target)).sha256, example_sha256)
            self.assertSHA256(os.path.join(root, target), example_sha256)

    def test_add_with_sha256(self):
        with self.tempdir("root") as root:
            url = "https://git.sr.ht/~rootmos/fetch/blob/439a01fc257bb5ffdcebf50d1fa95344bd7b4590/fetch"
            sha256 = "88832934c42c9ff0f0066da44bec48b31e45149ce02f6f3bd11b60cfb54af183"
            target = fresh.alphanum(prefix="t")
            stdout = self.run_exe("add", "--sha256="+sha256, url, target, cwd=root)
            stdout = self.run_exe("add", "--sha256="+example_sha256, example_url, target, cwd=root)
            self.assertEqual(stdout.splitlines(), [target.encode("UTF-8")])
            self.assertEqual(fresh.Bytes.read(os.path.join(root, target)).sha256, sha256)
            self.assertSHA256(os.path.join(root, target), sha256)
            self.assertEqual(fresh.Bytes.read(os.path.join(root, target)).sha256, example_sha256)
            self.assertSHA256(os.path.join(root, target), example_sha256)

    def test_add_with_invalid_sha256(self):
        with self.tempdir("root") as root:
            url = "https://git.sr.ht/~rootmos/fetch/blob/439a01fc257bb5ffdcebf50d1fa95344bd7b4590/fetch"
            p = os.path.join(root, "a")
            fresh.Bytes.fresh().write(p)
            url = f"file://{p}"
            sha256 = fresh.Bytes.zero.sha256
            target = fresh.alphanum(prefix="t")
            self.run_exe("add", "--sha256="+sha256, url, target, cwd=root, returncode=1)
            self.run_exe("add", "--sha256="+sha256, url, "b", cwd=root, returncode=1)

    def test_download(self):
        p = package_data("data", "example.json")
        with open(p) as f:
            j = json.load(f)

        with self.tempdir("root") as root:
            shutil.copy(p, os.path.join(root, ".fetch.json"))
            shutil.copy(example_json_path, os.path.join(root, ".fetch.json"))

            downloaded = self.run_exe("download", cwd=root).splitlines()
            for i in j:
            for i in example_json:
                self.assertIn(i["target"].encode("UTF-8"), downloaded)
                self.assertSHA256(os.path.join(root, i["target"]), i["sha256"])