Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed May 29, 2024
1 parent 1f67f2f commit f8d8866
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 11 deletions.
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Each of these examples demonstrates one aspect or feature of argc.
- [multiline.sh](./multiline.sh) - how to use multiline help text.
- [nested-commands](./nested-commands.sh) - how to use nested commands.
- [hooks.sh](./hooks.sh) - how to use argc hooks.
- [strict.sh](./strict.sh) - how to use strict mode
- [parallel.sh](./parallel.sh) - how to use `--argc-parallel`.

- [args.sh](./args.sh) - all kinds of `@arg`.
Expand Down
25 changes: 25 additions & 0 deletions examples/strict.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

set -eu


# @flag --fa
# @option --oa
# @option --of*, multi-occurs + comma-separated list
# @option --oda=a default
# @option --oca[a|b] choice
# @option --ofa[`_choice_fn`] choice from fn
# @option --oxa~ capture all remaining args

main() {
( set -o posix ; set ) | grep ^argc_
echo "${argc__fn:-}" "$@"
}

_choice_fn() {
echo abc
echo def
echo ghi
}

eval "$(argc --argc-eval "$0" "$@")"
4 changes: 2 additions & 2 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const UTIL_FNS: [(&str, &str); 6] = [
_argc_take_args() {
_argc_take_args_values=()
_argc_take_args_len=0
local param="${1:-}" min="${2:-}" max="${3:-}" signs="${4:-}" delimiter="${5:-}"
local param="$1" min="$2" max="$3" signs="$4" delimiter="$5"
if [[ "$min" -eq 0 ]] && [[ "$max" -eq 0 ]]; then
return
fi
Expand Down Expand Up @@ -140,7 +140,7 @@ _argc_require_params() {
r#"
_argc_validate_choices() {
local render_name="$1" raw_choices="$2" choices item choice concated_choices=""
IFS=$'\n' read -d '' -r -a choices <<<"$raw_choices"
IFS=$'\n' read -r -a choices <<<"$raw_choices"
for choice in "${choices[@]}"; do
if [[ -z "$concated_choices" ]]; then
concated_choices="$choice"
Expand Down
25 changes: 20 additions & 5 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,34 @@ fn build_stdout() {
}

#[test]
fn build_outpath() {
let path = locate_script("examples/demo.sh");
fn run_build() {
let path = locate_script("examples/strict.sh");
let tmpdir = tmpdir();
let outpath = tmpdir.join("demo.sh");
let outpath = tmpdir.join("strict.sh");
Command::cargo_bin("argc")
.unwrap()
.arg("--argc-build")
.arg(&path)
.arg(&outpath)
.assert()
.success();
let script = std::fs::read_to_string(&outpath).unwrap();
assert!(script.contains("# ARGC-BUILD"));

Command::cargo_bin("argc")
.unwrap()
.arg("--argc-run")
.arg(&outpath)
.args([
"--fa",
"--oa",
"oa1",
"--of=of1,of2",
"--oca=a",
"--ofa",
"abc",
])
.assert()
.stdout(predicates::str::contains("argc__fn=main"))
.success();
}

#[test]
Expand Down
1 change: 0 additions & 1 deletion tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ pub fn run_script<T: AsRef<Path>>(
let envs: HashMap<&str, &str> = envs.iter().cloned().collect();
let shell_path = argc::NativeRuntime.shell_path().unwrap();
let output = std::process::Command::new(shell_path)
.arg("-u")
.arg(script_path.as_ref())
.args(args)
.env("PATH", path_env_var.clone())
Expand Down
6 changes: 3 additions & 3 deletions tests/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ fn choice_access_vars() {
# @flag --fa
# @arg val[`_choice_fn`]
_choice_fn() {
if [[ ${argc_fa:-} == 1 ]]; then
if [[ $argc_fa == 1 ]]; then
echo abc
else
echo def
Expand All @@ -266,10 +266,10 @@ fn choice_slash() {
# @arg foo
# @arg bar[`_choice_fn`]
cmd() {
echo ${1:-}
echo $1
}
_choice_fn() {
echo ${1:-}
echo $1
}
"###;
snapshot_multi!(script, [vec!["prog", "cmd", "a\\b", "a\\b"],]);
Expand Down

0 comments on commit f8d8866

Please sign in to comment.