[feature] Support for .swagger-codegen-ignore

Adds a .swagger-codegen-ignore file with instructions and examples.
The .swagger-codegen-ignore file is treated as a supporting file.

Every project will generate a .swagger-codegen-ignore file containing
instructions and examples.

This also adds support for 'common' files (defaults like
.swagger-codegen-ignore). In the case of the ignore file, a generator
may include a compiled template ignore file which outputs to the
outputDir folder as .swagger-codegen-ignore and the default file
generation will honor the already generated file.

The rules for .swagger-codegen-ignore are a simple subset of what you'd
find in .gitignore or .dockerignore. It supports recursive matching
(**), simple matching (*), matching files in the project root
(/filename), matching against directories (dir/), negation rules
(!previously/excluded/**/file).
This commit is contained in:
Jim Schubert
2016-05-15 21:24:32 -04:00
parent ea15b72b3f
commit 21e2b7bb2a
19 changed files with 1310 additions and 113 deletions

View File

@@ -0,0 +1,21 @@
package io.swagger.codegen.ignore;
import org.testng.annotations.Test;
public class CodegenIgnoreProcessorTest {
@Test
public void loadCodegenRules() throws Exception {
}
@Test
public void getInclusionRules() throws Exception {
}
@Test
public void getExclusionRules() throws Exception {
}
}

View File

@@ -0,0 +1,70 @@
package io.swagger.codegen.ignore.rules;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.List;
import static org.testng.Assert.*;
public class FileRuleTest {
@Test
public void testMatchComplex() throws Exception {
// Arrange
final String definition = "path/to/**/complex/*.txt";
final String relativePath = "path/to/some/nested/complex/xyzzy.txt";
final List<Part> syntax = Arrays.asList(
new Part(IgnoreLineParser.Token.ROOTED_MARKER),
new Part(IgnoreLineParser.Token.TEXT, "path"),
new Part(IgnoreLineParser.Token.PATH_DELIM),
new Part(IgnoreLineParser.Token.TEXT, "to"),
new Part(IgnoreLineParser.Token.PATH_DELIM),
new Part(IgnoreLineParser.Token.MATCH_ALL),
new Part(IgnoreLineParser.Token.PATH_DELIM),
new Part(IgnoreLineParser.Token.TEXT, "complex"),
new Part(IgnoreLineParser.Token.PATH_DELIM),
new Part(IgnoreLineParser.Token.MATCH_ANY),
new Part(IgnoreLineParser.Token.TEXT, ".txt")
);
Rule rule = new FileRule(syntax, definition);
Boolean actual = null;
// Act
actual = rule.matches(relativePath);
// Assert
assertTrue(actual);
}
@Test
public void testNonMatchComplex() throws Exception {
// Arrange
final String definition = "path/to/**/complex/*.txt";
final String relativePath = "path/to/some/nested/invalid/xyzzy.txt";
final List<Part> syntax = Arrays.asList(
new Part(IgnoreLineParser.Token.ROOTED_MARKER),
new Part(IgnoreLineParser.Token.TEXT, "path"),
new Part(IgnoreLineParser.Token.PATH_DELIM),
new Part(IgnoreLineParser.Token.TEXT, "to"),
new Part(IgnoreLineParser.Token.PATH_DELIM),
new Part(IgnoreLineParser.Token.MATCH_ALL),
new Part(IgnoreLineParser.Token.TEXT, "complex"),
new Part(IgnoreLineParser.Token.PATH_DELIM),
new Part(IgnoreLineParser.Token.MATCH_ANY),
new Part(IgnoreLineParser.Token.TEXT, ".txt")
);
Rule rule = new FileRule(syntax, definition);
Boolean actual = null;
// Act
actual = rule.matches(relativePath);
// Assert
assertFalse(actual);
}
}

View File

@@ -0,0 +1,158 @@
package io.swagger.codegen.ignore.rules;
import org.testng.annotations.Test;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import static org.testng.Assert.*;
public class IgnoreLineParserTest {
private IgnoreLineParser.Token verifyInputToSingleToken(final String input, IgnoreLineParser.Token token) throws ParserException {
// Act
List<Part> result = IgnoreLineParser.parse(input);
// Assert
assertNotNull(result);
assertEquals(result.size(), 1);
IgnoreLineParser.Token actual = result.get(0).getToken();
assertEquals(actual, token);
return actual;
}
@Test
public void parseMatchAll() throws Exception {
verifyInputToSingleToken("**", IgnoreLineParser.Token.MATCH_ALL);
}
@Test
public void parseMatchAny() throws Exception {
verifyInputToSingleToken("*", IgnoreLineParser.Token.MATCH_ANY);
}
@Test(expectedExceptions = ParserException.class,
expectedExceptionsMessageRegExp = "Negation with no negated pattern\\.")
public void parseNegate() throws Exception {
verifyInputToSingleToken("!", IgnoreLineParser.Token.NEGATE);
// Assert
fail("Expected simple pattern '!' to throw a ParserException.");
}
@Test
public void parseComment() throws Exception {
// Arrange
final String input = "# This is a comment";
Part actual = null;
// Act
List<Part> result = IgnoreLineParser.parse(input);
// Assert
assertEquals(result.size(), 1);
actual = result.get(0);
assertEquals(actual.getToken(), IgnoreLineParser.Token.COMMENT);
assertEquals(actual.getValue(), input);
}
@Test
public void parseEscapedExclamation() throws Exception {
final String input = "\\!";
verifyInputToSingleToken(input, IgnoreLineParser.Token.ESCAPED_EXCLAMATION);
}
@Test
public void parseEscapedSpace() throws Exception {
final String input = "\\ ";
verifyInputToSingleToken(input, IgnoreLineParser.Token.ESCAPED_SPACE);
}
@Test
public void parseDirectoryMarker() throws Exception {
// Arrange
final String input = "foo/";
Part actual = null;
// Act
List<Part> result = IgnoreLineParser.parse(input);
// Assert
assertEquals(result.size(), 2);
actual = result.get(0);
assertEquals(actual.getToken(), IgnoreLineParser.Token.TEXT);
assertEquals(actual.getValue(), "foo");
actual = result.get(1);
assertEquals(actual.getToken(), IgnoreLineParser.Token.DIRECTORY_MARKER);
}
@Test
public void parseRooted() throws Exception {
// Arrange
final String input = "/abcd";
Part actual = null;
// Act
List<Part> result = IgnoreLineParser.parse(input);
// Assert
assertEquals(result.size(), 2);
actual = result.get(0);
assertEquals(actual.getToken(), IgnoreLineParser.Token.ROOTED_MARKER);
actual = result.get(1);
assertEquals(actual.getToken(), IgnoreLineParser.Token.TEXT);
assertEquals(actual.getValue(), "abcd");
}
@Test
public void parseComplex() throws Exception {
// Arrange
final String input = "**/abcd/**/foo/bar/sample.txt";
Part current = null;
// Act
Queue<Part> result = new LinkedList<>(IgnoreLineParser.parse(input));
// Assert
current = result.remove();
assertEquals(current.getToken(), IgnoreLineParser.Token.MATCH_ALL);
current = result.remove();
assertEquals(current.getToken(), IgnoreLineParser.Token.PATH_DELIM);
current = result.remove();
assertEquals(current.getToken(), IgnoreLineParser.Token.TEXT);
assertEquals(current.getValue(), "abcd");
current = result.remove();
assertEquals(current.getToken(), IgnoreLineParser.Token.PATH_DELIM);
current = result.remove();
assertEquals(current.getToken(), IgnoreLineParser.Token.MATCH_ALL);
current = result.remove();
assertEquals(current.getToken(), IgnoreLineParser.Token.PATH_DELIM);
current = result.remove();
assertEquals(current.getToken(), IgnoreLineParser.Token.TEXT);
assertEquals(current.getValue(), "foo");
current = result.remove();
assertEquals(current.getToken(), IgnoreLineParser.Token.PATH_DELIM);
current = result.remove();
assertEquals(current.getToken(), IgnoreLineParser.Token.TEXT);
assertEquals(current.getValue(), "bar");
current = result.remove();
assertEquals(current.getToken(), IgnoreLineParser.Token.PATH_DELIM);
current = result.remove();
assertEquals(current.getToken(), IgnoreLineParser.Token.TEXT);
assertEquals(current.getValue(), "sample.txt");
}
@Test(expectedExceptions = ParserException.class,
expectedExceptionsMessageRegExp = "The pattern \\*\\*\\* is invalid\\.")
public void parseTripleStarPattern() throws Exception {
// Arrange
final String input = "should/throw/***/anywhere";
// Act
List<Part> result = IgnoreLineParser.parse(input);
// Assert
fail("Expected pattern containing '***' to throw a ParserException.");
}
}

View File

@@ -0,0 +1,285 @@
package io.swagger.codegen.ignore.rules;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.List;
import static org.testng.Assert.*;
public class RootedFileRuleTest {
@Test
public void testMatchFilenameOnly() throws Exception {
// Arrange
final String definition = "/foo";
final String relativePath = "foo";
final List<Part> syntax = Arrays.asList(
new Part(IgnoreLineParser.Token.ROOTED_MARKER),
new Part(IgnoreLineParser.Token.TEXT, "foo")
);
Rule rule = new RootedFileRule(syntax, definition);
Boolean actual = null;
// Act
actual = rule.matches(relativePath);
// Assert
assertTrue(actual);
}
@Test
public void testNonMatchFilenameOnly() throws Exception {
// Arrange
final String definition = "/foo";
final String relativePath = "bar";
final List<Part> syntax = Arrays.asList(
new Part(IgnoreLineParser.Token.ROOTED_MARKER),
new Part(IgnoreLineParser.Token.TEXT, "foo")
);
Rule rule = new RootedFileRule(syntax, definition);
Boolean actual = null;
// Act
actual = rule.matches(relativePath);
// Assert
assertFalse(actual);
}
@Test
public void testMatchFilenameAndExtension() throws Exception {
// Arrange
final String definition = "/foo.txt";
final String relativePath = "foo.txt";
final List<Part> syntax = Arrays.asList(
new Part(IgnoreLineParser.Token.ROOTED_MARKER),
new Part(IgnoreLineParser.Token.TEXT, "foo.txt")
);
Rule rule = new RootedFileRule(syntax, definition);
Boolean actual = null;
// Act
actual = rule.matches(relativePath);
// Assert
assertTrue(actual);
}
@Test
public void testNonMatchFilenameAndExtension() throws Exception {
// Arrange
final String definition = "/foo.txt";
final String relativePath = "bar.baz";
final List<Part> syntax = Arrays.asList(
new Part(IgnoreLineParser.Token.ROOTED_MARKER),
new Part(IgnoreLineParser.Token.TEXT, "foo.txt")
);
Rule rule = new RootedFileRule(syntax, definition);
Boolean actual = null;
// Act
actual = rule.matches(relativePath);
// Assert
assertFalse(actual);
}
@Test
public void testMatchFilenameWithGlob() throws Exception {
// Arrange
final String definition = "/foo*";
final String relativePath = "foobarbaz";
final List<Part> syntax = Arrays.asList(
new Part(IgnoreLineParser.Token.ROOTED_MARKER),
new Part(IgnoreLineParser.Token.TEXT, "foo"),
new Part(IgnoreLineParser.Token.MATCH_ANY)
);
Rule rule = new RootedFileRule(syntax, definition);
Boolean actual = null;
// Act
actual = rule.matches(relativePath);
// Assert
assertTrue(actual);
}
@Test
public void testNonMatchFilenameWithGlob() throws Exception {
// Arrange
final String definition = "/foo*";
final String relativePath = "boobarbaz";
final List<Part> syntax = Arrays.asList(
new Part(IgnoreLineParser.Token.ROOTED_MARKER),
new Part(IgnoreLineParser.Token.TEXT, "foo"),
new Part(IgnoreLineParser.Token.MATCH_ANY)
);
Rule rule = new RootedFileRule(syntax, definition);
Boolean actual = null;
// Act
actual = rule.matches(relativePath);
// Assert
assertFalse(actual);
}
@Test
public void testMatchFilenameAndExtensionWithFilenameGlob() throws Exception {
// Arrange
final String definition = "/foo*.txt";
final String relativePath = "foobarbaz.txt";
final List<Part> syntax = Arrays.asList(
new Part(IgnoreLineParser.Token.ROOTED_MARKER),
new Part(IgnoreLineParser.Token.TEXT, "foo"),
new Part(IgnoreLineParser.Token.MATCH_ANY),
new Part(IgnoreLineParser.Token.TEXT, ".txt")
);
Rule rule = new RootedFileRule(syntax, definition);
Boolean actual = null;
// Act
actual = rule.matches(relativePath);
// Assert
assertTrue(actual);
}
@Test
public void testNonMatchFilenameAndExtensionWithFilenameGlob() throws Exception {
// Arrange
final String definition = "/foo*qux.txt";
final String relativePath = "foobarbaz.txt";
final List<Part> syntax = Arrays.asList(
new Part(IgnoreLineParser.Token.ROOTED_MARKER),
new Part(IgnoreLineParser.Token.TEXT, "foo"),
new Part(IgnoreLineParser.Token.MATCH_ANY),
new Part(IgnoreLineParser.Token.TEXT, "qux.txt")
);
Rule rule = new RootedFileRule(syntax, definition);
Boolean actual = null;
// Act
actual = rule.matches(relativePath);
// Assert
assertFalse(actual);
}
@Test
public void testMatchFilenameAndExtensionWithExtensionGlob() throws Exception {
// Arrange
final String definition = "/foo.*";
final String relativePath = "foo.bak";
final List<Part> syntax = Arrays.asList(
new Part(IgnoreLineParser.Token.ROOTED_MARKER),
new Part(IgnoreLineParser.Token.TEXT, "foo."),
new Part(IgnoreLineParser.Token.MATCH_ANY)
);
Rule rule = new RootedFileRule(syntax, definition);
Boolean actual = null;
// Act
actual = rule.matches(relativePath);
// Assert
assertTrue(actual);
}
@Test
public void testMatchFilenameAndExtensionWithMultiplePeriods() throws Exception {
// Arrange
final String definition = "/foo*.xyzzy.txt";
final String relativePath = "foo.bar.baz.xyzzy.txt";
final List<Part> syntax = Arrays.asList(
new Part(IgnoreLineParser.Token.ROOTED_MARKER),
new Part(IgnoreLineParser.Token.TEXT, "foo"),
new Part(IgnoreLineParser.Token.MATCH_ANY),
new Part(IgnoreLineParser.Token.TEXT, ".xyzzy.txt")
);
Rule rule = new RootedFileRule(syntax, definition);
Boolean actual = null;
// Act
actual = rule.matches(relativePath);
// Assert
assertTrue(actual);
}
@Test
public void testNonMatchFilenameAndExtensionWithMultiplePeriods() throws Exception {
// Arrange
final String definition = "/foo*.xyzzy.txt";
final String relativePath = "foo.bar.baz.qux.txt";
final List<Part> syntax = Arrays.asList(
new Part(IgnoreLineParser.Token.ROOTED_MARKER),
new Part(IgnoreLineParser.Token.TEXT, "foo"),
new Part(IgnoreLineParser.Token.MATCH_ANY),
new Part(IgnoreLineParser.Token.TEXT, ".xyzzy.txt")
);
Rule rule = new RootedFileRule(syntax, definition);
Boolean actual = null;
// Act
actual = rule.matches(relativePath);
// Assert
assertFalse(actual);
}
@Test
public void testMatchWithoutLeadingForwardSlash() throws Exception {
// Arrange
final String definition = "foo*.xyzzy.txt";
final String relativePath = "foo.bar.baz.xyzzy.txt";
final List<Part> syntax = Arrays.asList(
new Part(IgnoreLineParser.Token.ROOTED_MARKER),
new Part(IgnoreLineParser.Token.TEXT, "foo"),
new Part(IgnoreLineParser.Token.MATCH_ANY),
new Part(IgnoreLineParser.Token.TEXT, ".xyzzy.txt")
);
Rule rule = new RootedFileRule(syntax, definition);
Boolean actual = null;
// Act
actual = rule.matches(relativePath);
// Assert
assertTrue(actual);
}
@Test
public void testMatchesOnlyRooted() throws Exception {
// Arrange
final String definition = "/path/to/some/foo*.xyzzy.txt";
final String relativePath = "foo.bar.baz.xyzzy.txt";
final List<Part> syntax = Arrays.asList(
new Part(IgnoreLineParser.Token.ROOTED_MARKER),
new Part(IgnoreLineParser.Token.TEXT, "path"),
new Part(IgnoreLineParser.Token.PATH_DELIM),
new Part(IgnoreLineParser.Token.TEXT, "to"),
new Part(IgnoreLineParser.Token.PATH_DELIM),
new Part(IgnoreLineParser.Token.TEXT, "some"),
new Part(IgnoreLineParser.Token.PATH_DELIM),
new Part(IgnoreLineParser.Token.TEXT, "oo"),
new Part(IgnoreLineParser.Token.MATCH_ANY),
new Part(IgnoreLineParser.Token.TEXT, ".xyzzy.txt")
);
Rule rule = new RootedFileRule(syntax, definition);
Boolean actual = null;
// Act
actual = rule.matches(relativePath);
// Assert
assertFalse(actual);
}
}