Skip to content

Commit

Permalink
Issue 214: Support of config bundles with extra configuration files
Browse files Browse the repository at this point in the history
  • Loading branch information
piyush kumar sadangi authored and piyush kumar sadangi committed Nov 26, 2024
1 parent 250736e commit 6de8ea6
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 10 deletions.
1 change: 1 addition & 0 deletions RegexpHeader/Example2/extra-config-files.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
java.header
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,31 @@ public final class CheckstyleExampleExtractor {
*/
private static final int BUFFER_SIZE = 1024;

/**
* Number of expected arguments when processing a configuration file.
*/
private static final int CONFIG_FILE_ARG_COUNT = 5;

/**
* Index of the "--config-file" flag in the argument array.
*/
private static final int CONFIG_FILE_FLAG_INDEX = 1;

/**
* Index of the configuration file path in the argument array.
*/
private static final int CONFIG_FILE_PATH_INDEX = 2;

/**
* Index of the output file path in the argument array.
*/
private static final int CONFIG_OUTPUT_PATH_INDEX = 3;

/**
* Index of the projects output path in the argument array.
*/
private static final int CONFIG_PROJECTS_PATH_INDEX = 4;

/**
* Private constructor to prevent instantiation of this utility class.
*/
Expand Down Expand Up @@ -147,6 +172,19 @@ public static void main(final String[] args) throws Exception {
// Output default projects list
outputDefaultProjectsList(projectsOutputPath);
}
else if (args.length == CONFIG_FILE_ARG_COUNT
&& "--config-file".equals(args[CONFIG_FILE_FLAG_INDEX])) {
// New functionality: process external config file
final String configFilePath = args[CONFIG_FILE_PATH_INDEX];
final String configOutputPath = args[CONFIG_OUTPUT_PATH_INDEX];
final String projectsOutputPath = args[CONFIG_PROJECTS_PATH_INDEX];

// Process config file and generate output
processConfigFile(Paths.get(configFilePath), Paths.get(configOutputPath));

// Output default projects list
outputDefaultProjectsList(projectsOutputPath);
}
else {
// Functionality: process all examples
final String checkstyleRepoPath = args[0];
Expand All @@ -162,6 +200,33 @@ public static void main(final String[] args) throws Exception {
}
}

/**
* Processes a configuration file and generates an output file.
*
* @param configFile The path to the configuration file
* @param outputFile The path to the output file
* @throws Exception If an error occurs during processing
* @throws IOException If an error occurs during processing
*/
public static void processConfigFile(
final Path configFile,
final Path outputFile)
throws Exception {
// Check if the config file exists
if (!Files.exists(configFile)) {
LOGGER.severe("Config file does not exist: " + configFile);
throw new IOException("Config file does not exist: " + configFile);
}

// Read the config file content
final String configContent = Files.readString(configFile, StandardCharsets.UTF_8);

// Write the config content to the output file
Files.writeString(outputFile, configContent, StandardCharsets.UTF_8);

LOGGER.info("Generated configuration at " + outputFile);
}

/**
* Writes the default projects list to the specified file.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,20 @@
*/
class MainsLauncherTest {

/**
* The base path to the Checkstyle repository.
*/
/** The base path to the Checkstyle repository. */
private static final String CHECKSTYLE_REPO_PATH = "../.ci-temp/checkstyle";

/**
* Base path for Checkstyle checks package used in test resource files.
*/
/** Base path for Checkstyle checks package used in test resource files. */
private static final String CHECKSTYLE_CHECKS_BASE_PATH =
"com/puppycrawl/tools/checkstyle/checks";

/**
* The constant for default projects file.
*/
/** The constant for default projects file. */
private static final String DEFAULT_PROJECTS_FILE = "list-of-projects.yml";

/** Temporary folder for test files. */
@TempDir
Path temporaryFolder;

/**
* Tests the main method of CheckstyleExampleExtractor.
* This test ensures that the main method runs without throwing any exceptions.
Expand Down Expand Up @@ -122,7 +120,6 @@ private String loadDefaultProjectsList() throws IOException {

/**
* Tests the getTemplateFilePathForInputFile method.
*
*/
@Test
void testGetTemplateFilePathForInputFile() throws Exception {
Expand All @@ -143,4 +140,85 @@ private String loadToString(final String filePath) throws IOException {
final byte[] encoded = Files.readAllBytes(Paths.get(filePath));
return new String(encoded, StandardCharsets.UTF_8);
}

/**
* Tests the external config files functionality.
*/
@Test
void testMainWithExternalConfigFiles(@TempDir final Path tempDir) throws Exception {
// Create the 'config' directory inside the tempDir
final Path configDir = tempDir.resolve("config");
Files.createDirectories(configDir);

// Write the java.header file inside the 'config' directory
final String headerFileContent = """
^// Copyright \\(C\\) (\\d\\d\\d\\d -)? 2004 MyCompany$
^// All rights reserved$
""";

final Path headerFile = configDir.resolve("java.header");
Files.writeString(headerFile, headerFileContent);

// Write the config.xml file with ${execution.path} - this is key for DiffTool
final String configXmlContent = """
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="RegexpHeader">
<property name="headerFile" value="${execution.path}/config/java.header"/>
<property name="multiLines" value="10, 13"/>
</module>
</module>
""";

final Path configXmlFile = tempDir.resolve("config.xml");
Files.writeString(configXmlFile, configXmlContent);

// Write the test input file (could be an empty Java file)
final String testFileContent = """
// Sample header content
// That should be matched
public class TestClass {
// class content
}
""";

final Path testFile = tempDir.resolve("TestClass.java");
Files.writeString(testFile, testFileContent);

// Define the output files for the extractor
final Path outputConfigFile = tempDir.resolve("output-config.xml");
final Path outputProjectsFile = tempDir.resolve("output-projects.yml");

// Modify the CheckstyleExampleExtractor to accept a config file
assertDoesNotThrow(() -> {
CheckstyleExampleExtractor.main(new String[]{
CHECKSTYLE_REPO_PATH,
"--config-file",
configXmlFile.toString(),
outputConfigFile.toString(),
outputProjectsFile.toString(),
});
});

// Verify that the output config file is created
assertTrue(Files.exists(outputConfigFile), "Config output file should be created");

// Load the generated config content
final String generatedConfigContent = Files.readString(outputConfigFile);

// Verify that the generated config includes the external file reference with execution.path
assertThat(generatedConfigContent)
.contains("<property name=\"headerFile\" "
+ "value=\"${execution.path}/config/java.header\"/>");

// Verify that the projects file is created and not empty
assertTrue(Files.exists(outputProjectsFile), "Projects output file should be created");
final String generatedProjectsContent = Files.readString(outputProjectsFile);
assertFalse(generatedProjectsContent.isEmpty(), "Projects file should not be empty");
}
}

0 comments on commit 6de8ea6

Please sign in to comment.