From b3577fde8bb34be645e2fee00e138bce6c10eecd Mon Sep 17 00:00:00 2001 From: Shon Feder Date: Tue, 25 Nov 2025 22:23:18 -0500 Subject: [PATCH] Add library dependency in dune init proj tests Improves the generated template for projects by including a dependency on the project library in the test stanza. Signed-off-by: Shon Feder --- bin/dune_init.ml | 25 ++++++++++++++++--- doc/changes/changed/12791.md | 1 + .../test-cases/dune-init/dune-init.t/run.t | 14 +++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 doc/changes/changed/12791.md diff --git a/bin/dune_init.ml b/bin/dune_init.ml index c74d6fec29d..b1d5be32ab7 100644 --- a/bin/dune_init.ml +++ b/bin/dune_init.ml @@ -492,7 +492,6 @@ module Component = struct ;; let test ({ context; common; options } : Options.Test.t Options.t) = - (* Marking the current absence of test-specific options *) let dir = context.dir in let test_dune = Stanza_cst.test common options @@ -526,6 +525,16 @@ module Component = struct File.Dune { dir; content; name = "dune-project" } ;; + (* Convert a libname to a dune atom that can be used to declare a library as + a dependency *) + let lib_name_to_atom lib : Dune_lang.Atom.t = + Lib_name.to_string lib + |> String.map ~f:(function + | '-' -> '_' (* in case the lib_name is public, containing a `-` *) + | c -> c) + |> Dune_lang.Atom.of_string + ;; + let proj_exec dir ({ context; common; options } : Options.Project.t Options.t) = let lib_target = src @@ -536,10 +545,15 @@ module Component = struct in let test_target = let test_name = "test_" ^ Dune_lang.Atom.to_string common.name in + let libraries = + match common.public with + | None -> [] + | Some lib -> [ lib_name_to_atom lib ] + in test { context = { context with dir = Path.Source.relative dir "test" } ; options = () - ; common = { common with name = Dune_lang.Atom.of_string test_name } + ; common = { common with name = Dune_lang.Atom.of_string test_name; libraries } } in let bin_target = @@ -564,10 +578,15 @@ module Component = struct in let test_target = let test_name = "test_" ^ Dune_lang.Atom.to_string common.name in + let libraries = + match common.public with + | None -> [] + | Some lib -> [ lib_name_to_atom lib ] + in test { context = { context with dir = Path.Source.relative dir "test" } ; options = () - ; common = { common with name = Dune_lang.Atom.of_string test_name } + ; common = { common with name = Dune_lang.Atom.of_string test_name; libraries } } in lib_target @ test_target diff --git a/doc/changes/changed/12791.md b/doc/changes/changed/12791.md new file mode 100644 index 00000000000..3fa852c7461 --- /dev/null +++ b/doc/changes/changed/12791.md @@ -0,0 +1 @@ +- The `test/dune` file generated by `dune init proj` now depends on the project library. (#12791, @shonfeder) diff --git a/test/blackbox-tests/test-cases/dune-init/dune-init.t/run.t b/test/blackbox-tests/test-cases/dune-init/dune-init.t/run.t index c48ffb61a3f..7e26281a076 100644 --- a/test/blackbox-tests/test-cases/dune-init/dune-init.t/run.t +++ b/test/blackbox-tests/test-cases/dune-init/dune-init.t/run.t @@ -363,6 +363,13 @@ In particular, the `dune-project` file has the expected content: ; See the complete stanza docs at https://dune.readthedocs.io/en/stable/reference/dune-project/index.html +And the test has a dependency on the library + + $ cat new_exec_proj/test/dune + (test + (name test_new_exec_proj) + (libraries new_exec_proj)) + We can build the project: $ dune build --root new_exec_proj @@ -474,6 +481,13 @@ In particular, the `dune-project` file has the expected content: ; See the complete stanza docs at https://dune.readthedocs.io/en/stable/reference/dune-project/index.html +And the test has a dependency on the library + + $ cat new_lib_proj/test/dune + (test + (name test_new_lib_proj) + (libraries new_lib_proj)) + We can build and install the project: $ dune build --root new_lib_proj @install