diff --git a/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/CodeInspector.java b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/CodeInspector.java index a1c1062dab114ecd565e14e4bfcb8550330fbf58..3ca1bb85daa38295023b41ecf3b7d98c66f24a19 100644 --- a/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/CodeInspector.java +++ b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/CodeInspector.java @@ -4,40 +4,57 @@ import com.huawei.devkit.code.inspector.entity.CliOptions; import com.huawei.devkit.code.inspector.perload.DataBasePreLoad; import com.huawei.devkit.code.inspector.utils.PropertiesUtils; import com.huawei.devkit.code.inspector.wrappers.CheckStyleWrapper; +import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import lombok.extern.slf4j.Slf4j; import picocli.CommandLine; +import java.io.IOException; import java.util.Locale; import java.util.Properties; @Slf4j public class CodeInspector { + private static final int CODE_ERROR = 1; + private static final int PARAMETER_ERROR = 2; + private static final int OTHER_ERROR = 3; public static void main(String[] args) { final CliOptions cliOptions = new CliOptions(); final CommandLine commandLine = new CommandLine(cliOptions); try { - commandLine.setUsageHelpWidth(CliOptions.HELP_WIDTH); - commandLine.setCaseInsensitiveEnumValuesAllowed(true); - CommandLine.ParseResult parseResult = commandLine.parseArgs(args); - if (!CommandLine.printHelpIfRequested(parseResult)) { - log.info("start enter log"); - Properties properties = PropertiesUtils.loadProperties("config.properties"); - PropertiesUtils.configAndUpdate(properties); - if (cliOptions.getConfigurationFile() == null || cliOptions.getConfigurationFile().isEmpty()) { - cliOptions.setConfigurationFile(PropertiesUtils.ROOT_DIR + "/config/devkit_checkstyle.xml"); - } - DataBasePreLoad.preload(properties); - Locale.setDefault(Locale.SIMPLIFIED_CHINESE); - CheckStyleWrapper.checkStyle(cliOptions); + int ret = parseArgsAndExecute(cliOptions, commandLine, args); + if (ret > 0) { + Runtime.getRuntime().exit(CODE_ERROR); } } catch (CommandLine.ParameterException ex) { log.error("error", ex); System.err.println(ex.getMessage()); commandLine.usage(System.err); + Runtime.getRuntime().exit(PARAMETER_ERROR); } catch (Exception ex) { log.error("error", ex); System.err.println(ex.getMessage()); + Runtime.getRuntime().exit(OTHER_ERROR); } } + + public static int parseArgsAndExecute(CliOptions cliOptions, CommandLine commandLine, String[] args) + throws IOException, CheckstyleException { + commandLine.setUsageHelpWidth(CliOptions.HELP_WIDTH); + commandLine.setCaseInsensitiveEnumValuesAllowed(true); + CommandLine.ParseResult parseResult = commandLine.parseArgs(args); + if (!CommandLine.printHelpIfRequested(parseResult)) { + log.info("start enter log"); + Properties properties = PropertiesUtils.loadProperties("config.properties"); + PropertiesUtils.configAndUpdate(properties); + if (cliOptions.getConfigurationFile() == null || cliOptions.getConfigurationFile().isEmpty()) { + cliOptions.setConfigurationFile(PropertiesUtils.ROOT_DIR + "/config/devkit_checkstyle.xml"); + } + DataBasePreLoad.preload(properties); + Locale.setDefault(Locale.SIMPLIFIED_CHINESE); + return CheckStyleWrapper.checkStyle(cliOptions); + } + return 0; + + } } diff --git a/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/entity/CliOptions.java b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/entity/CliOptions.java index fc12f48b05d60af52936e863462fd766413b70db..87ab0cc7412f2872fdb3679aa5bd7487552c7691 100644 --- a/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/entity/CliOptions.java +++ b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/entity/CliOptions.java @@ -107,9 +107,12 @@ public class CliOptions { @CommandLine.Parameters(arity = "1..*", description = "One or more source files to verify.") public void setFiles(List files) { for (File file : files) { - if (!file.exists() || !file.canRead()) { + if (!file.exists()) { + continue; + } + if (!file.canRead()) { throw new CommandLine.ParameterException(spec.commandLine(), - String.format("%s: the file does not exist or the current user " + + String.format("%s: the current user " + "does not have read permissions to the file", file.getPath())); } } @@ -141,11 +144,15 @@ public class CliOptions { + " the configuration module. By default, the devkit_checkstyle.xml in the config directory is used.") public void setConfigurationFile(String configurationFile) { Path configuration = Paths.get(configurationFile); - if (!Files.exists(configuration) || Files.isDirectory(configuration) || !Files.isReadable(configuration)) { + if (!Files.exists(configuration) || !Files.isReadable(configuration)) { throw new CommandLine.ParameterException(spec.commandLine(), "The configuration file does not exist " + "or the current user does not have read permissions to the configuration file !"); } + if (Files.isDirectory(configuration)) { + throw new CommandLine.ParameterException(spec.commandLine(), "The configuration file cannot be a directory"); + } + try { ConfigurationLoader.loadConfiguration( configurationFile, new PropertiesExpander(System.getProperties())); diff --git a/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/wrappers/CheckStyleWrapper.java b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/wrappers/CheckStyleWrapper.java index d52f5a2d7ca20090fc3ce14c34556c73ea3603d5..cb78cefae3428c0b81df5c754797bb6487e26077 100644 --- a/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/wrappers/CheckStyleWrapper.java +++ b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/wrappers/CheckStyleWrapper.java @@ -63,19 +63,18 @@ public class CheckStyleWrapper { * * @throws IOException if there is a problem with files access **/ - public static void checkStyle(CliOptions cliOptions) throws IOException, CheckstyleException { + public static int checkStyle(CliOptions cliOptions) throws IOException, CheckstyleException { int errorCounter = 0; - try { - final List filesToProcess = getFilesToProcess(cliOptions); - errorCounter = runCheckstyle(cliOptions, filesToProcess); - } finally { - if (errorCounter > 0) { - final LocalizedMessage errorCounterViolation = new LocalizedMessage( - Definitions.CHECKSTYLE_BUNDLE, Main.class, - ERROR_COUNTER, String.valueOf(errorCounter)); - log.error(errorCounterViolation.getMessage()); - } + final List filesToProcess = getFilesToProcess(cliOptions); + errorCounter = runCheckstyle(cliOptions, filesToProcess); + if (errorCounter > 0) { + final LocalizedMessage errorCounterViolation = new LocalizedMessage( + Definitions.CHECKSTYLE_BUNDLE, Main.class, + ERROR_COUNTER, String.valueOf(errorCounter)); + log.error(errorCounterViolation.getMessage()); } + return errorCounter; + } /** diff --git a/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/BlockCheckTest.java b/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/BlockCheckTest.java index 66623a1cbda753b699a2273994d30b8b069cd0ae..e913d13360845a96897438274abe00127d38bde8 100644 --- a/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/BlockCheckTest.java +++ b/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/BlockCheckTest.java @@ -1,10 +1,12 @@ package com.huawei.devkit.code.inspector; +import com.huawei.devkit.code.inspector.entity.CliOptions; import com.huawei.devkit.code.inspector.utils.TestUtil; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; +import picocli.CommandLine; import java.util.Objects; @@ -29,6 +31,15 @@ public class BlockCheckTest { String configPath = Objects.requireNonNull(this.getClass().getClassLoader() .getResource("single_rules/block_checks/" + module + ".xml")).getPath(); String[] args = new String[]{"-c", configPath, "-o", root + "/test" + caseName + ".out", "-f", "json", filePath}; - Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + final CliOptions cliOptions = new CliOptions(); + final CommandLine commandLine = new CommandLine(cliOptions); + Assertions.assertDoesNotThrow(() -> { + try { + CodeInspector.parseArgsAndExecute(cliOptions, commandLine, args); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + ); } } \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/NamingConventionsTest.java b/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/NamingConventionsTest.java index e6df0b6eb4514c193c2ea8a22227497f79730a01..cffc76532c71772d8c99e3098c416af7c2d9a0e1 100644 --- a/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/NamingConventionsTest.java +++ b/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/NamingConventionsTest.java @@ -1,10 +1,12 @@ package com.huawei.devkit.code.inspector; +import com.huawei.devkit.code.inspector.entity.CliOptions; import com.huawei.devkit.code.inspector.utils.TestUtil; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import picocli.CommandLine; import java.util.Objects; @@ -22,7 +24,7 @@ public class NamingConventionsTest { void testNamingConventions(String rule) { TestUtil.execute("naming_conventions", rule); } - + @Test void testPackageName() { String root = System.getProperty("user.dir"); @@ -31,7 +33,16 @@ public class NamingConventionsTest { String configPath = Objects.requireNonNull(this.getClass().getClassLoader() .getResource("single_rules/naming_conventions/PackageName.xml")).getPath(); String[] args = new String[]{"-c", configPath, "-o", root + "/testPackageName.out", "-f", "json", filePath}; - Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + final CliOptions cliOptions = new CliOptions(); + final CommandLine commandLine = new CommandLine(cliOptions); + Assertions.assertDoesNotThrow(() -> { + try { + CodeInspector.parseArgsAndExecute(cliOptions, commandLine, args); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + ); } } diff --git a/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/utils/TestUtil.java b/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/utils/TestUtil.java index f34f0bfc16876d12e3049d23dcf5f726db28717b..d280935ecca6079c1cca1293d96cdee158a0e99d 100644 --- a/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/utils/TestUtil.java +++ b/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/utils/TestUtil.java @@ -1,7 +1,9 @@ package com.huawei.devkit.code.inspector.utils; import com.huawei.devkit.code.inspector.CodeInspector; +import com.huawei.devkit.code.inspector.entity.CliOptions; import org.junit.jupiter.api.Assertions; +import picocli.CommandLine; import java.util.Objects; @@ -18,6 +20,15 @@ public class TestUtil { String configPath = Objects.requireNonNull(TestUtil.class.getClassLoader() .getResource("single_rules/" + module + "/" + rule + ".xml")).getPath(); String[] args = new String[]{"-c", configPath, "-o", root + "/test" + rule + ".out", "-f", "json", filePath}; - Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + final CliOptions cliOptions = new CliOptions(); + final CommandLine commandLine = new CommandLine(cliOptions); + Assertions.assertDoesNotThrow(() -> { + try { + CodeInspector.parseArgsAndExecute(cliOptions, commandLine, args); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + ); } } diff --git a/tools/download_and_deploy/src/download/download_config.py b/tools/download_and_deploy/src/download/download_config.py index 15be33d7fdb885a1fd9199623b00875d143e42bc..31c3cac5ad2439d6647ee4e01db40ab916c13beb 100644 --- a/tools/download_and_deploy/src/download/download_config.py +++ b/tools/download_and_deploy/src/download/download_config.py @@ -28,7 +28,7 @@ CompatibilityTesting = { DevKitTester = { "component_name": "DevKitTester", "file": "https://gitee.com/openeuler/devkit-pipeline/releases/download/v1.0.3/devkit_tester.tar.gz", - "file_size": "36153017", + "file_size": "36156994", } A_FOT = {