summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/dune2
-rw-r--r--test/test_fossil.ml76
-rw-r--r--test/test_lockfile.ml71
-rw-r--r--test/test_main.ml3
-rw-r--r--test/test_upgrade.ml113
5 files changed, 265 insertions, 0 deletions
diff --git a/test/dune b/test/dune
index 563fb44..d934086 100644
--- a/test/dune
+++ b/test/dune
@@ -1,4 +1,6 @@
(test
(name test_main)
(flags (:standard -alert -deprecated))
+ (instrumentation
+ (backend bisect_ppx))
(libraries nixtamal alcotest qcheck qcheck-alcotest))
diff --git a/test/test_fossil.ml b/test/test_fossil.ml
new file mode 100644
index 0000000..c4bb56f
--- /dev/null
+++ b/test/test_fossil.ml
@@ -0,0 +1,76 @@
+(*─────────────────────────────────────────────────────────────────────────────┐
+│ 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 suite =
+ let t = Input.Template.make in
+ [
+ test_case "Manifest Fossil reference check-in from KDL" `Quick (fun () ->
+ let reference =
+ let kdl =
+ {|check-in "abc123"|}
+ |> KDL.of_string
+ |> Result.get_ok
+ in
+ match Manifest.Fossil.Reference.codec.of_kdl kdl with
+ | Ok ref -> ref
+ | Error err -> failwith Fmt.(str "%a" (list ~sep: semi KDL.Valid.pp_err) err)
+ in
+ check
+ (testable Input.Fossil.Reference.pp Input.Fossil.Reference.equal)
+ "check-in is decoded as Fossil reference"
+ (`Checkin "abc123")
+ reference
+ );
+ test_case "Manifest Fossil input roundtrip" `Quick (fun () ->
+ let fossil =
+ Manifest.Fossil.make
+ ~repository:(t "https://example.org/src.fossil")
+ ~reference:(`Branch "trunk")
+ in
+ let roundtrip =
+ fossil
+ |> Manifest.Fossil.codec.to_kdl
+ |> Manifest.Fossil.codec.of_kdl
+ |> Result.get_ok
+ in
+ check
+ (testable Manifest.Fossil.pp Manifest.Fossil.equal)
+ "Fossil KDL codec is shape-preserving"
+ fossil
+ roundtrip
+ );
+ test_case "Lockfile Fossil kind roundtrip" `Quick (fun () ->
+ let input_kind =
+ Input.make_kind_fossil
+ ~repository:(t "https://example.org/src.fossil")
+ ~reference:(`Tag "v1.0")
+ ~date:"2026-01-01T00:00:00Z"
+ ~latest_checkin:"abc123"
+ ()
+ in
+ let models = Input.jg_models2 @@ Input.make ~name:(Name.Name.make "fossil") ~kind:input_kind () in
+ let lock_kind = Lockfile.Kind.to_lock ~models input_kind in
+ let decoded =
+ lock_kind
+ |> Jsont.Json.encode Lockfile.Kind.jsont
+ |> Result.get_ok
+ |> Jsont.Json.decode Lockfile.Kind.jsont
+ |> Result.get_ok
+ in
+ check
+ (testable Lockfile.Kind.pp Lockfile.Kind.equal)
+ "Fossil lockfile kind JSON codec is shape-preserving"
+ lock_kind
+ decoded;
+ match lock_kind with
+ | `Fossil fossil_lock ->
+ check int "Fossil mirrors are always empty" 0 (List.length fossil_lock.mirrors);
+ check (option string) "Fossil lock datetime" (Some "2026-01-01T00:00:00Z") fossil_lock.datetime;
+ check (option string) "Fossil lock latest_checkin" (Some "abc123") fossil_lock.latest_checkin
+ | _ -> fail "Expected Fossil lock kind"
+ );
+ ]
diff --git a/test/test_lockfile.ml b/test/test_lockfile.ml
new file mode 100644
index 0000000..8d5f92b
--- /dev/null
+++ b/test/test_lockfile.ml
@@ -0,0 +1,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
+ );
+ ]
diff --git a/test/test_main.ml b/test/test_main.ml
index 27e0209..3daefce 100644
--- a/test/test_main.ml
+++ b/test/test_main.ml
@@ -5,4 +5,7 @@
let () =
Alcotest.run "Nixtamal" [
"Input", Test_input.suite;
+ "Upgrade", Test_upgrade.suite;
+ "Fossil", Test_fossil.suite;
+ "Lockfile", Test_lockfile.suite;
]
diff --git a/test/test_upgrade.ml b/test/test_upgrade.ml
new file mode 100644
index 0000000..c10c647
--- /dev/null
+++ b/test/test_upgrade.ml
@@ -0,0 +1,113 @@
+(*─────────────────────────────────────────────────────────────────────────────┐
+│ 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 read_file path =
+ Eio.Path.with_open_in path @@ fun flow ->
+ let buf = Eio.Buf_read.of_flow flow ~max_size: max_int in
+ Eio.Buf_read.take_all buf
+
+let has_substring haystack needle =
+ try
+ ignore (Str.search_forward (Str.regexp_string needle) haystack 0);
+ true
+ with Not_found -> false
+
+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 write_minimal_lockfile ~version dir =
+ let content = Fmt.str {|{"v":"%s","i":{}}|} version in
+ write_file Eio.Path.(dir / Lockfile.filename) content
+
+let suite =
+ [
+ test_case "Upgrade backup path naming" `Quick (fun () ->
+ check string "backup extension" "manifest.kdl.bak" (backup_path "manifest.kdl")
+ );
+ test_case "Upgrade dry-run keeps files untouched" `Quick (fun () ->
+ Eio_main.run @@ fun env ->
+ let dir = setup_workdir ~env "upgrade-dry-run" in
+ write_minimal_manifest ~version:"0.1.1" dir;
+ write_minimal_lockfile ~version:"0.1.1" dir;
+ Lockfile.lockfile := None;
+ let res = upgrade ~to_:Schema.Version.V0_2_0 ~dry_run:true () in
+ check bool "dry-run succeeds" true (Result.is_ok res);
+ let manifest_content = read_file Eio.Path.(dir / Manifest.filename) in
+ check bool "manifest stays old version" true (has_substring manifest_content "0.1.1");
+ check bool "manifest backup not created" false (Eio.Path.is_file Eio.Path.(dir / backup_path Manifest.filename));
+ check bool "lock backup not created" false (Eio.Path.is_file Eio.Path.(dir / backup_path Lockfile.filename))
+ );
+ test_case "Upgrade rewrites versions and cleans backups" `Quick (fun () ->
+ Eio_main.run @@ fun env ->
+ let dir = setup_workdir ~env "upgrade-success" in
+ write_minimal_manifest ~version:"0.1.1" dir;
+ write_minimal_lockfile ~version:"0.1.1" dir;
+ Lockfile.lockfile := None;
+ let res = upgrade ~to_:Schema.Version.V0_2_0 () in
+ check bool "upgrade succeeds" true (Result.is_ok res);
+ let manifest_content = read_file Eio.Path.(dir / Manifest.filename) in
+ check bool "manifest version upgraded" true (has_substring manifest_content "0.2.0");
+ let lock_version =
+ match Lockfile.read () with
+ | Ok (Some lock) -> lock.version
+ | Ok None -> fail "Lockfile missing after upgrade"
+ | Error err -> failf "Failed to read lockfile: %s" err
+ in
+ check string "lockfile version upgraded" "0.2.0" lock_version;
+ check bool "manifest backup cleaned" false (Eio.Path.is_file Eio.Path.(dir / backup_path Manifest.filename))
+ );
+ test_case "Upgrade aborts on version mismatch" `Quick (fun () ->
+ Eio_main.run @@ fun env ->
+ let dir = setup_workdir ~env "upgrade-version-mismatch" in
+ write_minimal_manifest ~version:"0.1.1" dir;
+ write_minimal_lockfile ~version:"0.1.1" dir;
+ Lockfile.lockfile := None;
+ let res = upgrade ~from:Schema.Version.V0_2_0 ~to_:Schema.Version.V0_2_0 () in
+ check bool "upgrade fails" true (Result.is_error res);
+ let lock_version =
+ match Lockfile.read () with
+ | Ok (Some lock) -> lock.version
+ | _ -> fail "Lockfile should still be present after abort"
+ in
+ check string "lockfile unchanged" "0.1.1" lock_version;
+ check bool "manifest backup not created" false (Eio.Path.is_file Eio.Path.(dir / backup_path Manifest.filename));
+ check bool "lockfile backup not created" false (Eio.Path.is_file Eio.Path.(dir / backup_path Lockfile.filename))
+ );
+ test_case "Upgrade detects unknown manifest version" `Quick (fun () ->
+ Eio_main.run @@ fun env ->
+ let dir = setup_workdir ~env "upgrade-version-detect" in
+ write_minimal_manifest ~version:"9.9.9" dir;
+ write_minimal_lockfile ~version:"0.1.1" dir;
+ Lockfile.lockfile := None;
+ let res = upgrade () in
+ check bool
+ "unknown schema version is rejected"
+ true
+ (match res with
+ | Error (`Upgrade msg) -> String.length msg > 0
+ | _ -> false)
+ );
+ ]