scoop-uv 0.15.1

Scoop up your Python envs with uv — the command is scuv
Documentation
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
//! `.scuv.toml` project manifest — opt-in declarative env definition
//! consumed by `scuv sync`.
//!
//! Resolution walks cwd → parents; within a single directory `.scuv.toml`
//! wins over a legacy `.scoop.toml` (deprecated; emits a one-shot warning).
//! A legacy file in a *nearer* directory still beats a new-named file in a
//! parent directory — nearest-directory-first is unchanged by the rename.
//!
//! DEPRECATION(0.16.0): remove the legacy branch.
//!
//! The schema is intentionally minimal for v1:
//!
//! ```toml
//! [environment]
//! name = "myproject"
//! python = "3.12"
//!
//! [packages]
//! default = ["pytest", "black", "mypy"]
//! dev = ["ipython", "debugpy"]
//! ```
//!
//! `[hooks]` and a `python_path` field are intentionally out of scope until a
//! separate threat-model / interpreter-override design lands.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use serde::Deserialize;

use crate::error::{Result, ScoopError};

/// Project manifest filename — searched cwd→parent like `.scuv-version`.
pub const MANIFEST_FILE: &str = ".scuv.toml";
/// DEPRECATION(0.16.0): remove the legacy manifest-filename fallback.
pub const LEGACY_MANIFEST_FILE: &str = ".scoop.toml";

/// Parsed `.scuv.toml`. Fields are validated at parse time so callers receive
/// a struct that's already meaningful (`name` passes `is_valid_env_name`,
/// `python` is non-empty).
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct ScoopManifest {
    pub environment: Environment,
    #[serde(default)]
    pub packages: Packages,
}

#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct Environment {
    pub name: String,
    pub python: String,
}

/// Package groups. `default` is always installed by `scuv sync`; any extra
/// keys become opt-in groups selected via `--with <group>`.
#[derive(Debug, Clone, Default, Deserialize, PartialEq, Eq)]
pub struct Packages {
    #[serde(default)]
    pub default: Vec<String>,
    /// Arbitrary named groups (e.g. `dev`, `docs`). Captured via `flatten` so
    /// the TOML shape stays one nested table instead of `[packages.groups]`.
    #[serde(flatten)]
    pub groups: BTreeMap<String, Vec<String>>,
}

impl ScoopManifest {
    /// Parse a `.scuv.toml` document from a string and validate.
    pub fn parse(content: &str) -> Result<Self> {
        let manifest: ScoopManifest =
            toml::from_str(content).map_err(|e| ScoopError::InvalidArgument {
                message: format!("{MANIFEST_FILE}: {}", e.message()),
            })?;
        manifest.validate()?;
        Ok(manifest)
    }

    /// Load and parse from a file path.
    pub fn load(path: &Path) -> Result<Self> {
        let content = std::fs::read_to_string(path)?;
        Self::parse(&content)
    }

    /// Returns the deduplicated package list for `default` plus the named
    /// `extra_groups`, preserving first-seen order so users can predict pin
    /// resolution. Unknown group names are silently ignored — the sync
    /// command is responsible for surfacing them to the user before calling
    /// in (see [`Self::group`]).
    pub fn packages_for(&self, extra_groups: &[String]) -> Vec<String> {
        let mut all = self.packages.default.clone();
        for g in extra_groups {
            if let Some(pkgs) = self.packages.groups.get(g) {
                all.extend(pkgs.iter().cloned());
            }
        }
        let mut seen = std::collections::HashSet::new();
        all.into_iter().filter(|p| seen.insert(p.clone())).collect()
    }

    /// Look up a group by name. Returns the `default` list when asked for
    /// `"default"` so callers can validate `--with default` without special
    /// casing.
    pub fn group(&self, name: &str) -> Option<&Vec<String>> {
        if name == "default" {
            return Some(&self.packages.default);
        }
        self.packages.groups.get(name)
    }

    fn validate(&self) -> Result<()> {
        if !crate::validate::is_valid_env_name(&self.environment.name) {
            return Err(ScoopError::InvalidEnvName {
                name: self.environment.name.clone(),
                reason: format!("invalid name in {MANIFEST_FILE} [environment]"),
            });
        }
        if self.environment.python.trim().is_empty() {
            return Err(ScoopError::InvalidPythonVersion {
                version: self.environment.python.clone(),
            });
        }
        Ok(())
    }
}

/// Walk from `start` up to filesystem root looking for `.scuv.toml`, falling
/// back to the legacy `.scoop.toml` name per directory (deprecated; emits a
/// one-shot warning). Mirrors the version-file resolution model so users get
/// the same mental model for "this directory is configured" detection.
///
/// DEPRECATION(0.16.0): remove the legacy fallback branch.
pub fn find_manifest(start: &Path) -> Option<PathBuf> {
    let mut current = start.to_path_buf();
    loop {
        let candidate = current.join(MANIFEST_FILE);
        if candidate.is_file() {
            return Some(candidate);
        }
        let legacy = current.join(LEGACY_MANIFEST_FILE);
        if legacy.is_file() {
            crate::output::deprecation::warn_once(&rust_i18n::t!("deprecation.manifest_file"));
            return Some(legacy);
        }
        if !current.pop() {
            return None;
        }
    }
}

/// Convenience: find from the current working directory.
pub fn find_manifest_from_cwd() -> Option<PathBuf> {
    std::env::current_dir().ok().and_then(|d| find_manifest(&d))
}

#[cfg(test)]
mod tests {
    use super::*;
    use rstest::rstest;
    use tempfile::TempDir;

    // ==========================================================================
    // Parse — valid documents
    // ==========================================================================

    #[rstest]
    #[case::minimal(
        r#"
        [environment]
        name = "myproject"
        python = "3.12"
        "#,
        "myproject",
        "3.12",
        0
    )]
    #[case::with_default_packages(
        r#"
        [environment]
        name = "webapp"
        python = "3.11"

        [packages]
        default = ["fastapi", "uvicorn"]
        "#,
        "webapp",
        "3.11",
        2
    )]
    #[case::with_extra_groups(
        r#"
        [environment]
        name = "ds"
        python = "3.12"

        [packages]
        default = ["numpy"]
        dev = ["ipython", "pytest"]
        docs = ["mkdocs"]
        "#,
        "ds",
        "3.12",
        1
    )]
    fn parse_valid(
        #[case] toml_src: &str,
        #[case] expected_name: &str,
        #[case] expected_python: &str,
        #[case] expected_default_count: usize,
    ) {
        let m = ScoopManifest::parse(toml_src).expect("valid manifest");
        assert_eq!(m.environment.name, expected_name);
        assert_eq!(m.environment.python, expected_python);
        assert_eq!(m.packages.default.len(), expected_default_count);
    }

    // ==========================================================================
    // Parse — invalid documents
    // ==========================================================================

    #[test]
    fn parse_rejects_missing_environment_section() {
        let err = ScoopManifest::parse(
            r#"
            [packages]
            default = ["pytest"]
            "#,
        )
        .unwrap_err();
        assert!(
            matches!(err, ScoopError::InvalidArgument { .. }),
            "expected InvalidArgument, got {err:?}"
        );
    }

    #[test]
    fn parse_rejects_missing_python_field() {
        let err = ScoopManifest::parse(
            r#"
            [environment]
            name = "myproject"
            "#,
        )
        .unwrap_err();
        assert!(matches!(err, ScoopError::InvalidArgument { .. }));
    }

    #[test]
    fn parse_rejects_reserved_env_name() {
        // `list` is a CLI subcommand and reserved by `validate::RESERVED_NAMES`.
        let err = ScoopManifest::parse(
            r#"
            [environment]
            name = "list"
            python = "3.12"
            "#,
        )
        .unwrap_err();
        assert!(matches!(err, ScoopError::InvalidEnvName { .. }));
    }

    #[test]
    fn parse_rejects_empty_python_version() {
        let err = ScoopManifest::parse(
            r#"
            [environment]
            name = "myproject"
            python = ""
            "#,
        )
        .unwrap_err();
        assert!(matches!(err, ScoopError::InvalidPythonVersion { .. }));
    }

    #[test]
    fn parse_rejects_unknown_top_level_key() {
        // `deny_unknown_fields` on the root prevents typo'd sections from
        // silently no-op'ing (e.g. `[package]` instead of `[packages]`).
        let err = ScoopManifest::parse(
            r#"
            [environment]
            name = "myproject"
            python = "3.12"

            [hooks]
            post-create = "echo hi"
            "#,
        )
        .unwrap_err();
        assert!(matches!(err, ScoopError::InvalidArgument { .. }));
    }

    // ==========================================================================
    // Group resolution
    // ==========================================================================

    fn manifest_with_groups() -> ScoopManifest {
        ScoopManifest::parse(
            r#"
            [environment]
            name = "ds"
            python = "3.12"

            [packages]
            default = ["numpy", "pandas"]
            dev = ["ipython", "pytest"]
            docs = ["mkdocs"]
            "#,
        )
        .unwrap()
    }

    #[test]
    fn packages_for_default_only() {
        let m = manifest_with_groups();
        assert_eq!(m.packages_for(&[]), vec!["numpy", "pandas"]);
    }

    #[test]
    fn packages_for_includes_extra_group() {
        let m = manifest_with_groups();
        assert_eq!(
            m.packages_for(&["dev".to_string()]),
            vec!["numpy", "pandas", "ipython", "pytest"]
        );
    }

    #[test]
    fn packages_for_combines_multiple_groups() {
        let m = manifest_with_groups();
        let pkgs = m.packages_for(&["dev".to_string(), "docs".to_string()]);
        assert_eq!(pkgs, vec!["numpy", "pandas", "ipython", "pytest", "mkdocs"]);
    }

    #[test]
    fn packages_for_dedups_first_seen_order() {
        let m = ScoopManifest::parse(
            r#"
            [environment]
            name = "x"
            python = "3.12"

            [packages]
            default = ["pkg-a", "pkg-b"]
            extra = ["pkg-b", "pkg-c"]
            "#,
        )
        .unwrap();
        // pkg-b appears in both groups — keep the earliest position only.
        assert_eq!(
            m.packages_for(&["extra".to_string()]),
            vec!["pkg-a", "pkg-b", "pkg-c"]
        );
    }

    #[test]
    fn packages_for_silently_skips_unknown_group() {
        // Sync is responsible for surfacing unknown groups; the lookup itself
        // must not error so callers can probe.
        let m = manifest_with_groups();
        assert_eq!(
            m.packages_for(&["ghost".to_string()]),
            vec!["numpy", "pandas"]
        );
    }

    #[test]
    fn group_lookup_returns_default() {
        let m = manifest_with_groups();
        assert_eq!(
            m.group("default"),
            Some(&vec!["numpy".to_string(), "pandas".to_string()])
        );
    }

    #[test]
    fn group_lookup_returns_named_group() {
        let m = manifest_with_groups();
        assert!(m.group("dev").is_some());
        assert!(m.group("ghost").is_none());
    }

    // ==========================================================================
    // find_manifest — directory walk
    // ==========================================================================

    #[test]
    fn find_manifest_locates_in_current_dir() {
        let dir = TempDir::new().unwrap();
        std::fs::write(dir.path().join(MANIFEST_FILE), "").unwrap();
        assert_eq!(
            find_manifest(dir.path()),
            Some(dir.path().join(MANIFEST_FILE))
        );
    }

    #[test]
    fn find_manifest_walks_to_parent() {
        let root = TempDir::new().unwrap();
        let child = root.path().join("nested").join("deep");
        std::fs::create_dir_all(&child).unwrap();
        std::fs::write(root.path().join(MANIFEST_FILE), "").unwrap();

        let found = find_manifest(&child).expect("should walk up to root");
        assert_eq!(found, root.path().join(MANIFEST_FILE));
    }

    #[test]
    fn find_manifest_walks_terminate_at_root() {
        // Search a path that definitely has no .scuv.toml ancestor: the
        // system root. find_manifest must terminate (not loop forever) and
        // return either Some (if root happens to have one — unusual but legal)
        // or None.
        let _ = find_manifest(Path::new("/"));
    }

    // ==========================================================================
    // find_manifest — dual-name walk (.scuv.toml / legacy .scoop.toml)
    // ==========================================================================

    /// New name wins when both are present in the same directory.
    #[test]
    fn find_manifest_new_name_wins_over_legacy_in_same_dir() {
        let dir = TempDir::new().unwrap();
        std::fs::write(dir.path().join(MANIFEST_FILE), "").unwrap();
        std::fs::write(dir.path().join(LEGACY_MANIFEST_FILE), "").unwrap();
        assert_eq!(
            find_manifest(dir.path()),
            Some(dir.path().join(MANIFEST_FILE))
        );
    }

    /// DEPRECATION(0.16.0): legacy-shim regression test — a directory with
    /// only the legacy `.scoop.toml` name must still resolve.
    #[test]
    fn find_manifest_legacy_only_still_resolves() {
        let dir = TempDir::new().unwrap();
        std::fs::write(dir.path().join(LEGACY_MANIFEST_FILE), "").unwrap();
        assert_eq!(
            find_manifest(dir.path()),
            Some(dir.path().join(LEGACY_MANIFEST_FILE))
        );
    }
}