Skip to content

Commit

Permalink
Add missing tests for FileUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
ascopes committed Feb 17, 2024
1 parent a81158d commit 19a0ec2
Showing 1 changed file with 83 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assumptions.assumeThat;

import io.github.ascopes.protobufmavenplugin.fixtures.TestFileSystem;
import java.io.IOException;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -100,7 +103,6 @@ void getFileNameWithoutExtensionReturnsTheExpectedValue(
" '', ",
})
@ParameterizedTest(name = ".getFileExtension(\".../{0}\") returns \"{1}\"")
@SuppressWarnings({"OptionalUsedAsFieldOrParameterType", "AssertBetweenInconvertibleTypes"})
void getFileExtensionReturnsTheExpectedValue(
String fileName,
@Nullable String expected,
Expand Down Expand Up @@ -145,4 +147,83 @@ void getFileSystemProviderThrowsExceptionIfTheProviderDoesNotExist() {
.isInstanceOf(FileSystemNotFoundException.class)
.hasMessage("No file system provider for %s was found", scheme);
}

@DisplayName(".makeExecutable makes the path executable when supported")
@Test
void makeExecutableMakesThePathExecutableWhenSupported() throws IOException {
// Given
try (var tempFs = TestFileSystem.linux()) {
var file = tempFs.givenFileExists("foo", "bar", "baz");

assumeThat(Files.isExecutable(file)).isFalse();

// When
FileUtils.makeExecutable(file);

// Then
assertThat(file)
.isRegularFile()
.isExecutable();
}
}

@DisplayName(".makeExecutable exits silently when not supported")
@Test
void makeExecutableMakesThePathExecutableWhenNotSupported() throws IOException {
// Given
try (var tempFs = TestFileSystem.windows()) {
var file = tempFs.givenFileExists("foo", "bar", "baz");

// When
FileUtils.makeExecutable(file);

// Then
assertThat(file)
.isRegularFile()
.isExecutable();
}
}

@DisplayName(".changeRelativePath produces the expected result on the same file system")
@Test
void changeRelativePathProducesTheExpectedResultOnTheSameFileSystem(
@TempDir Path tempDir
) throws IOException {
// Given
var dir1 = tempDir.resolve("foo").resolve("bar").resolve("baz");
var dir2 = tempDir.resolve("do").resolve("ray").resolve("me");
var existingPath = dir1.resolve("some").resolve("file.txt");
Files.createDirectories(dir1);
Files.createDirectories(dir2);

// When
var actualPath = FileUtils.changeRelativePath(dir2, dir1, existingPath);

// Then
assertThat(actualPath)
.isEqualTo(dir2.resolve("some").resolve("file.txt"));
}

@DisplayName(".changeRelativePath produces the expected result across file systems")
@Test
void changeRelativePathProducesTheExpectedResultAcrossFileSystems() throws IOException {
// Given
try (
var fs1 = TestFileSystem.linux();
var fs2 = TestFileSystem.windows()
) {
var dir1 = fs1.getRoot().resolve("foo").resolve("bar").resolve("baz");
var dir2 = fs2.getRoot().resolve("do").resolve("ray").resolve("me");
var existingPath = dir1.resolve("some").resolve("file.txt");
Files.createDirectories(dir1);
Files.createDirectories(dir2);

// When
var actualPath = FileUtils.changeRelativePath(dir2, dir1, existingPath);

// Then
assertThat(actualPath)
.isEqualTo(dir2.resolve("some").resolve("file.txt"));
}
}
}

0 comments on commit 19a0ec2

Please sign in to comment.