Skip to content

Commit c65ea41

Browse files
author
Lucas Yoon
committed
modularizing parsing arguments for tests
Signed-off-by: Lucas Yoon <[email protected]>
1 parent 004f49c commit c65ea41

File tree

6 files changed

+96
-145
lines changed

6 files changed

+96
-145
lines changed

tests/check_non_terminating.sh

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,13 @@ set -o nounset
33
set -o errexit
44

55
# Source shared utilities
6-
source "$(dirname "$0")/get_paths.sh"
7-
8-
POSITIONAL_ARGS=()
9-
10-
while [[ $# -gt 0 ]]; do
11-
case $1 in
12-
--stackDirs)
13-
stackDirs=$2
14-
shift # past argument
15-
shift
16-
;;
17-
--stacksPath)
18-
stacksPath=$2
19-
shift # past argument
20-
shift
21-
;;
22-
-*|--*)
23-
echo "Unknown option $1"
24-
exit 1
25-
;;
26-
*)
27-
POSITIONAL_ARGS+=("$1") # save positional arg
28-
shift # past argument
29-
;;
30-
esac
31-
done
6+
source "$(dirname "$0")/paths_util.sh"
7+
8+
# Parse all arguments
9+
parse_arguments "$@"
3210

3311
# Restore positional parameters
34-
restore_positional_args POSITIONAL_ARGS
12+
set -- "${POSITIONAL_ARGS[@]}"
3513

3614
# Set defaults for stack arguments
3715
set_stack_defaults

tests/check_odov3.sh

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,13 @@
33
set -x
44

55
# Source shared utilities
6-
source "$(dirname "$0")/get_paths.sh"
6+
source "$(dirname "$0")/paths_util.sh"
77

8-
POSITIONAL_ARGS=()
9-
VERBOSE="false"
10-
11-
while [[ $# -gt 0 ]]; do
12-
case $1 in
13-
--stackDirs)
14-
stackDirs=$2
15-
shift # past argument
16-
shift
17-
;;
18-
--stacksPath)
19-
stacksPath=$2
20-
shift # past argument
21-
shift
22-
;;
23-
-*|--*)
24-
echo "Unknown option $1"
25-
exit 1
26-
;;
27-
*)
28-
POSITIONAL_ARGS+=("$1") # save positional arg
29-
shift # past argument
30-
;;
31-
esac
32-
done
8+
# Parse all arguments
9+
parse_arguments "$@"
3310

3411
# Restore positional parameters
35-
restore_positional_args POSITIONAL_ARGS
12+
set -- "${POSITIONAL_ARGS[@]}"
3613

3714
# Set defaults for stack arguments
3815
set_stack_defaults

tests/common_args.sh

Lines changed: 0 additions & 29 deletions
This file was deleted.

tests/get_paths.sh

Lines changed: 0 additions & 29 deletions
This file was deleted.

tests/paths_util.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env bash
2+
3+
# Common variables used by all scripts
4+
stackDirs=''
5+
stacksPath=''
6+
POSITIONAL_ARGS=()
7+
8+
# Function to parse all arguments
9+
parse_arguments() {
10+
while [[ $# -gt 0 ]]; do
11+
case $1 in
12+
--stackDirs)
13+
if [ -z "${stackDirs}" ]; then
14+
stackDirs=$2
15+
else
16+
stackDirs="${stackDirs} $2"
17+
fi
18+
shift # past argument
19+
shift
20+
;;
21+
--stacksPath)
22+
stacksPath=$2
23+
shift # past argument
24+
shift
25+
;;
26+
-*|--*)
27+
# Try script-specific handler if it exists
28+
local consumed=0
29+
local errno=1
30+
if declare -f handle_additional_args > /dev/null 2>&1; then
31+
consumed=$(handle_additional_args "$@")
32+
errno=$?
33+
fi
34+
35+
if [ $errno -eq 0 ] && [ $consumed -gt 0 ]; then
36+
# Script handler consumed some arguments
37+
for ((i=0; i<consumed; i++)); do
38+
shift
39+
done
40+
else
41+
echo "Unknown option $1"
42+
return $errno
43+
fi
44+
;;
45+
*)
46+
POSITIONAL_ARGS+=("$1") # save positional arg
47+
shift # past argument
48+
;;
49+
esac
50+
done
51+
52+
return 0
53+
}
54+
55+
# Function to set default values for stack arguments
56+
set_stack_defaults() {
57+
if [ -z "$stackDirs" ]; then
58+
stackDirs=$(bash "$(pwd)/tests/get_stacks.sh")
59+
fi
60+
61+
if [ -z "$stacksPath" ]; then
62+
stacksPath="$(pwd)/stacks"
63+
fi
64+
}

tests/validate_devfile_schemas.sh

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,35 @@
11
#!/usr/bin/env bash
22

33
# Source shared utilities
4-
source "$(dirname "$0")/get_paths.sh"
4+
source "$(dirname "$0")/paths_util.sh"
55

6-
POSITIONAL_ARGS=()
76
SAMPLES="false"
87
VERBOSE="false"
98

10-
while [[ $# -gt 0 ]]; do
11-
case $1 in
12-
-s|--samples)
13-
SAMPLES="true"
14-
shift # past argument
15-
;;
16-
-v|--verbose)
17-
VERBOSE="true"
18-
shift # past argument
19-
;;
20-
--stackDirs)
21-
stackDirs=$2
22-
shift # past argument
23-
shift
24-
;;
25-
--stacksPath)
26-
stacksPath=$2
27-
shift # past argument
28-
shift
29-
;;
30-
-*|--*)
31-
echo "Unknown option $1"
32-
exit 1
33-
;;
34-
*)
35-
POSITIONAL_ARGS+=("$1") # save positional arg
36-
shift # past argument
37-
;;
38-
esac
39-
done
9+
handle_additional_args() {
10+
case $1 in
11+
-s|--samples)
12+
SAMPLES="true"
13+
echo 1
14+
return 0
15+
;;
16+
-v|--verbose)
17+
VERBOSE="true"
18+
echo 1
19+
return 0
20+
;;
21+
*)
22+
echo 0
23+
return 1
24+
;;
25+
esac
26+
}
27+
28+
# Parse all arguments
29+
parse_arguments "$@"
4030

4131
# Restore positional parameters
42-
restore_positional_args POSITIONAL_ARGS
32+
set -- "${POSITIONAL_ARGS[@]}"
4333

4434
set -x
4535

0 commit comments

Comments
 (0)