Skip to content

Commit 62e6b67

Browse files
authored
Feat/tests (#35)
* feat: add firt part of tests * feat: add missing tests * feat(github): add workflow * fix(worlflow): version of upload artifact * fix(permission): add gradlew permission
1 parent 5cfbd6e commit 62e6b67

30 files changed

Lines changed: 1267 additions & 15 deletions

.github/workflows/test-all.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main, develop ]
8+
workflow_dispatch:
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v3
17+
18+
- name: Make Gradle wrapper executable
19+
run: chmod +x gradlew
20+
21+
- name: Set up JDK 17
22+
uses: actions/setup-java@v3
23+
with:
24+
distribution: temurin
25+
java-version: 17
26+
27+
- name: Cache Gradle
28+
uses: actions/cache@v3
29+
with:
30+
path: |
31+
~/.gradle/caches
32+
~/.gradle/wrapper
33+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*','**/gradle-wrapper.properties') }}
34+
restore-keys: ${{ runner.os }}-gradle-
35+
36+
- name: Build & test
37+
run: ./gradlew clean test testAll --no-daemon
38+
39+
- name: Upload test reports
40+
if: always()
41+
uses: actions/upload-artifact@v4
42+
with:
43+
name: test-results
44+
path: |
45+
**/build/test-results/**/*.xml
46+
**/build/reports/tests/**

build.gradle

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
plugins {
2+
id 'com.adarshr.test-logger' version '4.0.0'
3+
}
4+
15
allprojects {
26
group = 'fr.traqueur.commands'
37
version = property('version')
@@ -6,10 +10,49 @@ allprojects {
610
plugin 'java-library'
711
}
812

13+
tasks.withType(JavaCompile).configureEach {
14+
options.compilerArgs += ['-nowarn']
15+
}
16+
917
repositories {
1018
mavenCentral()
1119
maven {
1220
url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'
1321
}
1422
}
15-
}
23+
}
24+
25+
subprojects {
26+
if (!project.name.contains('test-plugin')) {
27+
28+
apply plugin: 'com.adarshr.test-logger'
29+
30+
dependencies {
31+
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
32+
testImplementation 'org.mockito:mockito-core:5.3.1'
33+
}
34+
test {
35+
useJUnitPlatform()
36+
jvmArgs += ['-XX:+EnableDynamicAgentLoading']
37+
reports {
38+
html.required.set(true)
39+
junitXml.required.set(true)
40+
}
41+
42+
testLogging {
43+
events("passed", "skipped", "failed")
44+
exceptionFormat "full"
45+
}
46+
}
47+
}
48+
}
49+
50+
tasks.register("testAll") {
51+
group = "verification"
52+
description = "Execute tous les tests des sous-projets qui ont une tâche 'test'"
53+
54+
// Déclare la dépendance vers chaque tâche 'test' de chaque sous-projet
55+
dependsOn subprojects
56+
.findAll { it.tasks.findByName('test') != null }
57+
.collect { it.tasks.named('test') }
58+
}

core/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ java {
1111

1212
def generatedResourcesDir = "$buildDir/generated-resources"
1313

14-
task generateCommandsProperties {
14+
tasks.register('generateCommandsProperties') {
1515
doLast {
1616
def outputDir = file("$generatedResourcesDir")
1717
outputDir.mkdirs()

core/src/main/java/fr/traqueur/commands/api/Arguments.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ public <T> Optional<T> getAs(String argument, Class<T> typeRef) {
323323
throw new NoGoodTypeArgumentException();
324324
}
325325
} catch (NoGoodTypeArgumentException e) {
326+
logger.error("The argument " + argument + " is not the good type.");
326327
return Optional.empty();
327328
}
328329

core/src/main/java/fr/traqueur/commands/api/Command.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,10 @@ public String generateDefaultUsage(CommandPlatform<T,S> platform, S sender, Stri
497497
StringBuilder usage = new StringBuilder();
498498
usage.append("/");
499499
Arrays.stream(label.split("\\.")).forEach(s -> usage.append(s).append(" "));
500+
//remove the last space
501+
if(this.args.isEmpty() && this.optionalArgs.isEmpty()) {
502+
usage.deleteCharAt(usage.length() - 1);
503+
}
500504

501505
StringBuilder firstArg = new StringBuilder();
502506
this.getSubcommands()
@@ -511,8 +515,11 @@ public String generateDefaultUsage(CommandPlatform<T,S> platform, S sender, Stri
511515
}
512516

513517
usage.append(this.getArgs().stream().map(argument -> "<" + argument.arg() + ">").collect(Collectors.joining(" ")));
514-
usage.append(" ");
515-
usage.append(this.getOptinalArgs().stream().map(argument -> "[" + argument.arg() + "]").collect(Collectors.joining(" ")));
518+
if (!this.getOptinalArgs().isEmpty()) {
519+
usage.append(" ");
520+
usage.append(this.getOptinalArgs().stream().map(argument -> "[" + argument.arg() + "]").collect(Collectors.joining(" ")));
521+
}
522+
516523
return usage.toString();
517524
}
518525

core/src/main/java/fr/traqueur/commands/api/exceptions/ArgsWithInfiniteArgumentException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ public class ArgsWithInfiniteArgumentException extends Exception {
1010
* @param optional if the argument is optional
1111
*/
1212
public ArgsWithInfiniteArgumentException(boolean optional) {
13-
super((optional ? "Optional arguments" : "Arguments") + "cannot follow infinite arguments.");
13+
super((optional ? "Optional arguments" : "Arguments") + " cannot follow infinite arguments.");
1414
}
1515
}

core/src/main/java/fr/traqueur/commands/api/updater/Updater.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44
import java.net.HttpURLConnection;
5+
import java.net.MalformedURLException;
56
import java.net.URI;
67
import java.net.URL;
78
import java.util.Properties;
@@ -13,6 +14,26 @@
1314
*/
1415
public class Updater {
1516

17+
private static final String VERSION_PROPERTY_FILE = "commands.properties";
18+
private static URL URL_LATEST_RELEASE;
19+
private static Logger LOGGER = Logger.getLogger("CommandsAPI");
20+
21+
static {
22+
try {
23+
URL_LATEST_RELEASE = URI.create("https://api.github.com/repos/Traqueur-dev/CommandsAPI/releases/latest").toURL();
24+
} catch (MalformedURLException e) {
25+
throw new RuntimeException(e);
26+
}
27+
}
28+
29+
public static void setUrlLatestRelease(URL URL_LATEST_RELEASE) {
30+
Updater.URL_LATEST_RELEASE = URL_LATEST_RELEASE;
31+
}
32+
33+
public static void setLogger(Logger LOGGER) {
34+
Updater.LOGGER = LOGGER;
35+
}
36+
1637
/**
1738
* Private constructor to prevent instantiation
1839
*/
@@ -23,7 +44,7 @@ private Updater() {}
2344
*/
2445
public static void checkUpdates() {
2546
if(!Updater.isUpToDate()) {
26-
Logger.getLogger("CommandsAPI").warning("The framework is not up to date, the latest version is " + Updater.fetchLatestVersion());
47+
LOGGER.warning("The framework is not up to date, the latest version is " + Updater.fetchLatestVersion());
2748
}
2849
}
2950

@@ -34,7 +55,7 @@ public static void checkUpdates() {
3455
public static String getVersion() {
3556
Properties prop = new Properties();
3657
try {
37-
prop.load(Updater.class.getClassLoader().getResourceAsStream("commands.properties"));
58+
prop.load(Updater.class.getClassLoader().getResourceAsStream(VERSION_PROPERTY_FILE));
3859
return prop.getProperty("version");
3960
} catch (IOException e) {
4061
throw new RuntimeException(e);
@@ -60,8 +81,7 @@ public static boolean isUpToDate() {
6081
*/
6182
public static String fetchLatestVersion() {
6283
try {
63-
URL url = URI.create("https://api.github.com/repos/Traqueur-dev/CommandsAPI/releases/latest").toURL();
64-
String responseString = getString(url);
84+
String responseString = getString();
6585
int tagNameIndex = responseString.indexOf("\"tag_name\"");
6686
int start = responseString.indexOf('\"', tagNameIndex + 10) + 1;
6787
int end = responseString.indexOf('\"', start);
@@ -75,8 +95,8 @@ public static String fetchLatestVersion() {
7595
* Get the latest version of the plugin
7696
* @return The latest version of the plugin
7797
*/
78-
private static String getString(URL url) throws IOException {
79-
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
98+
private static String getString() throws IOException {
99+
HttpURLConnection connection = (HttpURLConnection) Updater.URL_LATEST_RELEASE.openConnection();
80100
connection.setRequestMethod("GET");
81101

82102
StringBuilder response = new StringBuilder();

core/src/main/java/fr/traqueur/commands/impl/arguments/BooleanArgument.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public BooleanArgument() {
2222

2323
@Override
2424
public Boolean apply(String s) {
25+
if(s == null || s.isEmpty()) {
26+
return null;
27+
}
2528
if(s.equalsIgnoreCase("true") || s.equalsIgnoreCase("false")) {
2629
return Boolean.parseBoolean(s);
2730
}

core/src/main/java/fr/traqueur/commands/impl/arguments/DoubleArgument.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public DoubleArgument() {}
2020
*/
2121
@Override
2222
public Double apply(String input) {
23+
if (input == null || input.isEmpty()) {
24+
return null;
25+
}
2326
try {
2427
return Double.valueOf(input);
2528
} catch (NumberFormatException e){

core/src/main/java/fr/traqueur/commands/impl/arguments/EnumArgument.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public EnumArgument(Class<T> clazz) {
2121

2222
@Override
2323
public Enum<T> apply(String s) {
24+
if (s == null || s.isEmpty()) {
25+
return null;
26+
}
2427
try {
2528
return Enum.valueOf(clazz, s);
2629
} catch (IllegalArgumentException e) {

0 commit comments

Comments
 (0)