Skip to content
Merged
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
25 changes: 22 additions & 3 deletions bin/dune_init.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 =
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions doc/changes/changed/12791.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- The `test/dune` file generated by `dune init proj` now depends on the project library. (#12791, @shonfeder)
14 changes: 14 additions & 0 deletions test/blackbox-tests/test-cases/dune-init/dune-init.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading