Skip to content
This repository was archived by the owner on Nov 15, 2021. It is now read-only.
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
12 changes: 9 additions & 3 deletions main/OpenCover.Console/CrashReporter/SendRequestState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ static private ExceptionInfo ConvertToExceptionInfo(Exception e, bool anonymous)
};
}

static private IPAddress GetGoogleDnsAddress()
{
using (var googleDns = new System.Net.Sockets.UdpClient("8.8.8.8", 53))
{
return ((IPEndPoint) googleDns.Client.LocalEndPoint).Address;
}
}

static private System.Net.NetworkInformation.PhysicalAddress GetMacAddress()
{
var googleDns = new System.Net.Sockets.UdpClient("8.8.8.8", 53);
var localAddress = ((IPEndPoint)googleDns.Client.LocalEndPoint).Address;
googleDns.Close();
var localAddress = GetGoogleDnsAddress();

foreach (var netInterface in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
{
Expand Down
4 changes: 3 additions & 1 deletion main/OpenCover.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ static int Main(string[] args)
{
var persistance = new FilePersistance(parser, Logger);
container.Initialise(filter, parser, persistance, perfCounter);
persistance.Initialise(outputFile, parser.MergeExistingOutputFile);
if (!persistance.Initialise(outputFile, parser.MergeExistingOutputFile))
return returnCodeOffset + 1;

returnCode = RunWithContainer(parser, container, persistance);
}

Expand Down
2 changes: 1 addition & 1 deletion main/OpenCover.Framework/Manager/MemoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,10 @@ public void Initialise(string @namespace, string key, IEnumerable<string> servic
public ManagedBufferBlock AllocateMemoryBuffer(int bufferSize, out uint bufferId)
{
bufferId = 0;
if (!_isIntialised) return null;

lock (_lockObject)
{
if (!_isIntialised) return null;
bufferId = _bufferId++;
var tuple = new ManagedBufferBlock
{
Expand Down
58 changes: 46 additions & 12 deletions main/OpenCover.Framework/Persistance/FilePersistance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,45 @@ public FilePersistance(ICommandLine commandLine, ILog logger) : base(commandLine
/// </summary>
/// <param name="fileName">The filename to save to</param>
/// <param name="loadExisting"></param>
public void Initialise(string fileName, bool loadExisting)
public bool Initialise(string fileName, bool loadExisting)
{
_fileName = fileName;
if (loadExisting && File.Exists(fileName))
return HandleFileAccess(() => {
_fileName = fileName;
if (loadExisting && File.Exists(fileName))
{
LoadCoverageFile();
}
// test the file location can be accessed
using (var fs = File.OpenWrite(fileName))
fs.Close();
}, fileName);
}

internal bool HandleFileAccess(Action loadFile, string fileName)
{
try
{
loadFile();
}
catch (DirectoryNotFoundException dex) // issue #456
{
LoadCoverageFile();
_logger.Info(string.Format("Could not find the directory of the supplied coverage file '{0}', Please check the path and try again.", fileName));
_logger.Debug(dex.Message, dex);
return false;
}
catch (IOException iex) // issue #458
{
_logger.Info(string.Format("Could not access the location of the supplied coverage file '{0}', Please check the path and your permissions and try again.", fileName));
_logger.Debug(iex.Message, iex);
return false;
}
catch (UnauthorizedAccessException iex) // issue #458
{
_logger.Info(string.Format("Could not access the location of the supplied coverage file '{0}', Please check the path and your permissions and try again.", fileName));
_logger.Debug(iex.Message, iex);
return false;
}
return true;
}

private void LoadCoverageFile()
Expand Down Expand Up @@ -78,16 +110,18 @@ public override void Commit()
SaveCoverageFile();
}

private void SaveCoverageFile()
private bool SaveCoverageFile()
{
var serializer = new XmlSerializer(typeof (CoverageSession),
new[] {typeof (Module), typeof (Model.File), typeof (Class)});
return HandleFileAccess(() => {
var serializer = new XmlSerializer(typeof(CoverageSession),
new[] { typeof(Module), typeof(Model.File), typeof(Class) });

using (var fs = new FileStream(_fileName, FileMode.Create))
using (var writer = new StreamWriter(fs, new UTF8Encoding()))
{
serializer.Serialize(writer, CoverageSession);
}
using (var fs = new FileStream(_fileName, FileMode.Create))
using (var writer = new StreamWriter(fs, new UTF8Encoding()))
{
serializer.Serialize(writer, CoverageSession);
}
}, _fileName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using Moq;
using NUnit.Framework;
using OpenCover.Framework;
Expand Down Expand Up @@ -125,5 +124,42 @@ public void CanLoadExistingFileWhenInitialising()
Assert.AreEqual(0, persistence2.CoverageSession.Modules[0].Classes[0].Summary.NumSequencePoints);
Assert.AreEqual(0, persistence2.CoverageSession.Modules[0].Classes[0].Methods[0].Summary.NumSequencePoints);
}

[Test]
public void HandleFileAccess_SuppliedActionSuccess_ReturnsTrue()
{
// arrange
var persistence = new FilePersistance(_mockCommandLine.Object, _mockLogger.Object);

// act
Assert.IsTrue(persistence.HandleFileAccess(() => { }, "file_name"));
}

[Test]
public void HandleFileAccess_SuppliedActionThrows_Exception_ReturnsException()
{
// arrange
var persistence = new FilePersistance(_mockCommandLine.Object, _mockLogger.Object);

// act
var expected = new Exception();
var actual = Assert.Throws<Exception>(() => persistence.HandleFileAccess(() => { throw expected; }, "file_name"));

// assert
Assert.AreSame(expected, actual);
}

[Test]
[TestCase(typeof(DirectoryNotFoundException))]
[TestCase(typeof(IOException))]
[TestCase(typeof(UnauthorizedAccessException))]
public void HandleFileAccess_SuppliedActionThrows_Exception_ReturnsFalse(Type exception)
{
// arrange
var persistence = new FilePersistance(_mockCommandLine.Object, _mockLogger.Object);

// act
Assert.IsFalse(persistence.HandleFileAccess(() => { throw (Exception) Activator.CreateInstance(exception); }, "file_name"));
}
}
}