nextest_runner/config/
archive.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

use super::TrackDefault;
use crate::config::helpers::deserialize_relative_path;
use camino::{Utf8Component, Utf8Path, Utf8PathBuf};
use serde::{de::Unexpected, Deserialize};
use std::fmt;

/// Configuration for archives.
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub struct ArchiveConfig {
    /// Files to include in the archive.
    pub include: Vec<ArchiveInclude>,
}

/// Type for the archive-include key.
///
/// # Notes
///
/// This is `deny_unknown_fields` because if we take additional arguments in the future, they're
/// likely to change semantics in an incompatible way.
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct ArchiveInclude {
    // We only allow well-formed relative paths within the target directory here. It's possible we
    // can relax this in the future, but better safe than sorry for now.
    #[serde(deserialize_with = "deserialize_relative_path")]
    path: Utf8PathBuf,
    relative_to: ArchiveRelativeTo,
    #[serde(default = "default_depth")]
    depth: TrackDefault<RecursionDepth>,
    #[serde(default = "default_on_missing")]
    on_missing: ArchiveIncludeOnMissing,
}

impl ArchiveInclude {
    /// The maximum depth of recursion.
    pub fn depth(&self) -> RecursionDepth {
        self.depth.value
    }

    /// Whether the depth was deserialized. If false, the default value was used.
    pub fn is_depth_deserialized(&self) -> bool {
        self.depth.is_deserialized
    }

    /// Join the path with the given target dir.
    pub fn join_path(&self, target_dir: &Utf8Path) -> Utf8PathBuf {
        match self.relative_to {
            ArchiveRelativeTo::Target => join_rel_path(target_dir, &self.path),
        }
    }

    /// What to do when the path is missing.
    pub fn on_missing(&self) -> ArchiveIncludeOnMissing {
        self.on_missing
    }
}

fn default_depth() -> TrackDefault<RecursionDepth> {
    // We use a high-but-not-infinite depth.
    TrackDefault::with_default_value(RecursionDepth::Finite(16))
}

fn default_on_missing() -> ArchiveIncludeOnMissing {
    ArchiveIncludeOnMissing::Warn
}

fn join_rel_path(a: &Utf8Path, rel: &Utf8Path) -> Utf8PathBuf {
    // This joins the subset of components that deserialize_relative_path
    // allows. We also always use "/" to ensure consistency across platforms.
    let mut out = String::from(a.to_owned());

    for component in rel.components() {
        match component {
            Utf8Component::CurDir => {}
            Utf8Component::Normal(p) => {
                out.push('/');
                out.push_str(p);
            }
            other => unreachable!(
                "found invalid component {other:?}, deserialize_relative_path should have errored"
            ),
        }
    }

    out.into()
}

/// What to do when an archive-include path is missing.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ArchiveIncludeOnMissing {
    /// Ignore and continue.
    Ignore,

    /// Warn and continue.
    Warn,

    /// Produce an error.
    Error,
}

impl<'de> Deserialize<'de> for ArchiveIncludeOnMissing {
    fn deserialize<D>(deserializer: D) -> Result<ArchiveIncludeOnMissing, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct ArchiveIncludeOnMissingVisitor;

        impl serde::de::Visitor<'_> for ArchiveIncludeOnMissingVisitor {
            type Value = ArchiveIncludeOnMissing;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("a string: \"ignore\", \"warn\", or \"error\"")
            }

            fn visit_str<E>(self, value: &str) -> Result<ArchiveIncludeOnMissing, E>
            where
                E: serde::de::Error,
            {
                match value {
                    "ignore" => Ok(ArchiveIncludeOnMissing::Ignore),
                    "warn" => Ok(ArchiveIncludeOnMissing::Warn),
                    "error" => Ok(ArchiveIncludeOnMissing::Error),
                    _ => Err(serde::de::Error::invalid_value(
                        Unexpected::Str(value),
                        &self,
                    )),
                }
            }
        }

        deserializer.deserialize_any(ArchiveIncludeOnMissingVisitor)
    }
}

/// Defines the base of the path
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum ArchiveRelativeTo {
    /// Path starts at the target directory
    Target,
    // TODO: add support for profile relative
    //TargetProfile,
}

/// Recursion depth.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum RecursionDepth {
    /// A specific depth.
    Finite(usize),

    /// Infinite recursion.
    Infinite,
}

impl RecursionDepth {
    pub(crate) const ZERO: RecursionDepth = RecursionDepth::Finite(0);

    pub(crate) fn is_zero(self) -> bool {
        self == Self::ZERO
    }

    pub(crate) fn decrement(self) -> Self {
        match self {
            Self::ZERO => panic!("attempted to decrement zero"),
            Self::Finite(n) => Self::Finite(n - 1),
            Self::Infinite => Self::Infinite,
        }
    }

    pub(crate) fn unwrap_finite(self) -> usize {
        match self {
            Self::Finite(n) => n,
            Self::Infinite => panic!("expected finite recursion depth"),
        }
    }
}

impl fmt::Display for RecursionDepth {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Finite(n) => write!(f, "{n}"),
            Self::Infinite => write!(f, "infinite"),
        }
    }
}

impl<'de> Deserialize<'de> for RecursionDepth {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct RecursionDepthVisitor;

        impl serde::de::Visitor<'_> for RecursionDepthVisitor {
            type Value = RecursionDepth;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("a non-negative integer or \"infinite\"")
            }

            // TOML uses i64, not u64
            fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                if value < 0 {
                    return Err(serde::de::Error::invalid_value(
                        Unexpected::Signed(value),
                        &self,
                    ));
                }
                Ok(RecursionDepth::Finite(value as usize))
            }

            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                match value {
                    "infinite" => Ok(RecursionDepth::Infinite),
                    _ => Err(serde::de::Error::invalid_value(
                        Unexpected::Str(value),
                        &self,
                    )),
                }
            }
        }

        deserializer.deserialize_any(RecursionDepthVisitor)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        config::{
            test_helpers::{build_platforms, temp_workspace},
            NextestConfig,
        },
        errors::ConfigParseErrorKind,
    };
    use camino::Utf8Path;
    use camino_tempfile::tempdir;
    use config::ConfigError;
    use indoc::indoc;
    use test_case::test_case;

    #[test]
    fn parse_valid() {
        let config_contents = indoc! {r#"
            [profile.default.archive]
            include = [
                { path = "foo", relative-to = "target" },
                { path = "bar", relative-to = "target", depth = 1, on-missing = "error" },
            ]

            [profile.profile1]
            archive.include = [
                { path = "baz", relative-to = "target", depth = 0, on-missing = "ignore" },
            ]

            [profile.profile2]
            archive.include = []

            [profile.profile3]
        "#};

        let workspace_dir = tempdir().unwrap();

        let graph = temp_workspace(workspace_dir.path(), config_contents);

        let config = NextestConfig::from_sources(
            graph.workspace().root(),
            &graph,
            None,
            [],
            &Default::default(),
        )
        .expect("config is valid");

        let default_config = ArchiveConfig {
            include: vec![
                ArchiveInclude {
                    path: "foo".into(),
                    relative_to: ArchiveRelativeTo::Target,
                    depth: default_depth(),
                    on_missing: ArchiveIncludeOnMissing::Warn,
                },
                ArchiveInclude {
                    path: "bar".into(),
                    relative_to: ArchiveRelativeTo::Target,
                    depth: TrackDefault::with_deserialized_value(RecursionDepth::Finite(1)),
                    on_missing: ArchiveIncludeOnMissing::Error,
                },
            ],
        };

        assert_eq!(
            config
                .profile("default")
                .expect("default profile exists")
                .apply_build_platforms(&build_platforms())
                .archive_config(),
            &default_config,
            "default matches"
        );

        assert_eq!(
            config
                .profile("profile1")
                .expect("profile exists")
                .apply_build_platforms(&build_platforms())
                .archive_config(),
            &ArchiveConfig {
                include: vec![ArchiveInclude {
                    path: "baz".into(),
                    relative_to: ArchiveRelativeTo::Target,
                    depth: TrackDefault::with_deserialized_value(RecursionDepth::ZERO),
                    on_missing: ArchiveIncludeOnMissing::Ignore,
                }],
            },
            "profile1 matches"
        );

        assert_eq!(
            config
                .profile("profile2")
                .expect("default profile exists")
                .apply_build_platforms(&build_platforms())
                .archive_config(),
            &ArchiveConfig { include: vec![] },
            "profile2 matches"
        );

        assert_eq!(
            config
                .profile("profile3")
                .expect("default profile exists")
                .apply_build_platforms(&build_platforms())
                .archive_config(),
            &default_config,
            "profile3 matches"
        );
    }

    #[test_case(
        indoc!{r#"
            [profile.default]
            archive.include = { path = "foo", relative-to = "target" }
        "#},
        r"invalid type: map, expected a sequence"
        ; "missing list")]
    #[test_case(
        indoc!{r#"
            [profile.default]
            archive.include = [
                { path = "foo" }
            ]
        "#},
        r"missing field `relative-to`"
        ; "missing relative-to")]
    #[test_case(
        indoc!{r#"
            [profile.default]
            archive.include = [
                { path = "bar", relative-to = "unknown" }
            ]
        "#},
        r"enum ArchiveRelativeTo does not have variant constructor unknown"
        ; "invalid relative-to")]
    #[test_case(
        indoc!{r#"
            [profile.default]
            archive.include = [
                { path = "bar", relative-to = "target", depth = -1 }
            ]
        "#},
        r#"invalid value: integer `-1`, expected a non-negative integer or "infinite""#
        ; "negative depth")]
    #[test_case(
        indoc!{r#"
            [profile.default]
            archive.include = [
                { path = "foo/../bar", relative-to = "target" }
            ]
        "#},
        r#"invalid value: string "foo/../bar", expected a relative path with no parent components"#
        ; "parent component")]
    #[test_case(
        indoc!{r#"
            [profile.default]
            archive.include = [
                { path = "/foo/bar", relative-to = "target" }
            ]
        "#},
        r#"invalid value: string "/foo/bar", expected a relative path with no parent components"#
        ; "absolute path")]
    #[test_case(
        indoc!{r#"
            [profile.default]
            archive.include = [
                { path = "foo", relative-to = "target", on-missing = "unknown" }
            ]
        "#},
        r#"invalid value: string "unknown", expected a string: "ignore", "warn", or "error""#
        ; "invalid on-missing")]
    #[test_case(
        indoc!{r#"
            [profile.default]
            archive.include = [
                { path = "foo", relative-to = "target", on-missing = 42 }
            ]
        "#},
        r#"invalid type: integer `42`, expected a string: "ignore", "warn", or "error""#
        ; "invalid on-missing type")]
    fn parse_invalid(config_contents: &str, expected_message: &str) {
        let workspace_dir = tempdir().unwrap();
        let workspace_path: &Utf8Path = workspace_dir.path();

        let graph = temp_workspace(workspace_path, config_contents);

        let config_err = NextestConfig::from_sources(
            graph.workspace().root(),
            &graph,
            None,
            [],
            &Default::default(),
        )
        .expect_err("config expected to be invalid");

        let message = match config_err.kind() {
            ConfigParseErrorKind::DeserializeError(path_error) => match path_error.inner() {
                ConfigError::Message(message) => message,
                other => {
                    panic!("for config error {config_err:?}, expected ConfigError::Message for inner error {other:?}");
                }
            },
            other => {
                panic!(
                    "for config error {other:?}, expected ConfigParseErrorKind::DeserializeError"
                );
            }
        };

        assert!(
            message.contains(expected_message),
            "expected message: {expected_message}\nactual message: {message}"
        );
    }

    #[test]
    fn test_join_rel_path() {
        let inputs = [
            ("a", "b", "a/b"),
            ("a", "b/c", "a/b/c"),
            ("a", "", "a"),
            ("a", ".", "a"),
        ];

        for (base, rel, expected) in inputs {
            assert_eq!(
                join_rel_path(Utf8Path::new(base), Utf8Path::new(rel)),
                Utf8Path::new(expected),
                "actual matches expected -- base: {base}, rel: {rel}"
            );
        }
    }
}