Skip to content

Commit

Permalink
RoslynCompilationService should set the generated code when compilati…
Browse files Browse the repository at this point in the history
…on is successful

Fixes aspnet#895
  • Loading branch information
pranavkm committed Aug 19, 2014
1 parent 64d797a commit 764b1e6
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/Microsoft.AspNet.Mvc.Razor/Compilation/CompilationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ public class CompilationResult
{
private Type _type;

private CompilationResult()
/// <summary>
/// Creates a new instance of <see cref="CompilationResult"/>.
/// </summary>
protected CompilationResult()
{
}

Expand All @@ -40,12 +43,12 @@ public string FilePath
public IEnumerable<CompilationMessage> Messages { get; private set; }

/// <summary>
/// Gets the generated C# content that was compiled.
/// Gets (or sets in derived types) the generated C# content that was compiled.
/// </summary>
public string CompiledContent { get; private set; }
public string CompiledContent { get; protected set; }

/// <summary>
/// Gets the type produced as a result of compilation.
/// Gets (or sets in derived types) the type produced as a result of compilation.
/// </summary>
/// <exception cref="CompilationFailedException">An error occured during compilation.</exception>
public Type CompiledType
Expand All @@ -59,6 +62,10 @@ public Type CompiledType

return _type;
}
protected set
{
_type = value;
}
}

private IFileInfo File { get; set; }
Expand Down Expand Up @@ -91,7 +98,7 @@ public static CompilationResult Successful([NotNull] Type type)
{
return new CompilationResult
{
_type = type
CompiledType = type
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public CompilationResult Compile(IFileInfo fileInfo, string compilationContent)
var type = assembly.GetExportedTypes()
.First();

return CompilationResult.Successful(type);
return UncachedCompilationResult.Successful(type, compilationContent);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;

namespace Microsoft.AspNet.Mvc.Razor
{
/// <summary>
/// Represents the result of compilation that does not come from the <see cref="CompilerCache" />.
/// </summary>
public class UncachedCompilationResult : CompilationResult
{
private UncachedCompilationResult()
{
}

/// <summary>
/// Creates a <see cref="UncachedCompilationResult"/> that represents a success in compilation.
/// </summary>
/// <param name="type">The compiled type.</param>
/// <param name="compiledContent">The generated C# content that was compiled.</param>
/// <returns>An <see cref="UncachedCompilationResult"/> instance that indicates a successful
/// compilation.</returns>
public static UncachedCompilationResult Successful([NotNull] Type type,
[NotNull] string compiledContent)
{
return new UncachedCompilationResult
{
CompiledType = type,
CompiledContent = compiledContent,
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.AspNet.FileSystems;
using Moq;
using Xunit;

namespace Microsoft.AspNet.Mvc.Razor.Test
{
public class CompilerCacheTest
{
[Fact]
public void GetOrAdd_ReturnsCompilationResultFromFactory()
{
// Arrange
var cache = new CompilerCache();
var fileInfo = Mock.Of<IFileInfo>();
var type = GetType();
var expected = UncachedCompilationResult.Successful(type, "hello world");

// Act
var actual = cache.GetOrAdd(fileInfo, () => expected);

// Assert
Assert.Same(expected, actual);
Assert.Equal("hello world", actual.CompiledContent);
Assert.Same(type, actual.CompiledType);
}

[Fact]
public void GetOrAdd_DoesNotCacheCompiledContent_OnCallsAfterInitial()
{
// Arrange
var lastModified = DateTime.UtcNow;
var cache = new CompilerCache();
var fileInfo = new Mock<IFileInfo>();
fileInfo.SetupGet(f => f.PhysicalPath)
.Returns("test");
fileInfo.SetupGet(f => f.LastModified)
.Returns(lastModified);
var type = GetType();
var uncachedResult = UncachedCompilationResult.Successful(type, "hello world");

// Act
cache.GetOrAdd(fileInfo.Object, () => uncachedResult);
var actual1 = cache.GetOrAdd(fileInfo.Object, () => uncachedResult);
var actual2 = cache.GetOrAdd(fileInfo.Object, () => uncachedResult);

// Assert
Assert.NotSame(uncachedResult, actual1);
Assert.NotSame(uncachedResult, actual2);
var result = Assert.IsType<CompilationResult>(actual1);
Assert.Null(actual1.CompiledContent);
Assert.Same(type, actual1.CompiledType);

result = Assert.IsType<CompilationResult>(actual2);
Assert.Null(actual2.CompiledContent);
Assert.Same(type, actual2.CompiledType);
}
}
}

0 comments on commit 764b1e6

Please sign in to comment.