Skip to content

Commit ae43f28

Browse files
authored
Merge pull request #103 from mbarbin/auto-load-root-dunolint
Auto load root dunolint
2 parents 14b33d0 + 8009da9 commit ae43f28

File tree

8 files changed

+66
-55
lines changed

8 files changed

+66
-55
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
### Changed
88

9+
- Now autoload config files if present at "$PWD/dunolint" (#102, @mbarbin).
910
- Change dune-lang sexps to be atoms (e.g. "3.19") instead of tuples (#101, @mbarbin).
1011
- Remove base and ppx dependencies from `dunolint-lib` (#99, @mbarbin).
1112
- Always use versioned sexp for config (#98, @mbarbin).

lib/dunolint_cli/src/cmd__lint.ml

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,7 @@ let main =
3737
>>| List.map ~f:(fun condition -> `enforce condition)
3838
in
3939
let config =
40-
match config with
41-
| Some filename -> Common_helpers.load_config_exn ~filename
42-
| None ->
43-
Dunolint.Config.create
44-
~skip_subtree:(Common_helpers.skip_subtree ~globs:[])
45-
~rules:[]
46-
()
47-
in
48-
let config =
49-
Dunolint.Config.create
50-
?skip_subtree:(Dunolint.Config.skip_subtree config)
51-
~rules:(Dunolint.Config.rules config @ enforce)
52-
()
40+
Common_helpers.load_config_opt_exn ~config ~append_extra_rules:enforce
5341
in
5442
Dunolint_engine.run ~config:dunolint_engine_config
5543
@@ fun dunolint_engine ->

lib/dunolint_cli/src/cmd__tools__lint_file.ml

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,19 +197,7 @@ let main =
197197
let save_in_place = save_in_place ~in_place ~file in
198198
let cwd = Unix.getcwd () |> Absolute_path.v in
199199
let config =
200-
match config with
201-
| Some filename -> Common_helpers.load_config_exn ~filename
202-
| None ->
203-
Dunolint.Config.create
204-
~skip_subtree:(Common_helpers.skip_subtree ~globs:[])
205-
~rules:[]
206-
()
207-
in
208-
let config =
209-
Dunolint.Config.create
210-
?skip_subtree:(Dunolint.Config.skip_subtree config)
211-
~rules:(Dunolint.Config.rules config @ enforce)
212-
()
200+
Common_helpers.load_config_opt_exn ~config ~append_extra_rules:enforce
213201
in
214202
let path = select_path ~cwd ~filename ~file in
215203
let linter = select_linter ~path:(path :> Fpath.t) in

lib/dunolint_cli/src/common_helpers.ml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,21 @@ let load_config_exn ~filename =
108108
in
109109
Err.raise ~loc [ message ])
110110
;;
111+
112+
let load_config_opt_exn ~config ~append_extra_rules =
113+
let config =
114+
match config with
115+
| Some filename -> load_config_exn ~filename
116+
| None ->
117+
let cwd = Unix.getcwd () |> Absolute_path.v in
118+
let default_file = Absolute_path.extend cwd (Fsegment.v "dunolint") in
119+
let filename = Absolute_path.to_string default_file in
120+
if Stdlib.Sys.file_exists filename
121+
then load_config_exn ~filename
122+
else Dunolint.Config.create ~skip_subtree:(skip_subtree ~globs:[]) ~rules:[] ()
123+
in
124+
Dunolint.Config.create
125+
?skip_subtree:(Dunolint.Config.skip_subtree config)
126+
~rules:(Dunolint.Config.rules config @ append_extra_rules)
127+
()
128+
;;

lib/dunolint_cli/src/common_helpers.mli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ val skip_subtree : globs:string list -> Dunolint.Config.Skip_subtree.t
3131
(** A helper for loading the config with some effort regarding producing located
3232
error messages when able. *)
3333
val load_config_exn : filename:string -> Dunolint.Config.t
34+
35+
(** A helper to load a config file, either supplied or inferred from the context
36+
and add some optional rules. *)
37+
val load_config_opt_exn
38+
: config:string option
39+
-> append_extra_rules:Dunolint.Config.Rule.t list
40+
-> Dunolint.Config.t

test/cram/interactive.t

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Where this is nothing to lint, the interactive command exits with no prompt.
2525
Let's create a config with some rules that are going to apply to the files we
2626
created.
2727

28-
$ cat > .dunolint <<EOF
28+
$ cat > dunolint <<EOF
2929
> ((rules (
3030
> (enforce (dune (library (name (equals foo)))))
3131
> (enforce (dune_project (name (equals foo))))
@@ -34,7 +34,7 @@ created.
3434

3535
We run the lint command in dry-run mode to visualize the changes suggested.
3636

37-
$ dunolint lint --config=.dunolint --dry-run
37+
$ dunolint lint --config=dunolint --dry-run
3838
dry-run: Would edit file "dune-project":
3939
-1,1 +1,1
4040
-|(name main)
@@ -48,7 +48,7 @@ We run the lint command in dry-run mode to visualize the changes suggested.
4848

4949
Note it is possible to restrict the run to a subdirectory only.
5050

51-
$ dunolint lint --config=.dunolint --dry-run --below lib/
51+
$ dunolint lint --config=dunolint --dry-run --below lib/
5252
dry-run: Would edit file "lib/foo/dune":
5353
-1,2 +1,2
5454
(library
@@ -63,7 +63,7 @@ We disable the pager for the test.
6363

6464
We can quit at any time during the interactive loop.
6565

66-
$ printf 'q\n' | dunolint lint --config=.dunolint --interactive
66+
$ printf 'q\n' | dunolint lint --config=dunolint --interactive
6767
Would edit file "dune-project":
6868
-1,1 +1,1
6969
-|(name main)
@@ -73,7 +73,7 @@ We can quit at any time during the interactive loop.
7373

7474
We can choose to refuse some diff, and accept others.
7575

76-
$ printf 'n\ny\n' | dunolint lint --config=.dunolint --interactive
76+
$ printf 'n\ny\n' | dunolint lint --config=dunolint --interactive
7777
Would edit file "dune-project":
7878
-1,1 +1,1
7979
-|(name main)

test/cram/lint-file.t

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,19 +109,19 @@ Next we are going to test the command with config.
109109

110110
The command may be passed a config file.
111111

112-
$ printf '((rules ()))\n' > .dunolint
112+
$ printf '((rules ()))\n' > dunolint
113113

114-
$ dunolint tools lint dune --config=.dunolint
114+
$ dunolint tools lint dune --config=dunolint
115115
(library
116116
(name mylib)
117117
(libraries a b c))
118118

119119
To make it easier to write in this test, we're generating an actual config from
120120
an OCaml file.
121121

122-
$ ./lint_file_config_gen.exe > .dunolint
122+
$ ./lint_file_config_gen.exe > dunolint
123123

124-
$ cat .dunolint
124+
$ cat dunolint
125125
;; This file is generated by [bin/lint_file_gen_config.ml]. Do not edit!
126126
((version 0)
127127
((skip_subtree (cond (((path (glob .git/*)) skip_subtree))))
@@ -137,18 +137,27 @@ an OCaml file.
137137
> (name my_project_name)
138138
> EOF
139139

140-
Currently the behavior of the lint-file command is to not load config files on
141-
its own. See below how the name of the project is not linted:
140+
Currently the behavior of the lint-file command is to load a config file on its
141+
own if it is in the current cwd and named "dunolint". See below how the name of
142+
the project is indeed linted:
142143

143144
$ dunolint tools lint-file dune-project
144145
(lang dune 3.17)
145146

147+
(name foo)
148+
149+
However, you may supply a config to use.
150+
151+
$ printf "((rules()))\n" > empty-config
152+
$ dunolint tools lint-file dune-project --config=empty-config
153+
(lang dune 3.17)
154+
146155
(name my_project_name)
147156

148-
However, you may supply a config to use. The test config enforces the project
149-
name so its effect is visible in this test (it is changed to "foo"):
157+
The test config enforces the project name so its effect is visible in this test
158+
(it is changed to "foo"):
150159

151-
$ dunolint tools lint-file dune-project --config=.dunolint
160+
$ dunolint tools lint-file dune-project --config=dunolint
152161
(lang dune 3.17)
153162

154163
(name foo)
@@ -170,7 +179,7 @@ The path that is used by the config is the filename supplied when it is
170179
overridden. In particular note how here we are executing the `return` statement
171180
of the config.
172181

173-
$ dunolint tools lint-file dune-project --config=.dunolint \
182+
$ dunolint tools lint-file dune-project --config=dunolint \
174183
> --filename=vendor/dune-project
175184
(lang dune 3.17)
176185

@@ -179,20 +188,20 @@ of the config.
179188
If the file is at a path configured to be in a skipped directory, the command
180189
won't apply linting rules to it.
181190

182-
$ cat dune | dunolint tools lint-file --filename=.git/dune
191+
$ cat dune | dunolint tools lint-file --filename=.git/dune --config=empty-config
183192
(library
184193
(name mylib)
185194
(libraries a b c))
186195

187-
$ cat dune | dunolint tools lint-file --filename=.git/dune --config=.dunolint
196+
$ cat dune | dunolint tools lint-file --filename=.git/dune --config=dunolint
188197
(library (name mylib)
189198
(libraries b c a))
190199

191200
Note however that the result is currently different when the `skip_subtree` is
192201
returned from a rule, rather than from the dedicated section. This is probably
193202
confusing and we may revisit this later. Keeping as a regression test for now.
194203

195-
$ cat dune | dunolint tools lint-file --filename=_build/dune --config=.dunolint
204+
$ cat dune | dunolint tools lint-file --filename=_build/dune --config=dunolint
196205
(library
197206
(name mylib)
198207
(libraries b c a))

test/cram/lint.t

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ its own. See below how the name of the project is not linted:
3737

3838
If the config is supplied, but it is invalid, dunolint will complain.
3939

40-
$ printf '(blah)\n' > .dunolint
40+
$ printf '(blah)\n' > dunolint
4141

42-
$ dunolint lint --yes --config .dunolint
43-
File ".dunolint", line 1, characters 1-5:
42+
$ dunolint lint --yes --config=dunolint
43+
File "dunolint", line 1, characters 1-5:
4444
1 | (blah)
4545
^^^^
4646
Error: config.v0.t_of_sexp: record conversion: only pairs expected, their
@@ -49,27 +49,27 @@ If the config is supplied, but it is invalid, dunolint will complain.
4949

5050
Unsupported config versions are reported with a located error message.
5151

52-
$ printf '((version 101101)(blah))\n' > .dunolint
52+
$ printf '((version 101101)(blah))\n' > dunolint
5353

54-
$ dunolint lint --yes --config .dunolint
55-
File ".dunolint", line 1, characters 10-16:
54+
$ dunolint lint --yes --config=dunolint
55+
File "dunolint", line 1, characters 10-16:
5656
1 | ((version 101101)(blah))
5757
^^^^^^
5858
Error: Unsupported dunolint config version [101101].
5959
[123]
6060

6161
If there are no rules, the linting will succeed but does nothing in this case.
6262

63-
$ printf '((rules ()))\n' > .dunolint
63+
$ printf '((rules ()))\n' > dunolint
6464

65-
$ dunolint lint --yes --config .dunolint
65+
$ dunolint lint --yes --config=dunolint
6666

6767
Now let's load a config. To make it easier to write in this test, we're
6868
generating an actual config from an OCaml file.
6969

70-
$ ./lint_file_config_gen.exe > .dunolint
70+
$ ./lint_file_config_gen.exe > dunolint
7171

72-
$ cat .dunolint
72+
$ cat dunolint
7373
;; This file is generated by [bin/lint_file_gen_config.ml]. Do not edit!
7474
((version 0)
7575
((skip_subtree (cond (((path (glob .git/*)) skip_subtree))))
@@ -81,7 +81,7 @@ generating an actual config from an OCaml file.
8181

8282
Let's lint with this config.
8383

84-
$ dunolint lint --yes --config .dunolint
84+
$ dunolint lint --yes --config=dunolint
8585
Editing file "dune-project":
8686
-1,3 +1,3
8787
(lang dune 3.17)

0 commit comments

Comments
 (0)