Skip to content

Commit

Permalink
Merge pull request #4100 from rgrinberg/cram-package
Browse files Browse the repository at this point in the history
Allow [package] on cram stanza
  • Loading branch information
rgrinberg authored Jan 13, 2021
2 parents 26a67bc + 257d44b commit 20cdd87
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 15 deletions.
18 changes: 15 additions & 3 deletions bin/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,23 @@ end

open Let_syntax

module Only_packages = struct
module Only_packages : sig
type t = private
{ names : Dune_engine.Package.Name.Set.t
; command_line_option : string
}

val create :
names:Dune_engine.Package.Name.Set.t -> command_line_option:string -> t
end = struct
type t =
{ names : Dune_engine.Package.Name.Set.t
; command_line_option : string
}

let create ~names ~command_line_option =
Clflags.only_packages := Some names;
{ names; command_line_option }
end

type t =
Expand Down Expand Up @@ -351,7 +363,7 @@ module Options_implied_by_dash_p = struct
build a particular $(b,<package>.install) target.|})
in
Option.map names ~f:(fun names ->
{ Only_packages.names; command_line_option = "only-packages" })
Only_packages.create ~names ~command_line_option:"only-packages")
in
{ t with only_packages }

Expand All @@ -374,7 +386,7 @@ module Options_implied_by_dash_p = struct
{ release_options with
only_packages =
Option.map pkgs ~f:(fun names ->
{ Only_packages.names; command_line_option = List.hd args })
Only_packages.create ~names ~command_line_option:(List.hd args))
}

let term =
Expand Down
2 changes: 1 addition & 1 deletion bin/common.mli
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ val root : t -> Workspace_root.t
val config : t -> Dune_engine.Config.t

module Only_packages : sig
type t =
type t = private
{ names : Dune_engine.Package.Name.Set.t
; command_line_option : string
(** Which of [-p], [--only-packages], ... was passed *)
Expand Down
2 changes: 2 additions & 0 deletions src/dune_engine/clflags.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ let always_show_command_line = ref false
let promote_install_files = ref false

let ignore_promoted_rules = ref false

let only_packages = ref None
3 changes: 3 additions & 0 deletions src/dune_engine/clflags.mli
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ val promote_install_files : bool ref

(** Wether we are ignorimg rules with [(mode promote)] *)
val ignore_promoted_rules : bool ref

(** Packages passed by --only-packages or -p *)
val only_packages : Package.Name.Set.t option ref
20 changes: 18 additions & 2 deletions src/dune_rules/cram_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ type effective =
; alias : Alias.Name.Set.t
; deps : unit Build.t list
; enabled_if : Blang.t list
; packages : Package.Name.Set.t
}

let empty_effective =
{ loc = Loc.none
; alias = Alias.Name.Set.singleton Alias.Name.runtest
; enabled_if = [ Blang.true_ ]
; deps = []
; packages = Package.Name.Set.empty
}

let missing_run_t (error : Cram_test.t) =
Expand Down Expand Up @@ -167,6 +169,20 @@ let rules ~sctx ~expander ~dir tests =
| None -> acc.alias
| Some a -> Alias.Name.Set.add acc.alias a
in
{ acc with enabled_if; deps; alias })
let packages =
match spec.package with
| None -> acc.packages
| Some (p : Package.t) ->
Package.Name.Set.add acc.packages (Package.Id.name p.id)
in
{ acc with enabled_if; deps; alias; packages })
in
test_rule ~sctx ~expander ~dir effective test)
let test_rule () = test_rule ~sctx ~expander ~dir effective test in
match !Clflags.only_packages with
| None -> test_rule ()
| Some only ->
if
Package.Name.Set.is_empty effective.packages
|| Package.Name.Set.(not (is_empty (inter only effective.packages)))
then
test_rule ())
10 changes: 8 additions & 2 deletions src/dune_rules/cram_stanza.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type t =
; alias : Alias.Name.t option
; deps : Dep_conf.t Bindings.t option
; enabled_if : Blang.t
; package : Package.t option
}

let decode =
Expand All @@ -36,5 +37,10 @@ let decode =
field "applies_to" decode_applies_to ~default:default_applies_to
and+ alias = field_o "alias" Alias.Name.decode
and+ deps = field_o "deps" (Bindings.decode Dep_conf.decode)
and+ enabled_if = Enabled_if.decode ~allowed_vars:Any ~since:None () in
{ loc; alias; deps; enabled_if; applies_to })
and+ enabled_if = Enabled_if.decode ~allowed_vars:Any ~since:None ()
and+ package =
Stanza_common.Pkg.field_opt
~check:(Dune_lang.Syntax.since Stanza.syntax (2, 8))
()
in
{ loc; alias; deps; enabled_if; applies_to; package })
1 change: 1 addition & 0 deletions src/dune_rules/cram_stanza.mli
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type t =
; alias : Alias.Name.t option
; deps : Dep_conf.t Bindings.t option
; enabled_if : Blang.t
; package : Package.t option
}

val decode : t Dune_lang.Decoder.t
6 changes: 3 additions & 3 deletions src/dune_rules/dune_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ module Plugin = struct
and+ libraries = field "libraries" (repeat (located Lib_name.decode))
and+ site =
field "site" (located (pair Package.Name.decode Section.Site.decode))
and+ package = Stanza_common.Pkg.field "package"
and+ package = Stanza_common.Pkg.field ~stanza:"plugin"
and+ optional = field_b "optional" in
{ name; libraries; site; package; optional })
end
Expand All @@ -966,7 +966,7 @@ module Install_conf = struct
fields
(let+ section = field "section" Install.Section_with_site.decode
and+ files = field "files" File_binding.Unexpanded.L.decode
and+ package = Stanza_common.Pkg.field "install"
and+ package = Stanza_common.Pkg.field ~stanza:"install"
and+ enabled_if =
let allowed_vars = Enabled_if.common_vars ~since:(2, 6) in
Enabled_if.decode ~allowed_vars ~since:(Some (2, 6)) ()
Expand Down Expand Up @@ -1928,7 +1928,7 @@ module Documentation = struct

let decode =
fields
(let+ package = Stanza_common.Pkg.field "documentation"
(let+ package = Stanza_common.Pkg.field ~stanza:"documentation"
and+ mld_files = Ordered_set_lang.field "mld_files"
and+ loc = loc in
{ loc; package; mld_files })
Expand Down
22 changes: 19 additions & 3 deletions src/dune_rules/stanza_common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,31 @@ module Pkg = struct
| Ok x -> x
| Error e -> raise (User_error.E { e with loc = Some loc })

let field stanza =
let field ~stanza =
map_validate
(let+ p = Dune_project.get_exn ()
and+ pkg = field_o "package" string in
and+ pkg = field_o "package" Package.Name.decode in
(p, pkg))
~f:(fun (p, pkg) ->
match pkg with
| None -> default p stanza
| Some name -> resolve p (Package.Name.of_string name))
| Some name -> resolve p name)

let field_opt ?check () =
let decode =
let decode = Package.Name.decode in
match check with
| None -> decode
| Some check -> check >>> decode
in
map_validate
(let+ p = Dune_project.get_exn ()
and+ pkg = field_o "package" decode in
(p, pkg))
~f:(fun (p, pkg) ->
match pkg with
| None -> Ok None
| Some name -> resolve p name |> Result.map ~f:Option.some)
end

let modules_field name = Ordered_set_lang.field name
7 changes: 6 additions & 1 deletion src/dune_rules/stanza_common.mli
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ module Pkg : sig
val resolve :
Dune_project.t -> Package.Name.t -> (Package.t, User_message.t) Result.t

val field : string -> Package.t Dune_lang.Decoder.fields_parser
val field : stanza:string -> Package.t Dune_lang.Decoder.fields_parser

val field_opt :
?check:unit Dune_lang.Decoder.t
-> unit
-> Package.t option Dune_lang.Decoder.fields_parser

val default_exn : loc:Loc.t -> Dune_project.t -> string -> Package.t
end
Expand Down
47 changes: 47 additions & 0 deletions test/blackbox-tests/test-cases/cram/package.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
We can attach cram tests to packages

This sub dir is needed because we are runnning cram inside and cram, and we
don't want our own test file to be visible.
$ mkdir subdir
$ cd subdir

$ cat >dune-project <<EOF
> (lang dune 2.8)
> (cram enable)
> (package (name foo))
> (package (name bar))
> EOF

$ cat >dune <<EOF
> (cram
> (applies_to foo)
> (package foo))
> (cram
> (applies_to bar)
> (package bar))
> EOF

$ cat >foo.t <<EOF
> $ echo foo
> EOF

$ cat >bar.t <<EOF
> $ echo bar
> EOF

$ dune build @runtest --only-packages foo
File "foo.t", line 1, characters 0-0:
Error: Files _build/default/foo.t and _build/default/foo.t.corrected differ.
[1]

$ dune promote foo.t
Promoting _build/default/foo.t.corrected to foo.t.


$ dune build @runtest --only-packages bar
File "bar.t", line 1, characters 0-0:
Error: Files _build/default/bar.t and _build/default/bar.t.corrected differ.
[1]

$ dune promote bar.t
Promoting _build/default/bar.t.corrected to bar.t.

0 comments on commit 20cdd87

Please sign in to comment.