Skip to content

Commit

Permalink
Merge pull request #285 from robertpanzer/asciidoctorj-1.6.0
Browse files Browse the repository at this point in the history
#284 Don't fail for files in root directory with absolute path
  • Loading branch information
lordofthejars committed Mar 23, 2015

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents ad28486 + bb5c645 commit 0f9cb5b
Showing 2 changed files with 64 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -26,31 +26,42 @@ public class GlobDirectoryWalker implements DirectoryWalker {

public GlobDirectoryWalker(String globExpression) {

if (isAbsoluteGlobExpression(globExpression)) {
// Determine leading part that is unglobbed, i.e. does not contain a '*'.
int indexOfUnglobbedPart = findIndexOfUnglobbedPart(globExpression);

// If the given path is an absolute path we want to split it into an absolute part containing
// directories without wildcards and a glob part with wildcards and file names
int indexOfUnglobbedPart = findIndexOfUnglobbedPart(globExpression);
this.rootDirectory = new File(globExpression.substring(0, indexOfUnglobbedPart));
this.globExpression = globExpression.substring(indexOfUnglobbedPart + 1);
checkInput(rootDirectory);
this.canonicalRootDir = getCanonicalPath(rootDirectory);
String unglobbedPart = globExpression.substring(0, indexOfUnglobbedPart + 1);

} else {
// It's a relative expression, current working dir is sufficient as root directory
rootDirectory = new File(".");
checkInput(rootDirectory);
this.canonicalRootDir = getCanonicalPath(rootDirectory);
this.globExpression = globExpression;

}
this.rootDirectory = new File(unglobbedPart).getAbsoluteFile();
this.globExpression = globExpression.substring(indexOfUnglobbedPart + 1);
checkInput(rootDirectory);
this.canonicalRootDir = getCanonicalPath(rootDirectory);
}

// An absolute path will consist of an absolute, unglobbed part and a globbed part.
// The globbed part starts at the first path element that contains a '*' or it is
// the part following the last separator
/**
* This method computes the index of the last separator that splits
* the given glob expression into a leading part not containing wildcards
* and a trailing part containing wildcards.
* The last part of the path will never be counted to the leading part.
*
* <p>Example</p>
* <code><pre>
* src/main/asciidoc/*.adoc
* ^ 17
* /tmp/test.adoc
* ^ 4
* /test.adoc
* ^ 0
* *.adoc
* ^ -1
* </pre></code>
*
* @param globExpression e.g. {@code src/main/asciidoc/*.adoc}, {@code *.adoc},
* {@code /tmp/test.adoc}
* @return The index of the last separator char that splits the string into an unglobbed and a globbed part.
* If there is no unglobbed part or the string is only a file name the method returns {@code -1}
*/
private int findIndexOfUnglobbedPart(String globExpression) {
int result = 0;
int result = -1;
for (int i = 0; i < globExpression.length(); i++) {
switch (globExpression.charAt(i)) {
case '/':
@@ -67,22 +78,6 @@ private int findIndexOfUnglobbedPart(String globExpression) {
return result;
}

// Determines if the given glob expression is an absolute path
// That is the expression starts with a separator or with [A-Za-z]:[/\] on Windows
private boolean isAbsoluteGlobExpression(String globExpression) {
if (globExpression.startsWith(File.separator)) {
return true;
}
if (isWindows()) {
return globExpression.matches("^[A-Za-z]:[\\\\/].*$");
}
return false;
}

private boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("win");
}

@Override
public List<File> scan() {
Pattern pattern = new Pattern(globExpression);
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package org.asciidoctor;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.junit.Assert.assertThat;
import org.asciidoctor.util.ClasspathResources;
import org.junit.Rule;
import org.junit.Test;

import java.io.File;
import java.util.List;

import org.asciidoctor.util.ClasspathResources;
import org.junit.Rule;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.junit.Assert.assertThat;

public class WhenGlobExpressionIsUsedForScanning {

@@ -54,5 +55,29 @@ public void no_should_be_returned_if_glob_expression_does_not_match() {

assertThat(asciidocFiles, is(empty()));
}


@Test
public void should_not_fail_with_file_in_root_dir() {

final String fileNameInRootDir = "/this_file_does_not_exist.adoc";

DirectoryWalker globDirectoryWalker = new GlobDirectoryWalker(fileNameInRootDir);

List<File> asciidocFiles = globDirectoryWalker.scan();

assertThat(asciidocFiles, contains(new File(fileNameInRootDir).getAbsoluteFile()));
}

@Test
public void should_not_fail_with_file_in_current_working_dir() {

final String fileNameInCurrentWorkingDir = "this_file_does_not_exist.adoc";

DirectoryWalker globDirectoryWalker = new GlobDirectoryWalker(fileNameInCurrentWorkingDir);

List<File> asciidocFiles = globDirectoryWalker.scan();

assertThat(asciidocFiles, contains(new File(fileNameInCurrentWorkingDir).getAbsoluteFile()));
}

}

0 comments on commit 0f9cb5b

Please sign in to comment.