Skip to content

Instantly share code, notes, and snippets.

@yut23
Last active May 3, 2017 01:39
Show Gist options
  • Save yut23/cd78a85599fd232f31404640238f3c9d to your computer and use it in GitHub Desktop.
Save yut23/cd78a85599fd232f31404640238f3c9d to your computer and use it in GitHub Desktop.
Bash script to setup and run EMMA coverage, like Submitty
#!/bin/bash
# setup_emma.sh
# requirements: bash, coreutils, java, unzip, wget
print_usage() {
echo "Usage: $(basename "$0") <path to svn root>"
}
if [[ $# -lt 1 || $1 == -h || $1 == --help ]]; then
print_usage
exit 1
fi
# where the Java packages are
SRC_DIR="$1/src"
if [[ ! -d $SRC_DIR ]]; then
>&2 echo "Source directory \"$SRC_DIR\" not found."
print_usage
exit 1
fi
if [[ ! -d "$1/.svn" ]]; then
echo "Warning: \"$1\" doesn't look like a SVN directory. If it doesn't have Java"
echo "packages in src/, then things will break later on."
fi
# make a new temporary directory
TEMPDIR="$(mktemp -d)"
pushd "$TEMPDIR" >/dev/null
# we need these for the script, since bash sometimes doesn't copy all the environment variables (???)
JUNIT_JAR="$TEMPDIR/junit.jar"
HAMCREST_JAR="$TEMPDIR/hamcrest.jar"
EMMA_JAR="$TEMPDIR/emma.jar"
cat > run_emma <<EOF
#!/bin/bash
if [[ \$# -lt 1 || \$1 == -h || \$1 == --help ]]; then
echo "Usage: \$(basename "\$0") <package to test>"
exit 1
fi
if [[ ! -d $SRC_DIR/\$1 ]]; then
>&2 echo "Package \"\$1\" does not seem to exist."
>&2 echo "It should be in \"$SRC_DIR\"."
exit 1
fi
# clean bin folder
rm -r bin
mkdir bin
# compile the java files
javac -cp "$JUNIT_JAR" -d bin -sourcepath "$SRC_DIR" "$SRC_DIR/\$1/"*.java &&
javac -cp "$JUNIT_JAR:$HAMCREST_JAR:$SRC_DIR:bin" -d bin -sourcepath "$SRC_DIR" "$SRC_DIR/\$1/test/"*Test.java &&
# and instrument them
java -cp "$EMMA_JAR" emma instr -quiet -Dmetadata.out.merge=false -d "instr/\$1" -ip "bin/\$1"
# go to the source directory, so we can use the data files there
pushd "$SRC_DIR" >/dev/null
java -noverify -cp "$JUNIT_JAR:$HAMCREST_JAR:$EMMA_JAR:$TEMPDIR/instr:$TEMPDIR/bin:$TEMPDIR" -Demma.coverage.out.file="$TEMPDIR/coverage.ec" -Demma.coverage.out.merge=false TestRunner "$TEMPDIR/instr" "\$1"
popd >/dev/null
echo $'\\n===============================================================================\\n'
# generate coverage report
if [[ "\$0" =~ run_emma_html ]]; then
java -cp "$EMMA_JAR" emma report -r html -in coverage.em,coverage.ec -sp "$SRC_DIR"
xdg-open "$TEMPDIR/coverage/index.html"
elif [[ "\$0" =~ run_emma ]]; then
java -cp "$EMMA_JAR" emma report -quiet -r txt -in coverage.em,coverage.ec -sp "$SRC_DIR" -Dreport.txt.out.file=/dev/stdout
fi
EOF
chmod u+x run_emma
ln -s run_emma run_emma_html
# download all the things
wget --quiet 'https://github.com/Submitty/emma/releases/download/2.0.5312/emma-2.0.5312.zip'
unzip -q -j emma-2.0.5312.zip emma-2.0.5312/lib/emma.jar
mv emma.jar "$EMMA_JAR" >/dev/null 2>&1
rm emma-2.0.5312.zip
wget --quiet 'https://search.maven.org/remotecontent?filepath=junit/junit/4.12/junit-4.12.jar' -O "$JUNIT_JAR"
wget --quiet 'https://search.maven.org/remotecontent?filepath=org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar' -O "$HAMCREST_JAR"
wget --quiet 'https://raw.githubusercontent.com/Submitty/Submitty/master/junit_test_runner/TestRunner.java' -O TestRunner.java
# we need to patch this so we can have the class files in a different directory
patch --quiet <<'EOF'
--- TestRunner.java 2017-04-19 20:23:59.089223605 -0400
+++ TestRunner.java 2017-04-19 20:28:06.328051149 -0400
@@ -12,17 +12,17 @@
@param: relative name of the folder where the tests are
@effects: runs all tests in folder ./$homework/test/ with JUnit
*/
- public static void runAllTestsInTestDirectory(String homework) {
+ public static void runAllTestsInTestDirectory(String baseDir, String homework) {
String folderName = homework+"/test/";
- File folder = new File(folderName);
+ File folder = new File(baseDir+"/"+folderName);
// Retrieves the listOfFiles in folder
File[] listOfFiles = folder.listFiles();
if (listOfFiles == null) {
- System.out.println("Folder not found: " + folderName);
+ System.out.println("Folder not found: " + baseDir+"/"+folderName);
return;
}
@@ -88,8 +88,9 @@
public static void main(String args[]) {
- String folderName = args[0];
- runAllTestsInTestDirectory(folderName);
+ String baseDir = args[0];
+ String folderName = args[1];
+ runAllTestsInTestDirectory(baseDir, folderName);
}
}
EOF
# compile the test runner
javac -cp "$JUNIT_JAR" TestRunner.java
mkdir bin
echo -e '\nRun ./run_emma <package> for a text report, or ./run_emma_html <package> for an html report (opens browser automatically).'
bash
popd >/dev/null
# clean up
rm -r "$TEMPDIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment