Skip to content

Commit

Permalink
Solves issue #6
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanprbt authored Feb 12, 2024
2 parents 7a01bff + 01e6d55 commit 4d9b63b
Show file tree
Hide file tree
Showing 48 changed files with 1,862 additions and 10 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ dependencies {

test {
useJUnitPlatform()
}
}


39 changes: 35 additions & 4 deletions src/main/java/se/kth/ci/CIServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
// HTTP server utilities
import static spark.Spark.*;

// JSON parsing utilities
import org.json.*;

// I/O
import java.io.File;
import java.io.IOException;

// Recursive directory deletion
import org.apache.commons.io.FileUtils;
// JSON parsing utilities
import org.json.JSONObject;

/**
* @author Rickard Cornell, Elissa Arias Sosa, Raahitya Botta, Zaina Ramadan, Jean Perbet
Expand Down Expand Up @@ -45,6 +43,8 @@ public CIServer(int port, String endpoint, String buildDirectory) {
exitCode = triggerBuild(buildDirectory);
if (exitCode == ErrorCode.SUCCESS) {
System.out.println("Build was successful.");
System.out.println("Running tests..");
triggerTesting(parameters[0], buildDirectory);
} else {
System.out.println("Build failed.");
}
Expand Down Expand Up @@ -121,6 +121,37 @@ public ErrorCode triggerBuild(String buildDirectory){
}
}

/**
* Method for running Junit tests.
*
*/
public ErrorCode triggerTesting(String branchName, String testDirectory) {
File testDir = new File(testDirectory);
if (testDir.exists() && testDir.isDirectory()){
System.out.println("Test directory exists, running tests.");
String[] testCommand = new String[]{"./gradlew", "test"};
try {
Process testProcess = Runtime.getRuntime().exec(testCommand, null, testDir);
int testExitCode = testProcess.waitFor();
if (testExitCode == 0) {
System.out.println("tests for branch " + branchName + " succeeded.");
return ErrorCode.SUCCESS;
} else {
System.err.println("tests for branch " + branchName + " failed. Exit code: " + testExitCode);
return ErrorCode.ERROR_TEST;
}
} catch (IOException | InterruptedException e) {
System.err.println("Error running shell commands " + e.getMessage());
return ErrorCode.ERROR_IO;
}

}
return ErrorCode.ERROR_IO;

}



/**
* Method for parsing JSON response from GitHub webhook into relevant
* parameters for triggering build process.
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/se/kth/ci/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public enum ErrorCode {
// An error occurred while building the project
ERROR_BUILD,

// An error occurred while running the project tests
ERROR_TEST,

NO_TESTS,

// The build was successful
SUCCESS
}
60 changes: 55 additions & 5 deletions src/test/java/se/kth/ci/CIServerTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package se.kth.ci;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

import static org.junit.jupiter.api.Assertions.*;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;


class CIServerTest {
Expand Down Expand Up @@ -137,4 +137,54 @@ public void triggerValidBuild(){
}

}

/**
* Test for method `triggerTesting`
* Checks that when a project does not contain tests, the method returns NO_TESTS
*/
@Test
public void repoWithoutTests(){
String branchName = "main";
ClassLoader classLoader = getClass().getClassLoader();
try {
String filePath = Objects.requireNonNull(classLoader.getResource("valid_build_test")).getFile();
ErrorCode exitCodeTest = server.triggerTesting(branchName,filePath);
assertEquals(ErrorCode.SUCCESS, exitCodeTest, "Testing for a project without tests failed");
} catch (NullPointerException e){
System.err.println("Error while getting file path.");
}

}

/**
* Test for method `triggerTesting`
* Checks that when a project has valid tests, the method returns SUCCESS
*/
@Test
public void triggerValidTests(){
String branchName = "main";
ClassLoader classLoader = getClass().getClassLoader();
String filePath = Objects.requireNonNull(classLoader.getResource("second_valid_tests")).getFile();
ErrorCode exitCodeTest = server.triggerTesting(branchName,filePath);
assertEquals(ErrorCode.SUCCESS, exitCodeTest, "Testing for valid tests failed");

}

/**
* Test for method `triggerTesting`
* Checks that when a project has invalid tests, the method returns ERROR_TEST
*/
@Test
public void triggerInvalidTests(){
String branchName = "main";
ClassLoader classLoader = getClass().getClassLoader();
try {
String filePath = Objects.requireNonNull(classLoader.getResource("second_invalid_tests")).getFile();
ErrorCode exitCodeTest = server.triggerTesting(branchName,filePath);
assertEquals(ErrorCode.ERROR_TEST, exitCodeTest, "Testing for invalid tests was successful");
} catch (NullPointerException e){
System.err.println("Error while getting file path.");
}

}
}
9 changes: 9 additions & 0 deletions src/test/resources/second_invalid_tests/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# Linux start script should use lf
/gradlew text eol=lf

# These are Windows script files and should use crlf
*.bat text eol=crlf

5 changes: 5 additions & 0 deletions src/test/resources/second_invalid_tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ignore Gradle project-specific cache directory
.gradle

# Ignore Gradle build output directory
build
26 changes: 26 additions & 0 deletions src/test/resources/second_invalid_tests/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This is a general purpose Gradle build.
* To learn more about Gradle by exploring our Samples at https://docs.gradle.org/8.6/samples
*/

plugins {
id 'java'
id 'application'
}

repositories {
mavenCentral()
}

dependencies {
testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
implementation 'com.sparkjava:spark-core:2.9.4'
implementation 'org.slf4j:slf4j-nop:2.0.11'
}

test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This file was generated by the Gradle 'init' task.
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 4d9b63b

Please sign in to comment.