Skip to content

Commit

Permalink
43: Repository.get should return Optional.empty on non-existing direc…
Browse files Browse the repository at this point in the history
…tory

Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Jul 4, 2019
1 parent e83e473 commit 90edbab
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,10 @@ public Hash head() throws IOException {
}

public static Optional<Repository> get(Path p) throws IOException {
if (!Files.exists(p)) {
return Optional.empty();
}

var r = new GitRepository(p);
return r.exists() ? Optional.of(new GitRepository(r.root())) : Optional.empty();
}
Expand Down
8 changes: 8 additions & 0 deletions vcs/src/main/java/org/openjdk/skara/vcs/hg/HgRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ public boolean isClean() throws IOException {

@Override
public boolean exists() throws IOException {
if (!Files.exists(dir)) {
return false;
}

try {
root();
return true;
Expand Down Expand Up @@ -657,6 +661,10 @@ public List<String> config(String key) throws IOException {
}

public static Optional<Repository> get(Path p) throws IOException {
if (!Files.exists(p)) {
return Optional.empty();
}

var r = new HgRepository(p);
return r.exists() ? Optional.of(new HgRepository(r.root())) : Optional.empty();
}
Expand Down
8 changes: 8 additions & 0 deletions vcs/src/test/java/org/openjdk/skara/vcs/RepositoryTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.openjdk.skara.test.TemporaryDirectory;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

Expand Down Expand Up @@ -1430,4 +1431,11 @@ void testShowOnExecutableFiles(VCS vcs) throws IOException {
assertEquals(Optional.of(List.of("echo 'goodbye'")), r.lines(readWriteExecutableFile, hash2));
}
}

@Test
void testGetAndExistsOnNonExistingDirectory() throws IOException {
var nonExistingDirectory = Path.of("this", "does", "not", "exist");
assertEquals(Optional.empty(), Repository.get(nonExistingDirectory));
assertEquals(false, Repository.exists(nonExistingDirectory));
}
}

0 comments on commit 90edbab

Please sign in to comment.