diff --git a/src/lib/ast/block.ml b/src/lib/ast/block.ml index 6910ea9e..e8f55b73 100644 --- a/src/lib/ast/block.ml +++ b/src/lib/ast/block.ml @@ -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); ]) diff --git a/src/lib/ast/datafile.ml b/src/lib/ast/datafile.ml index 60b0455c..f6160625 100644 --- a/src/lib/ast/datafile.ml +++ b/src/lib/ast/datafile.ml @@ -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 diff --git a/src/lib/ast/leaf.ml b/src/lib/ast/leaf.ml index 6f5e84ef..a4570efa 100644 --- a/src/lib/ast/leaf.ml +++ b/src/lib/ast/leaf.ml @@ -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 diff --git a/src/test/block.ml b/src/test/block.ml index 9c3d0002..a4ff9a5d 100644 --- a/src/test/block.ml +++ b/src/test/block.ml @@ -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 diff --git a/src/test/datafile.ml b/src/test/datafile.ml index 46ec0971..8801a4d4 100644 --- a/src/test/datafile.ml +++ b/src/test/datafile.ml @@ -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); ] diff --git a/src/test/leaf.ml b/src/test/leaf.ml index 8b72090e..064a2840 100644 --- a/src/test/leaf.ml +++ b/src/test/leaf.ml @@ -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") ] @@ -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);