Skip to content

Commit

Permalink
Merge pull request #24 from actionshrimp/fix-for-dune-3
Browse files Browse the repository at this point in the history
Resolve path as relative path in sandbox rather than using absolute path
  • Loading branch information
bbc2 authored Apr 4, 2024
2 parents 96bb533 + 271dd47 commit e19c0e2
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 24 deletions.
3 changes: 2 additions & 1 deletion dune-project
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
(lang dune 1.6)
(lang dune 1.11)
(name ppx_blob)
(explicit_js_mode)
14 changes: 7 additions & 7 deletions integration-test/dune-lang-3/src/foo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ let () =
List.exists Result.is_error
[
(* Path relative to this file *)
(* check ~path:"../root.inc" ~expected:"included-root"
~actual:[%blob "../root.inc"]; *)
(* check ~path:"src.inc" ~expected:"included-src" ~actual:[%blob "src.inc"]; *)
check ~path:"../root.inc" ~expected:"included-root"
~actual:[%blob "../root.inc"];
check ~path:"src.inc" ~expected:"included-src" ~actual:[%blob "src.inc"];
(* Path relative to build directory *)
check ~path:"root.inc" ~expected:"included-root"
~actual:[%blob "root.inc"];
check ~path:"src/src.inc" ~expected:"included-src"
~actual:[%blob "src/src.inc"];
(* Ambiguous path *)
check ~path:"common.inc" ~expected:"included-common-root" (* violates precedence *)
check ~path:"common.inc" ~expected:"included-common-src"
~actual:[%blob "common.inc"];
(* check ~path:"../common.inc" ~expected:"included-common-root"
~actual:[%blob "../common.inc"]; *)
check ~path:"../common.inc" ~expected:"included-common-root"
~actual:[%blob "../common.inc"];
(* Absolute path *)
check ~path:"/etc/hostname" ~expected:(input_line (open_in "/etc/hostname"))
check ~path:"/etc/hostname" ~expected:"included-hostname"
~actual:[%blob "/etc/hostname"];
]
then exit 1
2 changes: 1 addition & 1 deletion ppx_blob.opam
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ build: [
]
depends: [
"ocaml"
"dune" {>= "1.6"}
"dune" {>= "1.11"}
"ppxlib"
"alcotest" {with-test}
]
Expand Down
6 changes: 3 additions & 3 deletions src/ppx_blob.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ let location_errorf ~loc =
)

let find_file_path ~loc file_name =
let dirname = Ocaml_common.Location.absolute_path loc.Ocaml_common.Location.loc_start.pos_fname |> Filename.dirname in
let absolute_path = Filename.concat dirname file_name in
List.find Sys.file_exists [absolute_path; file_name]
let dirname = loc.Ocaml_common.Location.loc_start.pos_fname |> Filename.dirname in
let relative_path = Filename.concat dirname file_name in
List.find Sys.file_exists [relative_path; file_name]

let get_blob ~loc file_name =
try
Expand Down
14 changes: 4 additions & 10 deletions test/dune
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
(executable
(name test)
(tests
(libraries alcotest)
(preprocessor_deps (file test_file) (file subdir/test_file))
(preprocess (pps ppx_blob))
(preprocessor_deps (file test_file))
(libraries alcotest))

(alias
(name runtest)
(deps (:test test.exe))
(action (run %{test})))

(names test))
5 changes: 5 additions & 0 deletions test/subdir/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(tests
(libraries alcotest)
(preprocessor_deps (file test_file) (file ../test_file))
(preprocess (pps ppx_blob))
(names test))
14 changes: 14 additions & 0 deletions test/subdir/test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let suite = [
("path relative to source file", `Quick, fun () ->
Alcotest.(check string) "file contents" "test/subdir/test_file\n" [%blob "test_file"]
);

("path relative to source file (parent dir)", `Quick, fun () ->
Alcotest.(check string) "file contents" "test/test_file\n" [%blob "../test_file"]
);
]

let () =
Alcotest.run "ppx_blob" [
"basic", suite
]
1 change: 1 addition & 0 deletions test/subdir/test_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/subdir/test_file
5 changes: 4 additions & 1 deletion test/test.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
let suite = [
("path relative to source file", `Quick, fun () ->
Alcotest.(check string) "file contents" "foo\n" [%blob "test_file"]
Alcotest.(check string) "file contents" "test/test_file\n" [%blob "test_file"]
);
("path relative to source file (subdir)", `Quick, fun () ->
Alcotest.(check string) "file contents" "test/subdir/test_file\n" [%blob "subdir/test_file"]
);
]

Expand Down
2 changes: 1 addition & 1 deletion test/test_file
Original file line number Diff line number Diff line change
@@ -1 +1 @@
foo
test/test_file

0 comments on commit e19c0e2

Please sign in to comment.