Skip to content

Commit dcd97cf

Browse files
Feature/run it test locally (#151)
Add comprehensive integration test automation with cleanup - Implement integration test script for automated tool testing - Add path normalization for cross-platform compatibility - Integrate cleanup functionality with trap handlers - Refactor GitHub workflow to use centralized test script - Ensure proper restoration of original configurations
1 parent 579b70a commit dcd97cf

File tree

3 files changed

+95
-39
lines changed

3 files changed

+95
-39
lines changed

.github/workflows/it-test.yml

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -93,34 +93,8 @@ jobs:
9393
continue-on-error: true
9494
shell: bash
9595
run: |
96-
# Make the script executable
97-
chmod +x run-tool-tests.sh
98-
99-
# Initialize failed tools file
100-
rm -f /tmp/failed_tools.txt
101-
touch /tmp/failed_tools.txt
102-
103-
# Run tests for each tool directory
104-
for tool_dir in plugins/tools/*/; do
105-
tool_name=$(basename "$tool_dir")
106-
if [ -d "$tool_dir/test/src" ]; then
107-
echo "Running tests for $tool_name..."
108-
./run-tool-tests.sh "$tool_name" || {
109-
echo "❌ Test failed for $tool_name"
110-
echo "$tool_name" >> /tmp/failed_tools.txt
111-
}
112-
fi
113-
done
114-
115-
# Check if any tools failed
116-
if [ -s /tmp/failed_tools.txt ] && [ "$(wc -l < /tmp/failed_tools.txt)" -gt 0 ]; then
117-
echo -e "\n❌ The following tools failed their tests:"
118-
cat /tmp/failed_tools.txt
119-
echo "::error::Some tool tests failed. Please check the logs above for details."
120-
exit 1
121-
else
122-
echo "✅ All tool tests passed successfully!"
123-
fi
96+
chmod +x integration-tests/test-tools.sh
97+
./integration-tests/test-tools.sh
12498
12599
- name: Check test results
126100
if: always()

integration-tests/test-tools.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
3+
# Function to cleanup all generated test files
4+
cleanup_all_test_files() {
5+
echo "🧹 Cleaning up test files..."
6+
7+
# Remove generated SARIF and sorted files from all tool test directories
8+
find plugins/tools/*/test/src -name "actual.sarif" -o -name "actual.sorted.json" -o -name "expected.sorted.json" -o -name "codacy.yaml.backup" | xargs rm -f 2>/dev/null || true
9+
10+
# Restore original codacy.yaml files if they were modified
11+
git checkout -- plugins/tools/*/test/src/.codacy/codacy.yaml 2>/dev/null || true
12+
13+
# Clean up any empty .codacy directories that were created during testing
14+
find plugins/tools/*/test/src -name ".codacy" -type d -empty | xargs rmdir 2>/dev/null || true
15+
16+
echo "✅ Cleanup completed"
17+
}
18+
19+
# Set up trap to ensure cleanup happens even if script fails
20+
trap cleanup_all_test_files EXIT
21+
22+
# Initialize failed tools file
23+
rm -f /tmp/failed_tools.txt
24+
touch /tmp/failed_tools.txt
25+
26+
# Run tests for each tool directory
27+
for tool_dir in plugins/tools/*/; do
28+
tool_name=$(basename "$tool_dir")
29+
if [ -d "$tool_dir/test/src" ]; then
30+
echo "Running tests for $tool_name..."
31+
./run-tool-tests.sh "$tool_name" || {
32+
echo "❌ Test failed for $tool_name"
33+
echo "$tool_name" >>/tmp/failed_tools.txt
34+
}
35+
fi
36+
done
37+
38+
# Check if any tools failed
39+
if [ -s /tmp/failed_tools.txt ] && [ "$(wc -l </tmp/failed_tools.txt)" -gt 0 ]; then
40+
echo -e "\n❌ The following tools failed their tests:"
41+
cat /tmp/failed_tools.txt
42+
echo "::error::Some tool tests failed. Please check the logs above for details."
43+
exit 1
44+
else
45+
echo "✅ All tool tests passed successfully!"
46+
fi

run-tool-tests.sh

100644100755
Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,29 @@
33
# Function to normalize paths in a file
44
normalize_paths() {
55
local file=$1
6-
local path_prefix
6+
# Get the repository root directory (5 levels up from current test/src directory)
7+
local repo_root=$(cd ../../../../.. && pwd)
78

9+
# Normalize absolute paths to relative ones for consistent testing
810
if [[ "$OSTYPE" == "darwin"* ]]; then
9-
path_prefix="/Users/runner/work/codacy-cli-v2/codacy-cli-v2/"
11+
# Replace absolute paths with relative paths in URI contexts
12+
sed -i '' "s#file://${repo_root}/plugins/tools/#file:///plugins/tools/#g" "$file"
13+
sed -i '' "s#${repo_root}/plugins/tools/#/plugins/tools/#g" "$file"
14+
# Handle CI runner paths for macOS
15+
sed -i '' "s#file:///Users/runner/work/codacy-cli-v2/codacy-cli-v2/plugins/tools/#file:///plugins/tools/#g" "$file"
16+
sed -i '' "s#/Users/runner/work/codacy-cli-v2/codacy-cli-v2/plugins/tools/#/plugins/tools/#g" "$file"
1017
else
11-
path_prefix="/home/runner/work/codacy-cli-v2/codacy-cli-v2/"
12-
fi
13-
14-
if [[ "$OSTYPE" == "darwin"* ]]; then
15-
sed -i '' "s|file://${path_prefix}|file:///|g" "$file"
16-
sed -i '' "s|${path_prefix}|/|g" "$file"
17-
else
18-
sed -i "s|file://${path_prefix}|file:///|g" "$file"
19-
sed -i "s|${path_prefix}|/|g" "$file"
18+
# Replace absolute paths with relative paths in URI contexts
19+
sed -i "s#file://${repo_root}/plugins/tools/#file:///plugins/tools/#g" "$file"
20+
sed -i "s#${repo_root}/plugins/tools/#/plugins/tools/#g" "$file"
21+
# Handle CI runner paths for Linux
22+
sed -i "s#file:///home/runner/work/codacy-cli-v2/codacy-cli-v2/plugins/tools/#file:///plugins/tools/#g" "$file"
23+
sed -i "s#/home/runner/work/codacy-cli-v2/codacy-cli-v2/plugins/tools/#/plugins/tools/#g" "$file"
2024
fi
2125
}
2226

2327
# Function to sort SARIF file
28+
2429
sort_sarif() {
2530
local input=$1
2631
local output=$2
@@ -56,6 +61,35 @@ fi
5661
# Change to the tool's test directory
5762
cd "$TOOL_DIR" || exit 1
5863

64+
# Store initial state for cleanup
65+
initial_codacy_config=""
66+
if [ -f .codacy/codacy.yaml ]; then
67+
# Backup existing config if it exists
68+
cp .codacy/codacy.yaml .codacy/codacy.yaml.backup
69+
initial_codacy_config="exists"
70+
fi
71+
72+
# Function to cleanup generated files
73+
cleanup_test_files() {
74+
# Remove generated SARIF and sorted files
75+
rm -f actual.sarif actual.sorted.json expected.sorted.json
76+
77+
# Restore or clean up .codacy/codacy.yaml
78+
if [ "$initial_codacy_config" = "exists" ] && [ -f .codacy/codacy.yaml.backup ]; then
79+
# Restore original config
80+
mv .codacy/codacy.yaml.backup .codacy/codacy.yaml
81+
elif [ "$initial_codacy_config" != "exists" ]; then
82+
# Remove generated config and directory if they didn't exist initially
83+
rm -f .codacy/codacy.yaml
84+
if [ -d .codacy ]; then
85+
rmdir .codacy 2>/dev/null || true # Only remove if empty
86+
fi
87+
fi
88+
}
89+
90+
# Set trap to ensure cleanup happens even if script fails
91+
trap cleanup_test_files EXIT
92+
5993
# Run analysis
6094
"$CLI_PATH" install
6195
"$CLI_PATH" analyze --tool "$TOOL_NAME" --format sarif --output actual.sarif
@@ -75,6 +109,8 @@ if ! diff expected.sorted.json actual.sorted.json; then
75109
echo -e "\nActual SARIF output:"
76110
cat actual.sorted.json
77111
echo "$TOOL_NAME" >> /tmp/failed_tools.txt
112+
# Return to original directory before exit
113+
cd ../../../../.. || exit 1
78114
exit 1
79115
else
80116
echo "✅ Tests passed successfully for $TOOL_NAME"

0 commit comments

Comments
 (0)