hyperskill / hs-test

A framework that simplifies testing educational projects for Hyperskill. It is built on top of JUnit.
30 stars 10 forks source link

Use reflection to get user's main method, not direct link from the importing #41

Closed aaaaaa2493 closed 3 years ago

aaaaaa2493 commented 5 years ago

Relates to #46

vyahhi commented 4 years ago

In Stepik we use the following bash script to get the main class name in Java (and somewhat similar in Scala and Kotlin):

#!/bin/bash
# Grep *.class files in the given directory to find one with psvm method defined
# Usage: java_lookup_main.sh DIRECTORY_PATH

PSVM_REGEX="public static( final)? void main\(java\.lang\.String(\[\]|\.\.\.)\)"

shopt -s nullglob
for f in "$1"/*.class; do
    if javap -p "$f" 2> /dev/null | grep -Pq "$PSVM_REGEX"; then
        filename=$(basename "$f")
        echo -n "${filename%.*}"
        exit 0
    fi
done
exit 1

It's here https://github.com/StepicOrg/epicbox-images/blob/145de3624e81b358406dffdb39c485bbc2e16144/epicbox-java/11/java_lookup_main.sh.

Full code to work with this script: https://github.com/bioinf/edy/blob/e7fda90797557eecf841b86ef78e6ebc71a5572b/plugins/stepic_plugins/quizzes/code/runners.py#L192..L242

aaaaaa2493 commented 4 years ago

Also should be checked if there are more than one Main method and show feedback to the user that there should be just one main method

aaaaaa2493 commented 3 years ago

Fixed in 55e1caf4f37783a49ab8d378b0b7a8820fbbfb2a

The script will be useful for implementing #91