Skip to content

Commit f1220cc

Browse files
authored
Merge pull request #69 from little-arhat/feature-required-features-support
Add support for 'required-features'.
2 parents 1ffbbfe + 7e491e9 commit f1220cc

File tree

5 files changed

+93
-25
lines changed

5 files changed

+93
-25
lines changed

flycheck-rust.el

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,11 @@ FILE-NAME is the path of the file that is matched against the
119119
`src_path' value in the list of `targets' returned by `cargo
120120
read-manifest'.
121121
122-
Return a cons cell (KIND . NAME) where KIND is the target
123-
kind (lib, bin, test, example or bench), and NAME the target
124-
name (usually, the crate name). If FILE-NAME exactly matches a
125-
target `src-path', this target is returned. Otherwise, return
122+
Return an alist ((KIND . k) (NAME . n) (REQUIRED-FEATURES . rf))
123+
where KIND is the target kind (lib, bin, test, example or bench),
124+
NAME the target name (usually, the crate name), and REQUIRED-FEATURES is the
125+
optional list of features required to build the selected target. If FILE-NAME
126+
exactly matches a target `src-path', this target is returned. Otherwise, return
126127
the closest matching target, or nil if no targets could be found.
127128
128129
See http://doc.crates.io/manifest.html#the-project-layout for a
@@ -160,7 +161,10 @@ description of the conventional Cargo project layout."
160161
(--find (not (equal target it))))))
161162
(when target
162163
(let-alist target
163-
(cons (flycheck-rust-normalize-target-kind .kind) .name))))))
164+
(seq-filter (lambda (kv) (cdr kv))
165+
(list (cons 'kind (flycheck-rust-normalize-target-kind .kind))
166+
(cons 'name .name)
167+
(cons 'required-features .required-features))))))))
164168

165169

166170
(defun flycheck-rust-normalize-target-kind (kinds)
@@ -194,9 +198,11 @@ Flycheck according to the Cargo project layout."
194198
;; https://github.com/flycheck/flycheck-rust/issues/40#issuecomment-253760883).
195199
(with-demoted-errors "Error in flycheck-rust-setup: %S"
196200
(-when-let* ((file-name (buffer-file-name))
197-
((kind . name) (flycheck-rust-find-cargo-target file-name)))
198-
(setq-local flycheck-rust-crate-type kind)
199-
(setq-local flycheck-rust-binary-name name))))
201+
(target (flycheck-rust-find-cargo-target file-name)))
202+
(let-alist target
203+
(setq-local flycheck-rust-features .required-features)
204+
(setq-local flycheck-rust-crate-type .kind)
205+
(setq-local flycheck-rust-binary-name .name)))))
200206

201207
(provide 'flycheck-rust)
202208

tests/crate-with-features/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "test-crate-with-features"
3+
version = "0.1.0"
4+
5+
[features]
6+
fea1 = []
7+
fea2 = []
8+
9+
[[bin]]
10+
name = "main"
11+
path = "src/main.rs"
12+
required-features = ["fea1"]
13+
14+
[[example]]
15+
name = "example"
16+
path = "example/example.rs"
17+
required-features = ["fea1", "fea2"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

tests/crate-with-features/src/main.rs

Whitespace-only changes.

tests/test-rust-setup.el

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,97 +38,141 @@
3838
(defun crate-file (file-name)
3939
(expand-file-name file-name "tests/test-crate"))
4040

41+
(defun crate-with-features-file (file-name)
42+
(expand-file-name file-name "tests/crate-with-features"))
43+
4144
(defun lib-crate-file (file-name)
4245
(expand-file-name file-name "tests/custom-lib-target"))
4346

4447
(defun build-script-crate-file (file-name)
4548
(expand-file-name file-name "tests/build-script-test"))
4649

50+
(defun cdrassoc (sym alist) (cdr (assoc sym alist)))
51+
52+
(defun get-cargo-version ()
53+
(let ((cargo (funcall flycheck-executable-find "cargo")))
54+
(with-output-to-string
55+
(call-process cargo nil standard-output nil "--version"))))
56+
57+
(defun cargo-version ()
58+
(pcase-let
59+
((`(,ignored1 ,version ,ignored2) (split-string (get-cargo-version))))
60+
(split-string version "-")))
61+
62+
(defun cargo-older-than-1.29-nightly ()
63+
(pcase-let ((`(,version ,channel) (cargo-version)))
64+
(or (version< version "1.29")
65+
(and (version= version "1.29") (string= channel "beta")))))
66+
4767
(describe
4868
"`flycheck-rust-find-cargo-target' associates"
4969

5070
(it "'src/lib.rs' to the library target"
5171
(expect
52-
(car (flycheck-rust-find-cargo-target (crate-file "src/lib.rs")))
72+
(cdrassoc 'kind (flycheck-rust-find-cargo-target (crate-file "src/lib.rs")))
5373
:to-equal "lib"))
5474

5575
(it "'src/a.rs' to the library target"
5676
(expect
57-
(car (flycheck-rust-find-cargo-target (crate-file "src/a.rs")))
77+
(cdrassoc 'kind (flycheck-rust-find-cargo-target (crate-file "src/a.rs")))
5878
:to-equal "lib"))
5979

6080
(it "'src/main.rs' to the main binary target"
6181
(expect
6282
(flycheck-rust-find-cargo-target (crate-file "src/main.rs"))
63-
:to-equal '("bin" . "test-crate")))
83+
:to-equal '((kind . "bin") (name . "test-crate"))))
6484

6585
(it "'src/bin/a.rs' to the 'a' binary target"
6686
(expect
6787
(flycheck-rust-find-cargo-target (crate-file "src/bin/a.rs"))
68-
:to-equal '("bin" . "a")))
88+
:to-equal '((kind . "bin") (name . "a"))))
6989

7090
(it "'src/bin/b.rs' to the 'b' binary target"
7191
(expect
7292
(flycheck-rust-find-cargo-target (crate-file "src/bin/b.rs"))
73-
:to-equal '("bin" . "b")))
93+
:to-equal '((kind . "bin") (name . "b"))))
7494

7595
(it "'src/bin/support/mod.rs' to any binary target"
7696
(expect
7797
(flycheck-rust-find-cargo-target (crate-file "src/bin/support/mod.rs"))
78-
:to-equal-one-of '("bin". "a") '("bin". "b")))
98+
:to-equal-one-of
99+
'((kind . "bin") (name . "a"))
100+
'((kind . "bin") (name . "b"))))
79101

80102
(it "'tests/a.rs' to the 'a' test target"
81103
(expect
82104
(flycheck-rust-find-cargo-target (crate-file "tests/a.rs"))
83-
:to-equal '("test" . "a")))
105+
:to-equal '((kind . "test") (name . "a"))))
84106

85107
(it "'tests/support/mod.rs' to any test target"
86108
(expect
87109
(flycheck-rust-find-cargo-target (crate-file "tests/support/mod.rs"))
88-
:to-equal-one-of '("test". "a") '("test". "b")))
110+
:to-equal-one-of
111+
'((kind . "test") (name . "a"))
112+
'((kind . "test") (name . "b"))))
89113

90114
(it "'examples/a.rs' to the 'a' example target"
91115
(expect
92116
(flycheck-rust-find-cargo-target (crate-file "examples/a.rs"))
93-
:to-equal '("example" . "a")))
117+
:to-equal '((kind . "example") (name . "a"))))
94118

95119
(it "'examples/b.rs' to the 'b' example target"
96120
(expect
97121
(flycheck-rust-find-cargo-target (crate-file "examples/b.rs"))
98-
:to-equal '("example" . "b")))
122+
:to-equal '((kind . "example") (name . "b"))))
99123

100124
(it "'examples/support/mod.rs' to any example target"
101125
(expect
102126
(flycheck-rust-find-cargo-target (crate-file "examples/support/mod.rs"))
103-
:to-equal-one-of '("example" . "a") '("example" . "b")))
127+
:to-equal-one-of
128+
'((kind . "example") (name . "a"))
129+
'((kind . "example") (name . "b"))))
104130

105131
(it "'benches/a.rs' to the 'a' bench target"
106132
(expect
107133
(flycheck-rust-find-cargo-target (crate-file "benches/a.rs"))
108-
:to-equal '("bench" . "a")))
134+
:to-equal '((kind . "bench") (name . "a"))))
109135

110136
(it "'benches/b.rs' to the 'b' bench target"
111137
(expect
112138
(flycheck-rust-find-cargo-target (crate-file "benches/b.rs"))
113-
:to-equal '("bench" . "b")))
139+
:to-equal '((kind . "bench") (name . "b"))))
114140

115141
(it "'benches/support/mod.rs' to any bench target"
116142
(expect
117143
(flycheck-rust-find-cargo-target (crate-file "benches/support/mod.rs"))
118-
:to-equal-one-of '("bench" . "a") '("bench" . "b")))
144+
:to-equal-one-of
145+
'((kind . "bench") (name . "a"))
146+
'((kind . "bench") (name . "b"))))
119147

120148
(it "'src/lib.rs' to the library target (custom-lib-target)"
121149
(expect
122150
(car (flycheck-rust-find-cargo-target (lib-crate-file "src/lib.rs")))
123-
:to-equal "lib"))
151+
:to-equal '(kind . "lib")))
124152

125153
(it "'build.rs' to any target in the same workspace member (parent)"
126154
(expect
127155
(flycheck-rust-find-cargo-target (build-script-crate-file "build.rs"))
128-
:to-equal (cons "bin" "build-script-test")))
156+
:to-equal '((kind . "bin") (name . "build-script-test"))))
129157

130158
(it "'build.rs' to any target in the same workspace member (child)"
131159
(expect
132160
(flycheck-rust-find-cargo-target (build-script-crate-file "lib-test/build.rs"))
133-
:to-equal (cons "lib" "lib-test")))
161+
:to-equal '((kind . "lib") (name . "lib-test"))))
162+
163+
(it "'src/main.rs' to the bin target with required-features (fea1)"
164+
(when (cargo-older-than-1.29-nightly)
165+
(signal 'buttercup-pending "requires cargo 1.29"))
166+
(expect
167+
(flycheck-rust-find-cargo-target (crate-with-features-file "src/main.rs"))
168+
:to-equal '((kind . "bin") (name . "main") (required-features . ("fea1")))))
169+
170+
(it "'example/example.rs' to the example target with required-features (fea1 fea2)"
171+
(when (cargo-older-than-1.29-nightly)
172+
(signal 'buttercup-pending "requires cargo 1.29"))
173+
(expect
174+
(flycheck-rust-find-cargo-target (crate-with-features-file "example/example.rs"))
175+
:to-equal '((kind . "example")
176+
(name . "example")
177+
(required-features . ("fea1" "fea2")))))
134178
)

0 commit comments

Comments
 (0)