summaryrefslogtreecommitdiff
path: root/test/test_lockfile.ml
blob: 8d5f92b5fbb887e994198bfc4a553bd2ff94527c (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
(*─────────────────────────────────────────────────────────────────────────────┐
│ SPDX-FileCopyrightText: 2026 toastal <https://toast.al/contact/>             │
│ SPDX-License-Identifier: LGPL-2.1-or-later WITH OCaml-LGPL-linking-exception │
└─────────────────────────────────────────────────────────────────────────────*)
open Alcotest
open Nixtamal

let write_file path content =
	Eio.Path.with_open_out ~create:(`Or_truncate 0o644) path @@ fun flow ->
	Eio.Flow.copy_string content flow

let setup_workdir ~env name =
	let cwd = Eio.Stdenv.cwd env in
	let dir = Eio.Path.(cwd / "_build" / "tests" / name) in
	(
		match Eio.Path.kind ~follow:true dir with
		| `Not_found -> ()
		| _ -> Eio.Path.rmtree dir
	);
	Eio.Path.mkdirs ~perm:0o755 dir;
	Working_directory.set ~directory:dir;
	dir

let write_minimal_manifest ~version dir =
	let content = Fmt.str {|version "%s"
inputs {}
|} version in
	write_file Eio.Path.(dir / Manifest.filename) content

let suite =
	[
		test_case "Lockfile auto-creation from manifest" `Quick (fun () ->
			Eio_main.run @@ fun env ->
			let dir = setup_workdir ~env "lockfile-auto-create" in
			write_minimal_manifest ~version:"0.1.1" dir;
			Lockfile.lockfile := None;
			let res = read_manifest_and_lockfile () in
			check bool "manifest and lock read succeeds" true (Result.is_ok res);
			check bool "lockfile was created in memory" true (Option.is_some !Lockfile.lockfile)
		);
		test_case "Lockfile write/read roundtrip" `Quick (fun () ->
			Eio_main.run @@ fun env ->
			let dir = setup_workdir ~env "lockfile-roundtrip" in
			write_minimal_manifest ~version:"0.1.1" dir;
			let name = Name.Name.make "lockfile-roundtrip-input" in
			let input =
				Input.make
					~name
					~kind:(Input.make_kind_file ~url:(Input.Template.make "https://example.org/archive.tar.gz") ())
					()
			in
			ignore (Input_foreman.add name input);
			let made =
				match Lockfile.make ~version:"0.1.1" () with
				| Ok lock -> lock
				| Error err -> failf "Failed to make lockfile: %s" (Fmt.str "%a" Error.pp_lockfile_error err)
			in
			check bool "write succeeds" true (Result.is_ok (Lockfile.write ()));
			let reread =
				match Lockfile.read () with
				| Ok (Some lock) -> lock
				| Ok None -> fail "Expected lockfile on disk"
				| Error err -> failf "Failed to read lockfile: %s" err
			in
			check
				(testable Lockfile.pp Lockfile.equal)
				"serialized lockfile roundtrips"
				made
				reread
		);
	]