@@ -89,112 +89,34 @@ rec {
89
89
|| baseName == "tests.nix"
90
90
) ;
91
91
92
- /* Returns a crate which depends on successful test execution
93
- of crate given as the second argument.
94
-
95
- testCrateFlags: list of flags to pass to the test exectuable
96
- testInputs: list of packages that should be available during test execution
97
- */
98
- crateWithTest = { crate , testCrate , testCrateFlags , testInputs , testPreRun , testPostRun } :
99
- assert builtins . typeOf testCrateFlags == "list" ;
100
- assert builtins . typeOf testInputs == "list" ;
101
- assert builtins . typeOf testPreRun == "string" ;
102
- assert builtins . typeOf testPostRun == "string" ;
103
- let
104
- # override the `crate` so that it will build and execute tests instead of
105
- # building the actual lib and bin targets We just have to pass `--test`
106
- # to rustc and it will do the right thing. We execute the tests and copy
107
- # their log and the test executables to $out for later inspection.
108
- test =
109
- let
110
- drv = testCrate . override
111
- (
112
- _ : {
113
- buildTests = true ;
114
- }
115
- ) ;
116
- # If the user hasn't set any pre/post commands, we don't want to
117
- # insert empty lines. This means that any existing users of crate2nix
118
- # don't get a spurious rebuild unless they set these explicitly.
119
- testCommand = pkgs . lib . concatStringsSep "\n "
120
- ( pkgs . lib . filter ( s : s != "" ) [
121
- testPreRun
122
- "$f $testCrateFlags 2>&1 | tee -a $out"
123
- testPostRun
124
- ] ) ;
125
- in
126
- pkgs . runCommand "run-tests-${ testCrate . name } "
127
- {
128
- inherit testCrateFlags ;
129
- buildInputs = testInputs ;
130
- } ''
131
- set -ex
132
-
133
- export RUST_BACKTRACE=1
134
-
135
- # recreate a file hierarchy as when running tests with cargo
136
-
137
- # the source for test data
138
- ${ pkgs . xorg . lndir } /bin/lndir ${ crate . src }
139
-
140
- # build outputs
141
- testRoot=target/debug
142
- mkdir -p $testRoot
143
-
144
- # executables of the crate
145
- # we copy to prevent std::env::current_exe() to resolve to a store location
146
- for i in ${ crate } /bin/*; do
147
- cp "$i" "$testRoot"
148
- done
149
- chmod +w -R .
150
-
151
- # test harness executables are suffixed with a hash, like cargo does
152
- # this allows to prevent name collision with the main
153
- # executables of the crate
154
- hash=$(basename $out)
155
- for file in ${ drv } /tests/*; do
156
- f=$testRoot/$(basename $file)-$hash
157
- cp $file $f
158
- ${ testCommand }
159
- done
160
- '' ;
161
- in
162
- pkgs . runCommand "${ crate . name } -linked"
163
- {
164
- inherit ( crate ) outputs crateName ;
165
- passthru = ( crate . passthru or { } ) // {
166
- inherit test ;
167
- } ;
168
- } ''
169
- echo tested by ${ test }
170
- ${ lib . concatMapStringsSep "\n " ( output : "ln -s ${ crate . ${ output } } ${ "$" } ${ output } " ) crate . outputs }
171
- '' ;
172
-
173
92
/* A restricted overridable version of builtRustCratesWithFeatures. */
174
93
buildRustCrateWithFeatures =
175
94
{ packageId
176
95
, features ? rootFeatures
177
96
, crateOverrides ? defaultCrateOverrides
178
97
, buildRustCrateForPkgsFunc ? null
179
- , runTests ? false
180
- , testCrateFlags ? [ ]
181
- , testInputs ? [ ]
182
- # Any command to run immediatelly before a test is executed.
183
- , testPreRun ? ""
184
- # Any command run immediatelly after a test is executed.
185
- , testPostRun ? ""
98
+ # Available: [ "lib" "bin" ] or ["test" "bench" "example"]
99
+ , buildKinds ? [ "lib" "bin" ]
186
100
} :
187
101
lib . makeOverridable
188
102
(
189
103
{ features
190
104
, crateOverrides
191
- , runTests
192
- , testCrateFlags
193
- , testInputs
194
- , testPreRun
195
- , testPostRun
105
+ , buildKinds
196
106
} :
197
107
let
108
+ isDevBuild =
109
+ let
110
+ inherit ( pkgs . buildRustCrateHelpers . kinds ) isLib isBin isExample isTest isBench ;
111
+
112
+ notDev = builtins . any ( k : isLib k || isBin k ) buildKinds ;
113
+ isDev = builtins . any ( k : isBench k || isExample k || isTest k ) buildKinds ;
114
+ in
115
+ assert ( buildKinds != [ ] ) ;
116
+ # Can't have build dev and non dev kinds
117
+ assert ( notDev != isDev ) ;
118
+ isDev ;
119
+
198
120
buildRustCrateForPkgsFuncOverriden =
199
121
if buildRustCrateForPkgsFunc != null
200
122
then buildRustCrateForPkgsFunc
@@ -207,31 +129,16 @@ rec {
207
129
defaultCrateOverrides = crateOverrides ;
208
130
}
209
131
) ;
132
+
210
133
builtRustCrates = builtRustCratesWithFeatures {
211
- inherit packageId features ;
134
+ inherit packageId features buildKinds isDevBuild ;
212
135
buildRustCrateForPkgsFunc = buildRustCrateForPkgsFuncOverriden ;
213
- runTests = false ;
214
136
} ;
215
- builtTestRustCrates = builtRustCratesWithFeatures {
216
- inherit packageId features ;
217
- buildRustCrateForPkgsFunc = buildRustCrateForPkgsFuncOverriden ;
218
- runTests = true ;
219
- } ;
220
- drv = builtRustCrates . crates . ${ packageId } ;
221
- testDrv = builtTestRustCrates . crates . ${ packageId } ;
222
- derivation =
223
- if runTests then
224
- crateWithTest
225
- {
226
- crate = drv ;
227
- testCrate = testDrv ;
228
- inherit testCrateFlags testInputs testPreRun testPostRun ;
229
- }
230
- else drv ;
137
+
231
138
in
232
- derivation
139
+ builtRustCrates . crates . ${ packageId }
233
140
)
234
- { inherit features crateOverrides runTests testCrateFlags testInputs testPreRun testPostRun ; } ;
141
+ { inherit features crateOverrides buildKinds ; } ;
235
142
236
143
/* Returns an attr set with packageId mapped to the result of buildRustCrateForPkgsFunc
237
144
for the corresponding crate.
@@ -241,21 +148,24 @@ rec {
241
148
, features
242
149
, crateConfigs ? crates
243
150
, buildRustCrateForPkgsFunc
244
- , runTests
151
+ , buildKinds
152
+ , isDevBuild
245
153
, makeTarget ? makeDefaultTarget
246
154
} @ args :
247
155
assert ( builtins . isAttrs crateConfigs ) ;
248
156
assert ( builtins . isString packageId ) ;
249
157
assert ( builtins . isList features ) ;
250
158
assert ( builtins . isAttrs ( makeTarget stdenv . hostPlatform ) ) ;
251
- assert ( builtins . isBool runTests ) ;
159
+ assert ( builtins . isBool isDevBuild ) ;
160
+ assert ( builtins . isList buildKinds ) ;
252
161
let
253
162
rootPackageId = packageId ;
254
163
mergedFeatures = mergePackageFeatures
255
164
(
256
165
args // {
257
166
inherit rootPackageId ;
258
- target = makeTarget stdenv . hostPlatform // { test = runTests ; } ;
167
+ # What does test do for target?
168
+ target = makeTarget stdenv . hostPlatform // { test = false ; } ;
259
169
}
260
170
) ;
261
171
# Memoize built packages so that reappearing packages are only built once.
@@ -277,7 +187,7 @@ rec {
277
187
builtins . removeAttrs crateConfig' [ "resolvedDefaultFeatures" "devDependencies" ] ;
278
188
devDependencies =
279
189
lib . optionals
280
- ( runTests && packageId == rootPackageId )
190
+ ( isDevBuild && packageId == rootPackageId )
281
191
( crateConfig' . devDependencies or [ ] ) ;
282
192
dependencies =
283
193
dependencyDerivations {
@@ -351,6 +261,7 @@ rec {
351
261
}
352
262
) ;
353
263
extraRustcOpts = lib . lists . optional ( targetFeatures != [ ] ) "-C target-feature=${ lib . concatMapStringsSep "," ( x : "+${ x } " ) targetFeatures } " ;
264
+ buildKinds = if ( packageId == rootPackageId ) then buildKinds else [ "lib" ] ;
354
265
inherit features dependencies buildDependencies crateRenames release ;
355
266
}
356
267
) ;
@@ -472,7 +383,7 @@ rec {
472
383
, featuresByPackageId ? { }
473
384
, target
474
385
# Adds devDependencies to the crate with rootPackageId.
475
- , runTests ? false
386
+ , isDevBuild ? false
476
387
, ...
477
388
} @ args :
478
389
assert ( builtins . isAttrs crateConfigs ) ;
@@ -482,7 +393,7 @@ rec {
482
393
assert ( builtins . isList dependencyPath ) ;
483
394
assert ( builtins . isAttrs featuresByPackageId ) ;
484
395
assert ( builtins . isAttrs target ) ;
485
- assert ( builtins . isBool runTests ) ;
396
+ assert ( builtins . isBool isDevBuild ) ;
486
397
let
487
398
crateConfig = crateConfigs . "${ packageId } " or ( builtins . throw "Package not found: ${ packageId } " ) ;
488
399
expandedFeatures = expandFeatures ( crateConfig . features or { } ) features ;
@@ -517,7 +428,7 @@ rec {
517
428
mergePackageFeatures {
518
429
features = combinedFeatures ;
519
430
featuresByPackageId = cache ;
520
- inherit crateConfigs packageId target runTests rootPackageId ;
431
+ inherit crateConfigs packageId target isDevBuild rootPackageId ;
521
432
}
522
433
) ;
523
434
cacheWithSelf =
@@ -533,7 +444,7 @@ rec {
533
444
(
534
445
crateConfig . dependencies or [ ]
535
446
++ lib . optionals
536
- ( runTests && packageId == rootPackageId )
447
+ ( isDevBuild && packageId == rootPackageId )
537
448
( crateConfig . devDependencies or [ ] )
538
449
) ;
539
450
cacheWithAll =
0 commit comments