summaryrefslogtreecommitdiffstatshomepage
path: root/tests/test_crate_fetchurl.py
blob: 50363b3269693df6564b244917c6234823747d63 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import json
import os
import subprocess
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch

ROOT = Path(__file__).resolve().parents[1]


class CrateFetchTests(unittest.TestCase):
    def test_checkout_path_with_special_characters(self):
        attrs = {"url": "https://example.org/source.tar.gz", "hash": "unchanged"}
        with tempfile.TemporaryDirectory() as directory:
            checkout = Path(directory) / 'rui@canonical.com space " ${name}'
            checkout.mkdir()
            (checkout / "nix").symlink_to(ROOT / "nix", target_is_directory=True)
            with patch(f"{__name__}.ROOT", checkout):
                self.assertEqual(self.evaluate(attrs), attrs)

    def evaluate(self, attrs):
        expression = (
            'let fetch = import (builtins.toPath (builtins.getEnv "CRATE_TEST_SOURCE")) (args: args); '
            'in fetch (builtins.fromJSON (builtins.getEnv "CRATE_TEST_ATTRS"))'
        )
        return json.loads(
            subprocess.check_output(
                ["nix", "eval", "--impure", "--json", "--expr", expression],
                text=True,
                env={
                    **os.environ,
                    "CRATE_TEST_SOURCE": str(ROOT / "nix/fetch-crate.nix"),
                    "CRATE_TEST_ATTRS": json.dumps(attrs),
                },
            )
        )

    def test_crate_uses_static_archive_and_keeps_hash(self):
        attrs = {
            "name": "download-hashbrown-0.16.1",
            "url": "https://crates.io/api/v1/crates/hashbrown/0.16.1/download",
            "sha256": "locked-checksum",
        }
        self.assertEqual(
            self.evaluate(attrs),
            {
                **attrs,
                "url": "https://static.crates.io/crates/hashbrown/hashbrown-0.16.1.crate",
            },
        )

    def test_other_downloads_are_unchanged(self):
        attrs = {"url": "https://example.org/source.tar.gz", "hash": "unchanged"}
        self.assertEqual(self.evaluate(attrs), attrs)

    def test_url_lists_are_unchanged(self):
        attrs = {"urls": ["https://example.org/source.tar.gz"], "hash": "unchanged"}
        self.assertEqual(self.evaluate(attrs), attrs)