From cb464d76d753d3611f06a4722fd9723acafbc223 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Str=C3=B6m?= Date: Mon, 19 Feb 2024 05:29:27 +0100 Subject: [PATCH 1/3] feat: Added `gradle.yml` for automatically running the tests in GitHub. Automatically running the tests in GitHub CI when creating a PR or pushing a commit. Also changed `gradlew` to have u+x permission for it to work on GitHub. --- .github/workflows/gradle.yml | 30 ++++++++++++++++++++++++++++++ gradlew | 0 2 files changed, 30 insertions(+) create mode 100644 .github/workflows/gradle.yml mode change 100644 => 100755 gradlew diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml new file mode 100644 index 0000000..b651bc9 --- /dev/null +++ b/.github/workflows/gradle.yml @@ -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 diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 From dfd8e612471b907e1fe8ed7585e121184a0f916b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Stro=CC=88m?= Date: Mon, 19 Feb 2024 07:31:57 +0100 Subject: [PATCH 2/3] fix: Fixed so the tests are working for UNIX systems (Linux/Mac OS). Now using `File.separator` for file paths. Fixed permissions for the files needed when running the bash script in README.md. --- README.md | 14 ++++++-- src/main/java/CodeCheck/CheckDirectory.java | 1 + src/main/java/CodeCheck/ConfigInterface.java | 2 +- .../java/CodeCheck/WriteObjectToFile.java | 8 ++--- src/test/java/CodeCheckTest.java | 35 +++++++++++-------- 5 files changed, 38 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 77c9877..1f1ac18 100644 --- a/README.md +++ b/README.md @@ -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 ``` \ No newline at end of file diff --git a/src/main/java/CodeCheck/CheckDirectory.java b/src/main/java/CodeCheck/CheckDirectory.java index 84c9cda..a137357 100644 --- a/src/main/java/CodeCheck/CheckDirectory.java +++ b/src/main/java/CodeCheck/CheckDirectory.java @@ -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 + ":" + diff --git a/src/main/java/CodeCheck/ConfigInterface.java b/src/main/java/CodeCheck/ConfigInterface.java index 26c687f..451e82e 100644 --- a/src/main/java/CodeCheck/ConfigInterface.java +++ b/src/main/java/CodeCheck/ConfigInterface.java @@ -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"); diff --git a/src/main/java/CodeCheck/WriteObjectToFile.java b/src/main/java/CodeCheck/WriteObjectToFile.java index d303825..0caf2d5 100644 --- a/src/main/java/CodeCheck/WriteObjectToFile.java +++ b/src/main/java/CodeCheck/WriteObjectToFile.java @@ -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. @@ -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); } } diff --git a/src/test/java/CodeCheckTest.java b/src/test/java/CodeCheckTest.java index 048e6d2..296df80 100644 --- a/src/test/java/CodeCheckTest.java +++ b/src/test/java/CodeCheckTest.java @@ -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 conf = List.of("LOGGING_LEVEL=DEBUG", + List conf = List.of( + "LOGGING_LEVEL=DEBUG", "TEMP_FILE_ENABLED=false", "TEMP_FILE=debugFile.txt", "PATH_TO_RESULTS=" + resultFolder, @@ -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() { @@ -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()) { From 954a9be50e3eeb8a25d2aa0ef899c73ba1c9f1fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Stro=CC=88m?= Date: Mon, 19 Feb 2024 08:19:07 +0100 Subject: [PATCH 3/3] fix: Fixed deprecation warning when running tests with Gradle. Updated org.junit:junit-bom from 5.9.1 to 5.9.2. --- build.gradle.kts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 2a972ea..5c42fe8 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 {