Skip to content

Replace assertFalse/assertTrue for file existence #52092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void test50StartAndStop() throws Exception {
throw e;
}

assertTrue("gc logs exist", Files.exists(installation.logs.resolve("gc.log")));
assertThat(installation.logs.resolve("gc.log"), fileExists());
ServerUtils.runElasticsearchTests();

stopElasticsearch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.nio.file.Paths;

import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileExists;
import static org.elasticsearch.packaging.util.FileUtils.assertPathsDontExist;
import static org.elasticsearch.packaging.util.FileUtils.assertPathsDoNotExist;
import static org.elasticsearch.packaging.util.FileUtils.assertPathsExist;
import static org.elasticsearch.packaging.util.Packages.SYSVINIT_SCRIPT;
import static org.elasticsearch.packaging.util.Packages.assertInstalled;
Expand Down Expand Up @@ -76,14 +76,14 @@ public void test20Remove() throws Exception {

// keystore was removed

assertPathsDontExist(
assertPathsDoNotExist(
installation.config("elasticsearch.keystore"),
installation.config(".elasticsearch.keystore.initial_md5sum")
);

// doc files were removed

assertPathsDontExist(
assertPathsDoNotExist(
Paths.get("/usr/share/doc/" + distribution().flavor.name),
Paths.get("/usr/share/doc/" + distribution().flavor.name + "/copyright")
);
Expand All @@ -100,7 +100,7 @@ public void test30Purge() throws Exception {

assertRemoved(distribution());

assertPathsDontExist(
assertPathsDoNotExist(
installation.config,
installation.envFile,
SYSVINIT_SCRIPT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileDoesNotExist;
import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileExists;
import static org.elasticsearch.packaging.util.FileUtils.append;
import static org.elasticsearch.packaging.util.FileUtils.assertPathsDontExist;
import static org.elasticsearch.packaging.util.FileUtils.assertPathsDoNotExist;
import static org.elasticsearch.packaging.util.FileUtils.assertPathsExist;
import static org.elasticsearch.packaging.util.FileUtils.cp;
import static org.elasticsearch.packaging.util.FileUtils.fileWithGlobExist;
Expand Down Expand Up @@ -205,7 +205,7 @@ public void test50Remove() throws Exception {

}

assertPathsDontExist(
assertPathsDoNotExist(
installation.bin,
installation.lib,
installation.modules,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@
import static org.elasticsearch.packaging.util.Cleanup.cleanEverything;
import static org.elasticsearch.packaging.util.Docker.ensureImageIsLoaded;
import static org.elasticsearch.packaging.util.Docker.removeContainer;
import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileExists;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;

Expand Down Expand Up @@ -326,7 +327,7 @@ public void assertElasticsearchFailure(Shell.Result result, List<String> expecte

// If log file exists, then we have bootstrapped our logging and the
// error should be in the logs
assertTrue("log file exists", Files.exists(installation.logs.resolve("elasticsearch.log")));
assertThat(installation.logs.resolve("elasticsearch.log"), fileExists());
String logfile = FileUtils.slurp(installation.logs.resolve("elasticsearch.log"));

assertThat(logfile, anyOf(stringMatchers));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
import org.elasticsearch.packaging.util.Shell;
import org.junit.BeforeClass;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileDoesNotExist;
import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileExists;
import static org.elasticsearch.packaging.util.FileUtils.append;
import static org.elasticsearch.packaging.util.FileUtils.assertPathsDontExist;
import static org.elasticsearch.packaging.util.FileUtils.assertPathsDoNotExist;
import static org.elasticsearch.packaging.util.Packages.SYSTEMD_SERVICE;
import static org.elasticsearch.packaging.util.Packages.SYSVINIT_SCRIPT;
import static org.elasticsearch.packaging.util.Packages.assertInstalled;
Expand Down Expand Up @@ -103,7 +102,7 @@ public void test30PreserveConfig() throws Exception {
assertThat(sh.runIgnoreExitCode("systemctl is-enabled elasticsearch.service").exitCode, is(1));
}

assertPathsDontExist(
assertPathsDoNotExist(
installation.bin,
installation.lib,
installation.modules,
Expand Down Expand Up @@ -137,7 +136,7 @@ public void test30PreserveConfig() throws Exception {
private void assertConfFilePreserved(String configFile) {
final Path original = installation.config(configFile);
final Path saved = installation.config(configFile + ".rpmsave");
assertFalse(original + " should not exist", Files.exists(original));
assertTrue(saved + " should exist", Files.exists(saved));
assertThat(original, fileDoesNotExist());
assertThat(saved, fileExists());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
import org.elasticsearch.packaging.util.Shell;
import org.junit.BeforeClass;

import java.nio.file.Files;

import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileExists;
import static org.elasticsearch.packaging.util.FileUtils.assertPathsExist;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.equalTo;
Expand Down Expand Up @@ -59,7 +58,7 @@ public void test10Install() throws Exception {

public void test20Start() throws Exception {
startElasticsearch();
assertTrue("gc logs exist", Files.exists(installation.logs.resolve("gc.log")));
assertThat(installation.logs.resolve("gc.log"), fileExists());
ServerUtils.runElasticsearchTests();
sh.run("service elasticsearch status"); // returns 0 exit status when ok
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.stream.Stream;

import static java.util.stream.Collectors.joining;
import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileDoesNotExist;
import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileExists;
import static org.elasticsearch.packaging.util.FileMatcher.Fileness.Directory;
import static org.elasticsearch.packaging.util.FileMatcher.Fileness.File;
import static org.elasticsearch.packaging.util.FileMatcher.file;
Expand All @@ -48,8 +50,6 @@
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* Installation and verification logic for archive distributions
Expand Down Expand Up @@ -268,7 +268,7 @@ public static Shell.Result startElasticsearchWithTty(Installation installation,
public static Shell.Result runElasticsearchStartCommand(Installation installation, Shell sh, String keystorePassword) {
final Path pidFile = installation.home.resolve("elasticsearch.pid");

assertFalse("Pid file doesn't exist when starting Elasticsearch", Files.exists(pidFile));
assertThat(pidFile, fileDoesNotExist());

final Installation.Executables bin = installation.executables();

Expand Down Expand Up @@ -338,14 +338,14 @@ public static void assertElasticsearchStarted(Installation installation) throws
final Path pidFile = installation.home.resolve("elasticsearch.pid");
ServerUtils.waitForElasticsearch(installation);

assertTrue("Starting Elasticsearch produced a pid file at " + pidFile, Files.exists(pidFile));
assertThat("Starting Elasticsearch produced a pid file at " + pidFile, pidFile, fileExists());
String pid = slurp(pidFile).trim();
assertThat(pid, is(not(emptyOrNullString())));
}

public static void stopElasticsearch(Installation installation) throws Exception {
Path pidFile = installation.home.resolve("elasticsearch.pid");
assertTrue("pid file should exist", Files.exists(pidFile));
assertThat(pidFile, fileExists());
String pid = slurp(pidFile).trim();
assertThat(pid, is(not(emptyOrNullString())));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.http.client.fluent.Request;
import org.elasticsearch.common.CheckedRunnable;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFileAttributes;
Expand All @@ -39,6 +38,7 @@
import java.util.stream.Stream;

import static java.nio.file.attribute.PosixFilePermissions.fromString;
import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileExists;
import static org.elasticsearch.packaging.util.FileMatcher.p644;
import static org.elasticsearch.packaging.util.FileMatcher.p660;
import static org.elasticsearch.packaging.util.FileMatcher.p755;
Expand Down Expand Up @@ -156,7 +156,7 @@ private static void executeDockerRun(Distribution distribution, Map<Path, Path>
// Bind-mount any volumes
if (volumes != null) {
volumes.forEach((localPath, containerPath) -> {
assertTrue(localPath + " doesn't exist", Files.exists(localPath));
assertThat(localPath, fileExists());

if (Platforms.WINDOWS == false && System.getProperty("user.name").equals("root")) {
// The tests are running as root, but the process in the Docker container runs as `elasticsearch` (UID 1000),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipException;

import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileDoesNotExist;
import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileExists;
import static org.hamcrest.Matchers.emptyIterable;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThat;

/**
* Wrappers and convenience methods for common filesystem operations
Expand Down Expand Up @@ -308,8 +309,8 @@ public static Path getDistributionFile(Distribution distribution) {
return distribution.path;
}

public static void assertPathsExist(Path... paths) {
Arrays.stream(paths).forEach(path -> assertTrue(path + " should exist", Files.exists(path)));
public static void assertPathsExist(final Path... paths) {
Arrays.stream(paths).forEach(path -> assertThat(path, fileExists()));
}

public static Matcher<Path> fileWithGlobExist(String glob) throws IOException {
Expand All @@ -326,8 +327,8 @@ protected Iterable<Path> featureValueOf(Path actual) {
};
}

public static void assertPathsDontExist(Path... paths) {
Arrays.stream(paths).forEach(path -> assertFalse(path + " should not exist", Files.exists(path)));
public static void assertPathsDoNotExist(final Path... paths) {
Arrays.stream(paths).forEach(path -> assertThat(path, fileDoesNotExist()));
}

public static void deleteIfExists(Path path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.regex.Pattern;
import java.util.stream.Stream;

import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileDoesNotExist;
import static org.elasticsearch.packaging.util.FileMatcher.Fileness.Directory;
import static org.elasticsearch.packaging.util.FileMatcher.Fileness.File;
import static org.elasticsearch.packaging.util.FileMatcher.file;
Expand Down Expand Up @@ -164,7 +165,7 @@ private static void verifyOssInstallation(Installation es, Distribution distribu

final Result passwdResult = sh.run("getent passwd elasticsearch");
final Path homeDir = Paths.get(passwdResult.stdout.trim().split(":")[5]);
assertFalse("elasticsearch user home directory must not exist", Files.exists(homeDir));
assertThat("elasticsearch user home directory must not exist", homeDir, fileDoesNotExist());

Stream.of(
es.home,
Expand Down