From 194200c7733053f9692cfac22085cf6bc435c625 Mon Sep 17 00:00:00 2001 From: John Seaman Date: Mon, 15 Jul 2019 13:38:29 -0400 Subject: [PATCH] Keep track of which Less dependencies have been checked. (#426) * Keep track of which Less dependencies have been checked. * Fixed Less circular dependency check. --- src/WebCompiler/Config/Config.cs | 12 ++++++++++-- src/WebCompilerTest/Compile/LessTest.cs | 18 +++++++++++++++++- src/WebCompilerTest/WebCompilerTest.csproj | 3 +++ .../artifacts/less/circrefa.less | 3 +++ .../artifacts/less/circrefb.less | 5 +++++ .../artifacts/lessconfigCircRef.json | 10 ++++++++++ 6 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 src/WebCompilerTest/artifacts/less/circrefa.less create mode 100644 src/WebCompilerTest/artifacts/less/circrefb.less create mode 100644 src/WebCompilerTest/artifacts/lessconfigCircRef.json diff --git a/src/WebCompiler/Config/Config.cs b/src/WebCompiler/Config/Config.cs index c2465e23..096dafd5 100644 --- a/src/WebCompiler/Config/Config.cs +++ b/src/WebCompiler/Config/Config.cs @@ -105,13 +105,21 @@ private bool HasDependenciesNewerThanOutput(FileInfo input, FileInfo output) return false; } - private bool CheckForNewerDependenciesRecursively(string key, Dictionary dependencies, FileInfo output) + private bool CheckForNewerDependenciesRecursively(string key, Dictionary dependencies, FileInfo output, HashSet checkedDependencies = null) { + if (checkedDependencies == null) + checkedDependencies = new HashSet(); + + 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) @@ -120,7 +128,7 @@ private bool CheckForNewerDependenciesRecursively(string key, Dictionary output.LastWriteTimeUtc) return true; - if (CheckForNewerDependenciesRecursively(file, dependencies, output)) + if (CheckForNewerDependenciesRecursively(file, dependencies, output, checkedDependencies)) return true; } diff --git a/src/WebCompilerTest/Compile/LessTest.cs b/src/WebCompilerTest/Compile/LessTest.cs index 1b2858f3..19ad9bc5 100644 --- a/src/WebCompilerTest/Compile/LessTest.cs +++ b/src/WebCompilerTest/Compile/LessTest.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using WebCompiler; @@ -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")] @@ -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()); + } + public void CompileLessLegacyStrictMath() { var result = _processor.Process("../../artifacts/lessconfigLegacyStrictMath.json"); diff --git a/src/WebCompilerTest/WebCompilerTest.csproj b/src/WebCompilerTest/WebCompilerTest.csproj index 4a1a90b4..1eb9eb54 100644 --- a/src/WebCompilerTest/WebCompilerTest.csproj +++ b/src/WebCompilerTest/WebCompilerTest.csproj @@ -86,6 +86,9 @@ + + + diff --git a/src/WebCompilerTest/artifacts/less/circrefa.less b/src/WebCompilerTest/artifacts/less/circrefa.less new file mode 100644 index 00000000..d6552e0f --- /dev/null +++ b/src/WebCompilerTest/artifacts/less/circrefa.less @@ -0,0 +1,3 @@ +@import "circrefb.less"; + +@primary-color: #f00; diff --git a/src/WebCompilerTest/artifacts/less/circrefb.less b/src/WebCompilerTest/artifacts/less/circrefb.less new file mode 100644 index 00000000..b423691b --- /dev/null +++ b/src/WebCompilerTest/artifacts/less/circrefb.less @@ -0,0 +1,5 @@ +@import (reference) "circrefa.less"; + +.btn { + background-color: @primary-color; +} diff --git a/src/WebCompilerTest/artifacts/lessconfigCircRef.json b/src/WebCompilerTest/artifacts/lessconfigCircRef.json new file mode 100644 index 00000000..fb13264c --- /dev/null +++ b/src/WebCompilerTest/artifacts/lessconfigCircRef.json @@ -0,0 +1,10 @@ +[ + { + "outputFile": "less/circrefa.css", + "inputFile": "less/circrefa.less", + "includeInProject": true, + "options": { + "relativeUrls": true + } + } +] \ No newline at end of file