[codegen ignore] normalize path separator for Windows, add *.ext tests (#4476)

* [codegen ignore] Fix windows paths for ignore processing

* [codegen ignore] Add missing glob rule tests
This commit is contained in:
Jim Schubert 2017-01-03 03:56:50 -05:00 committed by wing328
parent 0252d1a534
commit c94e18abd8
3 changed files with 47 additions and 5 deletions

View File

@ -474,7 +474,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
if (!of.isDirectory()) { if (!of.isDirectory()) {
of.mkdirs(); of.mkdirs();
} }
String outputFilename = outputFolder + File.separator + support.destinationFilename; String outputFilename = outputFolder + File.separator + support.destinationFilename.replace('/', File.separatorChar);
if (!config.shouldOverwrite(outputFilename)) { if (!config.shouldOverwrite(outputFilename)) {
LOGGER.info("Skipped overwriting " + outputFilename); LOGGER.info("Skipped overwriting " + outputFilename);
continue; continue;
@ -657,7 +657,8 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
} }
private File processTemplateToFile(Map<String, Object> templateData, String templateName, String outputFilename) throws IOException { private File processTemplateToFile(Map<String, Object> templateData, String templateName, String outputFilename) throws IOException {
if(ignoreProcessor.allowsFile(new File(outputFilename.replaceAll("//", "/")))) { String adjustedOutputFilename = outputFilename.replaceAll("//", "/").replace('/', File.separatorChar);
if(ignoreProcessor.allowsFile(new File(adjustedOutputFilename))) {
String templateFile = getFullTemplateFile(config, templateName); String templateFile = getFullTemplateFile(config, templateName);
String template = readTemplate(templateFile); String template = readTemplate(templateFile);
Mustache.Compiler compiler = Mustache.compiler(); Mustache.Compiler compiler = Mustache.compiler();
@ -672,11 +673,11 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
.defaultValue("") .defaultValue("")
.compile(template); .compile(template);
writeToFile(outputFilename, tmpl.execute(templateData)); writeToFile(adjustedOutputFilename, tmpl.execute(templateData));
return new File(outputFilename); return new File(adjustedOutputFilename);
} }
LOGGER.info("Skipped generation of " + outputFilename + " due to rule in .swagger-codegen-ignore"); LOGGER.info("Skipped generation of " + adjustedOutputFilename + " due to rule in .swagger-codegen-ignore");
return null; return null;
} }

View File

@ -107,6 +107,8 @@ public class CodegenIgnoreProcessorTest {
return new Object[] { return new Object[] {
// Matching filenames // Matching filenames
new CodegenIgnoreProcessorTest("build.sh", "build.sh", "A file when matching should ignore.").ignored(), new CodegenIgnoreProcessorTest("build.sh", "build.sh", "A file when matching should ignore.").ignored(),
new CodegenIgnoreProcessorTest("build.sh", "*.sh", "A file when matching glob should ignore.").ignored(),
new CodegenIgnoreProcessorTest("src/build.sh", "*.sh", "A nested file when matching non-nested simple glob should allow.").allowed(),
new CodegenIgnoreProcessorTest("src/build.sh", "**/build.sh", "A file when matching nested files should ignore.").ignored(), new CodegenIgnoreProcessorTest("src/build.sh", "**/build.sh", "A file when matching nested files should ignore.").ignored(),
new CodegenIgnoreProcessorTest("Build.sh", "build.sh", "A file when non-matching should allow.").allowed().skipOnCondition(SystemUtils.IS_OS_WINDOWS), new CodegenIgnoreProcessorTest("Build.sh", "build.sh", "A file when non-matching should allow.").allowed().skipOnCondition(SystemUtils.IS_OS_WINDOWS),
new CodegenIgnoreProcessorTest("build.sh", "/build.sh", "A rooted file when matching should ignore.").ignored(), new CodegenIgnoreProcessorTest("build.sh", "/build.sh", "A rooted file when matching should ignore.").ignored(),

View File

@ -67,4 +67,43 @@ public class FileRuleTest {
assertFalse(actual); assertFalse(actual);
} }
@Test
public void testGlobbingRecursive() throws Exception {
// Arrange
final String definition = "*.txt";
final String relativePath = "path/to/some/nested/location/xyzzy.txt";
// Act
final List<Part> syntax = Arrays.asList(
new Part(IgnoreLineParser.Token.MATCH_ALL),
new Part(IgnoreLineParser.Token.DIRECTORY_MARKER),
new Part(IgnoreLineParser.Token.MATCH_ANY),
new Part(IgnoreLineParser.Token.TEXT, ".txt")
);
Rule rule = new FileRule(syntax, definition);
Boolean actual = rule.matches(relativePath);
// Assert
assertTrue(actual);
}
@Test
public void testGlobbingNotRecursive() throws Exception {
// Arrange
final String definition = "*.txt";
final String relativePath = "path/to/some/nested/location/xyzzy.txt";
// Act
final List<Part> syntax = Arrays.asList(
new Part(IgnoreLineParser.Token.MATCH_ANY),
new Part(IgnoreLineParser.Token.TEXT, ".txt")
);
Rule rule = new FileRule(syntax, definition);
Boolean actual = rule.matches(relativePath);
// Assert
assertFalse(actual);
}
} }