@@ -53,50 +53,74 @@ jobs:
5353 sudo apt-get update
5454 sudo apt-get install -y gcc g++ make
5555
56- - name : 🔍 Get C/C++ files
57- id : cpp-files
58- run : |
59- echo "files<<EOF" >> $GITHUB_OUTPUT
60- find C/ CPP/ -name "*.c" -o -name "*.cpp" 2>/dev/null || echo "No C/C++ files found"
61- echo "EOF" >> $GITHUB_OUTPUT
56+ - name : 🔍 Get changed C/C++ files
57+ id : changed-files
58+ uses : tj-actions/changed-files@v40
59+ with :
60+ files : |
61+ C/**/*.c
62+ CPP/**/*.cpp
6263
63- - name : 🧪 Test C files
64+ - name : 🧪 Test changed C/C++ files
65+ if : steps.changed-files.outputs.any_changed == 'true'
6466 run : |
65- echo "🔍 Testing C files..."
66- find C/ -name "*.c" -type f 2>/dev/null | while read file; do
67+ echo "🔍 Testing only changed C/C++ files..."
68+ echo "Changed files: ${{ steps.changed-files.outputs.all_changed_files }}"
69+
70+ for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
71+ echo ""
72+ echo "========================================"
6773 echo "Testing: $file"
68- gcc -o "${file%.c}" "$file" -lm
69- if [ $? -eq 0 ]; then
70- echo "✅ $file compiled successfully"
71- # Run basic execution test if file has main function
72- if grep -q "int main" "$file"; then
73- echo "Running: $file"
74- timeout 5s "./${file%.c}" < /dev/null || echo "⚠️ Runtime test completed"
75- fi
74+ echo "========================================"
75+
76+ # Determine compiler based on extension
77+ if [[ "$file" == *.c ]]; then
78+ compiler="gcc"
79+ output="${file%.c}.out"
80+ compile_cmd="gcc -o \"$output\" \"$file\" -lm"
7681 else
77- echo "❌ $file failed to compile"
78- exit 1
82+ compiler="g++"
83+ output="${file%.cpp}.out"
84+ compile_cmd="g++ -std=c++17 -o \"$output\" \"$file\""
7985 fi
80- done || echo "No C files to test"
8186
82- - name : 🧪 Test C++ files
83- run : |
84- echo "🔍 Testing C++ files..."
85- find CPP/ -name "*.cpp" -type f 2>/dev/null | while read file; do
86- echo "Testing: $file"
87- g++ -std=c++17 -o "${file%.cpp}" "$file"
88- if [ $? -eq 0 ]; then
87+ # Compile the file
88+ echo "📦 Compiling with $compiler..."
89+ if eval $compile_cmd 2>&1; then
8990 echo "✅ $file compiled successfully"
90- # Run basic execution test if file has main function
91+
92+ # Only run if it has main function and doesn't require cin/scanf
9193 if grep -q "int main" "$file"; then
92- echo "Running: $file"
93- timeout 5s "./${file%.cpp}" < /dev/null || echo "⚠️ Runtime test completed"
94+ # Check if file requires user input
95+ if grep -qE "(cin|scanf|getline|getchar|gets)" "$file"; then
96+ echo "⏭️ Skipping runtime test (requires user input)"
97+ else
98+ echo "🏃 Running compiled program..."
99+ if timeout 2s "$output" < /dev/null 2>&1; then
100+ echo "✅ Program executed successfully"
101+ else
102+ exit_code=$?
103+ if [ $exit_code -eq 124 ]; then
104+ echo "⏱️ Program timed out (might be waiting for input or infinite loop)"
105+ else
106+ echo "⚠️ Program exited with code $exit_code"
107+ fi
108+ fi
109+ fi
110+ else
111+ echo "ℹ️ No main function found, skipping execution"
94112 fi
113+
114+ # Cleanup
115+ rm -f "$output"
95116 else
96117 echo "❌ $file failed to compile"
97118 exit 1
98119 fi
99- done || echo "No C++ files to test"
120+ done
121+
122+ echo ""
123+ echo "✅ All changed C/C++ files tested successfully!"
100124
101125 test-java :
102126 runs-on : ubuntu-latest
@@ -113,25 +137,59 @@ jobs:
113137 distribution : ' temurin'
114138 java-version : ' 17'
115139
116- - name : 🧪 Test Java files
140+ - name : 🔍 Get changed Java files
141+ id : changed-files
142+ uses : tj-actions/changed-files@v40
143+ with :
144+ files : |
145+ Java/**/*.java
146+
147+ - name : 🧪 Test changed Java files
148+ if : steps.changed-files.outputs.any_changed == 'true'
117149 run : |
118- echo "🔍 Testing Java files..."
119- find Java/ -name "*.java" -type f 2>/dev/null | while read file; do
150+ echo "🔍 Testing only changed Java files..."
151+ echo "Changed files: ${{ steps.changed-files.outputs.all_changed_files }}"
152+
153+ for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
154+ echo ""
155+ echo "========================================"
120156 echo "Testing: $file"
121- javac "$file"
122- if [ $? -eq 0 ]; then
157+ echo "========================================"
158+
159+ # Compile
160+ echo "📦 Compiling Java file..."
161+ if javac "$file" 2>&1; then
123162 echo "✅ $file compiled successfully"
163+
124164 # Try to run if it has main method
125165 if grep -q "public static void main" "$file"; then
126- classname=$(basename "${file%.java}")
127- echo "Running: $classname"
128- timeout 5s java -cp "$(dirname "$file")" "$classname" < /dev/null || echo "⚠️ Runtime test completed"
166+ # Check if requires user input
167+ if grep -qE "(Scanner|BufferedReader|readLine|nextInt|nextLine)" "$file"; then
168+ echo "⏭️ Skipping runtime test (requires user input)"
169+ else
170+ classname=$(basename "${file%.java}")
171+ classpath=$(dirname "$file")
172+ echo "🏃 Running: $classname..."
173+ if timeout 2s java -cp "$classpath" "$classname" < /dev/null 2>&1; then
174+ echo "✅ Program executed successfully"
175+ else
176+ echo "⚠️ Runtime test completed with warnings"
177+ fi
178+ fi
179+ else
180+ echo "ℹ️ No main method found, skipping execution"
129181 fi
182+
183+ # Cleanup .class files
184+ rm -f "$(dirname "$file")"/*.class
130185 else
131186 echo "❌ $file failed to compile"
132187 exit 1
133188 fi
134- done || echo "No Java files to test"
189+ done
190+
191+ echo ""
192+ echo "✅ All changed Java files tested successfully!"
135193
136194 test-python :
137195 runs-on : ubuntu-latest
@@ -157,24 +215,54 @@ jobs:
157215 # Install common packages for algorithms
158216 pip install numpy scipy matplotlib pandas
159217
160- - name : 🧪 Test Python files
218+ - name : 🔍 Get changed Python files
219+ id : changed-files
220+ uses : tj-actions/changed-files@v40
221+ with :
222+ files : |
223+ Python/**/*.py
224+
225+ - name : 🧪 Test changed Python files
226+ if : steps.changed-files.outputs.any_changed == 'true'
161227 run : |
162- echo "🔍 Testing Python files with Python ${{ matrix.python-version }}..."
163- find Python/ -name "*.py" -type f 2>/dev/null | while read file; do
228+ echo "🔍 Testing only changed Python files with Python ${{ matrix.python-version }}..."
229+ echo "Changed files: ${{ steps.changed-files.outputs.all_changed_files }}"
230+
231+ for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
232+ echo ""
233+ echo "========================================"
164234 echo "Testing: $file"
165- python -m py_compile "$file"
166- if [ $? -eq 0 ]; then
235+ echo "========================================"
236+
237+ # Check syntax
238+ echo "📦 Checking Python syntax..."
239+ if python -m py_compile "$file" 2>&1; then
167240 echo "✅ $file syntax is valid"
241+
168242 # Try to run if it has main guard
169243 if grep -q "if __name__ == ['\"]__main__['\"]" "$file"; then
170- echo "Running: $file"
171- timeout 5s python "$file" < /dev/null || echo "⚠️ Runtime test completed"
244+ # Check if requires user input
245+ if grep -qE "(input\(|raw_input\()" "$file"; then
246+ echo "⏭️ Skipping runtime test (requires user input)"
247+ else
248+ echo "🏃 Running Python script..."
249+ if timeout 2s python "$file" < /dev/null 2>&1; then
250+ echo "✅ Program executed successfully"
251+ else
252+ echo "⚠️ Runtime test completed with warnings"
253+ fi
254+ fi
255+ else
256+ echo "ℹ️ No __main__ guard found, skipping execution"
172257 fi
173258 else
174259 echo "❌ $file has syntax errors"
175260 exit 1
176261 fi
177- done || echo "No Python files to test"
262+ done
263+
264+ echo ""
265+ echo "✅ All changed Python files tested successfully!"
178266
179267 test-javascript :
180268 runs-on : ubuntu-latest
0 commit comments