Skip to content

Commit

Permalink
clearer error message
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-pol committed Aug 19, 2024
1 parent d1c848b commit 83c052c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Stream<String> getPids() throws IOException {
return Stream.of(defaultId);
}

Stream<String> lines = null;
Stream<String> lines;

if ("-".equals(singleIdOrIdFile)) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Expand All @@ -47,6 +47,9 @@ public Stream<String> getPids() throws IOException {
else {
var pidFile = Paths.get(singleIdOrIdFile);
if (Files.exists(pidFile)) {
if (!Files.isRegularFile(pidFile)) {
throw new IOException(singleIdOrIdFile + " is not a regular file");
}
lines = Files.lines(pidFile)
.flatMap(line -> Arrays.stream(line.trim().split("\\s+")));
}
Expand Down
12 changes: 5 additions & 7 deletions src/test/java/nl/knaw/dans/dvcli/action/SingleOrTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import org.mockito.Mockito;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
Expand All @@ -36,7 +36,6 @@
import static nl.knaw.dans.dvcli.action.SingleIdOrIdsFile.DEFAULT_TARGET_PLACEHOLDER;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -112,11 +111,10 @@ public void getDatasets_should_parse_file_with_white_space() throws Exception {
@Test
public void getDatasets_should_throw_when_parsing_a_directory() throws Exception {

var datasets = new SingleDatasetOrDatasetsFile("target", getClient())
.getDatasets();
var exception = assertThrows(RuntimeException.class, datasets::toList);
assertThat( exception.getMessage())
.isEqualTo("java.io.IOException: Is a directory");
var ids = new SingleDatasetOrDatasetsFile("target", getClient());
assertThatThrownBy(ids::getDatasets)
.isInstanceOf(IOException.class)
.hasMessage("target is not a regular file");
}

@Test
Expand Down

0 comments on commit 83c052c

Please sign in to comment.