#compdef cargo-nextest

# cargo-nextest is a cargo subcommand: the binary expects `nextest` (or the
# `ntr` shortcut) as its first argument, then a command. This file completes the
# `cargo-nextest` binary directly; `cargo nextest …` is handled by _cargo.

_cargo-nextest() {
  local curcontext="$curcontext" state line
  typeset -A opt_args

  # Option groups shared across the build-and-run commands. Declared local;
  # zsh's dynamic scope makes them visible to _cargo_nextest_cmd below.
  local -a _nx_global _nx_config _nx_build _nx_filter _nx_runner _nx_reporter _nx_reuse

  _nx_global=(
    '--color=[Produce color output]:when:(auto always never)'
    '--no-pager[Do not pipe output through a pager]'
    '(-v --verbose)'{-v,--verbose}'[Verbose output]'
    '(-h --help)'{-h,--help}'[Print help]'
  )

  _nx_config=(
    '--config-file=[Config file]:file:_files'
    '--user-config-file=[User config file]:file:_files'
    '*--tool-config-file=[Tool-specific config file (tool:abs_path)]:tool\:path:'
    '--override-version-check[Override nextest minimum-version checks]'
    '(-P --profile)'{-P+,--profile=}'[The nextest profile to use]:profile:'
  )

  _nx_build=(
    '*'{-p+,--package=}'[Package to test]:package:'
    '--workspace[Test all packages in the workspace]'
    '*--exclude=[Exclude packages from the test]:package:'
    '--all[Alias for --workspace (deprecated)]'
    '--lib[Test only this package'\''s library]'
    '*--bin=[Test only the specified binary]:bin:'
    '--bins[Test all binaries]'
    '*--example=[Test only the specified example]:example:'
    '--examples[Test all examples]'
    '*--test=[Test only the specified test target]:test:'
    '--tests[Test all test targets]'
    '*--bench=[Test only the specified bench target]:bench:'
    '--benches[Test all bench targets]'
    '--all-targets[Test all targets]'
    '*'{-F+,--features=}'[Space or comma separated list of features]:features:'
    '--all-features[Activate all available features]'
    '--no-default-features[Do not activate the default feature]'
    '--build-jobs=[Number of build jobs to run]:jobs:'
    '(-r --release)'{-r,--release}'[Build artifacts in release mode]'
    '--cargo-profile=[Build artifacts with the specified Cargo profile]:profile:'
    '--target=[Build for the target triple]:triple:'
    '--target-dir=[Directory for all generated artifacts]:dir:_directories'
    '--unit-graph[Output the unit graph in JSON]'
    '--timings=-[Output build timing information]::fmts:'
    '--frozen[Require Cargo.lock and cache are up to date]'
    '--locked[Require Cargo.lock is up to date]'
    '--offline[Run without accessing the network]'
    '--cargo-message-format=[Cargo message format]:fmt:(human short json json-diagnostic-short json-diagnostic-rendered-ansi json-render-diagnostics)'
    '*--cargo-quiet[Do not print cargo log messages]'
    '*--cargo-verbose[Use cargo verbose output]'
    '--ignore-rust-version[Ignore rust-version specification in packages]'
    '--future-incompat-report[Output a future-incompat report]'
    '*--config=[Override a Cargo configuration value]:key=value:'
    '*-Z+[Unstable (nightly-only) cargo flags]:flag:'
  )

  _nx_filter=(
    '*'{-E+,--filterset=}'[Filterset expression to match tests]:expr:'
    '--run-ignored=[Run ignored tests]:which:(default only all)'
    '--partition=[Partition tests into buckets]:partition:'
    '--platform-filter=[Filter tests by build platform]:platform:'
    '--ignore-default-filter[Ignore the default filter configured in profiles]'
  )

  _nx_runner=(
    '(-R --rerun)'{-R+,--rerun=}'[Rerun tests from a previous run]:run id or recording:'
    '--no-run[Compile but do not run tests]'
    '(-j --test-threads)'{-j+,--test-threads=}'[Number of tests to run simultaneously]:n:'
    '--retries=[Number of retries for failing tests]:n:'
    '--flaky-result=[How to treat flaky tests]:result:(pass fail)'
    '--fail-fast[Cancel the run on the first failure]'
    '--no-fail-fast[Run all tests regardless of failure]'
    '--max-fail=[Number of failures before cancelling the run]:max failures:'
    '--debugger=[Run tests under a debugger]:debugger:'
    '--tracer=[Run tests under a tracer]:tracer:'
    '--no-tests=[Behavior when no tests are found]:action:(auto pass warn fail)'
    '--no-capture[Run tests serially and do not capture output]'
    '--stress-count=[Repeat the run a fixed number of times]:count:'
    '--stress-duration=[Repeat the run for a duration]:duration:'
  )

  _nx_reporter=(
    '--failure-output=[When to display output for failed tests]:when:(immediate immediate-final final never)'
    '--success-output=[When to display output for passed tests]:when:(immediate immediate-final final never)'
    '--status-level=[Status level of tests to display]:level:(none fail retry slow leak pass skip all)'
    '--final-status-level=[Status level at the end of the run]:level:(none fail flaky slow skip pass all)'
    '--no-output-indent[Do not indent captured test output]'
    '--show-progress=[How to display run progress]:show:(auto none bar counter only)'
    '--hide-progress-bar[Hide the progress bar]'
    '--no-input-handler[Disable the interactive input handler]'
    '--max-progress-running=[Max number of running tests in the progress bar]:n:'
    '--message-format=[Output format for machine-readable results]:format:(human libtest-json libtest-json-plus)'
    '--message-format-version=[Format version for machine-readable output]:version:'
  )

  _nx_reuse=(
    '--archive-file=[Archive file to extract and run from]:file:_files'
    '--archive-format=[Archive format]:format:(auto tar-zst)'
    '--extract-to=[Destination directory to extract the archive to]:dir:_directories'
    '--extract-overwrite[Overwrite existing contents in the extract destination]'
    '--persist-extract-tempdir[Persist the temporary directory used for extraction]'
    '--cargo-metadata=[Path to cargo metadata JSON]:file:_files'
    '--workspace-remap=[Remap the workspace root]:dir:_directories'
    '--binaries-metadata=[Path to binaries-metadata JSON]:file:_files'
    '--target-dir-remap=[Remap the target directory]:dir:_directories'
    '--build-dir-remap=[Remap the build directory]:dir:_directories'
  )

  _arguments -C \
    '1:nextest subcommand:(( nextest\:"run the nextest test runner" ntr\:"shortcut for nextest run" ))' \
    '*::args:->args'

  case $state in
    args)
      case $line[1] in
        nextest) _cargo_nextest_cmd ;;
        ntr)     _arguments "${_nx_global[@]}" "${_nx_config[@]}" \
                   "${_nx_build[@]}" "${_nx_filter[@]}" "${_nx_runner[@]}" \
                   "${_nx_reporter[@]}" "${_nx_reuse[@]}" '*::test filter:' ;;
      esac
      ;;
  esac
}

_cargo_nextest_cmd() {
  local curcontext="$curcontext" state line
  typeset -A opt_args

  _arguments -C \
    "${_nx_global[@]}" \
    '1:command:->command' \
    '*::args:->cmdargs'

  case $state in
    command)
      local -a commands=(
        'list:List tests in workspace'
        'run:Build and run tests'
        'bench:Build and run benchmarks (experimental)'
        'archive:Build and archive tests'
        'show-config:Show nextest configuration in this workspace'
        'self:Manage the nextest installation'
        'help:Print help for a subcommand'
      )
      _describe -t commands 'cargo nextest command' commands
      ;;
    cmdargs)
      case $line[1] in
        list)
          _arguments "${_nx_global[@]}" "${_nx_config[@]}" "${_nx_build[@]}" \
            "${_nx_filter[@]}" "${_nx_reuse[@]}" \
            '(-T --message-format)'{-T+,--message-format=}'[Output format]:format:(auto human oneline json json-pretty)' \
            '--list-type=[Type of listing]:type:(full binaries-only)' \
            '*::test filter:'
          ;;
        run|r)
          _arguments "${_nx_global[@]}" "${_nx_config[@]}" "${_nx_build[@]}" \
            "${_nx_filter[@]}" "${_nx_runner[@]}" "${_nx_reporter[@]}" \
            "${_nx_reuse[@]}" '*::test filter:'
          ;;
        bench|b)
          _arguments "${_nx_global[@]}" "${_nx_config[@]}" "${_nx_build[@]}" \
            "${_nx_filter[@]}" "${_nx_runner[@]}" "${_nx_reporter[@]}" \
            '*::benchmark filter:'
          ;;
        archive)
          _arguments "${_nx_global[@]}" "${_nx_config[@]}" "${_nx_build[@]}" \
            '*'{-E+,--filterset=}'[Filterset expression]:expr:' \
            '--archive-file=[Path to archive to create]:file:_files' \
            '--archive-format=[Archive format]:format:(auto tar-zst)' \
            '--zstd-level=[Zstandard compression level]:level:'
          ;;
        show-config)
          _arguments "${_nx_global[@]}" \
            '1:show-config command:(( version\:"Show version-related configuration" test-groups\:"Show defined test groups" ))'
          ;;
        self)
          _arguments "${_nx_global[@]}" \
            '1:self command:(( schema\:"Print an embedded JSON Schema" update\:"Update nextest (disabled in this build)" ))'
          ;;
      esac
      ;;
  esac
}

_cargo-nextest "$@"
