Skip to content

Commit

Permalink
Merge pull request FeKozma#15 from FeKozma/add-gradle
Browse files Browse the repository at this point in the history
Added gradle.yml for automatically running the tests when pushed to GitHub.
  • Loading branch information
Livila authored Feb 19, 2024
2 parents 1927068 + 954a9be commit c3cd346
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 24 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Running Gradle jobs

on:
push:
branches: []
pull_request:
branches: []

jobs:
testing:
name: Running tests

runs-on: ubuntu-latest

permissions:
contents: read # To fetch code (actions/checkout)

steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'

- name: Setup Gradle
uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0

- name: Run tests with Gradle Wrapper
run: ./gradlew test
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
Do you have a problem with duplicated functions in your java project? Here is the solution on how to find all the duplicate functions, specify the location of your repository in the local-config.properties file (just make a copy of config.properties) and run the code! Then you will get a file in "results" with all the places where functions match.

### There are even some extra features such as ###
* query an LLM on complicated situations were 2 functions could be similar
* query an LLM on complicated situations where 2 functions could be similar
* lots of configuration
* it even has a test!
* it even has testing for it!

----------------
*Please setup before commit:*
### *Please set up before committing* ###
This will run tests when committing and pushing.

To skip the testing you can use the `-n` or `--no-verify` when committing.

``` bash
cp pre-commit.tests .git/hooks/pre-commit
cp pre-commit.tests .git/hooks/pre-push
chmod u+x gradlew \
.git/hooks/pre-commit \
.git/hooks/pre-push
```
5 changes: 3 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ repositories {
}

dependencies {
testImplementation(platform("org.junit:junit-bom:5.9.1"))
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.10.2")
testImplementation(platform("org.junit:junit-bom:5.9.2"))
testImplementation("org.junit.jupiter:junit-jupiter")
implementation ("com.hexadevlabs:gpt4all-java-binding:1.1.5")
implementation("com.hexadevlabs:gpt4all-java-binding:1.1.5")
}

tasks.test {
Expand Down
Empty file modified gradlew
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions src/main/java/CodeCheck/CheckDirectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public ManyFunctions checkFile(File file) {
manyFunctions.commitFile(file.getName());
manyFunctions.commitLine(lineNumber);

Log.trace("Full path: " + file.getAbsolutePath());
Log.debug("Function: " +
manyFunctions.commit.name + " in " +
manyFunctions.commit.file + ":" +
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/CodeCheck/ConfigInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void loadConfig() throws IOException {
this.appProps = new Properties();

String rootDirRegex = "(.*Code-check/).*";
String rootPath = FileSystems.getDefault().getPath("").toAbsolutePath().toString() + "/";
String rootPath = FileSystems.getDefault().getPath("").toAbsolutePath().toString() + File.separator;

String appConfigPath = rootPath + "config.properties";
File test_conf = new File(rootPath + "test-config.properties");
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/CodeCheck/WriteObjectToFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public WriteObjectToFile() throws Exception {

// Delete the temp result file if enabled and if it exists.
if (TEMP_FILE_ENABLED) {
File tempFile = new File(PATH_TO_RESULTS + "/" + TEMP_FILE);
File tempFile = new File(PATH_TO_RESULTS + File.separator + TEMP_FILE);
if (tempFile.exists()) tempFile.delete();
Log.log("Debug mode enabled, deleting the temporary file %s before continuing... ".formatted(PATH_TO_RESULTS + "/" + TEMP_FILE));
Log.log("Debug mode enabled, deleting the temporary file %s before continuing... ".formatted(PATH_TO_RESULTS + File.separator + TEMP_FILE));
}

// Create results directory if it doesn't exist.
Expand Down Expand Up @@ -51,11 +51,11 @@ public WriteObjectToFile() throws Exception {
}

private boolean createFile(String fileName, boolean isFirstTry) {
file = Util.createFile(PATH_TO_RESULTS + "/" + fileName, isFirstTry);
file = Util.createFile(PATH_TO_RESULTS + File.separator + fileName, isFirstTry);
return file != null;
}

public void write(String obj) {
Util.write(PATH_TO_RESULTS + "/" + file.getName(), obj);
Util.write(PATH_TO_RESULTS + File.separator + file.getName(), obj);
}
}
35 changes: 21 additions & 14 deletions src/test/java/CodeCheckTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,38 @@

public class CodeCheckTest {

private String testConfPath = FileSystems.getDefault()
.getPath("").toAbsolutePath() + "\\test-config.properties";
private final String testConfPath = FileSystems.getDefault()
.getPath("").toAbsolutePath() + File.separator + "test-config.properties";

private final String resultFolder = "test-results";
private final String resultNamePrefix = "test-result_{nr}";
private final String resultFileName = "test-result_0.txt";

private String resultFolder = "test-results";
private String resultNamePrefix = "test-result_{nr}";
private String resultFileName = "test-result_0.txt";
@Test
public void executeTest() throws Exception {
delete(FileSystems.getDefault()
.getPath("").toAbsolutePath() + "\\" +resultFolder);
.getPath("").toAbsolutePath() + File.separator + resultFolder);

Util.createFile(testConfPath, true);
writeConf();
CodeCheck.execute();

assertEquals(2, readResult().split("\n").length-1);
assertEquals(2, readResult().split("\n").length - 1);

emptyTestConf();
}

private void delete(String path) {
File dir = new File(path);
Arrays.stream(dir.listFiles()).forEach(File::delete);
dir.delete();

if (dir.exists()) {
Arrays.stream(dir.listFiles()).forEach(File::delete);
dir.delete();
}
}

private void writeConf() {
List<String> conf = List.of("LOGGING_LEVEL=DEBUG",
List<String> conf = List.of(
"LOGGING_LEVEL=DEBUG",
"TEMP_FILE_ENABLED=false",
"TEMP_FILE=debugFile.txt",
"PATH_TO_RESULTS=" + resultFolder,
Expand All @@ -50,8 +54,8 @@ private void writeConf() {
"PATH_TO_CODE=src/test/resources/",
"EXCLUDED_PATHS=[]");

Util.write(testConfPath, "# properties used under testing - this file will be overwriten during test", false);
conf.stream().forEach(line -> Util.write(testConfPath, line));
Util.write(testConfPath, "# properties used under testing - this file will be overwritten during testing", false);
conf.forEach(line -> Util.write(testConfPath, line));
}

private void emptyTestConf() {
Expand All @@ -61,7 +65,10 @@ private void emptyTestConf() {
private String readResult(){
String content = "";
try {
String path = FileSystems.getDefault().getPath("").toAbsolutePath() + "\\" + resultFolder + "\\" + resultFileName;
String path = FileSystems.getDefault().getPath("").toAbsolutePath() +
File.separator + resultFolder +
File.separator + resultFileName;

File myObj = new File(path);
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
Expand Down

0 comments on commit c3cd346

Please sign in to comment.