diff options
author | ajmichael <ajmichael@google.com> | 2018-02-09 10:19:29 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-02-09 10:20:51 -0800 |
commit | ad32f62952e2b6f8293a26ea5fd677e61975c9ff (patch) | |
tree | f5036251b75b6822fba34fce5ac2fa6425e5383e /src/test/java/com/google/devtools/build/lib/unix/NativePosixFilesTest.java | |
parent | 8cc1706ac8e9751e347720054ede3a3d88385c88 (diff) |
Make getxattr not throw an exception when attribute does not exist on Mac.
This matches the current behavior on Linux. When an extended attribute is not present on a file, getxattr on Linux returns ENODATA whereas getxattr on Mac returns ENOATTR. Previously, we were special casing ENODATA to not throw an exception but not ENOATTR. Now we treat them the same.
RELNOTES: None
PiperOrigin-RevId: 185157964
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/unix/NativePosixFilesTest.java')
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/unix/NativePosixFilesTest.java | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/unix/NativePosixFilesTest.java b/src/test/java/com/google/devtools/build/lib/unix/NativePosixFilesTest.java index ea5039d89f..ddad8aa159 100644 --- a/src/test/java/com/google/devtools/build/lib/unix/NativePosixFilesTest.java +++ b/src/test/java/com/google/devtools/build/lib/unix/NativePosixFilesTest.java @@ -14,11 +14,15 @@ package com.google.devtools.build.lib.unix; import static com.google.common.truth.Truth.assertThat; +import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.Assert.fail; +import static org.junit.Assume.assumeTrue; import com.google.common.collect.ImmutableMap; import com.google.common.hash.HashCode; import com.google.devtools.build.lib.testutil.TestUtils; +import com.google.devtools.build.lib.util.OS; import com.google.devtools.build.lib.vfs.FileAccessException; import com.google.devtools.build.lib.vfs.FileSystem; import com.google.devtools.build.lib.vfs.FileSystemUtils; @@ -26,6 +30,7 @@ import com.google.devtools.build.lib.vfs.Path; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.nio.file.Files; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -111,4 +116,43 @@ public class NativePosixFilesTest { assertThat(e).hasMessage(foo + " (Read-only file system)"); } } + + /** Skips the test if the file system does not support extended attributes. */ + private static void assumeXattrsSupported() throws Exception { + // The standard file systems on macOS support extended attributes by default, so we can assume + // that the test will work on that platform. For other systems, we currently don't have a + // mechanism to validate this so the tests are skipped unconditionally. + assumeTrue(OS.getCurrent() == OS.DARWIN); + } + + @Test + public void testGetxattr_AttributeFound() throws Exception { + assumeXattrsSupported(); + + String myfile = Files.createTempFile("getxattrtest", null).toString(); + Runtime.getRuntime().exec("xattr -w foo bar " + myfile).waitFor(); + + assertThat(new String(NativePosixFiles.getxattr(myfile, "foo"), UTF_8)).isEqualTo("bar"); + assertThat(new String(NativePosixFiles.lgetxattr(myfile, "foo"), UTF_8)).isEqualTo("bar"); + } + + @Test + public void testGetxattr_AttributeNotFoundReturnsNull() throws Exception { + assumeXattrsSupported(); + + String myfile = Files.createTempFile("getxattrtest", null).toString(); + + assertThat(NativePosixFiles.getxattr(myfile, "foo")).isNull(); + assertThat(NativePosixFiles.lgetxattr(myfile, "foo")).isNull(); + } + + @Test + public void testGetxattr_FileNotFound() throws Exception { + String nonexistentFile = workingDir.getChild("nonexistent").toString(); + + assertThrows( + FileNotFoundException.class, () -> NativePosixFiles.getxattr(nonexistentFile, "foo")); + assertThrows( + FileNotFoundException.class, () -> NativePosixFiles.lgetxattr(nonexistentFile, "foo")); + } } |