scoop-uv 0.9.0

Scoop up your Python envs — pyenv-style workflow powered by uv
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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//! Zsh shell integration

use crate::{file_resolution_check, scoop_version_check};

/// Generate zsh initialization script
pub fn init_script() -> &'static str {
    concat!(
        r#"# scoop shell integration for zsh

# Disable completion sorting for scoop (preserves command order)
zstyle ':completion:*:scoop:*' sort false

# Wrapper function for scoop
scoop() {
    local command="${1:-}"

    case "$command" in
        use)
            command scoop "$@"
            local ret=$?
            if [[ $ret -eq 0 ]]; then
                shift  # remove 'use'
                local name=""
                for arg in "$@"; do
                    case "$arg" in
                        -*) ;;  # skip options
                        *) name="$arg"; break ;;
                    esac
                done
                if [[ -n "$name" ]]; then
                    eval "$(command scoop activate "$name")"
                fi
            fi
            return $ret
            ;;
        activate|deactivate|shell)
            # Pass through help/version flags without eval
            if [[ "$*" == *--help* ]] || [[ "$*" == *-h* ]] || [[ "$*" == *--version* ]] || [[ "$*" == *-V* ]]; then
                command scoop "$@"
            else
                eval "$(command scoop "$@")"
            fi
            ;;
        *)
            command scoop "$@"
            ;;
    esac
}

# Auto-activate hook
_scoop_hook() {
"#,
        scoop_version_check!(zsh),
        file_resolution_check!(zsh),
        r#"
}

# Set up chpwd hook for auto-activate
if [[ -z "$SCOOP_NO_AUTO" ]]; then
    autoload -Uz add-zsh-hook
    add-zsh-hook chpwd _scoop_hook
fi

# Run hook on startup
_scoop_hook

# Zsh completion for scoop
_scoop() {
    local curcontext="$curcontext" state line
    typeset -A opt_args
    local cur="${words[$CURRENT]}"

    _arguments -C \
        '1: :->command' \
        '*: :->args'

    case $state in
        command)
            local commands=(
                'list:List all virtual environments'
                'use:Set local environment for current directory'
                'create:Create a new virtual environment'
                'remove:Remove a virtual environment'
                'info:Show detailed information about a virtual environment'
                'install:Install a Python version'
                'uninstall:Uninstall a Python version'
                'doctor:Diagnose installation issues'
                'init:Output shell initialization script'
                'completions:Output shell completion script'
                'activate:Activate a virtual environment'
                'deactivate:Deactivate current virtual environment'
                'shell:Set shell-specific environment'
                'migrate:Migrate environments from other tools'
                'lang:Set or show language preference'
            )
            _describe -V 'command' commands
            ;;
        args)
            case $line[1] in
                use)
                    if [[ $cur == -* ]]; then
                        local opts=('--help:Show help')
                        local has_unset=false has_global=false has_link=false has_quiet=false has_nocolor=false
                        for w in "${words[@]}"; do
                            case "$w" in
                                --unset) has_unset=true ;;
                                --global) has_global=true ;;
                                --link|--no-link) has_link=true ;;
                                -q|--quiet) has_quiet=true ;;
                                --no-color) has_nocolor=true ;;
                            esac
                        done
                        [[ $has_unset == false ]] && opts+=('--unset:Remove version setting')
                        [[ $has_link == false ]] && opts+=('--link:Create .venv symlink' '--no-link:Do not create .venv symlink')
                        [[ $has_global == false ]] && opts+=('--global:Set as global default')
                        [[ $has_quiet == false ]] && opts+=('-q:Suppress all output' '--quiet:Suppress all output')
                        [[ $has_nocolor == false ]] && opts+=('--no-color:Disable colored output')
                        _describe 'option' opts
                    else
                        # Check if env name already provided (exclude current word being typed)
                        local has_env=false
                        local prev_args=("${words[@]:2:$((CURRENT-3))}")
                        for w in "${prev_args[@]}"; do
                            [[ $w != -* && -n $w ]] && has_env=true && break
                        done
                        if [[ $has_env == false ]]; then
                            local envs=(${(f)"$(command scoop list --bare 2>/dev/null)"})
                            compadd -a envs
                        fi
                    fi
                    ;;
                remove)
                    if [[ $cur == -* ]]; then
                        local opts=('--help:Show help')
                        local has_force=false has_quiet=false has_nocolor=false
                        for w in "${words[@]}"; do
                            case "$w" in
                                --force) has_force=true ;;
                                -q|--quiet) has_quiet=true ;;
                                --no-color) has_nocolor=true ;;
                            esac
                        done
                        [[ $has_force == false ]] && opts+=('--force:Skip confirmation')
                        [[ $has_quiet == false ]] && opts+=('-q:Suppress all output' '--quiet:Suppress all output')
                        [[ $has_nocolor == false ]] && opts+=('--no-color:Disable colored output')
                        _describe 'option' opts
                    else
                        # Check if env name already provided (exclude current word being typed)
                        local has_env=false
                        local prev_args=("${words[@]:2:$((CURRENT-3))}")
                        for w in "${prev_args[@]}"; do
                            [[ $w != -* && -n $w ]] && has_env=true && break
                        done
                        if [[ $has_env == false ]]; then
                            local envs=(${(f)"$(command scoop list --bare 2>/dev/null)"})
                            compadd -a envs
                        fi
                    fi
                    ;;
                info)
                    if [[ $cur == -* ]]; then
                        local opts=('--help:Show help')
                        local has_json=false has_allpackages=false has_nosize=false has_quiet=false has_nocolor=false
                        for w in "${words[@]}"; do
                            case "$w" in
                                --json) has_json=true ;;
                                --all-packages) has_allpackages=true ;;
                                --no-size) has_nosize=true ;;
                                -q|--quiet) has_quiet=true ;;
                                --no-color) has_nocolor=true ;;
                            esac
                        done
                        [[ $has_json == false ]] && opts+=('--json:Output as JSON')
                        [[ $has_allpackages == false ]] && opts+=('--all-packages:Show all installed packages')
                        [[ $has_nosize == false ]] && opts+=('--no-size:Skip directory size calculation')
                        [[ $has_quiet == false ]] && opts+=('-q:Suppress all output' '--quiet:Suppress all output')
                        [[ $has_nocolor == false ]] && opts+=('--no-color:Disable colored output')
                        _describe 'option' opts
                    else
                        # Check if env name already provided (exclude current word being typed)
                        local has_env=false
                        local prev_args=("${words[@]:2:$((CURRENT-3))}")
                        for w in "${prev_args[@]}"; do
                            [[ $w != -* && -n $w ]] && has_env=true && break
                        done
                        if [[ $has_env == false ]]; then
                            local envs=(${(f)"$(command scoop list --bare 2>/dev/null)"})
                            compadd -a envs
                        fi
                    fi
                    ;;
                activate)
                    # Check if env name already provided (exclude current word being typed)
                    local has_env=false
                    local prev_args=("${words[@]:2:$((CURRENT-3))}")
                    for w in "${prev_args[@]}"; do
                        [[ $w != -* && -n $w ]] && has_env=true && break
                    done
                    if [[ $has_env == false ]]; then
                        local envs=(${(f)"$(command scoop list --bare 2>/dev/null)"})
                        compadd -a envs
                    fi
                    ;;
                install)
                    if [[ $cur == -* ]]; then
                        local opts=('--help:Show help')
                        local has_version_opt=false has_quiet=false has_nocolor=false
                        for w in "${words[@]}"; do
                            case "$w" in
                                --latest|--stable) has_version_opt=true ;;
                                -q|--quiet) has_quiet=true ;;
                                --no-color) has_nocolor=true ;;
                            esac
                        done
                        [[ $has_version_opt == false ]] && opts+=('--latest:Install latest stable Python' '--stable:Install oldest fully-supported Python')
                        [[ $has_quiet == false ]] && opts+=('-q:Suppress all output' '--quiet:Suppress all output')
                        [[ $has_nocolor == false ]] && opts+=('--no-color:Disable colored output')
                        _describe 'option' opts
                    fi
                    ;;
                uninstall)
                    if [[ $cur == -* ]]; then
                        local opts=('--help:Show help')
                        local has_quiet=false has_nocolor=false
                        for w in "${words[@]}"; do
                            case "$w" in
                                -q|--quiet) has_quiet=true ;;
                                --no-color) has_nocolor=true ;;
                            esac
                        done
                        [[ $has_quiet == false ]] && opts+=('-q:Suppress all output' '--quiet:Suppress all output')
                        [[ $has_nocolor == false ]] && opts+=('--no-color:Disable colored output')
                        _describe 'option' opts
                    else
                        # Check if version already provided (exclude current word being typed)
                        local has_ver=false
                        local prev_args=("${words[@]:2:$((CURRENT-3))}")
                        for w in "${prev_args[@]}"; do
                            [[ $w != -* && -n $w ]] && has_ver=true && break
                        done
                        if [[ $has_ver == false ]]; then
                            local pythons=(${(uf)"$(command scoop list --pythons --bare 2>/dev/null)"})
                            compadd -a pythons
                        fi
                    fi
                    ;;
                list)
                    if [[ $cur == -* ]]; then
                        local opts=('--help:Show help')
                        local has_pythons=false has_json=false has_quiet=false has_nocolor=false
                        for w in "${words[@]}"; do
                            case "$w" in
                                --pythons) has_pythons=true ;;
                                --json) has_json=true ;;
                                -q|--quiet) has_quiet=true ;;
                                --no-color) has_nocolor=true ;;
                            esac
                        done
                        [[ $has_pythons == false ]] && opts+=('--pythons:Show installed Python versions')
                        [[ $has_json == false ]] && opts+=('--json:Output as JSON')
                        [[ $has_quiet == false ]] && opts+=('-q:Suppress all output' '--quiet:Suppress all output')
                        [[ $has_nocolor == false ]] && opts+=('--no-color:Disable colored output')
                        _describe 'option' opts
                    fi
                    ;;
                doctor)
                    if [[ $cur == -* ]]; then
                        local opts=('--help:Show help')
                        # Check which options are already used
                        local has_verbose=false has_quiet=false has_json=false has_nocolor=false
                        for w in "${words[@]}"; do
                            case "$w" in
                                -v|--verbose) has_verbose=true ;;
                                -q|--quiet) has_quiet=true ;;
                                --json) has_json=true ;;
                                --no-color) has_nocolor=true ;;
                            esac
                        done
                        # Add only unused options
                        [[ $has_verbose == false ]] && opts+=('-v:Increase verbosity' '--verbose:Increase verbosity')
                        [[ $has_quiet == false ]] && opts+=('-q:Suppress all output' '--quiet:Suppress all output')
                        [[ $has_json == false ]] && opts+=('--json:Output as JSON')
                        [[ $has_nocolor == false ]] && opts+=('--no-color:Disable colored output')
                        _describe 'option' opts
                    fi
                    ;;
                create)
                    if [[ $cur == -* ]]; then
                        local opts=('--help:Show help')
                        local has_force=false has_quiet=false has_nocolor=false
                        for w in "${words[@]}"; do
                            case "$w" in
                                --force) has_force=true ;;
                                -q|--quiet) has_quiet=true ;;
                                --no-color) has_nocolor=true ;;
                            esac
                        done
                        [[ $has_force == false ]] && opts+=('--force:Overwrite existing environment')
                        [[ $has_quiet == false ]] && opts+=('-q:Suppress all output' '--quiet:Suppress all output')
                        [[ $has_nocolor == false ]] && opts+=('--no-color:Disable colored output')
                        _describe 'option' opts
                    else
                        # Count positional args before current word
                        local pos_count=0
                        local prev_args=("${words[@]:2:$((CURRENT-3))}")
                        for w in "${prev_args[@]}"; do
                            [[ $w != -* && -n $w ]] && ((pos_count++))
                        done
                        if [[ $pos_count -eq 1 ]]; then
                            # Second positional arg: python version
                            local pythons=(${(uf)"$(command scoop list --pythons --bare 2>/dev/null)"})
                            compadd -a pythons
                        fi
                    fi
                    ;;
                init|completions)
                    local shells=('bash:Bash shell' 'zsh:Zsh shell' 'fish:Fish shell' 'powershell:PowerShell')
                    _describe 'shell' shells
                    ;;
                lang)
                    if [[ $cur == -* ]]; then
                        local opts=('--help:Show help')
                        local has_list=false has_reset=false has_json=false
                        for w in "${words[@]}"; do
                            case "$w" in
                                --list) has_list=true ;;
                                --reset) has_reset=true ;;
                                --json) has_json=true ;;
                            esac
                        done
                        [[ $has_list == false ]] && opts+=('--list:List supported languages')
                        [[ $has_reset == false ]] && opts+=('--reset:Reset to system default')
                        [[ $has_json == false ]] && opts+=('--json:Output as JSON')
                        _describe 'option' opts
                    else
                        local langs=('en:English' 'ko:Korean' 'ja:Japanese' 'pt-BR:Portuguese (Brazilian)')
                        _describe 'language' langs
                    fi
                    ;;
                shell)
                    if [[ $cur == -* ]]; then
                        local opts=('--help:Show help')
                        local has_unset=false has_quiet=false has_nocolor=false
                        for w in "${words[@]}"; do
                            case "$w" in
                                --unset) has_unset=true ;;
                                -q|--quiet) has_quiet=true ;;
                                --no-color) has_nocolor=true ;;
                            esac
                        done
                        [[ $has_unset == false ]] && opts+=('--unset:Clear shell-specific environment')
                        [[ $has_quiet == false ]] && opts+=('-q:Suppress all output' '--quiet:Suppress all output')
                        [[ $has_nocolor == false ]] && opts+=('--no-color:Disable colored output')
                        _describe 'option' opts
                    else
                        # Check if env name already provided (exclude current word being typed)
                        local has_env=false
                        local prev_args=("${words[@]:2:$((CURRENT-3))}")
                        for w in "${prev_args[@]}"; do
                            [[ $w != -* && -n $w ]] && has_env=true && break
                        done
                        if [[ $has_env == false ]]; then
                            local envs=(${(f)"$(command scoop list --bare 2>/dev/null)"})
                            compadd -a envs
                        fi
                    fi
                    ;;
                migrate)
                    if [[ $cur == -* ]]; then
                        local opts=('--help:Show help')
                        _describe 'option' opts
                    else
                        local subcmds=('list:List environments available for migration' 'all:Migrate all environments' '@env:Migrate a specific environment')
                        _describe 'subcommand' subcmds
                    fi
                    ;;
            esac
            ;;
    esac
}

# Register completion only if compdef is available (requires compinit)
(( $+functions[compdef] )) && compdef _scoop scoop
"#
    )
}

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

    // =========================================================================
    // Real Test: Syntax Validation with zsh -n
    // =========================================================================

    /// Validates that the generated script has valid zsh syntax.
    /// This is a REAL test - it actually runs zsh to check the script.
    #[test]
    #[cfg(unix)]
    fn test_init_script_has_valid_zsh_syntax() {
        let script = init_script();

        // Use zsh -n for syntax checking (parse only, don't execute)
        let output = std::process::Command::new("zsh")
            .arg("-n") // syntax check only
            .arg("-c")
            .arg(script)
            .output();

        match output {
            Ok(result) => {
                assert!(
                    result.status.success(),
                    "Zsh script has syntax errors:\n{}",
                    String::from_utf8_lossy(&result.stderr)
                );
            }
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                // zsh not available, skip test
                eprintln!("Skipping zsh syntax test: zsh not found");
            }
            Err(e) => panic!("Failed to run zsh: {}", e),
        }
    }

    // =========================================================================
    // Structural Tests: Minimal checks for required components
    // =========================================================================

    #[test]
    fn test_init_script_not_empty() {
        let script = init_script();
        assert!(!script.is_empty(), "Script should not be empty");
    }

    #[test]
    fn test_init_script_defines_required_functions() {
        let script = init_script();

        // These functions MUST exist for the shell integration to work
        let required_functions = ["scoop()", "_scoop_hook()", "_scoop()"];

        for func in required_functions {
            assert!(
                script.contains(func),
                "Script missing required function: {}",
                func
            );
        }
    }

    #[test]
    fn test_init_script_registers_chpwd_hook() {
        let script = init_script();

        // zsh uses chpwd hook for directory change detection
        assert!(
            script.contains("add-zsh-hook chpwd _scoop_hook"),
            "Script must register chpwd hook for auto-activation"
        );
    }

    #[test]
    fn test_init_script_registers_completion() {
        let script = init_script();

        // Must register completion function with compdef
        assert!(
            script.contains("compdef _scoop scoop"),
            "Script must register zsh completion"
        );
    }

    #[test]
    fn test_init_script_loads_zsh_hooks_module() {
        let script = init_script();

        // Must autoload zsh hooks module
        assert!(
            script.contains("autoload -Uz add-zsh-hook"),
            "Script must load zsh hooks module"
        );
    }

    // =========================================================================
    // Real Test: Best Practices Validation with shellcheck
    // =========================================================================

    /// Validates that the generated script follows shell best practices.
    /// Note: shellcheck uses bash mode for zsh (no native zsh support),
    /// but catches most common issues.
    #[test]
    #[cfg(unix)]
    fn test_init_script_passes_shellcheck() {
        let script = init_script();

        // Write script to temp file (shellcheck requires file input)
        let temp_file = tempfile::NamedTempFile::new().unwrap();
        std::fs::write(temp_file.path(), script).unwrap();

        // shellcheck doesn't have native zsh support, but bash mode catches most issues
        // Use --exclude for zsh-specific constructs that bash doesn't understand
        let output = std::process::Command::new("shellcheck")
            .arg("--shell=bash")
            .arg("--severity=warning")
            // Exclude zsh-specific constructs:
            // SC1087: $line[1] array syntax (zsh doesn't require braces)
            // SC2034: Unused variable (zsh uses typeset -A)
            // SC2154: Variable referenced but not assigned (zsh completion vars)
            // SC2168: 'local' outside function (zsh completion context)
            // SC2206: Quote to prevent word splitting (zsh handles this differently)
            // SC2207: COMPREPLY=($(compgen ...)) is standard completion idiom
            // SC2296: ${(f)...} parameter expansion flags (zsh-specific)
            // SC3030: Array syntax (zsh-specific)
            // SC3057: Associative array syntax (zsh-specific)
            .arg("--exclude=SC1087,SC2034,SC2154,SC2168,SC2206,SC2207,SC2296,SC3030,SC3057")
            .arg(temp_file.path())
            .output();

        match output {
            Ok(result) => {
                assert!(
                    result.status.success(),
                    "shellcheck found issues in zsh script:\n{}",
                    String::from_utf8_lossy(&result.stdout)
                );
            }
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                eprintln!(
                    "Skipping shellcheck test: shellcheck not found (install: brew install shellcheck)"
                );
            }
            Err(e) => panic!("Failed to run shellcheck: {}", e),
        }
    }
}