From 0c6b930667cc9a5da615935db2e1c045cd172a13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gaboso=E2=84=A2?= Date: Sun, 12 Feb 2023 20:43:16 +0000 Subject: [PATCH 1/5] General refactoring in modules and lib updates --- pom.xml | 6 +-- .../java/com/github/gaboso/ModuleRunner.java | 48 ++++++++++++------- .../com/github/gaboso/format/Formatter.java | 25 ---------- .../github/gaboso/helper/CommandHelper.java | 12 ++--- .../com/github/gaboso/helper/OsHelper.java | 24 ---------- .../com/github/gaboso/module/GitModule.java | 15 +++--- .../java/com/github/gaboso/module/Module.java | 4 +- .../github/gaboso/module/ModuleTypeEnum.java | 26 ++++++++++ .../gaboso/module/impl/BowerModule.java | 20 ++++---- .../gaboso/module/impl/GruntModule.java | 20 ++++---- .../gaboso/module/impl/MavenModule.java | 20 ++++---- .../github/gaboso/module/impl/NpmModule.java | 20 ++++---- .../com/github/gaboso/os/OpSystemEnum.java | 28 +++++++++++ 13 files changed, 151 insertions(+), 117 deletions(-) delete mode 100644 src/main/java/com/github/gaboso/format/Formatter.java delete mode 100644 src/main/java/com/github/gaboso/helper/OsHelper.java create mode 100644 src/main/java/com/github/gaboso/module/ModuleTypeEnum.java create mode 100644 src/main/java/com/github/gaboso/os/OpSystemEnum.java diff --git a/pom.xml b/pom.xml index 5014bed..3a8df28 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ br.com.gaboso scriptum - 2.0.0 + 3.0.0 com.github.gaboso @@ -32,7 +32,7 @@ UTF-8 1.8 1.8 - 2.18.0 + 2.19.0 @@ -55,7 +55,7 @@ org.apache.maven.plugins maven-assembly-plugin - 3.4.0 + 3.4.2 diff --git a/src/main/java/com/github/gaboso/ModuleRunner.java b/src/main/java/com/github/gaboso/ModuleRunner.java index 77377f3..1f34e4c 100644 --- a/src/main/java/com/github/gaboso/ModuleRunner.java +++ b/src/main/java/com/github/gaboso/ModuleRunner.java @@ -2,10 +2,13 @@ import com.github.gaboso.module.GitModule; import com.github.gaboso.module.Module; +import com.github.gaboso.module.ModuleTypeEnum; import com.github.gaboso.module.impl.BowerModule; import com.github.gaboso.module.impl.GruntModule; import com.github.gaboso.module.impl.MavenModule; import com.github.gaboso.module.impl.NpmModule; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.io.File; import java.util.Arrays; @@ -17,20 +20,17 @@ */ public class ModuleRunner { - private final List modules; + private static final Logger LOGGER = LogManager.getLogger(ModuleRunner.class.getName()); - public ModuleRunner() { - Module bowerModule = new BowerModule(); - Module gruntModule = new GruntModule(); - Module mavenModule = new MavenModule(); - Module npmModule = new NpmModule(); - modules = Arrays.asList(bowerModule, gruntModule, mavenModule, npmModule); - } + private final List modules = Arrays.asList( + new BowerModule(), + new GruntModule(), + new MavenModule(), + new NpmModule() + ); public void runAll(List folders) { - for (File folder : folders) { - run(folder); - } + folders.forEach(this::run); } public void run(File folder) { @@ -38,7 +38,10 @@ public void run(File folder) { String folderName = folder.getName(); if (GitModule.isProject(path)) { - GitModule.executeCommands(folderName, path); + String typeName = ModuleTypeEnum.GIT.getTypeName(); + printStartMessage(typeName, folderName); + GitModule.executeCommands(path); + printFinishMessage(typeName, folderName); } File[] listFiles = folder.listFiles(); @@ -46,11 +49,22 @@ public void run(File folder) { return; } - for (Module module : modules) { - if (module.isProject(listFiles)) { - module.executeCommands(folderName, path); - } - } + modules.stream() + .filter(module -> module.isProject(listFiles)) + .forEach(module -> { + String typeName = module.getType().getTypeName(); + printStartMessage(typeName, folderName); + module.executeCommands(path); + printFinishMessage(typeName, folderName); + }); + } + + private void printStartMessage(String typeName, String folderName) { + LOGGER.info("--------- Starting update {} --- [ {} ] ---------", typeName, folderName); + } + + private void printFinishMessage(String typeName, String folderName) { + LOGGER.info("--------- Finished update {} --- [ {} ] ---------", typeName, folderName); } } diff --git a/src/main/java/com/github/gaboso/format/Formatter.java b/src/main/java/com/github/gaboso/format/Formatter.java deleted file mode 100644 index d5157e5..0000000 --- a/src/main/java/com/github/gaboso/format/Formatter.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.github.gaboso.format; - -/** - * @since 1.3.2 - * Log message Formatter - */ -public class Formatter { - - private final String type; - private final String projectName; - - public Formatter(String type, String projectName) { - this.type = type; - this.projectName = projectName; - } - - public String getMessageStartUpdate() { - return String.format("Updating %s project --- [ %s ]", type, projectName); - } - - public String getMessageFinishUpdate() { - return String.format("Finish update %s project --- [ %s ]", type, projectName); - } - -} \ No newline at end of file diff --git a/src/main/java/com/github/gaboso/helper/CommandHelper.java b/src/main/java/com/github/gaboso/helper/CommandHelper.java index 3e66153..1837aa6 100644 --- a/src/main/java/com/github/gaboso/helper/CommandHelper.java +++ b/src/main/java/com/github/gaboso/helper/CommandHelper.java @@ -1,6 +1,6 @@ package com.github.gaboso.helper; -import com.github.gaboso.format.Formatter; +import com.github.gaboso.os.OpSystemEnum; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -19,11 +19,10 @@ public class CommandHelper { private CommandHelper() { } - public static void executeCMD(String path, String cmd, Formatter formatter) { - LOGGER.info(formatter.getMessageStartUpdate()); - - String runner = OsHelper.getRunner(); - String option = OsHelper.getOption(); + public static void executeCMD(String path, String cmd) { + OpSystemEnum currentOs = OpSystemEnum.getCurrentOs(); + String runner = currentOs.getRunner(); + String option = currentOs.getOption(); String command = "cd " + path + "/ && " + cmd; @@ -46,7 +45,6 @@ public static void executeCMD(String path, String cmd, Formatter formatter) { } catch (IOException e) { LOGGER.error(e.getMessage(), e); } - LOGGER.info(formatter.getMessageFinishUpdate()); } } diff --git a/src/main/java/com/github/gaboso/helper/OsHelper.java b/src/main/java/com/github/gaboso/helper/OsHelper.java deleted file mode 100644 index 65be826..0000000 --- a/src/main/java/com/github/gaboso/helper/OsHelper.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.github.gaboso.helper; - -/** - * @since 1.3.3 - * Operational System Helper - */ -public class OsHelper { - - private OsHelper() { - } - - public static String getRunner() { - return isWindows() ? "cmd.exe" : "/bin/bash"; - } - - private static boolean isWindows() { - return System.getProperty("os.name").contains("Windows"); - } - - public static String getOption() { - return isWindows() ? "/c" : "-c"; - } - -} \ No newline at end of file diff --git a/src/main/java/com/github/gaboso/module/GitModule.java b/src/main/java/com/github/gaboso/module/GitModule.java index ebfd563..a6cb504 100644 --- a/src/main/java/com/github/gaboso/module/GitModule.java +++ b/src/main/java/com/github/gaboso/module/GitModule.java @@ -1,8 +1,7 @@ package com.github.gaboso.module; -import com.github.gaboso.format.Formatter; import com.github.gaboso.helper.CommandHelper; -import com.github.gaboso.helper.OsHelper; +import com.github.gaboso.os.OpSystemEnum; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -22,8 +21,9 @@ private GitModule() { } public static boolean isProject(String path) { - String runner = OsHelper.getRunner(); - String option = OsHelper.getOption(); + OpSystemEnum currentOs = OpSystemEnum.getCurrentOs(); + String runner = currentOs.getRunner(); + String option = currentOs.getOption(); ProcessBuilder builder = new ProcessBuilder(runner, option, "cd " + path + "/ && git rev-parse --is-inside-work-tree"); builder.redirectErrorStream(true); @@ -40,10 +40,9 @@ public static boolean isProject(String path) { return false; } - public static void executeCommands(String projectName, String projectPath) { - Formatter formatter = new Formatter("Git", projectName); - - CommandHelper.executeCMD(projectPath, "git fetch && git pull origin", formatter); + public static void executeCommands(String projectPath) { + String command = ModuleTypeEnum.GIT.getCommand(); + CommandHelper.executeCMD(projectPath, command); } } diff --git a/src/main/java/com/github/gaboso/module/Module.java b/src/main/java/com/github/gaboso/module/Module.java index 6fd9e7a..1471c95 100644 --- a/src/main/java/com/github/gaboso/module/Module.java +++ b/src/main/java/com/github/gaboso/module/Module.java @@ -6,6 +6,8 @@ public interface Module { boolean isProject(File[] listFiles); - void executeCommands(String projectName, String projectPath); + void executeCommands(String projectPath); + + ModuleTypeEnum getType(); } diff --git a/src/main/java/com/github/gaboso/module/ModuleTypeEnum.java b/src/main/java/com/github/gaboso/module/ModuleTypeEnum.java new file mode 100644 index 0000000..58bf3b8 --- /dev/null +++ b/src/main/java/com/github/gaboso/module/ModuleTypeEnum.java @@ -0,0 +1,26 @@ +package com.github.gaboso.module; + +public enum ModuleTypeEnum { + + MAVEN("Maven", "mvn clean install -DskipTests=true"), + GRUNT("Grunt", "grunt"), + BOWER("Bower", "bower install && bower update"), + NPM("NPM", "npm install && npm update"), + GIT("Git", "git fetch && git pull origin"); + + private final String typeName; + private final String command; + + ModuleTypeEnum(String name, String command) { + this.typeName = name; + this.command = command; + } + + public String getTypeName() { + return typeName; + } + + public String getCommand() { + return command; + } +} diff --git a/src/main/java/com/github/gaboso/module/impl/BowerModule.java b/src/main/java/com/github/gaboso/module/impl/BowerModule.java index ff3096b..f259a1b 100644 --- a/src/main/java/com/github/gaboso/module/impl/BowerModule.java +++ b/src/main/java/com/github/gaboso/module/impl/BowerModule.java @@ -1,8 +1,8 @@ package com.github.gaboso.module.impl; -import com.github.gaboso.format.Formatter; import com.github.gaboso.helper.CommandHelper; import com.github.gaboso.module.Module; +import com.github.gaboso.module.ModuleTypeEnum; import java.io.File; import java.util.Arrays; @@ -17,17 +17,21 @@ public class BowerModule implements Module { @Override public boolean isProject(File[] listFiles) { return Arrays.stream(listFiles) - .filter(Objects::nonNull) - .filter(file -> !file.isDirectory()) - .map(File::getName) - .anyMatch("bower.json"::equals); + .filter(Objects::nonNull) + .filter(file -> !file.isDirectory()) + .map(File::getName) + .anyMatch("bower.json"::equals); } @Override - public void executeCommands(String projectName, String projectPath) { - Formatter formatter = new Formatter("Bower", projectName); + public void executeCommands(String projectPath) { + String command = this.getType().getCommand(); + CommandHelper.executeCMD(projectPath, command); + } - CommandHelper.executeCMD(projectPath, "bower install && bower update", formatter); + @Override + public ModuleTypeEnum getType() { + return ModuleTypeEnum.BOWER; } } diff --git a/src/main/java/com/github/gaboso/module/impl/GruntModule.java b/src/main/java/com/github/gaboso/module/impl/GruntModule.java index 45998c7..340d441 100644 --- a/src/main/java/com/github/gaboso/module/impl/GruntModule.java +++ b/src/main/java/com/github/gaboso/module/impl/GruntModule.java @@ -1,8 +1,8 @@ package com.github.gaboso.module.impl; -import com.github.gaboso.format.Formatter; import com.github.gaboso.helper.CommandHelper; import com.github.gaboso.module.Module; +import com.github.gaboso.module.ModuleTypeEnum; import java.io.File; import java.util.Arrays; @@ -17,17 +17,21 @@ public class GruntModule implements Module { @Override public boolean isProject(File[] listFiles) { return Arrays.stream(listFiles) - .filter(Objects::nonNull) - .filter(file -> !file.isDirectory()) - .map(File::getName) - .anyMatch("gruntfile.js"::equals); + .filter(Objects::nonNull) + .filter(file -> !file.isDirectory()) + .map(File::getName) + .anyMatch("gruntfile.js"::equals); } @Override - public void executeCommands(String projectName, String projectPath) { - Formatter formatter = new Formatter("Grunt", projectName); + public void executeCommands(String projectPath) { + String command = this.getType().getCommand(); + CommandHelper.executeCMD(projectPath, command); + } - CommandHelper.executeCMD(projectPath, "grunt", formatter); + @Override + public ModuleTypeEnum getType() { + return ModuleTypeEnum.GRUNT; } } diff --git a/src/main/java/com/github/gaboso/module/impl/MavenModule.java b/src/main/java/com/github/gaboso/module/impl/MavenModule.java index 563bcae..5c1573a 100644 --- a/src/main/java/com/github/gaboso/module/impl/MavenModule.java +++ b/src/main/java/com/github/gaboso/module/impl/MavenModule.java @@ -1,8 +1,8 @@ package com.github.gaboso.module.impl; -import com.github.gaboso.format.Formatter; import com.github.gaboso.helper.CommandHelper; import com.github.gaboso.module.Module; +import com.github.gaboso.module.ModuleTypeEnum; import java.io.File; import java.util.Arrays; @@ -17,17 +17,21 @@ public class MavenModule implements Module { @Override public boolean isProject(File[] listFiles) { return Arrays.stream(listFiles) - .filter(Objects::nonNull) - .filter(file -> !file.isDirectory()) - .map(File::getName) - .anyMatch("pom.xml"::equals); + .filter(Objects::nonNull) + .filter(file -> !file.isDirectory()) + .map(File::getName) + .anyMatch("pom.xml"::equals); } @Override - public void executeCommands(String projectName, String projectPath) { - Formatter formatter = new Formatter("Maven", projectName); + public void executeCommands(String projectPath) { + String command = this.getType().getCommand(); + CommandHelper.executeCMD(projectPath, command); + } - CommandHelper.executeCMD(projectPath, "mvn clean install -DskipTests=true", formatter); + @Override + public ModuleTypeEnum getType() { + return ModuleTypeEnum.MAVEN; } } diff --git a/src/main/java/com/github/gaboso/module/impl/NpmModule.java b/src/main/java/com/github/gaboso/module/impl/NpmModule.java index baf11b9..5834d4b 100644 --- a/src/main/java/com/github/gaboso/module/impl/NpmModule.java +++ b/src/main/java/com/github/gaboso/module/impl/NpmModule.java @@ -1,8 +1,8 @@ package com.github.gaboso.module.impl; -import com.github.gaboso.format.Formatter; import com.github.gaboso.helper.CommandHelper; import com.github.gaboso.module.Module; +import com.github.gaboso.module.ModuleTypeEnum; import java.io.File; import java.util.Arrays; @@ -17,17 +17,21 @@ public class NpmModule implements Module { @Override public boolean isProject(File[] listFiles) { return Arrays.stream(listFiles) - .filter(Objects::nonNull) - .filter(file -> !file.isDirectory()) - .map(File::getName) - .anyMatch("package.json"::equals); + .filter(Objects::nonNull) + .filter(file -> !file.isDirectory()) + .map(File::getName) + .anyMatch("package.json"::equals); } @Override - public void executeCommands(String projectName, String projectPath) { - Formatter formatter = new Formatter("NPM", projectName); + public void executeCommands(String projectPath) { + String command = this.getType().getCommand(); + CommandHelper.executeCMD(projectPath, command); + } - CommandHelper.executeCMD(projectPath, "npm install && npm update", formatter); + @Override + public ModuleTypeEnum getType() { + return ModuleTypeEnum.NPM; } } diff --git a/src/main/java/com/github/gaboso/os/OpSystemEnum.java b/src/main/java/com/github/gaboso/os/OpSystemEnum.java new file mode 100644 index 0000000..48dd092 --- /dev/null +++ b/src/main/java/com/github/gaboso/os/OpSystemEnum.java @@ -0,0 +1,28 @@ +package com.github.gaboso.os; + +public enum OpSystemEnum { + + WINDOWS("cmd.exe", "/c"), + LINUX("/bin/bash", "-c"); + + private final String runner; + private final String option; + + OpSystemEnum(String runner, String option) { + this.runner = runner; + this.option = option; + } + + public String getRunner() { + return runner; + } + + public String getOption() { + return option; + } + + public static OpSystemEnum getCurrentOs() { + return System.getProperty("os.name").contains("Windows") ? WINDOWS : LINUX; + } + +} \ No newline at end of file From d2adfd15fa1d288cbc006c696fa7aff7eaf39518 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gaboso=E2=84=A2?= Date: Mon, 13 Feb 2023 23:05:16 +0000 Subject: [PATCH 2/5] Add Unit tests to BowerModule and GruntModule --- pom.xml | 34 +++++++++++ .../java/com/github/gaboso/ModuleRunner.java | 27 ++++++--- ...ommandHelper.java => CommandExecutor.java} | 9 +-- .../com/github/gaboso/module/GitModule.java | 13 +++-- .../gaboso/module/impl/BowerModule.java | 10 +++- .../gaboso/module/impl/GruntModule.java | 12 +++- .../gaboso/module/impl/MavenModule.java | 10 +++- .../github/gaboso/module/impl/NpmModule.java | 10 +++- .../gaboso/module/impl/BowerModuleTest.java | 54 ++++++++++++++++++ .../gaboso/module/impl/GruntModuleTest.java | 54 ++++++++++++++++++ src/test/resources/invalid/image.jpg | Bin 0 -> 95 bytes src/test/resources/valid/bower/bower.json | 8 +++ src/test/resources/valid/grunt/gruntfile.js | 23 ++++++++ 13 files changed, 236 insertions(+), 28 deletions(-) rename src/main/java/com/github/gaboso/helper/{CommandHelper.java => CommandExecutor.java} (88%) create mode 100644 src/test/java/com/github/gaboso/module/impl/BowerModuleTest.java create mode 100644 src/test/java/com/github/gaboso/module/impl/GruntModuleTest.java create mode 100644 src/test/resources/invalid/image.jpg create mode 100644 src/test/resources/valid/bower/bower.json create mode 100644 src/test/resources/valid/grunt/gruntfile.js diff --git a/pom.xml b/pom.xml index 3a8df28..bb4d053 100644 --- a/pom.xml +++ b/pom.xml @@ -33,6 +33,9 @@ 1.8 1.8 2.19.0 + 5.9.0 + 5.1.1 + 3.0.0-M8 @@ -46,6 +49,31 @@ log4j-core ${log.version} + + org.junit.jupiter + junit-jupiter-engine + ${junit.version} + test + + + org.junit.jupiter + junit-jupiter-params + ${junit.version} + test + + + org.mockito + mockito-core + ${mockito.version} + test + + + org.mockito + mockito-junit-jupiter + ${mockito.version} + test + + @@ -77,6 +105,12 @@ + + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire-plugin.version} + diff --git a/src/main/java/com/github/gaboso/ModuleRunner.java b/src/main/java/com/github/gaboso/ModuleRunner.java index 1f34e4c..cea54ee 100644 --- a/src/main/java/com/github/gaboso/ModuleRunner.java +++ b/src/main/java/com/github/gaboso/ModuleRunner.java @@ -1,5 +1,6 @@ package com.github.gaboso; +import com.github.gaboso.helper.CommandExecutor; import com.github.gaboso.module.GitModule; import com.github.gaboso.module.Module; import com.github.gaboso.module.ModuleTypeEnum; @@ -22,12 +23,22 @@ public class ModuleRunner { private static final Logger LOGGER = LogManager.getLogger(ModuleRunner.class.getName()); - private final List modules = Arrays.asList( - new BowerModule(), - new GruntModule(), - new MavenModule(), - new NpmModule() - ); + private final GitModule gitModule; + + private final List modules; + + public ModuleRunner() { + CommandExecutor commandExecutor = new CommandExecutor(); + + this.gitModule = new GitModule(commandExecutor); + + this.modules = Arrays.asList( + new BowerModule(commandExecutor), + new GruntModule(commandExecutor), + new MavenModule(commandExecutor), + new NpmModule(commandExecutor) + ); + } public void runAll(List folders) { folders.forEach(this::run); @@ -37,10 +48,10 @@ public void run(File folder) { String path = folder.getPath(); String folderName = folder.getName(); - if (GitModule.isProject(path)) { + if (gitModule.isProject(path)) { String typeName = ModuleTypeEnum.GIT.getTypeName(); printStartMessage(typeName, folderName); - GitModule.executeCommands(path); + gitModule.executeCommands(path); printFinishMessage(typeName, folderName); } diff --git a/src/main/java/com/github/gaboso/helper/CommandHelper.java b/src/main/java/com/github/gaboso/helper/CommandExecutor.java similarity index 88% rename from src/main/java/com/github/gaboso/helper/CommandHelper.java rename to src/main/java/com/github/gaboso/helper/CommandExecutor.java index 1837aa6..297639c 100644 --- a/src/main/java/com/github/gaboso/helper/CommandHelper.java +++ b/src/main/java/com/github/gaboso/helper/CommandExecutor.java @@ -12,14 +12,11 @@ * @since 1.0 * Command Helper */ -public class CommandHelper { +public class CommandExecutor { - private static final Logger LOGGER = LogManager.getLogger(CommandHelper.class.getName()); + private static final Logger LOGGER = LogManager.getLogger(CommandExecutor.class.getName()); - private CommandHelper() { - } - - public static void executeCMD(String path, String cmd) { + public void executeCMD(String path, String cmd) { OpSystemEnum currentOs = OpSystemEnum.getCurrentOs(); String runner = currentOs.getRunner(); String option = currentOs.getOption(); diff --git a/src/main/java/com/github/gaboso/module/GitModule.java b/src/main/java/com/github/gaboso/module/GitModule.java index a6cb504..024c9b6 100644 --- a/src/main/java/com/github/gaboso/module/GitModule.java +++ b/src/main/java/com/github/gaboso/module/GitModule.java @@ -1,6 +1,6 @@ package com.github.gaboso.module; -import com.github.gaboso.helper.CommandHelper; +import com.github.gaboso.helper.CommandExecutor; import com.github.gaboso.os.OpSystemEnum; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -17,10 +17,13 @@ public class GitModule { private static final Logger LOGGER = LogManager.getLogger(GitModule.class.getName()); - private GitModule() { + private final CommandExecutor commandExecutor; + + public GitModule(CommandExecutor commandExecutor) { + this.commandExecutor = commandExecutor; } - public static boolean isProject(String path) { + public boolean isProject(String path) { OpSystemEnum currentOs = OpSystemEnum.getCurrentOs(); String runner = currentOs.getRunner(); String option = currentOs.getOption(); @@ -40,9 +43,9 @@ public static boolean isProject(String path) { return false; } - public static void executeCommands(String projectPath) { + public void executeCommands(String projectPath) { String command = ModuleTypeEnum.GIT.getCommand(); - CommandHelper.executeCMD(projectPath, command); + commandExecutor.executeCMD(projectPath, command); } } diff --git a/src/main/java/com/github/gaboso/module/impl/BowerModule.java b/src/main/java/com/github/gaboso/module/impl/BowerModule.java index f259a1b..b84f20e 100644 --- a/src/main/java/com/github/gaboso/module/impl/BowerModule.java +++ b/src/main/java/com/github/gaboso/module/impl/BowerModule.java @@ -1,6 +1,6 @@ package com.github.gaboso.module.impl; -import com.github.gaboso.helper.CommandHelper; +import com.github.gaboso.helper.CommandExecutor; import com.github.gaboso.module.Module; import com.github.gaboso.module.ModuleTypeEnum; @@ -14,6 +14,12 @@ */ public class BowerModule implements Module { + private final CommandExecutor commandExecutor; + + public BowerModule(CommandExecutor commandExecutor) { + this.commandExecutor = commandExecutor; + } + @Override public boolean isProject(File[] listFiles) { return Arrays.stream(listFiles) @@ -26,7 +32,7 @@ public boolean isProject(File[] listFiles) { @Override public void executeCommands(String projectPath) { String command = this.getType().getCommand(); - CommandHelper.executeCMD(projectPath, command); + commandExecutor.executeCMD(projectPath, command); } @Override diff --git a/src/main/java/com/github/gaboso/module/impl/GruntModule.java b/src/main/java/com/github/gaboso/module/impl/GruntModule.java index 340d441..8f71e63 100644 --- a/src/main/java/com/github/gaboso/module/impl/GruntModule.java +++ b/src/main/java/com/github/gaboso/module/impl/GruntModule.java @@ -1,6 +1,6 @@ package com.github.gaboso.module.impl; -import com.github.gaboso.helper.CommandHelper; +import com.github.gaboso.helper.CommandExecutor; import com.github.gaboso.module.Module; import com.github.gaboso.module.ModuleTypeEnum; @@ -14,19 +14,25 @@ */ public class GruntModule implements Module { + private final CommandExecutor commandExecutor; + + public GruntModule(CommandExecutor commandExecutor) { + this.commandExecutor = commandExecutor; + } + @Override public boolean isProject(File[] listFiles) { return Arrays.stream(listFiles) .filter(Objects::nonNull) .filter(file -> !file.isDirectory()) .map(File::getName) - .anyMatch("gruntfile.js"::equals); + .anyMatch("gruntfile.js"::equalsIgnoreCase); } @Override public void executeCommands(String projectPath) { String command = this.getType().getCommand(); - CommandHelper.executeCMD(projectPath, command); + commandExecutor.executeCMD(projectPath, command); } @Override diff --git a/src/main/java/com/github/gaboso/module/impl/MavenModule.java b/src/main/java/com/github/gaboso/module/impl/MavenModule.java index 5c1573a..e368036 100644 --- a/src/main/java/com/github/gaboso/module/impl/MavenModule.java +++ b/src/main/java/com/github/gaboso/module/impl/MavenModule.java @@ -1,6 +1,6 @@ package com.github.gaboso.module.impl; -import com.github.gaboso.helper.CommandHelper; +import com.github.gaboso.helper.CommandExecutor; import com.github.gaboso.module.Module; import com.github.gaboso.module.ModuleTypeEnum; @@ -14,6 +14,12 @@ */ public class MavenModule implements Module { + private final CommandExecutor commandExecutor; + + public MavenModule(CommandExecutor commandExecutor) { + this.commandExecutor = commandExecutor; + } + @Override public boolean isProject(File[] listFiles) { return Arrays.stream(listFiles) @@ -26,7 +32,7 @@ public boolean isProject(File[] listFiles) { @Override public void executeCommands(String projectPath) { String command = this.getType().getCommand(); - CommandHelper.executeCMD(projectPath, command); + commandExecutor.executeCMD(projectPath, command); } @Override diff --git a/src/main/java/com/github/gaboso/module/impl/NpmModule.java b/src/main/java/com/github/gaboso/module/impl/NpmModule.java index 5834d4b..a37d417 100644 --- a/src/main/java/com/github/gaboso/module/impl/NpmModule.java +++ b/src/main/java/com/github/gaboso/module/impl/NpmModule.java @@ -1,6 +1,6 @@ package com.github.gaboso.module.impl; -import com.github.gaboso.helper.CommandHelper; +import com.github.gaboso.helper.CommandExecutor; import com.github.gaboso.module.Module; import com.github.gaboso.module.ModuleTypeEnum; @@ -14,6 +14,12 @@ */ public class NpmModule implements Module { + private final CommandExecutor commandExecutor; + + public NpmModule(CommandExecutor commandExecutor) { + this.commandExecutor = commandExecutor; + } + @Override public boolean isProject(File[] listFiles) { return Arrays.stream(listFiles) @@ -26,7 +32,7 @@ public boolean isProject(File[] listFiles) { @Override public void executeCommands(String projectPath) { String command = this.getType().getCommand(); - CommandHelper.executeCMD(projectPath, command); + commandExecutor.executeCMD(projectPath, command); } @Override diff --git a/src/test/java/com/github/gaboso/module/impl/BowerModuleTest.java b/src/test/java/com/github/gaboso/module/impl/BowerModuleTest.java new file mode 100644 index 0000000..2dbbe92 --- /dev/null +++ b/src/test/java/com/github/gaboso/module/impl/BowerModuleTest.java @@ -0,0 +1,54 @@ +package com.github.gaboso.module.impl; + +import com.github.gaboso.helper.CommandExecutor; +import com.github.gaboso.module.ModuleTypeEnum; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.io.File; + +@ExtendWith(MockitoExtension.class) +class BowerModuleTest { + + @Mock + CommandExecutor commandExecutor; + + @InjectMocks + BowerModule bowerModule; + + @Test + void isProject_WithValidFile_ReturnsTrue() throws NullPointerException { + File bowerJson = new File(getClass().getResource("/valid/bower/bower.json").getFile()); + File[] listFiles = {bowerJson}; + boolean result = bowerModule.isProject(listFiles); + Assertions.assertTrue(result); + } + + @Test + void isProject_WithInvalidFile_ReturnsFalse() throws NullPointerException { + File file = new File(getClass().getResource("/invalid/image.jpg").getFile()); + File[] listFiles = {file}; + boolean result = bowerModule.isProject(listFiles); + Assertions.assertFalse(result); + } + + @Test + void executeCommands_ShouldCallCommandExecutor() { + bowerModule.executeCommands("myProjectUsingBowerPath"); + + Mockito.verify(commandExecutor, Mockito.times(1)) + .executeCMD("myProjectUsingBowerPath", ModuleTypeEnum.BOWER.getCommand()); + } + + @Test + void getType_ReturnsBower() { + ModuleTypeEnum result = bowerModule.getType(); + Assertions.assertEquals(ModuleTypeEnum.BOWER, result); + } + +} \ No newline at end of file diff --git a/src/test/java/com/github/gaboso/module/impl/GruntModuleTest.java b/src/test/java/com/github/gaboso/module/impl/GruntModuleTest.java new file mode 100644 index 0000000..0a06227 --- /dev/null +++ b/src/test/java/com/github/gaboso/module/impl/GruntModuleTest.java @@ -0,0 +1,54 @@ +package com.github.gaboso.module.impl; + +import com.github.gaboso.helper.CommandExecutor; +import com.github.gaboso.module.ModuleTypeEnum; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.io.File; + +@ExtendWith(MockitoExtension.class) +class GruntModuleTest { + + @Mock + CommandExecutor commandExecutor; + + @InjectMocks + GruntModule gruntModule; + + @Test + void isProject_WithValidFile_ReturnsTrue() throws NullPointerException { + File gruntfileJs = new File(getClass().getResource("/valid/grunt/gruntfile.js").getFile()); + File[] listFiles = {gruntfileJs}; + boolean result = gruntModule.isProject(listFiles); + Assertions.assertTrue(result); + } + + @Test + void isProject_WithInvalidFile_ReturnsFalse() throws NullPointerException { + File file = new File(getClass().getResource("/invalid/image.jpg").getFile()); + File[] listFiles = {file}; + boolean result = gruntModule.isProject(listFiles); + Assertions.assertFalse(result); + } + + @Test + void executeCommands_ShouldCallCommandExecutor() { + gruntModule.executeCommands("myProjectUsingGruntPath"); + + Mockito.verify(commandExecutor, Mockito.times(1)) + .executeCMD("myProjectUsingGruntPath", ModuleTypeEnum.GRUNT.getCommand()); + } + + @Test + void getType_ReturnsGrunt() { + ModuleTypeEnum type = gruntModule.getType(); + Assertions.assertEquals(ModuleTypeEnum.GRUNT, type); + } + +} \ No newline at end of file diff --git a/src/test/resources/invalid/image.jpg b/src/test/resources/invalid/image.jpg new file mode 100644 index 0000000000000000000000000000000000000000..1914264c08781d1f30ee0b8482bccf44586f2dc1 GIT binary patch literal 95 zcmeAS@N?(olHy`uVBq!ia0vp^j3CU&3?x-=hn)ga%mF?ju0VQumF+E%TuG2$FoVOh l8)-lem#2$k2*>s01R$Gz9%CSj!PC{xWt~$(697H@6ZHT9 literal 0 HcmV?d00001 diff --git a/src/test/resources/valid/bower/bower.json b/src/test/resources/valid/bower/bower.json new file mode 100644 index 0000000..1030407 --- /dev/null +++ b/src/test/resources/valid/bower/bower.json @@ -0,0 +1,8 @@ +{ + "name": "example-app", + "version": "0.0.1", + "main": "public/javascripts/app.js", + "dependencies": { + "lodash": "~4.0.0" + } +} \ No newline at end of file diff --git a/src/test/resources/valid/grunt/gruntfile.js b/src/test/resources/valid/grunt/gruntfile.js new file mode 100644 index 0000000..00ec2a7 --- /dev/null +++ b/src/test/resources/valid/grunt/gruntfile.js @@ -0,0 +1,23 @@ +module.exports = function (grunt) { + + grunt.initConfig({ + jshint: { + files: ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js'], + options: { + globals: { + jQuery: true + } + } + }, + watch: { + files: ['<%= jshint.files %>'], + tasks: ['jshint'] + } + }); + + grunt.loadNpmTasks('grunt-contrib-jshint'); + grunt.loadNpmTasks('grunt-contrib-watch'); + + grunt.registerTask('default', ['jshint']); + +}; \ No newline at end of file From 0ad04d2e51ddffa21bcb33e595c8e0bf06ee789e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gaboso=E2=84=A2?= Date: Tue, 14 Feb 2023 21:15:49 -0300 Subject: [PATCH 3/5] Add unit tests to MavenModule and NpomModule --- .../gaboso/module/impl/MavenModuleTest.java | 54 +++++++++++++++++++ .../gaboso/module/impl/NpmModuleTest.java | 54 +++++++++++++++++++ src/test/resources/valid/maven/pom.xml | 6 +++ src/test/resources/valid/npm/package.json | 4 ++ 4 files changed, 118 insertions(+) create mode 100644 src/test/java/com/github/gaboso/module/impl/MavenModuleTest.java create mode 100644 src/test/java/com/github/gaboso/module/impl/NpmModuleTest.java create mode 100644 src/test/resources/valid/maven/pom.xml create mode 100644 src/test/resources/valid/npm/package.json diff --git a/src/test/java/com/github/gaboso/module/impl/MavenModuleTest.java b/src/test/java/com/github/gaboso/module/impl/MavenModuleTest.java new file mode 100644 index 0000000..9a9b5bc --- /dev/null +++ b/src/test/java/com/github/gaboso/module/impl/MavenModuleTest.java @@ -0,0 +1,54 @@ +package com.github.gaboso.module.impl; + +import com.github.gaboso.helper.CommandExecutor; +import com.github.gaboso.module.ModuleTypeEnum; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.io.File; + +@ExtendWith(MockitoExtension.class) +class MavenModuleTest { + + @Mock + CommandExecutor commandExecutor; + + @InjectMocks + MavenModule mavenModule; + + @Test + void isProject_WithValidFile_ReturnsTrue() throws NullPointerException { + File pomXml = new File(getClass().getResource("/valid/maven/pom.xml").getFile()); + File[] listFiles = {pomXml}; + boolean result = mavenModule.isProject(listFiles); + Assertions.assertTrue(result); + } + + @Test + void isProject_WithInvalidFile_ReturnsFalse() throws NullPointerException { + File file = new File(getClass().getResource("/invalid/image.jpg").getFile()); + File[] listFiles = {file}; + boolean result = mavenModule.isProject(listFiles); + Assertions.assertFalse(result); + } + + @Test + void executeCommands_ShouldCallCommandExecutor() { + mavenModule.executeCommands("myProjectUsingMavenPath"); + + Mockito.verify(commandExecutor, Mockito.times(1)) + .executeCMD("myProjectUsingMavenPath", ModuleTypeEnum.MAVEN.getCommand()); + } + + @Test + void getType_ReturnsMaven() { + ModuleTypeEnum type = mavenModule.getType(); + Assertions.assertEquals(ModuleTypeEnum.MAVEN, type); + } + +} \ No newline at end of file diff --git a/src/test/java/com/github/gaboso/module/impl/NpmModuleTest.java b/src/test/java/com/github/gaboso/module/impl/NpmModuleTest.java new file mode 100644 index 0000000..6d3129f --- /dev/null +++ b/src/test/java/com/github/gaboso/module/impl/NpmModuleTest.java @@ -0,0 +1,54 @@ +package com.github.gaboso.module.impl; + +import com.github.gaboso.helper.CommandExecutor; +import com.github.gaboso.module.ModuleTypeEnum; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.io.File; + +@ExtendWith(MockitoExtension.class) +class NpmModuleTest { + + @Mock + CommandExecutor commandExecutor; + + @InjectMocks + NpmModule npmModule; + + @Test + void isProject_WithValidFile_ReturnsTrue() throws NullPointerException { + File packageJson = new File(getClass().getResource("/valid/npm/package.json").getFile()); + File[] listFiles = {packageJson}; + boolean result = npmModule.isProject(listFiles); + Assertions.assertTrue(result); + } + + @Test + void isProject_WithInvalidFile_ReturnsFalse() throws NullPointerException { + File file = new File(getClass().getResource("/invalid/image.jpg").getFile()); + File[] listFiles = {file}; + boolean result = npmModule.isProject(listFiles); + Assertions.assertFalse(result); + } + + @Test + void executeCommands_ShouldCallCommandExecutor() { + npmModule.executeCommands("myProjectUsingNpmPath"); + + Mockito.verify(commandExecutor, Mockito.times(1)) + .executeCMD("myProjectUsingNpmPath", ModuleTypeEnum.NPM.getCommand()); + } + + @Test + void getType_ReturnsNpm() { + ModuleTypeEnum type = npmModule.getType(); + Assertions.assertEquals(ModuleTypeEnum.NPM, type); + } + +} \ No newline at end of file diff --git a/src/test/resources/valid/maven/pom.xml b/src/test/resources/valid/maven/pom.xml new file mode 100644 index 0000000..8829086 --- /dev/null +++ b/src/test/resources/valid/maven/pom.xml @@ -0,0 +1,6 @@ + + 4.0.0 + com.mycompany.app + my-app + 1 + \ No newline at end of file diff --git a/src/test/resources/valid/npm/package.json b/src/test/resources/valid/npm/package.json new file mode 100644 index 0000000..11ff496 --- /dev/null +++ b/src/test/resources/valid/npm/package.json @@ -0,0 +1,4 @@ +{ + "name": "my-program", + "version": "1.2.5" +} \ No newline at end of file From 1bb85c5900e8fcbe9698e2b4f05b130266937234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gaboso=E2=84=A2?= Date: Wed, 15 Feb 2023 19:57:55 -0300 Subject: [PATCH 4/5] Add UTs and also update changelog --- CHANGELOG.md | 21 +++++++++++- .../github/gaboso/module/ModuleTypeEnum.java | 4 +++ .../com/github/gaboso/os/OpSystemEnum.java | 4 +++ .../github/gaboso/os/OpSystemEnumTest.java | 33 +++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/github/gaboso/os/OpSystemEnumTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 046df4b..5a65578 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,11 +2,30 @@ All notable changes to this project will be documented in this file. +## [3.0.0] - 2023-02-15 + +### Changed +- Update log4j dependency from 2.18.0 to 2.19.0 +- Update maven-assembly-plugin dependency from 3.4.0 to 3.4.2 +- Improve logger logic in ModuleRunner +- Renamed class from CommandHelper to CommandExecutor + +### Added + +- Unit tests and unit tests dependencies: JUnit and mockito +- Created ModuleTypeEnum to store module name and commands +- Created OpSystemEnum to store OS runner and option + +### Removed + +- Formatter class +- OsHelper class + ## [2.0.0] - 2022-07-05 ### Changed -- Update log4j dependency from 1.17.2 to 1.18.0 +- Update log4j dependency from 2.17.2 to 2.18.0 - Update maven-assembly-plugin dependency from 3.3.0 to 3.4.0 - Split Scriptum class in other two classes - Change jar entry point from Scriptum class to Main class diff --git a/src/main/java/com/github/gaboso/module/ModuleTypeEnum.java b/src/main/java/com/github/gaboso/module/ModuleTypeEnum.java index 58bf3b8..2d27e4e 100644 --- a/src/main/java/com/github/gaboso/module/ModuleTypeEnum.java +++ b/src/main/java/com/github/gaboso/module/ModuleTypeEnum.java @@ -1,5 +1,9 @@ package com.github.gaboso.module; +/** + * @since 3.0.0 + * ModuleTypeEnum + */ public enum ModuleTypeEnum { MAVEN("Maven", "mvn clean install -DskipTests=true"), diff --git a/src/main/java/com/github/gaboso/os/OpSystemEnum.java b/src/main/java/com/github/gaboso/os/OpSystemEnum.java index 48dd092..520efdc 100644 --- a/src/main/java/com/github/gaboso/os/OpSystemEnum.java +++ b/src/main/java/com/github/gaboso/os/OpSystemEnum.java @@ -1,5 +1,9 @@ package com.github.gaboso.os; +/** + * @since 3.0.0 + * OpSystemEnum + */ public enum OpSystemEnum { WINDOWS("cmd.exe", "/c"), diff --git a/src/test/java/com/github/gaboso/os/OpSystemEnumTest.java b/src/test/java/com/github/gaboso/os/OpSystemEnumTest.java new file mode 100644 index 0000000..01f1884 --- /dev/null +++ b/src/test/java/com/github/gaboso/os/OpSystemEnumTest.java @@ -0,0 +1,33 @@ +package com.github.gaboso.os; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class OpSystemEnumTest { + + @Test + void windows_GetOption_AssertTrue() { + String result = OpSystemEnum.WINDOWS.getOption(); + Assertions.assertEquals("/c", result); + } + + @Test + void windows_GetRunner_AssertTrue() { + String result = OpSystemEnum.WINDOWS.getRunner(); + Assertions.assertEquals("cmd.exe", result); + } + + @Test + void linux_GetOption_AssertTrue() { + String result = OpSystemEnum.LINUX.getOption(); + Assertions.assertEquals("-c", result); + } + + @Test + void linux_GetRunner_AssertTrue() { + String result = OpSystemEnum.LINUX.getRunner(); + Assertions.assertEquals("/bin/bash", result); + } + + +} \ No newline at end of file From 776aa9e7dc37e886852e9c0e509a5e333d13db25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gaboso=E2=84=A2?= Date: Wed, 15 Feb 2023 19:59:54 -0300 Subject: [PATCH 5/5] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a65578..cc7d0a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ All notable changes to this project will be documented in this file. ### Changed - Update log4j dependency from 2.18.0 to 2.19.0 -- Update maven-assembly-plugin dependency from 3.4.0 to 3.4.2 +- Update maven-assembly-plugin dependency from 3.4.1 to 3.4.2 - Improve logger logic in ModuleRunner - Renamed class from CommandHelper to CommandExecutor