Compare commits

..

1 commit

Author SHA1 Message Date
Bryan Bennett
5e505b178b
WIP 2024-01-05 06:25:13 -05:00
23 changed files with 418 additions and 649 deletions

2
.gitignore vendored
View file

@ -1,2 +0,0 @@
_build/
_coverage/

View file

@ -1,2 +0,0 @@
version=0.26.1
profile=default

View file

@ -1,10 +0,0 @@
build:
@dune build
test:
@dune test -f
@bisect-ppx-report html
@bisect-ppx-report summary --per-file
fmt:
@dune build @fmt --auto-promote

View file

@ -40,13 +40,6 @@ watch_file **/*.nix
use flake_env . use flake_env .
``` ```
## Developing
This repo uses [just](https://just.systems/) to manage simple build jobs.
After activating the development environment in the repo, run `just -l` to list tasks.
Please remember to format code before issuing patches.
## Credits ## Credits
This takes huge inspiration (and literal code-chunks) from nix-direnv. This takes huge inspiration (and literal code-chunks) from nix-direnv.

View file

@ -1,14 +1,5 @@
(executable (executable
(name flake_env) (name flake_env)
(public_name flake_env) (public_name flake_env)
(libraries (libraries core core_unix core_unix.filename_unix core_unix.sys_unix ppx_yojson_conv re sha lib)
core (preprocess (pps ppx_yojson_conv ppx_jane)))
core_unix
core_unix.filename_unix
core_unix.sys_unix
ppx_yojson_conv
re
sha
lib)
(preprocess
(pps ppx_yojson_conv ppx_jane)))

View file

@ -6,52 +6,50 @@ open Lib;
let main = () => { let main = () => {
let argv = Sys.get_argv(); let argv = Sys.get_argv();
switch (Util.get_args(argv)) { switch (Util.get_args(argv)) {
| Ok((layout_directory, flake_specifier, other_args)) => | Ok((layout_directory, flake_specifier, other_args)) => {
switch (preflight(layout_directory)) { switch (preflight(layout_directory)) {
| Ok () => | Ok() => {
switch (Lib.Watches.get()) { switch (Lib.Watches.get()) {
| Ok(watches) => | Ok(watches) => {
let paths = Array.map(~f=watch => watch.path, watches); let paths = Array.map(~f=watch => watch.path, watches);
let hash = Util.hash_files(paths); let hash = Util.hash_files(paths);
let profile = layout_directory ++ "/flake-profile-" ++ hash; let profile = layout_directory ++ "/flake-profile-" ++ hash;
let profile_rc = profile ++ ".rc"; let profile_rc = profile ++ ".rc";
switch (Sys_unix.is_file(profile_rc), Sys_unix.is_file(profile)) { switch ((Sys_unix.is_file(profile_rc), Sys_unix.is_file(profile))) {
| (`Yes, `Yes) => | (`Yes, `Yes) => {
let profile_rc_mtime = Unix.stat(profile_rc).st_mtime; let profile_rc_mtime = Unix.stat(profile_rc).st_mtime;
let all_older = let all_older = Array.map(
Array.map(~f=watch => watch.modtime, watches) ~f=watch => watch.modtime, watches)
|> Array.for_all(~f=watch_mtime => |> Array.for_all(
watch_mtime <= int_of_float(profile_rc_mtime) ~f=watch_mtime => watch_mtime <= int_of_float(profile_rc_mtime)
); )
if (all_older) { if (all_older) {
print_cur_cache(profile_rc); print_cur_cache(profile_rc)
} else { } else {
freshen_cache(
layout_directory,
hash,
flake_specifier,
other_args,
);
};
| _ =>
freshen_cache(layout_directory, hash, flake_specifier, other_args) freshen_cache(layout_directory, hash, flake_specifier, other_args)
}
}
| _ => freshen_cache(layout_directory, hash, flake_specifier, other_args)
}
}; };
| Error(e) => | Error(e) => {
Printf.eprintf("%s\n", e); Printf.eprintf("%s\n", e);
exit(1); exit(1);
} }
| Error(e) => }
}
| Error(e) => {
Printf.eprintf("%s\n", e); Printf.eprintf("%s\n", e);
exit(1); exit(1);
} }
| Error () =>
Printf.eprintf(
"%s <layout_directory> <flake specifier> <...args>\n",
argv[0],
);
exit(1);
}; };
}
| Error() => {
Printf.eprintf("%s <layout_directory> <flake specifier> <...args>\n", argv[0]);
exit(1);
}
}
}; };
let () = main(); let () = main();

View file

@ -2,6 +2,7 @@
, core , core
, core_unix , core_unix
, findlib , findlib
, fmt
, lib , lib
, nix-filter , nix-filter
, ocaml , ocaml
@ -41,6 +42,7 @@ buildDunePackage {
core core
core_unix core_unix
findlib findlib
fmt
ocaml ocaml
ppx_yojson_conv ppx_yojson_conv
ppx_yojson_conv_lib ppx_yojson_conv_lib

View file

@ -1,11 +1,18 @@
(lang dune 3.12) (lang dune 3.11)
(name flake_env) (name flake_env)
(generate_opam_files true) (generate_opam_files true)
(source (sourcehut bryan_bennett/flake_env))
(source
(sourcehut bryan_bennett/flake_env))
(authors "Bryan Bennett") (authors "Bryan Bennett")
(maintainers "Bryan Bennett") (maintainers "Bryan Bennett")
(license MIT) (license MIT)
(documentation https://git.sr.ht/~bryan_bennett/flake_env) (documentation https://git.sr.ht/~bryan_bennett/flake_env)
(package (package

View file

@ -1,2 +0,0 @@
(lang dune 3.12)
(instrument_with bisect_ppx)

View file

@ -29,7 +29,6 @@
devShells.default = pkgs.mkShell { devShells.default = pkgs.mkShell {
inputsFrom = [ self'.packages.default ]; inputsFrom = [ self'.packages.default ];
packages = [ packages = [
pkgs.just
pkgs.ocamlPackages.alcotest pkgs.ocamlPackages.alcotest
pkgs.ocamlPackages.bisect_ppx pkgs.ocamlPackages.bisect_ppx
pkgs.ocamlPackages.dune_3 pkgs.ocamlPackages.dune_3

View file

@ -10,7 +10,7 @@ doc: "https://git.sr.ht/~bryan_bennett/flake_env"
bug-reports: "https://todo.sr.ht/~bryan_bennett/flake_env" bug-reports: "https://todo.sr.ht/~bryan_bennett/flake_env"
depends: [ depends: [
"ocaml" "ocaml"
"dune" {>= "3.12"} "dune" {>= "3.11"}
"odoc" {with-doc} "odoc" {with-doc}
] ]
build: [ build: [

View file

@ -0,0 +1,81 @@
open Core;
module Unix = Core_unix;
module Util = Flake_env__util;
type t = {
major: int,
minor: int,
point: int,
};
let init = (major, minor, point) => {
{major, minor, point}
};
let required_direnv_version = init(2, 21, 3);
let required_nix_version = init(2, 10, 0);
let semver_re = Re.compile(Re.Posix.re({|([0-9]+)\.([0-9]+)\.([0-9]+)|}));
let compare = (a, b) => {
switch (a, b) {
| (a, b) when a.major == b.major && a.minor == b.minor && a.point == b.point => 0
| (a, b) when a.major < b.major => -1
| (a, b) when a.major == b.major && a.minor < b.minor => -1
| (a, b) when a.major == b.major && a.minor == b.minor && a.point < b.point => -1
| _ => 1
}
}
let extract_version_number = (cmd) => {
switch (Util.run_process(cmd, ["--version"])) {
| (Ok(), stdout) when String.length(stdout) > 0 => {
let substrings = Re.exec(semver_re, stdout);
let groups = Re.Group.all(substrings);
if ((groups |> Array.length) == 4) {
Ok({
major: groups[1] |> int_of_string,
minor: groups[2] |> int_of_string,
point: groups[3] |> int_of_string
})
} else {
Error(Printf.sprintf("Stdout did not contain a version number for `%s --version`", cmd))
}
}
| _ => Error(Printf.sprintf("Failed executing '%s'\n", cmd))
}
};
let is_new_enough = (cur, needed) => {
switch (cur) {
| Ok(cur) => {
switch (compare(cur, needed)) {
| x when x < 0 => Ok(false)
| _ => Ok(true)
}
}
| Error(e) => Error(e)
}
};
let in_direnv = () => switch (Sys.getenv("direnv")) {
| Some(_) => true
| None => false
};
let preflight_versions = () => {
let is_nix_new_enough = is_new_enough(extract_version_number("nix"), required_nix_version);
let is_direnv_new_enough = is_new_enough(extract_version_number("direnv"), required_direnv_version);
switch (in_direnv(), is_direnv_new_enough, is_nix_new_enough) {
| (false, _, _) => Error("Not in direnv!")
| (_, Ok(false), _) => Error("Direnv version is not new enough")
| (_, _, Ok(false)) => Error("Nix version is not new enough")
| (_, Error(e), _) => Error(e)
| (_, _, Error(e)) => Error(e)
| (true, Ok(true), Ok(true)) => Ok()
}
};

View file

@ -6,10 +6,9 @@
core_unix core_unix
core_unix.filename_unix core_unix.filename_unix
core_unix.sys_unix core_unix.sys_unix
fmt
ppx_yojson_conv ppx_yojson_conv
re re
sha) sha)
(instrumentation (instrumentation (backend bisect_ppx))
(backend bisect_ppx)) (preprocess (pps ppx_yojson_conv ppx_jane ppx_inline_test)))
(preprocess
(pps ppx_yojson_conv ppx_jane)))

View file

@ -3,62 +3,53 @@ module Unix = Core_unix;
let run_process = (name, args) => { let run_process = (name, args) => {
/*** Run a process [name] with args [args], returning (exit_code, stdout text) */ /*** Run a process [name] with args [args], returning (exit_code, stdout text) */
let stdout_chan = let stdout_chan = Unix.open_process_in(name ++ " " ++ (args |> String.concat(~sep=" ")));
Unix.open_process_in(name ++ " " ++ (args |> String.concat(~sep=" ")));
let stdout_content = stdout_chan |> In_channel.input_all; let stdout_content = stdout_chan |> In_channel.input_all;
let exit_code = Unix.close_process_in(stdout_chan); let exit_code = Unix.close_process_in(stdout_chan);
(exit_code, stdout_content); (exit_code, stdout_content)
}; };
let nix = args => let nix = (args) => run_process("nix", ["--extra-experimental-features", "\"nix-command flakes\" ", ...args]);
run_process(
"nix",
["--extra-experimental-features", "\"nix-command flakes\" ", ...args],
);
let hash_files = filenames => { let hash_files = (filenames) => {
/*** Hash all entries in [filenames], returning a hex-encoded string of the hash of their contents */ /*** Hash all entries in [filenames], returning a hex-encoded string of the hash of their contents */
let ctx = Sha1.init(); let ctx = Sha1.init();
let () = let () = filenames
filenames |> Array.filter(~f=f => switch (Sys_unix.file_exists(f)) {
|> Array.filter(~f=f =>
switch (Sys_unix.file_exists(f)) {
| `Yes => true | `Yes => true
| _ => false | _ => false
} })
) |> Array.iter(~f=(f) => {
|> Array.iter(~f=f => { f |> In_channel.create |> In_channel.input_all |> Sha1.update_string(ctx);
f
|> In_channel.create
|> In_channel.input_all
|> Sha1.update_string(ctx)
}); });
Sha1.finalize(ctx) |> Sha1.to_hex; Sha1.finalize(ctx) |> Sha1.to_hex
}; };
let rec rmrf = path => { let rec rmrf = (path) => {
switch (Unix.lstat(path).st_kind) { switch (Unix.lstat(path).st_kind) {
| exception (Unix.Unix_error(_, _, _)) => () | exception Unix.Unix_error(_, _, _) => ()
| S_REG | S_REG => Unix.unlink(path)
| S_LNK => Unix.unlink(path) | S_LNK => Unix.unlink(path)
| S_DIR => | S_DIR => {
Sys_unix.readdir(path) Sys_unix.readdir(path) |> Array.iter(~f=name => rmrf(Filename.concat(path, name)));
|> Array.iter(~f=name => rmrf(Filename.concat(path, name))); Unix.rmdir(path)
Unix.rmdir(path); }
| _ => | S_CHR => Printf.eprintf("Don't know how to handle Character Device file\n")
Printf.eprintf( | S_BLK => Printf.eprintf("Don't know how to handle Block Device file\n")
"Unsupported file type (Chr or Block device, FIFO, or Socket)\n", | S_FIFO => Printf.eprintf("Don't know how to handle FIFO file\n")
) | S_SOCK => Printf.eprintf("Don't know how to handle Socket file\n")
}; }
}; };
let get_args = argv => { let get_args = (argv) => {
switch (Array.length(argv)) { switch (Array.length(argv)) {
| x when x >= 3 => | x when x >= 3 => {
let layout_directory = argv[1]; let layout_directory = argv[1];
let flake_specifier = argv[2]; let flake_specifier = argv[2];
let other_args = snd(List.split_n(List.of_array(argv), 3)); let other_args = snd(List.split_n(List.of_array(argv), 3));
Ok((layout_directory, flake_specifier, other_args)); Ok((layout_directory, flake_specifier, other_args))
}
| _ => Error() | _ => Error()
}
}; };
};

View file

@ -3,13 +3,16 @@ module Unix = Core_unix;
module Util = Flake_env__util; module Util = Flake_env__util;
type t = { type t = {
major: int, major: int,
minor: int, minor: int,
point: int, point: int,
}; };
let init = (major, minor, point) => {major, minor, point}; let init = (major, minor, point) => {
{major, minor, point}
};
let required_direnv_version = init(2, 21, 3); let required_direnv_version = init(2, 21, 3);
@ -17,61 +20,57 @@ let required_nix_version = init(2, 10, 0);
let semver_re = Re.compile(Re.Posix.re({|([0-9]+)\.([0-9]+)\.([0-9]+)|})); let semver_re = Re.compile(Re.Posix.re({|([0-9]+)\.([0-9]+)\.([0-9]+)|}));
let pprint = (pp_fmt, version) => Fmt.pf(pp_fmt, "{ major: %d, minor: %d, point: %d }", version.major, version.minor, version.point);
let compare = (a, b) => { let compare = (a, b) => {
switch (a, b) { switch (a, b) {
| (a, b) when a.major == b.major && a.minor == b.minor && a.point == b.point => 0 | (a, b) when a.major == b.major && a.minor == b.minor && a.point == b.point => 0
| (a, b) when a.major < b.major => (-1) | (a, b) when a.major < b.major => -1
| (a, b) when a.major == b.major && a.minor < b.minor => (-1) | (a, b) when a.major == b.major && a.minor < b.minor => -1
| (a, b) when a.major == b.major && a.minor == b.minor && a.point < b.point => (-1) | (a, b) when a.major == b.major && a.minor == b.minor && a.point < b.point => -1
| _ => 1 | _ => 1
}; }
}; }
let extract_version_number = cmd => { let extract_version_number = (cmd) => {
switch (Util.run_process(cmd, ["--version"])) { switch (Util.run_process(cmd, ["--version"])) {
| (Ok (), stdout) when String.length(stdout) > 0 => | (Ok(), stdout) when String.length(stdout) > 0 => {
switch (Re.exec(semver_re, stdout)) { let substrings = Re.exec(semver_re, stdout);
| exception Stdlib.Not_found =>
Error(
Printf.sprintf(
"Stdout did not contain a version number for `%s --version`",
cmd,
),
)
| substrings =>
let groups = Re.Group.all(substrings); let groups = Re.Group.all(substrings);
if ((groups |> Array.length) == 4) {
Ok({ Ok({
major: groups[1] |> int_of_string, major: groups[1] |> int_of_string,
minor: groups[2] |> int_of_string, minor: groups[2] |> int_of_string,
point: groups[3] |> int_of_string, point: groups[3] |> int_of_string
}); })
} else {
Error(Printf.sprintf("Stdout did not contain a version number for `%s --version`", cmd))
}
}
| _ => Error(Printf.sprintf("Failed executing '%s'\n", cmd))
} }
| _ => Error(Printf.sprintf("Failed executing '%s'", cmd))
};
}; };
let is_new_enough = (cur, needed) => { let is_new_enough = (cur, needed) => {
switch (cur) { switch (cur) {
| Ok(cur) => | Ok(cur) => {
switch (compare(cur, needed)) { switch (compare(cur, needed)) {
| x when x < 0 => Ok(false) | x when x < 0 => Ok(false)
| _ => Ok(true) | _ => Ok(true)
} }
}
| Error(e) => Error(e) | Error(e) => Error(e)
}; }
}; };
let in_direnv = () => let in_direnv = () => switch (Sys.getenv("direnv")) {
switch (Sys.getenv("direnv")) {
| Some(_) => true | Some(_) => true
| None => false | None => false
}; };
let preflight_versions = () => { let preflight_versions = () => {
let is_nix_new_enough = let is_nix_new_enough = is_new_enough(extract_version_number("nix"), required_nix_version);
is_new_enough(extract_version_number("nix"), required_nix_version); let is_direnv_new_enough = is_new_enough(extract_version_number("direnv"), required_direnv_version);
let is_direnv_new_enough =
is_new_enough(extract_version_number("direnv"), required_direnv_version);
switch (in_direnv(), is_direnv_new_enough, is_nix_new_enough) { switch (in_direnv(), is_direnv_new_enough, is_nix_new_enough) {
| (false, _, _) => Error("Not in direnv!") | (false, _, _) => Error("Not in direnv!")
@ -80,5 +79,5 @@ let preflight_versions = () => {
| (_, Error(e), _) => Error(e) | (_, Error(e), _) => Error(e)
| (_, _, Error(e)) => Error(e) | (_, _, Error(e)) => Error(e)
| (true, Ok(true), Ok(true)) => Ok() | (true, Ok(true), Ok(true)) => Ok()
}; }
}; };

View file

@ -9,52 +9,40 @@ module Util = Flake_env__util;
type watch = { type watch = {
exists: bool, exists: bool,
modtime: int, modtime: int,
path: string, path: string
}; };
[@deriving yojson] [@deriving yojson]
type watches = array(watch); type watches = array<watch>;
let get = () => { let get = () => {
let direnv_watch_str = let direnv_watch_str = Sys.getenv("DIRENV_WATCHES") |> Option.value_exn(~message="Environment missing DIRENV_WATCHES");
Sys.getenv("DIRENV_WATCHES") let proc_info = Unix.create_process(~prog="direnv", ~args=["show_dump", direnv_watch_str]);
|> Option.value_exn(~message="Environment missing DIRENV_WATCHES");
let proc_info =
Unix.create_process(
~prog="direnv",
~args=["show_dump", direnv_watch_str],
);
let sub_stdout = Unix.in_channel_of_descr(proc_info.stdout); let sub_stdout = Unix.in_channel_of_descr(proc_info.stdout);
switch (Unix.waitpid(proc_info.pid)) { switch (Unix.waitpid(proc_info.pid)) {
| Ok() => Ok(watches_of_yojson(Yojson.Safe.from_channel(sub_stdout))) | Ok() => Ok(watches_of_yojson(Yojson.Safe.from_channel(sub_stdout)))
| _ => Error("Failed to parse watches") | _ => Error("Failed to parse watches")
}; }
}; };
let get_path = doc => let get_path = (doc) => String.drop_prefix(doc |> member("path") |> to_string, 11);
String.drop_prefix(doc |> member("path") |> to_string, 11);
let rec get_paths_from_doc = (doc, paths) => { let rec get_paths_from_doc = (doc, paths) => {
let p = get_path(doc); let p = get_path(doc);
let sub_paths = let sub_paths = List.concat(
List.concat( doc |> member("inputs")
doc
|> member("inputs")
|> to_assoc |> to_assoc
|> List.map(~f=((_k, v)) => get_paths_from_doc(v, paths)), |> List.map(~f=((_k, v)) => get_paths_from_doc(v, paths)));
); List.concat([[p], sub_paths])
List.concat([[p], sub_paths]);
}; };
let get_input_paths = () => { let get_input_paths = () => {
switch (Util.nix(["flake", "archive", "--json", "--no-write-lock-file"])) { switch (Util.nix(["flake", "archive", "--json", "--no-write-lock-file"])) {
| (Ok (), output) => | (Ok(), output) => get_paths_from_doc(Yojson.Safe.from_string(output), [])
get_paths_from_doc(Yojson.Safe.from_string(output), []) | (Error(_), _) => {
| (Error(_), _) => Printf.eprintf("Failed to parse output of `nix flake archive --json`. Ignorning flake inputs. \n");
Printf.eprintf( []
"Failed to parse output of `nix flake archive --json`. Ignorning flake inputs. \n", }
); }
[];
};
}; };

View file

@ -5,11 +5,11 @@ module Util = Flake_env__util;
module Watches = Flake_env__watches; module Watches = Flake_env__watches;
module Versions = Flake_env__versions; module Versions = Flake_env__versions;
let print_cur_cache = profile_rc => { let print_cur_cache = (profile_rc) => {
In_channel.read_all(profile_rc) |> Printf.printf("%s"); In_channel.read_all(profile_rc) |> Printf.printf("%s")
}; };
let clean_old_gcroots = layout_dir => { let clean_old_gcroots = (layout_dir) => {
Util.rmrf(layout_dir ++ "/flake-inputs/"); Util.rmrf(layout_dir ++ "/flake-inputs/");
Util.rmrf(layout_dir); Util.rmrf(layout_dir);
Unix.mkdir_p(layout_dir ++ "/flake-inputs/"); Unix.mkdir_p(layout_dir ++ "/flake-inputs/");
@ -19,78 +19,55 @@ let add_gcroot = (store_path, symlink) => {
switch (Util.nix(["build", "--out-link", symlink, store_path])) { switch (Util.nix(["build", "--out-link", symlink, store_path])) {
| (Ok(), _) => Ok() | (Ok(), _) => Ok()
| (err, _) => err | (err, _) => err
}; }
}; };
let freshen_cache = (layout_dir, hash, flake_specifier, other_args) => { let freshen_cache = (layout_dir, hash, flake_specifier, other_args) => {
clean_old_gcroots(layout_dir); clean_old_gcroots(layout_dir);
let tmp_profile = let tmp_profile = layout_dir ++ "flake-tmp-profile." ++ Core.Pid.to_string(Core_unix.getpid());
layout_dir
++ "flake-tmp-profile."
++ Core.Pid.to_string(Core_unix.getpid());
let pde_args = [ let pde_args = ["print-dev-env", "--profile", tmp_profile, flake_specifier, ...other_args];
"print-dev-env",
"--profile",
tmp_profile,
flake_specifier,
...other_args,
];
let (exit_code, stdout_content) = Util.nix(pde_args); let (exit_code, stdout_content) = Util.nix(pde_args);
let profile = layout_dir ++ "/flake-profile-" ++ hash; let profile = layout_dir ++ "/flake-profile-" ++ hash;
let profile_rc = profile ++ ".rc"; let profile_rc = profile ++ ".rc";
switch (exit_code) { switch (exit_code) {
| Ok () => | Ok() => {
Out_channel.with_file( Out_channel.with_file(~f=f=> Out_channel.output_string(f, stdout_content), profile_rc);
~f=f => Out_channel.output_string(f, stdout_content),
profile_rc,
);
switch (add_gcroot(tmp_profile, profile)) { switch (add_gcroot(tmp_profile, profile)) {
| Ok () => | Ok() => {
Sys_unix.remove(tmp_profile); Sys_unix.remove(tmp_profile);
let flake_input_cache_path = layout_dir ++ "/flake-inputs/"; let flake_input_cache_path = layout_dir ++ "/flake-inputs/"
let flake_inputs = Watches.get_input_paths(); let flake_inputs = Watches.get_input_paths();
flake_inputs flake_inputs |> List.iter(~f=(inpt) => {
|> List.iter(~f=inpt => { switch (add_gcroot("/nix/store/" ++ inpt, flake_input_cache_path ++ inpt)) {
switch (
add_gcroot("/nix/store/" ++ inpt, flake_input_cache_path ++ inpt)
) {
| Ok() => () | Ok() => ()
| err => | err => Printf.eprintf("Failed creating flake-input gcroot: %s\n", Core_unix.Exit_or_signal.to_string_hum(err));
Printf.eprintf( };
"Failed creating flake-input gcroot: %s\n",
Core_unix.Exit_or_signal.to_string_hum(err),
)
}
}); });
print_cur_cache(profile_rc); print_cur_cache(profile_rc);
| err => }
Printf.eprintf( | err => {
"Failed creating gcroot: %s\n", Printf.eprintf("Failed creating gcroot: %s\n", Core_unix.Exit_or_signal.to_string_hum(err));
Core_unix.Exit_or_signal.to_string_hum(err),
);
exit(1); exit(1);
}
}; };
| err => }
Printf.eprintf( | err => {
"Failed evaluating flake: %s\n", Printf.eprintf("Failed evaluating flake: %s\n", Core_unix.Exit_or_signal.to_string_hum(err));
Core_unix.Exit_or_signal.to_string_hum(err),
);
exit(1); exit(1);
}
}; };
}; };
let preflight = layout_directory => { let preflight = (layout_directory) => {
switch ( switch (Versions.preflight_versions(), Sys_unix.is_directory(layout_directory)) {
Versions.preflight_versions(),
Sys_unix.is_directory(layout_directory),
) {
| (Ok(_), `Yes) => Ok() | (Ok(_), `Yes) => Ok()
| (Ok(_), _) => | (Ok(_), _) => {
Unix.mkdir_p(layout_directory); Unix.mkdir_p(layout_directory);
Ok(); Ok()
}
| (err, _) => err | (err, _) => err
}; }
}; }

View file

@ -1,10 +1,7 @@
(tests (tests
(names flake_env_test_versions flake_env_test_watches flake_env_test_util) (names
(deps spit_version.sh spit_gibberish.sh) flake_env_test_versions
(libraries flake_env_test_watches
lib flake_env_test_util)
alcotest (instrumentation (backend bisect_ppx))
core (libraries lib alcotest))
core_unix
core_unix.sys_unix
core_unix.filename_unix))

View file

@ -3,165 +3,37 @@ module Unix = Core_unix;
open Lib.Util; open Lib.Util;
let _pp_exit_or_signal = (pp_fmt, e) => let pp_exit_or_signal = (pp_fmt) => (e) => Fmt.pf(pp_fmt, "%s", Unix.Exit_or_signal.to_string_hum(e));
Fmt.pf(pp_fmt, "%s", Unix.Exit_or_signal.to_string_hum(e)); let exit_or_signal_eq = (a, b) => Unix.Exit_or_signal.compare(a, b) == 0;
let _exit_or_signal_eq = (a, b) => Unix.Exit_or_signal.compare(a, b) == 0; let testable_exit_or_signal = Alcotest.testable(pp_exit_or_signal, exit_or_signal_eq);
let testable_exit_or_signal =
Alcotest.testable(_pp_exit_or_signal, _exit_or_signal_eq);
let _syst_to_bool =
fun
| `Yes => true
| _ => false;
let check_exit_or_signal =
Alcotest.(check(Alcotest.pair(testable_exit_or_signal, string)));
let check_string = Alcotest.(check(string));
let check_bool = Alcotest.(check(bool));
let check_get_args =
Alcotest.(
check(
Alcotest.result(Alcotest.triple(string, string, list(string)), unit),
)
);
let test_run_process_success = () => let test_run_process_success = () =>
check_exit_or_signal( Alcotest.(check(Alcotest.pair(testable_exit_or_signal, string)))(
"Returns expected", "Returns expected",
(Ok(), ""), (Ok(), ""),
run_process("true", []), run_process("true", []));
);
let test_run_process_failure = () => let test_run_process_failure = () =>
check_exit_or_signal( Alcotest.(check(Alcotest.pair(testable_exit_or_signal, string)))(
"Returns expected", "Returns expected",
(Error(`Exit_non_zero(1)), ""), (Error(`Exit_non_zero(1)), ""),
run_process("false", []), run_process("false", []));
);
let test_run_process_stdout = () => let test_run_process_stdout = () =>
check_exit_or_signal( Alcotest.(check(Alcotest.pair(testable_exit_or_signal, string)))(
"Returns expected", "Returns expected",
(Ok(), "echoed\n"), (Ok(), "echoed\n"),
run_process("echo", ["echoed"]), run_process("echo", ["echoed"]));
);
let test_hash_one = () => {
check_string(
"Hash matches",
hash_files([|"../LICENSE"|]),
"b43cf2e824eb66ba0e8f939c08072a8e307b5e5f",
);
};
let test_hash_multiple = () => {
check_string(
"Hash matches",
hash_files([|"../LICENSE", "../LICENSE"|]),
"08304d8baeed02722f81252952b00f6ac011ce0c",
);
};
let test_hash_filters_nonexistent = () => {
check_string(
"Hash matches",
hash_files([|"../LICENSE", "FOOBARBAZ"|]),
"b43cf2e824eb66ba0e8f939c08072a8e307b5e5f",
);
};
let test_rmrf_file = () => {
let tmp_file_name = Filename_unix.temp_file("test", "txt");
rmrf(tmp_file_name);
check_bool(
"File removed",
false,
_syst_to_bool(Sys_unix.is_file(tmp_file_name)),
);
};
let test_rmrf_dir = () => {
let temp_dir_name = Filename_unix.temp_dir("test", "d");
let _ = Filename_unix.temp_file(~in_dir=temp_dir_name, "test", "txt");
rmrf(temp_dir_name);
check_bool(
"File removed",
false,
_syst_to_bool(Sys_unix.file_exists(temp_dir_name)),
);
};
let test_get_args_simple = () => {
check_get_args(
"Parses successfully",
Ok(("foo", "bar", ["oof", "rab", "zab"])),
get_args([|"000", "foo", "bar", "oof", "rab", "zab"|]),
);
};
let test_get_args_just_enough = () => {
check_get_args(
"Parses just enough args",
Ok(("foo", "bar", [])),
get_args([|"000", "foo", "bar"|]),
);
};
let test_get_args_error = () => {
check_get_args(
"Errors on too few args",
Error(),
get_args([|"000", "111"|]),
);
};
let () = let () =
Alcotest.( Alcotest.(
run( run(
"Watches", "Watches",
[ [("run_process",
(
"run_process",
[ [
test_case("Capture's Stdout", `Quick, test_run_process_stdout), test_case("Capture's Stdout", `Quick, test_run_process_stdout),
test_case("Success", `Quick, test_run_process_success), test_case("Success", `Quick, test_run_process_success),
test_case("Failure", `Quick, test_run_process_failure), test_case("Failure", `Quick, test_run_process_failure),
], ]),
), ]),
(
"hash_files",
[
test_case("Hashes one file", `Quick, test_hash_one),
test_case("Hashes multiple files", `Quick, test_hash_multiple),
test_case(
"Filters non-existent",
`Quick,
test_hash_filters_nonexistent,
),
],
),
(
"rmrf helper",
[
test_case("Removes file", `Quick, test_rmrf_file),
test_case("Removes dir", `Quick, test_rmrf_dir),
],
),
(
"get_args",
[
test_case("Parses Args", `Quick, test_get_args_simple),
test_case(
"Parses just enough args",
`Quick,
test_get_args_just_enough,
),
test_case("Handles too few args", `Quick, test_get_args_error),
],
),
],
)
); );

View file

@ -1,129 +1,86 @@
open Lib; open Lib;
let pprint = (pp_fmt, version) => {
Versions.(
Fmt.pf(
pp_fmt,
"{ major: %d, minor: %d, point: %d }",
version.major,
version.minor,
version.point,
)
);
};
let testable_version =
Alcotest.testable(pprint, (a, b) => Versions.compare(a, b) == 0);
let check_version =
Alcotest.(check(Alcotest.result(testable_version, string)));
let test_compare_equal = () => { let test_compare_equal = () => {
let a = Versions.init(1,0,0); let a = Versions.init(1,0,0);
Alcotest.(check(int))("equal", 0, Versions.compare(a, a)); Alcotest.(check(int))("equal", 0, Versions.compare(a, a))
}; };
let test_compare_first_major_greater = () => { let test_compare_first_major_greater = () => {
let a = Versions.init(2,0,0); let a = Versions.init(2,0,0);
let b = Versions.init(1, 0, 0); let b = Versions.init(1, 0, 0);
Alcotest.(check(int))("First major greater", 1, Versions.compare(a, b)); Alcotest.(check(int))("First major greater", 1, Versions.compare(a, b))
}; };
let test_compare_first_minor_greater = () => { let test_compare_first_minor_greater = () => {
let a = Versions.init(1,1,0); let a = Versions.init(1,1,0);
let b = Versions.init(1, 0, 0); let b = Versions.init(1, 0, 0);
Alcotest.(check(int))("First minor greater", 1, Versions.compare(a, b)); Alcotest.(check(int))("First minor greater", 1, Versions.compare(a, b))
}; };
let test_compare_first_point_greater = () => { let test_compare_first_point_greater = () => {
let a = Versions.init(1, 0, 1); let a = Versions.init(1, 0, 1);
let b = Versions.init(1,0,0); let b = Versions.init(1,0,0);
Alcotest.(check(int))("First point greater", 1, Versions.compare(a, b)); Alcotest.(check(int))("First point greater", 1, Versions.compare(a, b))
}; };
let test_compare_second_major_greater = () => { let test_compare_second_major_greater = () => {
let a = Versions.init(1,0,0); let a = Versions.init(1,0,0);
let b = Versions.init(2, 0, 0); let b = Versions.init(2, 0, 0);
Alcotest.(check(int))("Second major greater", -1, Versions.compare(a, b)); Alcotest.(check(int))("Second major greater", -1, Versions.compare(a, b))
}; };
let test_compare_second_minor_greater = () => { let test_compare_second_minor_greater = () => {
let a = Versions.init(1,0,0); let a = Versions.init(1,0,0);
let b = Versions.init(1, 1, 0); let b = Versions.init(1, 1, 0);
Alcotest.(check(int))("Second minor greater", -1, Versions.compare(a, b)); Alcotest.(check(int))("Second minor greater", -1, Versions.compare(a, b))
}; };
let test_compare_second_point_greater = () => { let test_compare_second_point_greater = () => {
let a = Versions.init(1,0,0); let a = Versions.init(1,0,0);
let b = Versions.init(1, 0, 1); let b = Versions.init(1, 0, 1);
Alcotest.(check(int))("Second point greater", -1, Versions.compare(a, b)); Alcotest.(check(int))("Second point greater", -1, Versions.compare(a, b))
}; };
let test_ine_cur_newer = () => { let test_ine_cur_newer = () => {
let a = Ok(Versions.init(2,0,0)); let a = Ok(Versions.init(2,0,0));
let b = Versions.init(1, 0, 0); let b = Versions.init(1, 0, 0);
let ine = Versions.is_new_enough(a, b); let ine = Versions.is_new_enough(a, b);
Alcotest.(check(bool))("Curr newer", true, ine |> Result.get_ok); Alcotest.(check(bool))("Curr newer", true, ine |> Result.get_ok)
}; };
let test_ine_cur_older = () => { let test_ine_cur_older = () => {
let a = Ok(Versions.init(1,0,0)); let a = Ok(Versions.init(1,0,0));
let b = Versions.init(2, 0, 0); let b = Versions.init(2, 0, 0);
let ine = Versions.is_new_enough(a, b); let ine = Versions.is_new_enough(a, b);
Alcotest.(check(bool))("Curr older", false, ine |> Result.get_ok); Alcotest.(check(bool))("Curr older", false, ine |> Result.get_ok)
}; };
let test_ine_cur_equal = () => { let test_ine_cur_equal = () => {
let a = Versions.init(1,0,0); let a = Versions.init(1,0,0);
let ine = Versions.is_new_enough(Ok(a), a); let ine = Versions.is_new_enough(Ok(a), a);
Alcotest.(check(bool))("Curr equal", true, ine |> Result.get_ok); Alcotest.(check(bool))("Curr equal", true, ine |> Result.get_ok)
}; };
let test_ine_error = () => { // TODO: figure out typing of `Alcotest.check` here.
let a = Error("foobarbaz"); // I think we just add a `pprint` and `equal` function, but that is verbose
let ine = Versions.is_new_enough(a, Versions.init(1, 0, 0)); // let test_ine_error = () => {
Alcotest.(check(Alcotest.result(bool, string)))( // let a = Error("foobarbaz");
"Error bubbled", // let ine = Versions.is_new_enough(a, Versions.init(1, 0, 0));
Error("foobarbaz"), // Alcotest.(check())("Error bubbled", Error("foobarbaz"), ine)
ine, // };
);
};
let test_in_direnv_true = () => { let test_in_direnv_true = () => {
Core_unix.putenv(~key="direnv", ~data="direnv"); Core_unix.putenv(~key="direnv", ~data="direnv");
Alcotest.(check(bool))("In direnv", true, Versions.in_direnv()); Alcotest.(check(bool))("In direnv", true, Versions.in_direnv())
}; };
let test_in_direnv_false = () => { let test_in_direnv_false = () => {
Core_unix.unsetenv("direnv"); Core_unix.unsetenv("direnv");
Alcotest.(check(bool))("Not in direnv", false, Versions.in_direnv()); Alcotest.(check(bool))("Not in direnv", false, Versions.in_direnv())
};
let test_extract_version_number_success = () => {
let result = Versions.extract_version_number("../tests/spit_version.sh");
check_version("Versions", Ok(Versions.init(1, 1, 1)), result);
};
let test_extract_version_number_no_version = () => {
let result = Versions.extract_version_number("../tests/spit_gibberish.sh");
check_version(
"Versions",
Error(
"Stdout did not contain a version number for `../tests/spit_gibberish.sh --version`",
),
result,
);
};
let test_extract_version_number_nonexistent = () => {
let result = Versions.extract_version_number("nonexistent.sh");
check_version(
"Versions",
Error("Failed executing 'nonexistent.sh'"),
result,
);
}; };
// TODO: Test: // TODO: Test:
// * extract_version_number: impure, don't know how to get a concrete version number to test against
// * preflight_versions? impure, but m // * preflight_versions? impure, but m
let () = let () =
Alcotest.( Alcotest.(
@ -134,36 +91,12 @@ let () =
"compare", "compare",
[ [
test_case("Versions Equal", `Quick, test_compare_equal), test_case("Versions Equal", `Quick, test_compare_equal),
test_case( test_case("First Major Greater", `Quick, test_compare_first_major_greater),
"First Major Greater", test_case("First Minor Greater", `Quick, test_compare_first_minor_greater),
`Quick, test_case("First Point Greater", `Quick, test_compare_first_point_greater),
test_compare_first_major_greater, test_case("Second Major Greater", `Quick, test_compare_second_major_greater),
), test_case("Second Minor Greater", `Quick, test_compare_second_minor_greater),
test_case( test_case("Second Point Greater", `Quick, test_compare_second_point_greater),
"First Minor Greater",
`Quick,
test_compare_first_minor_greater,
),
test_case(
"First Point Greater",
`Quick,
test_compare_first_point_greater,
),
test_case(
"Second Major Greater",
`Quick,
test_compare_second_major_greater,
),
test_case(
"Second Minor Greater",
`Quick,
test_compare_second_minor_greater,
),
test_case(
"Second Point Greater",
`Quick,
test_compare_second_point_greater,
),
], ],
), ),
( (
@ -172,8 +105,7 @@ let () =
test_case("Curr Newer", `Quick, test_ine_cur_newer), test_case("Curr Newer", `Quick, test_ine_cur_newer),
test_case("Curr Older", `Quick, test_ine_cur_older), test_case("Curr Older", `Quick, test_ine_cur_older),
test_case("Curr Equal", `Quick, test_ine_cur_equal), test_case("Curr Equal", `Quick, test_ine_cur_equal),
test_case("Error", `Quick, test_ine_error), ]
],
), ),
( (
"in_direnv", "in_direnv",
@ -181,23 +113,6 @@ let () =
test_case("true", `Quick, test_in_direnv_true), test_case("true", `Quick, test_in_direnv_true),
test_case("false", `Quick, test_in_direnv_false), test_case("false", `Quick, test_in_direnv_false),
], ],
),
(
"extract_version_number",
[
test_case("success", `Quick, test_extract_version_number_success),
test_case(
"no version number",
`Quick,
test_extract_version_number_no_version,
),
test_case(
"missing binary",
`Quick,
test_extract_version_number_nonexistent,
),
],
),
],
) )
])
); );

View file

@ -1,60 +1,41 @@
open Lib.Watches; open Lib.Watches;
let test_get_path_removes_prefix = () => { let test_get_path_removes_prefix = () => {
let input = `Assoc([("path", `String("aaaaaaaaaaabbbbb"))]); let input = `Assoc([
Alcotest.(check(string))("Prefix removed", "bbbbb", get_path(input)); ("path", `String("aaaaaaaaaaabbbbb"))
]);
Alcotest.(check(string))("Prefix removed", "bbbbb", get_path(input))
}; };
let test_get_paths_from_doc = () => { let test_get_paths_from_doc = () => {
let input = let input = `Assoc([
`Assoc([
("path", `String("aaaaaaaaaaabbbbb")), ("path", `String("aaaaaaaaaaabbbbb")),
( ("inputs", `Assoc([
"inputs", ("foo", `Assoc([
`Assoc([
(
"foo",
`Assoc([
("path", `String("aaaaaaaaaaaccccc")), ("path", `String("aaaaaaaaaaaccccc")),
( ("inputs", `Assoc([
"inputs", ("bar", `Assoc([
`Assoc([
(
"bar",
`Assoc([
("path", `String("aaaaaaaaaaaddddd")), ("path", `String("aaaaaaaaaaaddddd")),
("inputs", `Assoc([])), ("inputs", `Assoc([]))
]), ]))
), ]))
]), ]))
), ]))
]),
),
]),
),
]); ]);
Alcotest.(check(list(string)))( Alcotest.(check(list(string)))("Gathers all inputs", ["bbbbb", "ccccc", "ddddd"], get_paths_from_doc(input, []))
"Gathers all inputs",
["bbbbb", "ccccc", "ddddd"],
get_paths_from_doc(input, []),
);
}; };
let () = let () =
Alcotest.( Alcotest.(
run( run(
"Watches", "Watches",
[ [("get_path",
(
"get_path",
[ [
test_case("Removes prefix", `Quick, test_get_path_removes_prefix), test_case("Removes prefix", `Quick, test_get_path_removes_prefix),
], ]),
), ("get_paths_from_doc",
( [
"get_paths_from_doc", test_case("Collects all paths", `Quick, test_get_paths_from_doc),
[test_case("Collects all paths", `Quick, test_get_paths_from_doc)], ])
), ]),
],
)
); );

View file

@ -1,3 +0,0 @@
#!/usr/bin/env sh
echo "sdlfkjdsfweiojlsjslfj.dofiwoksdj/sfowiefjw0";

View file

@ -1,2 +0,0 @@
#!/usr/bin/env sh
echo "1.1.1";