-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from koenvzeijl/feature/runtime-sass-compiler
Expose interface to call the dart-sass executable during runtime
- Loading branch information
Showing
13 changed files
with
425 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
AspNetCore.SassCompiler.Tests/AspNetCore.SassCompiler.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<SassCompilerIncludeRuntime>true</SassCompilerIncludeRuntime> | ||
</PropertyGroup> | ||
|
||
<!-- Only needed because we're using a ProjectReference, this is done implicitly for PackageReference's --> | ||
<Import Project="..\AspNetCore.SassCompiler\build\AspNetCore.SassCompiler.props" /> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<SassCompilerTasksAssembly Condition=" '$(Configuration)' != '' ">$(MSBuildThisFileDirectory)..\AspNetCore.SassCompiler.Tasks\bin\$(Configuration)\netstandard2.0\AspNetCore.SassCompiler.Tasks.dll</SassCompilerTasksAssembly> | ||
<SassCompilerTasksAssembly Condition=" '$(Configuration)' == '' ">$(MSBuildThisFileDirectory)..\AspNetCore.SassCompiler.Tasks\bin\Debug\netstandard2.0\AspNetCore.SassCompiler.Tasks.dll</SassCompilerTasksAssembly> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" /> | ||
<PackageReference Include="xunit" Version="2.7.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="6.0.1"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\AspNetCore.SassCompiler\AspNetCore.SassCompiler.csproj" /> | ||
</ItemGroup> | ||
|
||
<!-- Only needed because we're using a ProjectReference, this is done implicitly for PackageReference's --> | ||
<Import Project="..\AspNetCore.SassCompiler\build\AspNetCore.SassCompiler.targets" /> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using System.Text; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Xunit; | ||
|
||
namespace AspNetCore.SassCompiler.Tests; | ||
|
||
public class SassCompilerTests | ||
{ | ||
[Fact] | ||
public async Task CompileAsync_WithoutStreams_Success() | ||
{ | ||
// Arrange | ||
var sassCompiler = new SassCompiler(NullLogger<SassCompiler>.Instance); | ||
|
||
var tempDirectory = Path.Join(Path.GetTempPath(), Path.GetRandomFileName()); | ||
Directory.CreateDirectory(tempDirectory); | ||
|
||
try | ||
{ | ||
await File.WriteAllTextAsync(Path.Join(tempDirectory, "input"), "body { color: black; }"); | ||
|
||
// Act | ||
await sassCompiler.CompileAsync(new[] { Path.Join(tempDirectory, "input"), Path.Join(tempDirectory, "output"), "--no-source-map" }); | ||
var result = await File.ReadAllTextAsync(Path.Join(tempDirectory, "output")); | ||
|
||
// Assert | ||
Assert.Equal("body {\n color: black;\n}\n", result); | ||
} | ||
finally | ||
{ | ||
Directory.Delete(tempDirectory, true); | ||
} | ||
} | ||
|
||
[Theory] | ||
[InlineData("--watch")] | ||
[InlineData("--interactive")] | ||
public async Task CompileAsync_ThrowsWithInvalidArguments(string argument) | ||
{ | ||
// Arrange | ||
var sassCompiler = new SassCompiler(NullLogger<SassCompiler>.Instance); | ||
|
||
var input = new MemoryStream(Encoding.UTF8.GetBytes("body { color: black; }")); | ||
|
||
// Act | ||
async Task Act() => await sassCompiler.CompileAsync(input, new[] { argument }); | ||
|
||
// Assert | ||
var exception = await Assert.ThrowsAsync<SassCompilerException>(Act); | ||
Assert.Equal($"The sass {argument} option is not supported.", exception.Message); | ||
Assert.Null(exception.ErrorOutput); | ||
} | ||
|
||
[Fact] | ||
public async Task CompileAsync_ThrowsWithInvalidInput() | ||
{ | ||
// Arrange | ||
var sassCompiler = new SassCompiler(NullLogger<SassCompiler>.Instance); | ||
|
||
var input = new MemoryStream(Encoding.UTF8.GetBytes("body { color: black;")); | ||
|
||
// Act | ||
async Task Act() => await sassCompiler.CompileAsync(input, Array.Empty<string>()); | ||
|
||
// Assert | ||
var exception = await Assert.ThrowsAsync<SassCompilerException>(Act); | ||
Assert.Equal("Sass process exited with non-zero exit code: 65.", exception.Message); | ||
Assert.StartsWith("Error: expected \"}\".", exception.ErrorOutput); | ||
} | ||
|
||
[Fact] | ||
public async Task CompileAsync_ReturningOutputStream_Success() | ||
{ | ||
// Arrange | ||
var sassCompiler = new SassCompiler(NullLogger<SassCompiler>.Instance); | ||
|
||
var input = new MemoryStream(Encoding.UTF8.GetBytes("body { color: black; }")); | ||
|
||
// Act | ||
var output = await sassCompiler.CompileAsync(input, Array.Empty<string>()); | ||
var result = await new StreamReader(output).ReadToEndAsync(); | ||
|
||
// Assert | ||
Assert.Equal("body {\n color: black;\n}\n", result); | ||
} | ||
|
||
[Fact] | ||
public async Task CompileAsync_WithInputAndOutputStream_Success() | ||
{ | ||
// Arrange | ||
var sassCompiler = new SassCompiler(NullLogger<SassCompiler>.Instance); | ||
|
||
var input = new MemoryStream(Encoding.UTF8.GetBytes("body { color: black; }")); | ||
var output = new MemoryStream(); | ||
|
||
// Act | ||
await sassCompiler.CompileAsync(input, output, Array.Empty<string>()); | ||
var result = Encoding.UTF8.GetString(output.ToArray()); | ||
|
||
// Assert | ||
Assert.Equal("body {\n color: black;\n}\n", result); | ||
} | ||
|
||
[Fact] | ||
public async Task CompileToStringAsync_Success() | ||
{ | ||
// Arrange | ||
var sassCompiler = new SassCompiler(NullLogger<SassCompiler>.Instance); | ||
|
||
var input = new MemoryStream(Encoding.UTF8.GetBytes("body { color: black; }")); | ||
|
||
// Act | ||
var result = await sassCompiler.CompileToStringAsync(input, Array.Empty<string>()); | ||
|
||
// Assert | ||
Assert.Equal("body {\n color: black;\n}\n", result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.