Skip to content

Commit b3577fd

Browse files
committed
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 <[email protected]>
1 parent bd29ed1 commit b3577fd

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

bin/dune_init.ml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,6 @@ module Component = struct
492492
;;
493493

494494
let test ({ context; common; options } : Options.Test.t Options.t) =
495-
(* Marking the current absence of test-specific options *)
496495
let dir = context.dir in
497496
let test_dune =
498497
Stanza_cst.test common options
@@ -526,6 +525,16 @@ module Component = struct
526525
File.Dune { dir; content; name = "dune-project" }
527526
;;
528527

528+
(* Convert a libname to a dune atom that can be used to declare a library as
529+
a dependency *)
530+
let lib_name_to_atom lib : Dune_lang.Atom.t =
531+
Lib_name.to_string lib
532+
|> String.map ~f:(function
533+
| '-' -> '_' (* in case the lib_name is public, containing a `-` *)
534+
| c -> c)
535+
|> Dune_lang.Atom.of_string
536+
;;
537+
529538
let proj_exec dir ({ context; common; options } : Options.Project.t Options.t) =
530539
let lib_target =
531540
src
@@ -536,10 +545,15 @@ module Component = struct
536545
in
537546
let test_target =
538547
let test_name = "test_" ^ Dune_lang.Atom.to_string common.name in
548+
let libraries =
549+
match common.public with
550+
| None -> []
551+
| Some lib -> [ lib_name_to_atom lib ]
552+
in
539553
test
540554
{ context = { context with dir = Path.Source.relative dir "test" }
541555
; options = ()
542-
; common = { common with name = Dune_lang.Atom.of_string test_name }
556+
; common = { common with name = Dune_lang.Atom.of_string test_name; libraries }
543557
}
544558
in
545559
let bin_target =
@@ -564,10 +578,15 @@ module Component = struct
564578
in
565579
let test_target =
566580
let test_name = "test_" ^ Dune_lang.Atom.to_string common.name in
581+
let libraries =
582+
match common.public with
583+
| None -> []
584+
| Some lib -> [ lib_name_to_atom lib ]
585+
in
567586
test
568587
{ context = { context with dir = Path.Source.relative dir "test" }
569588
; options = ()
570-
; common = { common with name = Dune_lang.Atom.of_string test_name }
589+
; common = { common with name = Dune_lang.Atom.of_string test_name; libraries }
571590
}
572591
in
573592
lib_target @ test_target

doc/changes/changed/12791.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- The `test/dune` file generated by `dune init proj` now depends on the project library. (#12791, @shonfeder)

test/blackbox-tests/test-cases/dune-init/dune-init.t/run.t

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,13 @@ In particular, the `dune-project` file has the expected content:
363363

364364
; See the complete stanza docs at https://dune.readthedocs.io/en/stable/reference/dune-project/index.html
365365

366+
And the test has a dependency on the library
367+
368+
$ cat new_exec_proj/test/dune
369+
(test
370+
(name test_new_exec_proj)
371+
(libraries new_exec_proj))
372+
366373
We can build the project:
367374

368375
$ dune build --root new_exec_proj
@@ -474,6 +481,13 @@ In particular, the `dune-project` file has the expected content:
474481

475482
; See the complete stanza docs at https://dune.readthedocs.io/en/stable/reference/dune-project/index.html
476483

484+
And the test has a dependency on the library
485+
486+
$ cat new_lib_proj/test/dune
487+
(test
488+
(name test_new_lib_proj)
489+
(libraries new_lib_proj))
490+
477491
We can build and install the project:
478492

479493
$ dune build --root new_lib_proj @install

0 commit comments

Comments
 (0)