Skip to content

Commit 39e2d9d

Browse files
author
Lucas Yoon
committed
modularizing parsing arguments for tests
Signed-off-by: Lucas Yoon <lyoon@lyoon-thinkpadp1gen7.boston.csb>
1 parent 004f49c commit 39e2d9d

File tree

5 files changed

+66
-121
lines changed

5 files changed

+66
-121
lines changed

tests/check_non_terminating.sh

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,8 @@ set -o errexit
55
# Source shared utilities
66
source "$(dirname "$0")/get_paths.sh"
77

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
32-
33-
# Restore positional parameters
34-
restore_positional_args POSITIONAL_ARGS
8+
# Parse all arguments
9+
parse_arguments "$@"
3510

3611
# Set defaults for stack arguments
3712
set_stack_defaults

tests/check_odov3.sh

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,8 @@ set -x
55
# Source shared utilities
66
source "$(dirname "$0")/get_paths.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
33-
34-
# Restore positional parameters
35-
restore_positional_args POSITIONAL_ARGS
8+
# Parse all arguments
9+
parse_arguments "$@"
3610

3711
# Set defaults for stack arguments
3812
set_stack_defaults

tests/common_args.sh

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

tests/get_paths.sh

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,52 @@
11
#!/usr/bin/env bash
22

3-
# Shared utilities for devfile registry test scripts
4-
# Usage: source this file and call set_stack_defaults() after parsing arguments
5-
63
# Common variables used by all scripts
74
stackDirs=''
85
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+
stackDirs=$2
14+
shift # past argument
15+
shift
16+
;;
17+
--stacksPath)
18+
stacksPath=$2
19+
shift # past argument
20+
shift
21+
;;
22+
-*|--*)
23+
# Try script-specific handler if it exists
24+
local consumed=0
25+
if declare -f handle_additional_args > /dev/null 2>&1; then
26+
handle_additional_args "$@"
27+
consumed=$?
28+
fi
29+
30+
if [ $consumed -gt 0 ]; then
31+
# Script handler consumed some arguments
32+
for ((i=0; i<consumed; i++)); do
33+
shift
34+
done
35+
else
36+
echo "Unknown option $1"
37+
exit 1
38+
fi
39+
;;
40+
*)
41+
POSITIONAL_ARGS+=("$1") # save positional arg
42+
shift # past argument
43+
;;
44+
esac
45+
done
46+
47+
# Restore positional parameters
48+
restore_positional_args POSITIONAL_ARGS
49+
}
950

1051
# Function to set default values for stack arguments
1152
set_stack_defaults() {

tests/validate_devfile_schemas.sh

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,27 @@
33
# Source shared utilities
44
source "$(dirname "$0")/get_paths.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
40-
41-
# Restore positional parameters
42-
restore_positional_args POSITIONAL_ARGS
9+
handle_additional_args() {
10+
case $1 in
11+
-s|--samples)
12+
SAMPLES="true"
13+
return 1
14+
;;
15+
-v|--verbose)
16+
VERBOSE="true"
17+
return 1
18+
;;
19+
*)
20+
return 0
21+
;;
22+
esac
23+
}
24+
25+
# Parse all arguments
26+
parse_arguments "$@"
4327

4428
set -x
4529

0 commit comments

Comments
 (0)