Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix CI: skip test when device can't be opened
  • Loading branch information
David Cantu authored and github-actions committed Oct 12, 2021
commit d6bfc02e92aee7b54742c5bce649f6f8a2055c4f
Original file line number Diff line number Diff line change
Expand Up @@ -16,58 +16,75 @@ public class CharacterDevice
[MemberData(nameof(DevicePath_FileOptions_TestData))]
public void CharacterDevice_FileStream_Write(string devicePath, FileOptions fileOptions)
{
VerifyDeviceExists(devicePath);
using FileStream fs = new(devicePath, new FileStreamOptions { Options = fileOptions, Access = FileAccess.Write });
FileStreamOptions options = new() { Options = fileOptions, Access = FileAccess.Write };
VerifyDeviceIsReachable(devicePath, options);

using FileStream fs = new(devicePath, options);
fs.Write(Encoding.UTF8.GetBytes("foo"));
}

[Theory]
[MemberData(nameof(DevicePath_FileOptions_TestData))]
public async Task CharacterDevice_FileStream_WriteAsync(string devicePath, FileOptions fileOptions)
{
VerifyDeviceExists(devicePath);
using FileStream fs = new(devicePath, new FileStreamOptions { Options = fileOptions, Access = FileAccess.Write });
FileStreamOptions options = new() { Options = fileOptions, Access = FileAccess.Write };
VerifyDeviceIsReachable(devicePath, options);

using FileStream fs = new(devicePath, options);
await fs.WriteAsync(Encoding.UTF8.GetBytes("foo"));
}

[Theory]
[MemberData(nameof(DevicePath_TestData))]
public void CharacterDevice_WriteAllBytes(string devicePath)
{
VerifyDeviceExists(devicePath);
VerifyDeviceIsReachable(devicePath, new FileStreamOptions{ Access = FileAccess.Write });

File.WriteAllBytes(devicePath, Encoding.UTF8.GetBytes("foo"));
}

[Theory]
[MemberData(nameof(DevicePath_TestData))]
public async Task CharacterDevice_WriteAllBytesAsync(string devicePath)
{
VerifyDeviceExists(devicePath);
VerifyDeviceIsReachable(devicePath, new FileStreamOptions{ Options = FileOptions.Asynchronous, Access = FileAccess.Write });

await File.WriteAllBytesAsync(devicePath, Encoding.UTF8.GetBytes("foo"));
}

[Theory]
[MemberData(nameof(DevicePath_TestData))]
public void CharacterDevice_WriteAllText(string devicePath)
{
VerifyDeviceExists(devicePath);
VerifyDeviceIsReachable(devicePath, new FileStreamOptions{ Access = FileAccess.Write });

File.WriteAllText(devicePath, "foo");
}

[Theory]
[MemberData(nameof(DevicePath_TestData))]
public async Task CharacterDevice_WriteAllTextAsync(string devicePath)
{
VerifyDeviceExists(devicePath);
VerifyDeviceIsReachable(devicePath, new FileStreamOptions{ Options = FileOptions.Asynchronous, Access = FileAccess.Write });

await File.WriteAllTextAsync(devicePath, "foo");
}

private static void VerifyDeviceExists(string devicePath)
private static void VerifyDeviceIsReachable(string devicePath, FileStreamOptions? options)
{
if (!File.Exists(devicePath))
{
throw new SkipTestException("Device does not exists in this platform");
}
throw new SkipTestException("Device does not exists");
}

try
{
File.Open(devicePath, options).Dispose();
}
catch (IOException ex)
{
throw new SkipTestException($"Device failed to open: {ex.Message}");
}
}

private static string[] DevicePaths = { "/dev/tty", "/dev/console", "/dev/null", "/dev/zero" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal static unsafe int ReadAtOffset(SafeFileHandle handle, Span<byte> buffer
// Historically we were able to handle /dev/tty using read so we need to fallback to read for that case.
Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
// We want to discover more errors that could make pread fail and add unit tests for them.
Debug.Assert(errorInfo.Error == Interop.Error.ENXIO);
Debug.Assert(errorInfo.Error == Interop.Error.ENXIO, $"Unexpected error: {errorInfo.Error}");
if (errorInfo.Error == Interop.Error.ENXIO)
{
handle.SupportsRandomAccess = false;
Expand Down Expand Up @@ -122,7 +122,7 @@ internal static unsafe void WriteAtOffset(SafeFileHandle handle, ReadOnlySpan<by
// Historically we were able to handle /dev/tty using write so we need to fallback to write for that case.
Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
// We want to discover more errors that could make pwrite fail and add unit tests for them.
Debug.Assert(errorInfo.Error == Interop.Error.ENXIO);
Debug.Assert(errorInfo.Error == Interop.Error.ENXIO, $"Unexpected error: {errorInfo.Error}");

if (errorInfo.Error == Interop.Error.ENXIO)
{
Expand Down