Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose interface to call the dart-sass executable during runtime #173

Merged
merged 4 commits into from
Mar 14, 2024
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
2 changes: 2 additions & 0 deletions .github/workflows/dotnetcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jobs:
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --configuration Release --no-build
- name: Validate generated css files
run: |
test -s Samples/AspNetCore.SassCompiler.BlazorSample/Components/Pages/Counter.razor.css
Expand Down
43 changes: 43 additions & 0 deletions AspNetCore.SassCompiler.Tests/AspNetCore.SassCompiler.Tests.csproj
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>
118 changes: 118 additions & 0 deletions AspNetCore.SassCompiler.Tests/SassCompilerTests.cs
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);
}
}
6 changes: 6 additions & 0 deletions AspNetCore.SassCompiler.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCore.SassCompiler.Bla
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCore.SassCompiler.RazorClassLibrary", "Samples\AspNetCore.SassCompiler.RazorClassLibrary\AspNetCore.SassCompiler.RazorClassLibrary.csproj", "{B318D98D-B145-4ACF-8B48-D601FB5C91E4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCore.SassCompiler.Tests", "AspNetCore.SassCompiler.Tests\AspNetCore.SassCompiler.Tests.csproj", "{0ACD7A9B-8D2A-4274-86F8-0FA4A2413903}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -50,6 +52,10 @@ Global
{B318D98D-B145-4ACF-8B48-D601FB5C91E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B318D98D-B145-4ACF-8B48-D601FB5C91E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B318D98D-B145-4ACF-8B48-D601FB5C91E4}.Release|Any CPU.Build.0 = Release|Any CPU
{0ACD7A9B-8D2A-4274-86F8-0FA4A2413903}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0ACD7A9B-8D2A-4274-86F8-0FA4A2413903}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0ACD7A9B-8D2A-4274-86F8-0FA4A2413903}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0ACD7A9B-8D2A-4274-86F8-0FA4A2413903}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 4 additions & 0 deletions AspNetCore.SassCompiler/AspNetCore.SassCompiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
<ProjectReference Include="..\AspNetCore.SassCompiler.Tasks\AspNetCore.SassCompiler.Tasks.csproj" ReferenceOutputAssembly="false" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="AspNetCore.SassCompiler.Tests" />
</ItemGroup>

<ItemGroup>
<None Include="build\*" Pack="true" PackagePath="build" />
<None Include="runtimes\**" Pack="true" PackagePath="runtimes" />
Expand Down
2 changes: 1 addition & 1 deletion AspNetCore.SassCompiler/ChildProcessTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace AspNetCore.SassCompiler
/// <remarks>References:
/// https://stackoverflow.com/a/4657392/386091
/// https://stackoverflow.com/a/9164742/386091 </remarks>
public static class ChildProcessTracker
internal static class ChildProcessTracker
{
/// <summary>
/// Add the process to be tracked. If our current process is killed, the child processes
Expand Down
Loading
Loading