Skip to content

Commit 926f261

Browse files
Enhance cleanup functionality in run-tool-tests.sh and test-tools.sh
- Updated run-tool-tests.sh to include a cleanup function that restores the original Codacy configuration and removes generated files after tests. - Added a new cleanup_all_test_files function in test-tools.sh to remove generated test files across all tool test directories and restore original configurations. - Implemented traps in both scripts to ensure cleanup occurs even if the script fails.
1 parent 8202985 commit 926f261

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

integration-tests/test-tools.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
#!/bin/bash
22

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+
322
# Initialize failed tools file
423
rm -f /tmp/failed_tools.txt
524
touch /tmp/failed_tools.txt

run-tool-tests.sh

100644100755
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ normalize_paths() {
2525
}
2626

2727
# Function to sort SARIF file
28+
2829
sort_sarif() {
2930
local input=$1
3031
local output=$2
@@ -60,6 +61,35 @@ fi
6061
# Change to the tool's test directory
6162
cd "$TOOL_DIR" || exit 1
6263

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+
6393
# Run analysis
6494
"$CLI_PATH" install
6595
"$CLI_PATH" analyze --tool "$TOOL_NAME" --format sarif --output actual.sarif
@@ -79,6 +109,8 @@ if ! diff expected.sorted.json actual.sorted.json; then
79109
echo -e "\nActual SARIF output:"
80110
cat actual.sorted.json
81111
echo "$TOOL_NAME" >> /tmp/failed_tools.txt
112+
# Return to original directory before exit
113+
cd ../../../../.. || exit 1
82114
exit 1
83115
else
84116
echo "✅ Tests passed successfully for $TOOL_NAME"

0 commit comments

Comments
 (0)