Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Feature/lint supported shells only #31

Merged
merged 5 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
#! /bin/bash
#! /usr/bin/env bash
# shellcheck disable=SC2155

input_paths="$1"
severity_mode="${2-style}"
severity_mode="${2}"
execution_mode="$3"
my_dir=$(pwd)
status_code="0"
invalid_files=()
scan_regex="#!.*[/ ](sh|bash|dash|ksh)$"

process_input(){
[ -n "$execution_mode" ] && my_dir="./test_data"

severity_mode="$(echo $severity_mode | tr '[:upper:]' '[:lower:]')"

if [[ "$severity_mode" != "style" && "$severity_mode" != "info" && "$severity_mode" != "warning" && "$severity_mode" != "error" ]]; then
echo "Error setting unknown severity mode. Defaulting severity mode to style."
echo "Warning: unknown severity mode. Defaulting severity mode to style."
severity_mode="style"
fi

Expand All @@ -25,9 +27,11 @@ process_input(){
scan_file "$path"
fi
done
[[ ${#invalid_files[@]} -gt 0 ]] && log_invalid_files
[ -z "$execution_mode" ] && exit $status_code
else
scan_all "$my_dir"
[[ ${#invalid_files[@]} -gt 0 ]] && log_invalid_files
[ -z "$execution_mode" ] && exit $status_code
fi
}
Expand All @@ -36,7 +40,8 @@ scan_file(){
local file_path=$1
local file=$(basename -- "$file_path")
local first_line=$(head -n 1 "$file_path")
if [[ "$first_line" == "#!"* ]]; then

if [[ "$first_line" =~ $scan_regex ]]; then
echo
echo "###############################################"
echo " Scanning $file"
Expand All @@ -50,7 +55,7 @@ scan_file(){
printf "\e[31m ERROR: ShellCheck detected issues in %s.\e[0m\n" "${file_path} 🐛"
fi
else
printf "\n\e[33m ⚠️ Warning: '%s' is not a valid shell script. Make sure shebang is on the first line.\e[0m\n" "$file_path"
invalid_files+=( $file_path )
fi
}

Expand All @@ -59,12 +64,21 @@ scan_all(){
while IFS= read -r script
do
local first_line=$(head -n 1 "$script")
if [[ "$first_line" == "#!"* ]]; then
if [[ "$first_line" =~ $scan_regex ]]; then
scan_file "$script"
else
printf "\n\e[33m ⚠️ Warning: '%s' is not scanned. If it is a shell script, make sure shebang is on the first line.\e[0m\n" "$script"
invalid_files+=( $script )
fi
done < <(find "$1" -name '*.sh' -o ! -name '*.*' -type f ! -path "$1/.git/*")
done < <(find "$1" -iname '*.sh' -o -iname '*.bash' -o -iname '*.ksh' -o ! -iname '*.*' -type f ! -path "$1/.git/*")
}

# Logging files with no extension that are not amongst the supported scripts or scripts that are supported but don't have a shebang.
log_invalid_files(){
printf "\n\e[33m ⚠️ Found %d unscanned files that could potentially be supported: \e[0m\n" "${#invalid_files[@]}"
for file in ${invalid_files[@]}; do
printf "\n\t\e[33m %s \e[0m\n" "$file"
done
printf "\n\e[33m ShellCheck only supports sh/bash/dash/ksh scripts. For supported scripts to be scanned, make sure to add a proper shebang on the first line of the script.\e[0m\n"
}

# To avoid execution when sourcing this script for testing
Expand Down
1 change: 1 addition & 0 deletions test_data/script_type/sample.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#!/bin/bash
3 changes: 3 additions & 0 deletions test_data/script_type/test.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/zsh

echo "hello"
3 changes: 3 additions & 0 deletions test_data/script_type/test_python
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/python3

print("hello, world!")
4 changes: 4 additions & 0 deletions test_data/script_type/test_script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// hello.js
function hello(string) {
return 'hello ' + string;
}
Empty file.
1 change: 1 addition & 0 deletions test_data/script_type/test_zsh_wsh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#!/bin/zsh
1 change: 0 additions & 1 deletion test_data/test.txt

This file was deleted.

1 change: 1 addition & 0 deletions test_data/test_dir/executable_script
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#! /bin/bash
File renamed without changes.
1 change: 0 additions & 1 deletion test_data/test_dir_2/test_script_1.sh

This file was deleted.

1 change: 0 additions & 1 deletion test_data/test_dir_2/test_script_2.sh

This file was deleted.

122 changes: 122 additions & 0 deletions tests/integration_tests/input_path_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#! /bin/bash
# shellcheck disable=SC2155

source ./entrypoint.sh "" "" "--test"

test_execution_mode(){
local expected_path=./test_data
process_input > /dev/null
local actual_path=$my_dir

assertEquals "ERROR: Values did not match." "$expected_path" "$actual_path"
}

test_invalid_script_with_extension(){
input_paths="./test_data/script_type/test_script.js"
local expected1="Found 1 unscanned files that could potentially be supported"
local expected2="ShellCheck only supports sh/bash/dash/ksh scripts. For supported scripts to be scanned, make sure to add a proper shebang on the first line of the script."
local actual=$(process_input)

assertContains "Actual messages:$actual Did not contain the expected message.\n" "$actual" "$expected1"
assertContains "Actual messages:$actual Did not contain the expected message.\n" "$actual" "$expected2"

}

test_invalid_script_without_extension(){
input_paths="./test_data/script_type/test_python"
local expected1="Found 1 unscanned files that could potentially be supported"
local expected2="ShellCheck only supports sh/bash/dash/ksh scripts. For supported scripts to be scanned, make sure to add a proper shebang on the first line of the script."
local actual=$(process_input)

assertContains "Actual messages:$actual Did not contain the expected message.\n" "$actual" "$expected1"
assertContains "Actual messages:$actual Did not contain the expected message.\n" "$actual" "$expected2"

}

test_unsupported_script_without_extension(){
input_paths="./test_data/script_type/test_zsh_wsh"
local expect1="Found 1 unscanned files that could potentially be supported"
local expect2="ShellCheck only supports sh/bash/dash/ksh scripts. For supported scripts to be scanned, make sure to add a proper shebang on the first line of the script."
local actual=$(process_input)

assertContains "Actual messages:$actual Did not contain the expected message.\n" "$actual" "$expect1"
assertContains "Actual messages:$actual Did not contain the expected message.\n" "$actual" "$expect2"
}

test_unsupported_script_with_extension(){
input_paths="./test_data/script_type/test.zsh"
local expect1="Found 1 unscanned files that could potentially be supported"
local expect2="ShellCheck only supports sh/bash/dash/ksh scripts. For supported scripts to be scanned, make sure to add a proper shebang on the first line of the script."
local actual=$(process_input)

assertContains "Actual messages:$actual Did not contain the expected message.\n" "$actual" "$expect1"
assertContains "Actual messages:$actual Did not contain the expected message.\n" "$actual" "$expect2"
}

test_valid_file_without_shebang(){
input_paths="./test_data/script_type/test_script_wosh.sh"
local expected1="Found 1 unscanned files that could potentially be supported"
local expected2="ShellCheck only supports sh/bash/dash/ksh scripts. For supported scripts to be scanned, make sure to add a proper shebang on the first line of the script."
local actual=$(process_input)

assertContains "Actual messages:$actual Did not contain the expected message.\n" "$actual" "$expected1"
assertContains "Actual messages:$actual Did not contain the expected message.\n" "$actual" "$expected2"

}

test_valid_file_input(){
input_paths="./test_data/test_dir/example_script.sh"
local expected="Scanning example_script.sh"
local actual=$(process_input)

assertContains "Actual messages:$actual Did not contain the expected message.\n" "$actual" "$expected"
}

test_default_input_file(){
input_paths="."
local expected_expect="Scanning all the shell scripts at ./test_data"
local actual=$(process_input)

assertContains "Actual messages:$actual Did not contain the expected message.\n" "$actual" "$expected_expect"
}

test_input_directory(){
input_paths="./test_data/test_dir"
local expect1="Scanning all the shell scripts at ./test_data/test_dir"
local expect2="Scanning example_script.sh"
local expect3="Scanning executable_script"
local expect4="Found 1 unscanned files that could potentially be supported"
local actual=$(process_input)

assertContains "Actual messages:$actual Did not contain the expected message.\n" "$actual" "$expect1"
assertContains "Actual messages:$actual Did not contain the expected message.\n" "$actual" "$expect2"
assertContains "Actual messages:$actual Did not contain the expected message.\n" "$actual" "$expect3"
assertContains "Actual messages:$actual Did not contain the expected message.\n" "$actual" "$expect4"
}

test_multiple_input_directories(){
input_paths="./test_data/test_dir,./test_data/script_type"
local expected1="Scanning all the shell scripts at ./test_data/test_dir"
local expected2="Scanning all the shell scripts at ./test_data/script_type"
local actual=$(process_input)

assertContains "Actual messages:$actual Did not contain the expected message.\n" "$actual" "$expect1"
assertContains "Actual messages:$actual Did not contain the expected message.\n" "$actual" "$expected2"
}

test_input_files_with_wildcard() {
input_paths="./test_data/script_type/test*.sh"
local expected1="Scanning test_script_wsh.sh"
local expected2="Found 1 unscanned files that could potentially be supported"
local actual=$(process_input)

assertContains "Actual messages:$actual Did not contain the expected message.\n" "$actual" "$expected1"
assertContains "Actual messages:$actual Did not contain the expected message.\n" "$actual" "$expected2"
}



tearDown(){
input_paths=""
}
source ./tests/shunit2
149 changes: 0 additions & 149 deletions tests/integration_tests/process_input_tests.sh

This file was deleted.

Loading