aboutsummaryrefslogtreecommitdiff
path: root/scripts/test_nix_cargo_hash.py
blob: c00b1a6c46f7f8f1e1844b9714bb545bd4a36287 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from __future__ import annotations

import unittest

from scripts.nix_cargo_hash import (
    NixCargoHashError,
    check_cargo_hash,
    find_cargo_hash_assignment,
    parse_got_hash,
    replace_cargo_hash_assignment,
)


PACKAGE_TEXT = """
rustPlatform.buildRustPackage {
  pname = "herdr";
  cargoHash = "sha256-oldHash=";
}
"""


class NixCargoHashTests(unittest.TestCase):
    def test_find_cargo_hash_assignment_reads_single_hash(self) -> None:
        self.assertEqual(find_cargo_hash_assignment(PACKAGE_TEXT), '"sha256-oldHash="')

    def test_replace_cargo_hash_assignment_updates_single_hash(self) -> None:
        updated = replace_cargo_hash_assignment(PACKAGE_TEXT, '"sha256-newHash="')

        self.assertIn('cargoHash = "sha256-newHash=";', updated)
        self.assertNotIn("sha256-oldHash", updated)

    def test_replace_cargo_hash_assignment_accepts_fake_hash(self) -> None:
        updated = replace_cargo_hash_assignment(PACKAGE_TEXT, "lib.fakeHash")

        self.assertIn("cargoHash = lib.fakeHash;", updated)

    def test_replace_cargo_hash_assignment_rejects_missing_hash(self) -> None:
        with self.assertRaisesRegex(NixCargoHashError, "found 0"):
            replace_cargo_hash_assignment('pname = "herdr";\n', "lib.fakeHash")

    def test_replace_cargo_hash_assignment_rejects_multiple_hashes(self) -> None:
        text = PACKAGE_TEXT + PACKAGE_TEXT

        with self.assertRaisesRegex(NixCargoHashError, "found 2"):
            replace_cargo_hash_assignment(text, "lib.fakeHash")

    def test_parse_got_hash_reads_nix_hash_mismatch(self) -> None:
        output = """
        specified: sha256-oldHash=
             got: sha256-newHash123+/=
        """

        self.assertEqual(parse_got_hash(output), "sha256-newHash123+/=")

    def test_parse_got_hash_rejects_missing_hash(self) -> None:
        with self.assertRaisesRegex(NixCargoHashError, "found 0"):
            parse_got_hash("error: no hash here")

    def test_parse_got_hash_rejects_multiple_hashes(self) -> None:
        output = "got: sha256-one=\ngot: sha256-two=\n"

        with self.assertRaisesRegex(NixCargoHashError, "found 2"):
            parse_got_hash(output)

    def test_check_cargo_hash_restores_file_on_failure(self) -> None:
        import contextlib
        import io
        import tempfile
        from pathlib import Path
        from unittest import mock

        with tempfile.TemporaryDirectory() as temp_dir:
            package = Path(temp_dir) / "package.nix"
            package.write_text(PACKAGE_TEXT, encoding="utf-8")

            with mock.patch("scripts.nix_cargo_hash.shutil.which", return_value="/nix/bin/nix"):
                with mock.patch("scripts.nix_cargo_hash.run_capture") as run_capture:
                    run_capture.side_effect = [
                        mock.Mock(returncode=0, stdout="x86_64-linux"),
                        mock.Mock(returncode=1, stdout="error without got hash"),
                    ]

                    with contextlib.redirect_stdout(io.StringIO()):
                        with self.assertRaisesRegex(NixCargoHashError, "found 0"):
                            check_cargo_hash(package)

            self.assertIn("--no-link", run_capture.call_args_list[1].args[0])
            self.assertEqual(package.read_text(encoding="utf-8"), PACKAGE_TEXT)


if __name__ == "__main__":
    unittest.main()