Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A couple of misc changes for LIFE #70

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/ast/block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ module Raw = struct
[
shell [ "/bin/sh"; "-c" ];
run ~network:[ "host" ] "apk add --no-cache curl";
run ~network:[ "host" ] "mkdir -p /data && curl -O %s %s"
run ~network:[ "host" ] "mkdir -p /data && curl -o %s %s"
(Fpath.to_string target_path)
(Uri.to_string url);
])
Expand Down
13 changes: 12 additions & 1 deletion src/lib/ast/datafile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@ let v ?subpath id path =
| None -> false
| Some p -> Char.equal p.[String.length p - 1] '*'
in
if wildcard then { id; path; subpath = None; wildcard = true }
let cleaned_subpath =
match subpath with
| None -> None
| Some p -> (
if wildcard == false then Some p
else
let newsub =
String.Sub.to_string (String.sub ~stop:(String.length p - 1) p)
in
match newsub with "" -> None | s -> Some s)
in
if wildcard then { id; path; subpath = cleaned_subpath; wildcard = true }
else { id; path; subpath; wildcard = false }

let id d = d.id
Expand Down
12 changes: 10 additions & 2 deletions src/lib/ast/leaf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ let to_string_for_inputs l (file_subs_map : (string * string list) list) :
let updated a =
List.map
(fun s ->
let regexp = Str.regexp (template_path ^ "\\*?") in
Str.global_replace regexp s a)
let f = Result.get_ok (Fpath.of_string s) in
let basename = Fpath.basename f in

let src_regexp = Str.regexp (template_path ^ "\\*?") in
let dst_regexp = Str.regexp "\\+" in

let p1 = Str.global_replace src_regexp s a in
match p1 == s with
| true -> p1
| false -> Str.global_replace dst_regexp basename p1)
substitutions
in
let u = List.map updated acc |> List.concat in
Expand Down
4 changes: 2 additions & 2 deletions src/test/block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ let test_http_import_block () =
let spec = Block.import_spec block in
let specbody = Sexplib.Sexp.to_string_hum (Obuilder_spec.sexp_of_t spec) in
Alcotest.(check bool)
"Found git command" true
(Astring.String.is_infix ~affix:"curl -O" specbody)
"Found curl command" true
(Astring.String.is_infix ~affix:"curl -o" specbody)

let test_file_import_block_no_schema () =
let body = "/home/michael/file.csv /data/file.csv" in
Expand Down
16 changes: 16 additions & 0 deletions src/test/datafile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,25 @@ let test_basic_dir_with_wildcard () =
Alcotest.(check bool) "Is wildcard" true (Datafile.is_wildcard test);
Alcotest.(check bool) "Is dir" true (Datafile.is_dir test)

let test_subpath_dir_with_wildcard () =
let testcase = Fpath.v "/data/test/" in
let test = Datafile.v ~subpath:"subpath/*" 42 testcase in
Alcotest.(check int) "Same id" 42 (Datafile.id test);
Alcotest.(check string)
"Same path" (Fpath.to_string testcase)
(Fpath.to_string (Datafile.path test));
Alcotest.(check string)
"Same full path" (Fpath.to_string testcase)
(Fpath.to_string (Datafile.fullpath test));
Alcotest.(check (option string))
"No subpath" (Some "subpath") (Datafile.subpath test);
Alcotest.(check bool) "Is wildcard" true (Datafile.is_wildcard test);
Alcotest.(check bool) "Is dir" true (Datafile.is_dir test)

let tests =
[
("Basic file", `Quick, test_basic_file_path);
("Basic file with subpath", `Quick, test_sub_path);
("Canonical dir with wildcard", `Quick, test_basic_dir_with_wildcard);
("Dir with subpath with wildcard", `Quick, test_basic_dir_with_wildcard);
]
38 changes: 38 additions & 0 deletions src/test/leaf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,42 @@ let test_leaf_sub_simplewildcard () =

Alcotest.(check (list string)) "Simple sub" expected test

let test_leaf_sub_simplewildcard_multi () =
let command = Command.of_string "test --i /data/arg1/* --o /data/arg2" in
let inputs = [ Datafile.v 0 (Fpath.v "/data/arg1/*") ]
and outputs = [ Datafile.v 1 (Fpath.v "/data/arg2") ] in
let leaf = Leaf.v 42 (Option.get command) Leaf.Command inputs outputs in

let sublist = [ ("/data/arg1/", [ "/some/path/1"; "/some/path/2" ]) ] in

let test = Leaf.to_string_for_inputs leaf sublist in
let expected =
[
"test --i /some/path/1 --o /data/arg2";
"test --i /some/path/2 --o /data/arg2";
]
in

Alcotest.(check (list string)) "Simple sub" expected test

let test_leaf_sub_simplewildcard_generate () =
let command = Command.of_string "test --i /data/arg1/* --o /data/arg2/+" in
let inputs = [ Datafile.v 0 (Fpath.v "/data/arg1/*") ]
and outputs = [ Datafile.v 1 (Fpath.v "/data/arg2") ] in
let leaf = Leaf.v 42 (Option.get command) Leaf.Command inputs outputs in

let sublist = [ ("/data/arg1/", [ "/some/path/1"; "/some/path/2" ]) ] in

let test = Leaf.to_string_for_inputs leaf sublist in
let expected =
[
"test --i /some/path/1 --o /data/arg2/1";
"test --i /some/path/2 --o /data/arg2/2";
]
in

Alcotest.(check (list string)) "Simple sub" expected test

let test_leaf_command_sub_one_multi () =
let command = Command.of_string "test --i /data/arg1 --o /data/arg2" in
let inputs = [ Datafile.v 0 (Fpath.v "/data/arg1") ]
Expand Down Expand Up @@ -101,6 +137,8 @@ let tests =
[
("Basic leaf test", `Quick, test_leaf_basics);
("Basic leaf simple wildcard", `Quick, test_leaf_sub_simplewildcard);
("Basic leaf multi wildcard", `Quick, test_leaf_sub_simplewildcard_multi);
("Generate target names", `Quick, test_leaf_sub_simplewildcard_generate);
("Basic leaf sub empty", `Quick, test_leaf_command_sub_empty);
("Basic leaf sub simple", `Quick, test_leaf_command_sub_simple);
("Basic leaf sub one map", `Quick, test_leaf_command_sub_one_multi);
Expand Down
Loading