Skip to content

Commit

Permalink
Keep track of which Less dependencies have been checked. (madskristen…
Browse files Browse the repository at this point in the history
…sen#426)

* Keep track of which Less dependencies have been checked.

* Fixed Less circular dependency check.
  • Loading branch information
jws305 authored and madskristensen committed Jul 15, 2019
1 parent e2ac3d7 commit 194200c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/WebCompiler/Config/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,21 @@ private bool HasDependenciesNewerThanOutput(FileInfo input, FileInfo output)
return false;
}

private bool CheckForNewerDependenciesRecursively(string key, Dictionary<string, Dependencies> dependencies, FileInfo output)
private bool CheckForNewerDependenciesRecursively(string key, Dictionary<string, Dependencies> dependencies, FileInfo output, HashSet<string> checkedDependencies = null)
{
if (checkedDependencies == null)
checkedDependencies = new HashSet<string>();

checkedDependencies.Add(key);

if (!dependencies.ContainsKey(key))
return false;

foreach (var file in dependencies[key].DependentOn.ToArray())
{
if (checkedDependencies.Contains(file))
continue;

var fileInfo = new FileInfo(file);

if (!fileInfo.Exists)
Expand All @@ -120,7 +128,7 @@ private bool CheckForNewerDependenciesRecursively(string key, Dictionary<string,
if (fileInfo.LastWriteTimeUtc > output.LastWriteTimeUtc)
return true;

if (CheckForNewerDependenciesRecursively(file, dependencies, output))
if (CheckForNewerDependenciesRecursively(file, dependencies, output, checkedDependencies))
return true;
}

Expand Down
18 changes: 17 additions & 1 deletion src/WebCompilerTest/Compile/LessTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WebCompiler;
Expand All @@ -25,6 +26,8 @@ public void Cleanup()
File.Delete("../../artifacts/less/error.css");
File.Delete("../../artifacts/less/relative.css");
File.Delete("../../artifacts/less/relative.min.css");
File.Delete("../../artifacts/less/circrefa.css");
File.Delete("../../artifacts/less/circrefa.min.css");
}

[TestMethod, TestCategory("LESS")]
Expand Down Expand Up @@ -92,6 +95,19 @@ public void OtherExtensionTypeSourceFileChangedTest()
}

[TestMethod, TestCategory("LESS")]
public void CompileCircularReference()
{
// Set the last write time and create outputs in a way that Config.CheckForNewerDependenciesRecursively will be called
File.SetLastWriteTimeUtc("../../artifacts/less/circrefa.less", DateTime.UtcNow);
File.SetLastWriteTimeUtc("../../artifacts/less/circrefb.less", DateTime.UtcNow);
File.WriteAllText("../../artifacts/less/circrefa.css", string.Empty);
File.WriteAllText("../../artifacts/less/circrefa.min.css", string.Empty);

// Since the outputs were generated after the inputs, no compilation should have occurred
var result = _processor.Process("../../artifacts/lessconfigCircRef.json");
Assert.AreEqual(0, result.Count<CompilerResult>());
}

public void CompileLessLegacyStrictMath()
{
var result = _processor.Process("../../artifacts/lessconfigLegacyStrictMath.json");
Expand Down
3 changes: 3 additions & 0 deletions src/WebCompilerTest/WebCompilerTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
<None Include="artifacts\handlebars\test.hbs" />
<None Include="artifacts\handlebars\error.hbs" />
<None Include="artifacts\handlebars\_partial.hbs" />
<None Include="artifacts\lessconfigCircRef.json" />
<None Include="artifacts\less\circrefa.less" />
<None Include="artifacts\less\circrefb.less" />
<None Include="artifacts\lessconfigLegacyStrictMath.json" />
<None Include="artifacts\scss\sub\foo.scss" />
<None Include="artifacts\stylusconfig.json" />
Expand Down
3 changes: 3 additions & 0 deletions src/WebCompilerTest/artifacts/less/circrefa.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@import "circrefb.less";

@primary-color: #f00;
5 changes: 5 additions & 0 deletions src/WebCompilerTest/artifacts/less/circrefb.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import (reference) "circrefa.less";

.btn {
background-color: @primary-color;
}
10 changes: 10 additions & 0 deletions src/WebCompilerTest/artifacts/lessconfigCircRef.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"outputFile": "less/circrefa.css",
"inputFile": "less/circrefa.less",
"includeInProject": true,
"options": {
"relativeUrls": true
}
}
]

0 comments on commit 194200c

Please sign in to comment.