Skip to content

Commit

Permalink
#365 delete if present
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Dec 8, 2019
1 parent 589599c commit 8f9892e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
7 changes: 0 additions & 7 deletions research/2018-April/Makefile

This file was deleted.

37 changes: 20 additions & 17 deletions src/main/java/org/jpeek/web/Reports.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import org.cactoos.BiFunc;
import org.cactoos.Func;
import org.cactoos.io.TeeInput;
Expand All @@ -41,7 +42,9 @@
/**
* All reports.
*
* <p>There is no thread-safety guarantee.
* <p>There is NO thread-safety guarantee. Moreover, this class is NOT
* thread-safe. You have to decorate it with a thread-safe
* {@link Futures}.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
Expand Down Expand Up @@ -86,14 +89,7 @@ public Func<String, Response> apply(final String group,
final String artifact) throws IOException {
final String grp = group.replace(".", "/");
final Path input = this.sources.resolve(grp).resolve(artifact);
if (Files.exists(input)) {
throw new IllegalStateException(
String.format(
"The input directory for %s:%s already exists: %s",
group, artifact, input
)
);
}
Reports.deleteIfPresent(input);
final String version = new XMLDocument(
new TextOf(
new URL(
Expand Down Expand Up @@ -148,14 +144,7 @@ public Func<String, Response> apply(final String group,
throw new IllegalStateException(ex);
}
final Path output = this.target.resolve(grp).resolve(artifact);
if (Files.exists(output)) {
throw new IllegalStateException(
String.format(
"The output directory for %s:%s already exists: %s",
group, artifact, output
)
);
}
Reports.deleteIfPresent(output);
new App(input, output).analyze();
synchronized (this.sources) {
new Results().add(String.format("%s:%s", group, artifact), output);
Expand All @@ -165,4 +154,18 @@ public Func<String, Response> apply(final String group,
return new TypedPages(new Pages(output));
}

/**
* Delete this dir if it's present.
* @param dir The dir
* @throws IOException If fails
*/
private static void deleteIfPresent(final Path dir) throws IOException {
if (Files.exists(dir)) {
Files.walk(dir)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}
}

}
20 changes: 14 additions & 6 deletions src/test/java/org/jpeek/web/ReportsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@
*/
package org.jpeek.web;

import com.jcabi.log.Logger;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import org.cactoos.BiFunc;
import org.cactoos.Func;
import org.cactoos.text.TextOf;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.takes.Response;
import org.takes.facets.hamcrest.HmRsStatus;

/**
Expand All @@ -48,20 +51,25 @@ public final class ReportsTest {
@Before
public void weAreOnline() {
try {
new TextOf(new URL("https://www.jpeek.org/")).asString();
new TextOf(new URL("http://www.jpeek.org/")).asString();
} catch (final IOException ex) {
Logger.debug(this, "We are not online: %s", ex.getMessage());
Assume.assumeTrue(false);
}
}

@Test
@Ignore
public void rendersOneReport() throws Exception {
final BiFunc<String, String, Func<String, Response>> reports =
new Reports(Files.createTempDirectory("x"));
new Assertion<>(
"Must return HTTP 200 OK status",
new Reports(
Files.createTempDirectory("x")
).apply("org.takes", "takes").apply("index.html"),
reports.apply("com.jcabi", "jcabi-urn").apply("index.html"),
new HmRsStatus(HttpURLConnection.HTTP_OK)
).affirm();
new Assertion<>(
"Must return HTTP 200 OK status",
reports.apply("com.jcabi", "jcabi-urn").apply("index.html"),
new HmRsStatus(HttpURLConnection.HTTP_OK)
).affirm();
}
Expand Down

0 comments on commit 8f9892e

Please sign in to comment.