Skip to content

Commit 0c34333

Browse files
committed
add unit tests
1 parent b39360e commit 0c34333

File tree

10 files changed

+418
-25
lines changed

10 files changed

+418
-25
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*Issue #, if available:*
2+
3+
*Description of changes:*
4+
5+
6+
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

.github/dependabot.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
2+
version: 2
3+
updates:
4+
- package-ecosystem: github-actions
5+
directory: /
6+
schedule:
7+
interval: weekly
8+
day: monday
9+
time: "10:00"
10+
commit-message:
11+
prefix: fix
12+
prefix-development: chore
13+
include: scope
14+
labels:
15+
- dependencies
16+
- gha
17+
- package-ecosystem: maven
18+
directory: /
19+
schedule:
20+
interval: weekly
21+
day: monday
22+
time: "10:00"
23+
commit-message:
24+
prefix: fix
25+
prefix-development: chore
26+
include: scope
27+
labels:
28+
- dependencies
29+
- mvn

.github/stale.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Configuration for probot-stale - https://github.com/probot/stale
2+
3+
# Number of days of inactivity before an issue becomes stale
4+
daysUntilStale: 120
5+
6+
# Number of days of inactivity before a stale issue is closed
7+
daysUntilClose: 14
8+
9+
# Issues with these labels will never be considered stale
10+
exemptLabels:
11+
- enhancement
12+
- pinned
13+
- security
14+
15+
# Label to use when marking an issue as stale
16+
staleLabel: wontfix
17+
18+
# Comment to post when marking an issue as stale. Set to `false` to disable
19+
markComment: >
20+
This issue has been automatically marked as stale because it has not had
21+
recent activity. It will be closed in 7 days if no further activity occurs.
22+
If the issue is still valid, please add a respective comment to prevent this
23+
issue from being closed automatically. Thank you for your contributions.
24+
25+
# Comment to post when closing a stale issue. Set to `false` to disable
26+
closeComment: false

.github/workflows/build.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions
2+
name: Build
3+
4+
on:
5+
push:
6+
branches-ignore: # build all branches except:
7+
- 'dependabot/**' # prevent GHA triggered twice (once for commit to the branch and once for opening/syncing the PR)
8+
tags-ignore: # don't build tags
9+
- '**'
10+
paths-ignore:
11+
- '**/*.adoc'
12+
- '**/*.md'
13+
- '.editorconfig'
14+
- '.git*'
15+
- '.github/*.yml'
16+
pull_request:
17+
workflow_dispatch:
18+
# https://github.blog/changelog/2020-07-06-github-actions-manual-triggers-with-workflow_dispatch/
19+
20+
defaults:
21+
run:
22+
shell: cmd
23+
24+
jobs:
25+
build:
26+
runs-on: windows-latest
27+
28+
steps:
29+
- name: Show environment variables
30+
run: set
31+
32+
- name: Git Checkout
33+
uses: actions/checkout@v3 # https://github.com/actions/checkout
34+
35+
- name: Run tests
36+
run: call tests/test_all.cmd

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# batch-functions
22

3+
[![Build Status](https://github.com/sebthom/batch-functions/workflows/Build/badge.svg "GitHub Actions")](https://github.com/sebthom/batch-functions/actions?query=workflow%3A%22Build%22)
34
[![License](https://img.shields.io/github/license/sebthom/batch-functions.svg?color=blue)](LICENSE.txt)
45
[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-v2.1%20adopted-ff69b4.svg)](CODE_OF_CONDUCT.md)
56

modules/args.cmd

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,26 @@ goto :eof
99

1010
:has_arg <SEARCH_FOR> <ARG,...>
1111
setlocal
12-
set search_for=%~1
13-
for %%a in (ignoreFirstArg%*) do (
14-
if "%%~a"=="%search_for%" exit /B 0
15-
)
16-
exit /B 1
12+
set "search_for=%~1" & shift /1
13+
set empty_args=0
14+
15+
REM not using "for %%a in (%*)" which automatically expands wildcard arguments
16+
:has_arg___CHECK_NEXT_ARG
17+
set "arg=%~1"
18+
if "%arg%" == "%search_for%" exit /B 0
19+
if "%arg%" == "" (
20+
REM stop looping if more than 6 empty args in a row were found. this is a workaround for the fact that one cannot
21+
REM distinguish between an empty "" argument and the end of the argument list
22+
if %empty_args% == 6 (
23+
exit /B 1
24+
) else (
25+
set /a empty_args+=1
26+
)
27+
) else (
28+
set empty_args=0
29+
)
30+
shift /1
31+
goto :has_arg___CHECK_NEXT_ARG
1732

1833

1934
:get_1st_positional_arg <RESULT_VAR> <ARG,...>
@@ -28,21 +43,22 @@ goto :eof
2843

2944
:get_nth_positional_arg <ARG_INDEX> <RESULT_VAR> <ARG,...>
3045
setlocal EnableDelayedExpansion
31-
set wanted_pos_arg_index=%~1
32-
set result_var=%~2
33-
set current_pos_arg_index=-2
34-
35-
for %%a in (%*) do (
36-
set a=%%~a
37-
set first_char=!a:~0,1!
38-
if not "!first_char!" == "-" (
39-
if not "!first_char!" == "/" (
40-
set /A current_pos_arg_index=!current_pos_arg_index!+1
41-
if !current_pos_arg_index! equ %wanted_pos_arg_index% (
42-
endlocal & set "%result_var%=%%a"
43-
exit /B 0
44-
)
46+
set "wanted_pos_arg_index=%~1" & shift /1
47+
set "result_var=%~1" & shift /1
48+
set current_pos_arg_index=0
49+
50+
REM not using "for %%a in (%*)" which automatically expands wildcard arguments
51+
:get_nth_positional_arg___CHECK_NEXT_ARG
52+
set "arg=%~1"
53+
set first_char=%arg:~0,1%
54+
if not "%first_char%" == "-" (
55+
if not "%first_char%" == "/" (
56+
set /A current_pos_arg_index=%current_pos_arg_index%+1
57+
if !current_pos_arg_index! equ %wanted_pos_arg_index% (
58+
endlocal & set "%result_var%=%arg%"
59+
exit /B 0
60+
)
4561
)
4662
)
47-
)
48-
goto :eof
63+
shift /1
64+
goto :get_nth_positional_arg___CHECK_NEXT_ARG

modules/strings.cmd

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,25 @@ goto :eof
88

99

1010
:ends_with <SEARCH_IN> <SEARCH_FOR>
11-
echo %~1|findstr /E /L %2 >NUL
11+
set search_in=%~1
12+
set search_for=%~2
13+
if "%search_for%" == "" exit /B 0
14+
if "%search_in%" == "%search_for%" exit /B 0
15+
16+
echo %search_in%|findstr /E /L "%search_for%" >NUL
1217
goto :eof
1318

1419

1520
:has_substring <SEARCH_IN> <SEARCH_FOR>
1621
setlocal
1722
set search_in=%~1
1823
set search_for=%~2
24+
if "%search_for%" == "" exit /B 0
25+
if "%search_in%" == "%search_for%" exit /B 0
1926

20-
call :replace_substring "%search_in%" "%search_for%" "" result
27+
call :replace_substrings "%search_in%" "%search_for%" "" result
2128

22-
REM substring not found
29+
:: substring not found
2330
if "%searchIn%" == "%result%" exit /B 1
2431
goto :eof
2532

@@ -32,7 +39,13 @@ goto :eof
3239
set replace_with=%~3
3340
set result_var=%~4
3441

35-
call set result=%%search_in:%search_for%=%replace_with%%%
42+
if "%search_in%" == "" (
43+
set result=%search_in%
44+
) else if "%search_for%" == "" (
45+
set result=%search_in%
46+
) else (
47+
call set result=%%search_in:%search_for%=%replace_with%%%
48+
)
3649

3750
if not defined result_var (
3851
echo %result%
@@ -56,4 +69,5 @@ goto :eof
5669
)
5770
exit /B 0
5871
)
72+
endlocal & set "%result_var%=%search_in%"
5973
goto :eof

tests/test_all.cmd

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@echo off
2+
:: SPDX-FileContributor: Sebastian Thomschke
3+
:: SPDX-License-Identifier: CC0-1.0
4+
:: SPDX-ArtifactOfProjectHomePage: https://github.com/sebthom/batch-functions
5+
6+
for %%f in ( %~dp0test_*.cmd) do (
7+
if not "%%f" == "%~f0" (
8+
echo ###########################################
9+
echo # Executing [%%~nxf]...
10+
echo ###########################################
11+
call %%f || exit /B 1
12+
echo.
13+
)
14+
)

tests/test_args.cmd

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
@echo off
2+
:: SPDX-FileContributor: Sebastian Thomschke
3+
:: SPDX-License-Identifier: CC0-1.0
4+
:: SPDX-ArtifactOfProjectHomePage: https://github.com/sebthom/batch-functions
5+
6+
setlocal
7+
8+
pushd %~dp0..\modules\
9+
10+
set failures=0
11+
12+
::::::::::::::::::::::::::::::::
13+
:: test has_arg
14+
::::::::::::::::::::::::::::::::
15+
call :assert_exec_ok has_arg foo foo
16+
call :assert_exec_ok has_arg foo foo bar
17+
call :assert_exec_ok has_arg foo bar foo
18+
call :assert_exec_ok has_arg foo "" foo
19+
call :assert_exec_ok has_arg foo "" "" "" "" "" "" foo
20+
call :assert_exec_ok has_arg foo "foo"
21+
call :assert_exec_ok has_arg "foo" foo
22+
call :assert_exec_ok has_arg * foo *
23+
call :assert_exec_ok has_arg * foo "*"
24+
call :assert_exec_nok has_arg foo
25+
call :assert_exec_nok has_arg foo bar
26+
call :assert_exec_nok has_arg *
27+
28+
29+
::::::::::::::::::::::::::::::::
30+
:: test get_1st_positional_arg
31+
::::::::::::::::::::::::::::::::
32+
call :exec get_1st_positional_arg OUTPUT foo bar
33+
call :assert_OUTPUT_equals foo
34+
35+
call :exec get_1st_positional_arg OUTPUT * foo bar
36+
call :assert_OUTPUT_equals *
37+
38+
call :exec get_1st_positional_arg OUTPUT "foo foo" "bar bar"
39+
call :assert_OUTPUT_equals "foo foo"
40+
41+
call :exec get_1st_positional_arg OUTPUT /a "foo foo" -b "bar bar"
42+
call :assert_OUTPUT_equals "foo foo"
43+
44+
call :exec get_1st_positional_arg OUTPUT
45+
call :assert_OUTPUT_equals ""
46+
47+
call :exec get_1st_positional_arg OUTPUT "" foo bar
48+
call :assert_OUTPUT_equals ""
49+
50+
51+
::::::::::::::::::::::::::::::::
52+
:: test get_2nd_positional_arg
53+
::::::::::::::::::::::::::::::::
54+
call :exec get_2nd_positional_arg OUTPUT foo bar
55+
call :assert_OUTPUT_equals bar
56+
57+
call :exec get_2nd_positional_arg OUTPUT foo * bar
58+
call :assert_OUTPUT_equals *
59+
60+
call :exec get_2nd_positional_arg OUTPUT /a "foo foo" -b "bar bar"
61+
call :assert_OUTPUT_equals "bar bar"
62+
63+
call :exec get_2nd_positional_arg OUTPUT
64+
call :assert_OUTPUT_equals ""
65+
66+
call :exec get_2nd_positional_arg OUTPUT foo "" bar
67+
call :assert_OUTPUT_equals ""
68+
69+
70+
::::::::::::::::::::::::::::::::
71+
:: test get_nth_positional_arg
72+
::::::::::::::::::::::::::::::::
73+
call :exec get_nth_positional_arg 5 OUTPUT arg1 arg2 arg3 arg4 arg5 arg6
74+
call :assert_OUTPUT_equals arg5
75+
76+
call :exec get_nth_positional_arg 5 OUTPUT arg1 arg2 arg3 arg4 * arg6
77+
call :assert_OUTPUT_equals *
78+
79+
call :exec get_nth_positional_arg 5 OUTPUT /a /b /c -d "" "" "" "arg4 arg4" /e /f -g "arg5 arg5" "arg6 arg6"
80+
call :assert_OUTPUT_equals "arg5 arg5"
81+
82+
call :exec get_nth_positional_arg 5 OUTPUT
83+
call :assert_OUTPUT_equals ""
84+
85+
call :exec get_nth_positional_arg 5 OUTPUT arg1 arg2 arg3 arg4 "" arg6
86+
call :assert_OUTPUT_equals ""
87+
88+
popd
89+
90+
exit /b %failures%
91+
92+
93+
::::::::::::::::::::::::::::::::
94+
:: utilities
95+
::::::::::::::::::::::::::::::::
96+
:exec
97+
echo Testing: args.cmd %*
98+
call args.cmd %*
99+
goto :eof
100+
101+
102+
:assert_exec_ok
103+
echo Testing: args.cmd %*
104+
call args.cmd %* || (
105+
echo -^> FAILED
106+
set /a failures+=1
107+
exit /b 1
108+
)
109+
echo -^> OK
110+
goto :eof
111+
112+
113+
:assert_exec_nok
114+
echo Testing: args.cmd %*
115+
call args.cmd %* && (
116+
set /a failures+=1
117+
echo -^> FAILED
118+
exit /b 1
119+
)
120+
echo -^> OK
121+
goto :eof
122+
123+
:assert_OUTPUT_equals
124+
if not "%OUTPUT%" == "%~1" (
125+
echo -^> FAILED (OUTPUT is not "%~1" but "%OUTPUT%"^)
126+
set /a failures+=1
127+
exit /b 1
128+
)
129+
echo -^> OK (OUTPUT == "%OUTPUT%"^)
130+
goto :eof
131+
132+
133+
:assert_OUTPUT_not_equals
134+
if "%OUTPUT%" == "%~1" (
135+
echo -^> FAILED (OUTPUT == "%~1" but should not be^)
136+
set /a failures+=1
137+
exit /b 1
138+
)
139+
echo -^> OK (OUTPUT != "%OUTPUT%"^)
140+
goto :eof

0 commit comments

Comments
 (0)