diff --git a/build.gradle b/build.gradle index 2254be02..3ab3ce00 100644 --- a/build.gradle +++ b/build.gradle @@ -48,6 +48,18 @@ clean { group = "JPF Build" } +task generateVersion { + description = "Generates the .version file with the current revision hash" + group = "JPF Build Properties" + + doLast { + def revision = "git rev-parse HEAD".execute().text + new File(".version").withWriter("utf-8") { writer -> + writer.writeLine revision + } + } +} + task compile(type: Copy) { group = "JPF Build" description = "Compiles all JPF core sources." @@ -55,11 +67,15 @@ task compile(type: Copy) { // These are automatic generated tasks from the Java Gradle Plugin. // Gradle is able to infer the ordering of the source sets // due to the compileClasspath attribute - dependsOn compileTestJava, compileExamplesJava + dependsOn compileTestJava, compileExamplesJava, generateVersion // Copies build.properties file to the build directory from "build.properties" into sourceSets.main.java.outputDir.path + "/gov/nasa/jpf" + + // Copies .version file to the build directory + from ".version" + into sourceSets.main.java.outputDir.path + "/gov/nasa/jpf" } task jpfClassesJar(type: Jar) { diff --git a/src/tests/ReporterResourcesTest.java b/src/tests/ReporterResourcesTest.java new file mode 100644 index 00000000..0e5d3f7c --- /dev/null +++ b/src/tests/ReporterResourcesTest.java @@ -0,0 +1,69 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import org.junit.Before; +import org.junit.Test; + +import gov.nasa.jpf.Config; +import gov.nasa.jpf.JPF; +import gov.nasa.jpf.util.test.TestJPF; + +/** + * This is a plain JUnit test to check whether required resource files exist on + * JPF classpath. + * + * @author Jeanderson Candido + * + */ +public class ReporterResourcesTest extends TestJPF { + + private JPF jpf; + + @Before + public void setup() { + String[] configArgs = { "+vm.class=.vm.MultiProcessVM", "+target.1=HelloWorld", "+target.2=HelloWorld" }; + this.jpf = new JPF(new Config(configArgs)); + } + + @Test + public void checkResources() throws IOException { + assertNotNull("build.properties should exist on classpath", jpf.getClass().getResourceAsStream("build.properties")); + assertNotNull(".version should exist on classpath", jpf.getClass().getResourceAsStream(".version")); + } + + @Test + public void hashMustMatch() { + InputStream stream = jpf.getClass().getResourceAsStream(".version"); + assertEquals("Should have the same hash", fetchCurrentRevisionFromVCS().trim(), readContentFrom(stream).trim()); + } + + private String fetchCurrentRevisionFromVCS() { + String currentRevision = ""; + try { + Process process = Runtime.getRuntime().exec("git rev-parse HEAD"); + process.waitFor(); + InputStream output = process.getInputStream(); + currentRevision = readContentFrom(output); + + } catch (Exception e) { + e.printStackTrace(); + } + return currentRevision; + } + + private String readContentFrom(InputStream stream) { + BufferedReader buffer = new BufferedReader(new InputStreamReader(stream)); + StringBuilder output = new StringBuilder(); + try { + while (buffer.ready()) { + output.append(buffer.readLine().trim()).append("\n"); + } + } catch (IOException e) { + fail("Should not have failed while reading the file"); + } + return output.toString(); + } + +}