Skip to content

Commit

Permalink
fix: handle multiple include/exclude paths with newlines
Browse files Browse the repository at this point in the history
When multiple paths are specified using the `|` operator in YAML, the resulting string contains newlines.
When this string is passed to `globs=$(eval "echo ${file_paths}")` bash is trying to execute paths as commend and fails with the error:

```sh
/action/functions.sh: line 215: tests/**: No such file or directory
```

We can avoid this error by replacing newlines with spaces from the paths.
  • Loading branch information
jamacku committed Apr 13, 2023
1 parent fcc8ee6 commit 224f422
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/differential-shellcheck.yml
Expand Up @@ -39,7 +39,9 @@ jobs:
uses: ./
with:
shell-scripts: .github/.differential-shellcheck-scripts.txt
exclude-path: test/**
exclude-path: |
test/**
src/**.{zsh,osh}
token: ${{ secrets.GITHUB_TOKEN }}

- if: ${{ always() }}
Expand Down
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
@@ -1,5 +1,9 @@
# Changelog

## v4.2.1

* Handle multiple include/exclude paths with newlines

## v4.2.0

* New option `exclude-path`. Allows to specify list of paths excluded from ShellCheck scanning. It supports globbing and brace expansion. e.g. `test/{test1,test2}/**`
Expand Down
8 changes: 6 additions & 2 deletions src/functions.sh
Expand Up @@ -208,10 +208,14 @@ is_directory () {
is_matched_by_path () {
[[ $# -le 1 ]] && return 1
local file="$1"
local file_paths="$2"

# When multiple paths are provided they might be separated by space and/or newline, lets replace all newlines with spaces in order to avoid issues with glob pattern matching in eval
# /action/functions.sh: line 215: tests/**: No such file or directory
local file_paths=""
file_paths=$(tr '\r\n' ' ' <<< "$2")

set -f
globs=$(eval "echo ${file_paths-""}")
globs=$(eval "echo ${file_paths}")

for pattern in ${globs}; do
# shellcheck disable=SC2053
Expand Down
13 changes: 13 additions & 0 deletions test/is_matched_by_path.bats
Expand Up @@ -61,6 +61,19 @@ setup () {
assert_success
}

@test "is_matched_by_path() - matching - multiline input" {
source "${PROJECT_ROOT}/src/functions.sh"

INPUT_EXCLUDE_PATH="test/{**,*,}
test/**
1.sh"

run is_matched_by_path "test/1.sh" "${INPUT_EXCLUDE_PATH}"
assert_success
run is_matched_by_path "1.sh" "${INPUT_EXCLUDE_PATH}"
assert_success
}

@test "is_matched_by_path() - bad number of arguments" {
source "${PROJECT_ROOT}/src/functions.sh"

Expand Down

0 comments on commit 224f422

Please sign in to comment.