scoop-uv 0.13.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
//! Bash shell integration

use crate::{file_resolution_check, scoop_version_check};

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

# 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!(bash),
        file_resolution_check!(bash),
        r#"
}

# Set up PROMPT_COMMAND for auto-activate
if [[ -z "$SCOOP_NO_AUTO" ]]; then
    if [[ -z "$PROMPT_COMMAND" ]]; then
        PROMPT_COMMAND="_scoop_hook"
    else
        PROMPT_COMMAND="_scoop_hook;$PROMPT_COMMAND"
    fi
fi

# Run hook on startup
_scoop_hook

# Bash completion for scoop
_scoop_complete() {
    local cur cmd i
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"

    # Get subcommand (COMP_WORDS[1])
    cmd=""
    if [[ ${COMP_CWORD} -ge 1 ]]; then
        cmd="${COMP_WORDS[1]}"
    fi

    # First argument: complete subcommands
    if [[ ${COMP_CWORD} -eq 1 ]]; then
        COMPREPLY=($(compgen -W "list use create remove info install uninstall doctor init completions activate deactivate shell migrate lang" -- "$cur"))
        return
    fi

    # Option completion (starts with -)
    if [[ "$cur" == -* ]]; then
        case "$cmd" in
            list)
                # Special-case `--sort` value completion: when the user
                # has typed `--sort=` or `--sort <TAB>`, offer the
                # enum values directly instead of the option list.
                local prev_word="${COMP_WORDS[COMP_CWORD-1]:-}"
                if [[ "$cur" == --sort=* ]]; then
                    local val="${cur#--sort=}"
                    COMPREPLY=($(compgen -W "name created last-used" -P "--sort=" -- "$val"))
                    return 0
                fi
                if [[ "$prev_word" == "--sort" ]]; then
                    COMPREPLY=($(compgen -W "name created last-used" -- "$cur"))
                    return 0
                fi
                local opts="--pythons --sort --json -q --quiet --no-color --help"
                for word in "${COMP_WORDS[@]}"; do
                    case "$word" in
                        --pythons) opts="${opts//--pythons }" ;;
                        --sort|--sort=*) opts="${opts//--sort }" ;;
                        --json) opts="${opts//--json }" ;;
                        -q|--quiet) opts="${opts//-q }"; opts="${opts//--quiet }" ;;
                        --no-color) opts="${opts//--no-color }" ;;
                    esac
                done
                COMPREPLY=($(compgen -W "$opts" -- "$cur"))
                ;;
            doctor)
                local opts="-v --verbose -q --quiet --json --no-color --help"
                # Filter out already used options
                for word in "${COMP_WORDS[@]}"; do
                    case "$word" in
                        -v|--verbose) opts="${opts//-v }"; opts="${opts//--verbose }" ;;
                        -q|--quiet) opts="${opts//-q }"; opts="${opts//--quiet }" ;;
                        --json) opts="${opts//--json }" ;;
                        --no-color) opts="${opts//--no-color }" ;;
                    esac
                done
                COMPREPLY=($(compgen -W "$opts" -- "$cur"))
                ;;
            create)
                local opts="--force -q --quiet --no-color --help"
                for word in "${COMP_WORDS[@]}"; do
                    case "$word" in
                        --force) opts="${opts//--force }" ;;
                        -q|--quiet) opts="${opts//-q }"; opts="${opts//--quiet }" ;;
                        --no-color) opts="${opts//--no-color }" ;;
                    esac
                done
                COMPREPLY=($(compgen -W "$opts" -- "$cur"))
                ;;
            use)
                local opts="--unset --link --global --no-link -q --quiet --no-color --help"
                for word in "${COMP_WORDS[@]}"; do
                    case "$word" in
                        --unset) opts="${opts//--unset }" ;;
                        --global) opts="${opts//--global }" ;;
                        --link|--no-link) opts="${opts//--link }"; opts="${opts//--no-link }" ;;
                        -q|--quiet) opts="${opts//-q }"; opts="${opts//--quiet }" ;;
                        --no-color) opts="${opts//--no-color }" ;;
                    esac
                done
                COMPREPLY=($(compgen -W "$opts" -- "$cur"))
                ;;
            remove)
                local opts="--force -q --quiet --no-color --help"
                for word in "${COMP_WORDS[@]}"; do
                    case "$word" in
                        --force) opts="${opts//--force }" ;;
                        -q|--quiet) opts="${opts//-q }"; opts="${opts//--quiet }" ;;
                        --no-color) opts="${opts//--no-color }" ;;
                    esac
                done
                COMPREPLY=($(compgen -W "$opts" -- "$cur"))
                ;;
            install)
                local opts="--latest --stable -q --quiet --no-color --help"
                for word in "${COMP_WORDS[@]}"; do
                    case "$word" in
                        --latest|--stable) opts="${opts//--latest }"; opts="${opts//--stable }" ;;
                        -q|--quiet) opts="${opts//-q }"; opts="${opts//--quiet }" ;;
                        --no-color) opts="${opts//--no-color }" ;;
                    esac
                done
                COMPREPLY=($(compgen -W "$opts" -- "$cur"))
                ;;
            uninstall)
                local opts="-q --quiet --no-color --help"
                for word in "${COMP_WORDS[@]}"; do
                    case "$word" in
                        -q|--quiet) opts="${opts//-q }"; opts="${opts//--quiet }" ;;
                        --no-color) opts="${opts//--no-color }" ;;
                    esac
                done
                COMPREPLY=($(compgen -W "$opts" -- "$cur"))
                ;;
            info)
                local opts="--json --all-packages --no-size -q --quiet --no-color --help"
                for word in "${COMP_WORDS[@]}"; do
                    case "$word" in
                        --json) opts="${opts//--json }" ;;
                        --all-packages) opts="${opts//--all-packages }" ;;
                        --no-size) opts="${opts//--no-size }" ;;
                        -q|--quiet) opts="${opts//-q }"; opts="${opts//--quiet }" ;;
                        --no-color) opts="${opts//--no-color }" ;;
                    esac
                done
                COMPREPLY=($(compgen -W "$opts" -- "$cur"))
                ;;
            init|completions)
                COMPREPLY=($(compgen -W "--help" -- "$cur"))
                ;;
            lang)
                local opts="--list --reset --json --help"
                for word in "${COMP_WORDS[@]}"; do
                    case "$word" in
                        --list) opts="${opts//--list }" ;;
                        --reset) opts="${opts//--reset }" ;;
                        --json) opts="${opts//--json }" ;;
                    esac
                done
                COMPREPLY=($(compgen -W "$opts" -- "$cur"))
                ;;
            shell)
                local opts="--unset -q --quiet --no-color --help"
                for word in "${COMP_WORDS[@]}"; do
                    case "$word" in
                        --unset) opts="${opts//--unset }" ;;
                        -q|--quiet) opts="${opts//-q }"; opts="${opts//--quiet }" ;;
                        --no-color) opts="${opts//--no-color }" ;;
                    esac
                done
                COMPREPLY=($(compgen -W "$opts" -- "$cur"))
                ;;
            migrate)
                local opts="--help"
                COMPREPLY=($(compgen -W "$opts" -- "$cur"))
                ;;
        esac
        return
    fi

    # Argument completion (by subcommand)
    case "$cmd" in
        use|remove|info|activate|shell)
            # Check if env name already provided
            local has_arg=false
            for ((i=2; i<COMP_CWORD; i++)); do
                if [[ "${COMP_WORDS[i]}" != -* ]]; then
                    has_arg=true
                    break
                fi
            done
            if [[ "$has_arg" == false ]]; then
                COMPREPLY=($(compgen -W "$(command scoop list --bare 2>/dev/null)" -- "$cur"))
            fi
            ;;
        uninstall)
            COMPREPLY=($(compgen -W "$(command scoop list --pythons --bare 2>/dev/null | sort -u)" -- "$cur"))
            ;;
        init|completions)
            COMPREPLY=($(compgen -W "bash zsh fish powershell" -- "$cur"))
            ;;
        create)
            # First arg: name, second arg: python version
            local arg_count=0
            for ((i=2; i<COMP_CWORD; i++)); do
                if [[ "${COMP_WORDS[i]}" != -* ]]; then
                    ((arg_count++))
                fi
            done
            if [[ $arg_count -eq 1 ]]; then
                # Second positional arg: python version
                COMPREPLY=($(compgen -W "$(command scoop list --pythons --bare 2>/dev/null | sort -u)" -- "$cur"))
            fi
            ;;
        lang)
            # Complete language codes
            COMPREPLY=($(compgen -W "en ko ja pt-BR" -- "$cur"))
            ;;
        migrate)
            # Complete migrate subcommands
            COMPREPLY=($(compgen -W "list all @env" -- "$cur"))
            ;;
    esac
}
complete -o nosort -F _scoop_complete scoop
"#
    )
}

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

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

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

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

        match output {
            Ok(result) => {
                assert!(
                    result.status.success(),
                    "Bash script has syntax errors:\n{}",
                    String::from_utf8_lossy(&result.stderr)
                );
            }
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                // bash not available, skip test
                eprintln!("Skipping bash syntax test: bash not found");
            }
            Err(e) => panic!("Failed to run bash: {}", 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_complete()"];

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

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

        // PROMPT_COMMAND must be set for auto-activation
        assert!(
            script.contains("PROMPT_COMMAND"),
            "Script must register PROMPT_COMMAND for auto-activation"
        );
    }

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

        // Must register completion function
        assert!(
            script.contains("complete -o nosort -F _scoop_complete scoop"),
            "Script must register bash completion"
        );
    }

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

    /// Validates that the generated script follows shell best practices.
    /// shellcheck catches common issues like:
    /// - Quoting problems (SC2086)
    /// - Useless use of cat (SC2002)
    /// - Deprecated syntax
    #[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();

        let output = std::process::Command::new("shellcheck")
            .arg("--shell=bash")
            .arg("--severity=warning") // Only warnings and above
            // SC2207: COMPREPLY=($(compgen ...)) is standard bash completion idiom
            .arg("--exclude=SC2207")
            .arg(temp_file.path())
            .output();

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