Skip to content

Commit ec97434

Browse files
committed
Added basic coverage on LiveReloadServer.
1 parent e72b504 commit ec97434

7 files changed

Lines changed: 101 additions & 7 deletions

File tree

src/clients/Wyam/LiveReload/LiveReloadServer.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reflection;
5+
using System.Runtime.CompilerServices;
56

67
using Microsoft.Owin;
78
using Microsoft.Owin.FileSystems;
@@ -22,9 +23,9 @@ internal class LiveReloadServer : IDisposable
2223
private readonly ReloadClientServiceLocator _clientServiceLocator;
2324
private IDisposable _server;
2425

25-
public LiveReloadServer()
26+
public LiveReloadServer(ReloadClientServiceLocator clientServiceLocator = null)
2627
{
27-
_clientServiceLocator = new ReloadClientServiceLocator();
28+
_clientServiceLocator = clientServiceLocator ?? new ReloadClientServiceLocator();
2829
}
2930

3031
public void StartStandaloneHost(int port = 35729)
@@ -63,8 +64,8 @@ public void InjectOwinMiddleware(IAppBuilder app)
6364

6465
public void RebuildCompleted(ICollection<string> filesChanged)
6566
{
66-
IEnumerable<ReloadClient> clientsToNotify = _clientServiceLocator.ReloadClients;
67-
foreach (ReloadClient client in clientsToNotify.Where(x => x.IsConnected))
67+
IEnumerable<IReloadClient> clientsToNotify = _clientServiceLocator.ReloadClients;
68+
foreach (IReloadClient client in clientsToNotify.Where(x => x.IsConnected))
6869
{
6970
foreach (string modifiedFile in filesChanged)
7071
{

src/clients/Wyam/LiveReload/ReloadClient.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@
1515

1616
namespace Wyam.LiveReload
1717
{
18-
public class ReloadClient : WebSocketConnection
18+
public interface IReloadClient
19+
{
20+
bool IsConnected { get; }
21+
void NotifyOfChanges(string modifiedFile, bool supportCssReload = true);
22+
}
23+
24+
public class ReloadClient : WebSocketConnection, IReloadClient
1925
{
2026
// Attempt to support the Livereload protocol v7.
2127
// http://feedback.livereload.com/knowledgebase/articles/86174-livereload-protocol

src/clients/Wyam/LiveReload/ReloadClientServiceLocator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ namespace Wyam.LiveReload
88
{
99
internal class ReloadClientServiceLocator : IServiceLocator
1010
{
11-
readonly ConcurrentBag<ReloadClient> _clients = new ConcurrentBag<ReloadClient>();
11+
private readonly ConcurrentBag<IReloadClient> _clients = new ConcurrentBag<IReloadClient>();
1212

13-
public IEnumerable<ReloadClient> ReloadClients => _clients.ToArray();
13+
public virtual IEnumerable<IReloadClient> ReloadClients => _clients.ToArray();
1414

1515
public object GetService(Type serviceType)
1616
{

src/clients/Wyam/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
using System.Runtime.CompilerServices;
33
using System.Runtime.InteropServices;
44

5+
// NSubstitute
6+
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
7+
58
[assembly: AssemblyTitle("Wyam")]
69
[assembly: AssemblyDescription("")]
710
[assembly: ComVisible(false)]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
using NSubstitute;
5+
6+
using NUnit.Framework;
7+
8+
using Ploeh.AutoFixture;
9+
10+
using Wyam.LiveReload;
11+
12+
namespace Wyam.Tests.LiveReload
13+
{
14+
[TestFixture]
15+
public class LiveReloadServerTests
16+
{
17+
private static readonly Fixture AutoFixture = new Fixture();
18+
19+
[Test]
20+
public void RebuildCompletedShouldNotifyConnectedClients()
21+
{
22+
var changedFiles = AutoFixture.CreateMany<string>(1).ToList();
23+
24+
var reloadClientMock = Substitute.For<IReloadClient>();
25+
reloadClientMock.IsConnected.Returns(true);
26+
27+
var serviceLocatorMock = Substitute.ForPartsOf<ReloadClientServiceLocator>();
28+
serviceLocatorMock.ReloadClients.Returns(new List<IReloadClient> { reloadClientMock });
29+
30+
var server = new LiveReloadServer(serviceLocatorMock);
31+
server.RebuildCompleted(changedFiles);
32+
33+
reloadClientMock.Received().NotifyOfChanges(Arg.Is<string>(s => changedFiles.Contains(s)), Arg.Is(true));
34+
}
35+
36+
[Test]
37+
public void RebuildCompletedShouldAvoidMissingClients()
38+
{
39+
var changedFiles = AutoFixture.CreateMany<string>(1).ToList();
40+
41+
var reloadClientMock = Substitute.For<IReloadClient>();
42+
reloadClientMock.IsConnected.Returns(false);
43+
44+
var serviceLocatorMock = Substitute.ForPartsOf<ReloadClientServiceLocator>();
45+
serviceLocatorMock.ReloadClients.Returns(new List<IReloadClient> { reloadClientMock });
46+
47+
var server = new LiveReloadServer(serviceLocatorMock);
48+
server.RebuildCompleted(changedFiles);
49+
50+
reloadClientMock.DidNotReceive().NotifyOfChanges(Arg.Any<string>(), Arg.Any<bool>());
51+
}
52+
53+
[Test]
54+
public void RebuildCompletedShouldNotifyOfAllChangedFiles()
55+
{
56+
var changedFiles = AutoFixture.CreateMany<string>().ToList();
57+
58+
var reloadClientMock = Substitute.For<IReloadClient>();
59+
reloadClientMock.IsConnected.Returns(true);
60+
61+
var serviceLocatorMock = Substitute.ForPartsOf<ReloadClientServiceLocator>();
62+
serviceLocatorMock.ReloadClients.Returns(new List<IReloadClient> { reloadClientMock });
63+
64+
var server = new LiveReloadServer(serviceLocatorMock);
65+
server.RebuildCompleted(changedFiles);
66+
67+
foreach (var changedFile in changedFiles)
68+
{
69+
reloadClientMock.Received().NotifyOfChanges(Arg.Is(changedFile), Arg.Is(true));
70+
}
71+
}
72+
}
73+
}

tests/clients/Wyam.Tests/Wyam.Tests.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
<WarningLevel>4</WarningLevel>
3232
</PropertyGroup>
3333
<ItemGroup>
34+
<Reference Include="Microsoft.Practices.ServiceLocation, Version=1.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
35+
<HintPath>..\..\..\packages\CommonServiceLocator.1.3\lib\portable-net4+sl5+netcore45+wpa81+wp8\Microsoft.Practices.ServiceLocation.dll</HintPath>
36+
<Private>True</Private>
37+
</Reference>
3438
<Reference Include="NSubstitute, Version=1.9.2.0, Culture=neutral, PublicKeyToken=92dd2e9066daa5ca, processorArchitecture=MSIL">
3539
<HintPath>..\..\..\packages\NSubstitute.1.9.2.0\lib\net45\NSubstitute.dll</HintPath>
3640
<Private>True</Private>
@@ -39,6 +43,10 @@
3943
<HintPath>..\..\..\packages\NUnit.3.0.1\lib\net45\nunit.framework.dll</HintPath>
4044
<Private>True</Private>
4145
</Reference>
46+
<Reference Include="Ploeh.AutoFixture, Version=3.50.2.0, Culture=neutral, PublicKeyToken=b24654c590009d4f, processorArchitecture=MSIL">
47+
<HintPath>..\..\..\packages\AutoFixture.3.50.2\lib\net40\Ploeh.AutoFixture.dll</HintPath>
48+
<Private>True</Private>
49+
</Reference>
4250
<Reference Include="System" />
4351
<Reference Include="System.ComponentModel.Composition" />
4452
<Reference Include="System.Core" />
@@ -84,6 +92,7 @@
8492
<Compile Include="..\..\..\SolutionInfo.cs">
8593
<Link>Properties\SolutionInfo.cs</Link>
8694
</Compile>
95+
<Compile Include="LiveReload\LiveReloadServerTests.cs" />
8796
<Compile Include="Properties\AssemblyInfo.cs" />
8897
<Compile Include="MetadataParserFixture.cs" />
8998
</ItemGroup>

tests/clients/Wyam.Tests/packages.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="AutoFixture" version="3.50.2" targetFramework="net462" />
4+
<package id="CommonServiceLocator" version="1.3" targetFramework="net462" />
35
<package id="NSubstitute" version="1.9.2.0" targetFramework="net452" />
46
<package id="NUnit" version="3.0.1" targetFramework="net452" />
57
<package id="System.Diagnostics.DiagnosticSource" version="4.3.0" targetFramework="net46" />

0 commit comments

Comments
 (0)