From c60034c8e2d316ed88e868f6d36ce7584050cfd1 Mon Sep 17 00:00:00 2001 From: Shannon Date: Fri, 18 Nov 2016 15:26:20 +0100 Subject: [PATCH] Implement abstract cache buster: ICacheBuster #51 --- .gitignore | 1 + .vs/restore.dg | 2 - src/Smidge.Web/Startup.cs | 38 ++++++++++++++++-- .../Cache/AppDomainLifetimeCacheBuster.cs | 26 ++++++++++++ src/Smidge/Cache/CacheBusterResolver.cs | 29 ++++++++++++++ src/Smidge/Cache/ConfigCacheBuster.cs | 23 +++++++++++ src/Smidge/Cache/ICacheBuster.cs | 14 +++++++ .../CompositeFiles/DefaultUrlManager.cs | 25 +++++++----- src/Smidge/CompositeFiles/IUrlManager.cs | 5 ++- src/Smidge/Models/BundleExtensions.cs | 15 +++++++ .../Options/BundleEnvironmentOptions.cs | 7 +++- src/Smidge/Options/BundleOptions.cs | 40 ++++++++++++++++++- src/Smidge/Options/BundleOptionsBuilder.cs | 11 ++++- src/Smidge/Options/SmidgeOptions.cs | 3 +- src/Smidge/Options/SmidgeOptionsSetup.cs | 2 +- src/Smidge/SmidgeHelper.cs | 15 +++++-- src/Smidge/SmidgeStartup.cs | 6 ++- test/Smidge.Tests/DefaultUrlManagerTests.cs | 22 +++++----- test/Smidge.Tests/SmidgeHelperTests.cs | 16 +++++--- 19 files changed, 256 insertions(+), 44 deletions(-) delete mode 100644 .vs/restore.dg create mode 100644 src/Smidge/Cache/AppDomainLifetimeCacheBuster.cs create mode 100644 src/Smidge/Cache/CacheBusterResolver.cs create mode 100644 src/Smidge/Cache/ConfigCacheBuster.cs create mode 100644 src/Smidge/Cache/ICacheBuster.cs diff --git a/.gitignore b/.gitignore index e5c7003..2563d81 100644 --- a/.gitignore +++ b/.gitignore @@ -189,3 +189,4 @@ src/Smidge.Web/App_Data/Smidge/* /src/Smidge.Web/project.lock.json /tests/Smidge.Tests/project.lock.json test/Smidge.Tests/project.lock.json +test/Smidge.Benchmarks/BenchmarkDotNet.Artifacts/* diff --git a/.vs/restore.dg b/.vs/restore.dg deleted file mode 100644 index 29f7bbb..0000000 --- a/.vs/restore.dg +++ /dev/null @@ -1,2 +0,0 @@ -#:X:\Projects\Smidge\test\Smidge.Tests\Smidge.Tests.xproj -X:\Projects\Smidge\test\Smidge.Tests\Smidge.Tests.xproj|X:\Projects\Smidge\src\Smidge\Smidge.xproj diff --git a/src/Smidge.Web/Startup.cs b/src/Smidge.Web/Startup.cs index 8047961..de0ba1e 100644 --- a/src/Smidge.Web/Startup.cs +++ b/src/Smidge.Web/Startup.cs @@ -1,13 +1,18 @@ -using System.IO; +using System.Collections.Generic; +using System.IO; using System.Linq; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.PlatformAbstractions; +using Smidge.Cache; using Smidge.Options; using Smidge.Models; using Smidge.FileProcessors; +using Smidge.JavaScriptServices; +using Smidge.Nuglify; namespace Smidge.Web { @@ -47,15 +52,39 @@ public void ConfigureServices(IServiceCollection services) { services.AddSingleton(); - services.AddMvc(); + services.AddMvc(); // Or use services.AddSmidge() to test from smidge.json config. services.AddSmidge(_config) - //Set the global Smidge options .Configure(options => { //options.FileWatchOptions.Enabled = true; + options.PipelineFactory.OnGetDefault = GetDefaultPipelineFactory; + options.DefaultBundleOptions.DebugOptions.SetCacheBusterType(); }); + + services.AddSmidgeJavaScriptServices(); + services.AddSmidgeNuglifyServices(); + } + + /// + /// A callback used to modify the default pipeline to use Nuglify for JS processing + /// + /// + /// + /// + private static PreProcessPipeline GetDefaultPipelineFactory(WebFileType fileType, IReadOnlyCollection processors) + { + switch (fileType) + { + case WebFileType.Js: + return new PreProcessPipeline(new IPreProcessor[] + { + processors.OfType().Single() + }); + } + //returning null will fallback to the logic defined in the registered PreProcessPipelineFactory + return null; } public void Configure(IApplicationBuilder app, IHostingEnvironment env) @@ -95,12 +124,13 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) //return some custom ordering return collection.OrderBy(x => x.FilePath); }); - + bundles.Create("test-bundle-2", WebFileType.Js, "~/Js/Bundle2") .WithEnvironmentOptions(BundleEnvironmentOptions.Create() .ForDebug(builder => builder .EnableCompositeProcessing() .EnableFileWatcher() + .SetCacheBusterType() .CacheControlOptions(enableEtag: false, cacheControlMaxAge: 0)) .Build() ); diff --git a/src/Smidge/Cache/AppDomainLifetimeCacheBuster.cs b/src/Smidge/Cache/AppDomainLifetimeCacheBuster.cs new file mode 100644 index 0000000..bb237e9 --- /dev/null +++ b/src/Smidge/Cache/AppDomainLifetimeCacheBuster.cs @@ -0,0 +1,26 @@ +using System; +using System.Globalization; + +namespace Smidge.Cache +{ + /// + /// Creates a cache bust value for the lifetime of the app domain + /// + /// + /// Essentially means that all caches will be busted when the app restarts + /// + public class AppDomainLifetimeCacheBuster : ICacheBuster + { + public AppDomainLifetimeCacheBuster() + { + _value = new Lazy(() => DateTime.UtcNow.Ticks.ToString(NumberFormatInfo.InvariantInfo)); + } + + private static Lazy _value; + + public string GetValue() + { + return _value.Value; + } + } +} \ No newline at end of file diff --git a/src/Smidge/Cache/CacheBusterResolver.cs b/src/Smidge/Cache/CacheBusterResolver.cs new file mode 100644 index 0000000..b9773cd --- /dev/null +++ b/src/Smidge/Cache/CacheBusterResolver.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Smidge.Cache +{ + /// + /// Used to resolve an instance of ICacheBuster from the registered ones in the container + /// + public sealed class CacheBusterResolver + { + private readonly IEnumerable _cacheBusters; + + public CacheBusterResolver(IEnumerable cacheBusters) + { + _cacheBusters = cacheBusters; + } + + /// + /// Get the cache buster for the given type + /// + /// + /// + public ICacheBuster GetCacheBuster(Type busterType) + { + return _cacheBusters.FirstOrDefault(x => x.GetType() == busterType); + } + } +} \ No newline at end of file diff --git a/src/Smidge/Cache/ConfigCacheBuster.cs b/src/Smidge/Cache/ConfigCacheBuster.cs new file mode 100644 index 0000000..d85ae55 --- /dev/null +++ b/src/Smidge/Cache/ConfigCacheBuster.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections; + +namespace Smidge.Cache +{ + /// + /// Based on a static string specified in config + /// + public class ConfigCacheBuster : ICacheBuster + { + private readonly ISmidgeConfig _config; + + public ConfigCacheBuster(ISmidgeConfig config) + { + if (config == null) throw new ArgumentNullException(nameof(config)); + _config = config; + } + public string GetValue() + { + return _config.Version; + } + } +} \ No newline at end of file diff --git a/src/Smidge/Cache/ICacheBuster.cs b/src/Smidge/Cache/ICacheBuster.cs new file mode 100644 index 0000000..6dc4ad2 --- /dev/null +++ b/src/Smidge/Cache/ICacheBuster.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Smidge.Cache +{ + /// + /// Returns the value to cache bust the request + /// + public interface ICacheBuster + { + string GetValue(); + } +} diff --git a/src/Smidge/CompositeFiles/DefaultUrlManager.cs b/src/Smidge/CompositeFiles/DefaultUrlManager.cs index 97661ea..6d79b18 100644 --- a/src/Smidge/CompositeFiles/DefaultUrlManager.cs +++ b/src/Smidge/CompositeFiles/DefaultUrlManager.cs @@ -4,6 +4,7 @@ using System.Text; using System.Linq; using Microsoft.Extensions.Options; +using Smidge.Cache; using Smidge.Options; using Smidge.Hashing; @@ -11,21 +12,21 @@ namespace Smidge.CompositeFiles { public class DefaultUrlManager : IUrlManager { - private readonly ISmidgeConfig _config; private readonly IHasher _hasher; private readonly IRequestHelper _requestHelper; private readonly UrlManagerOptions _options; - public DefaultUrlManager(IOptions options, ISmidgeConfig config, IHasher hasher, IRequestHelper requestHelper) + public DefaultUrlManager(IOptions options, IHasher hasher, IRequestHelper requestHelper) { _hasher = hasher; _requestHelper = requestHelper; _options = options.Value.UrlOptions; - _config = config; } - public string GetUrl(string bundleName, string fileExtension, bool debug) + public string GetUrl(string bundleName, string fileExtension, bool debug, ICacheBuster cacheBuster) { + if (cacheBuster == null) throw new ArgumentNullException(nameof(cacheBuster)); + const string handler = "~/{0}/{1}{2}.{3}{4}"; return _requestHelper.Content( string.Format( @@ -34,12 +35,14 @@ public string GetUrl(string bundleName, string fileExtension, bool debug) Uri.EscapeUriString(bundleName), fileExtension, debug ? 'd' : 'v', - _config.Version)); + cacheBuster.GetValue())); } - public IEnumerable GetUrls(IEnumerable dependencies, string fileExtension) + public IEnumerable GetUrls(IEnumerable dependencies, string fileExtension, ICacheBuster cacheBuster) { + if (cacheBuster == null) throw new ArgumentNullException(nameof(cacheBuster)); + var files = new List(); var currBuilder = new StringBuilder(); var delimitedBuilder = new StringBuilder(); @@ -57,7 +60,7 @@ public IEnumerable GetUrls(IEnumerable dependencies, strin if ((delimitedBuilder.Length + _options.CompositeFilePath.Length + fileExtension.Length - + _config.Version.Length + + cacheBuster.GetValue().Length //this number deals with slashes, etc... + 10) >= (_options.MaxUrlLength)) @@ -73,7 +76,7 @@ public IEnumerable GetUrls(IEnumerable dependencies, strin files.Add(new FileSetUrl { Key = _hasher.Hash(output), - Url = GetCompositeUrl(output, fileExtension) + Url = GetCompositeUrl(output, fileExtension, cacheBuster) }); //create some new output currBuilder = new StringBuilder(); @@ -96,7 +99,7 @@ public IEnumerable GetUrls(IEnumerable dependencies, strin files.Add(new FileSetUrl { Key = _hasher.Hash(output), - Url = GetCompositeUrl(output, fileExtension) + Url = GetCompositeUrl(output, fileExtension, cacheBuster) }); } @@ -139,7 +142,7 @@ public ParsedUrlPath ParsePath(string input) return result; } - private string GetCompositeUrl(string fileKey, string fileExtension) + private string GetCompositeUrl(string fileKey, string fileExtension, ICacheBuster cacheBuster) { //Create a delimited URL query string @@ -150,7 +153,7 @@ private string GetCompositeUrl(string fileKey, string fileExtension) _options.CompositeFilePath, Uri.EscapeUriString(fileKey), fileExtension, - _config.Version)); + cacheBuster.GetValue())); } } } \ No newline at end of file diff --git a/src/Smidge/CompositeFiles/IUrlManager.cs b/src/Smidge/CompositeFiles/IUrlManager.cs index d917375..7ef5c5a 100644 --- a/src/Smidge/CompositeFiles/IUrlManager.cs +++ b/src/Smidge/CompositeFiles/IUrlManager.cs @@ -2,14 +2,15 @@ using System; using System.Collections.Generic; using Microsoft.AspNetCore.Http; +using Smidge.Cache; namespace Smidge.CompositeFiles { public interface IUrlManager { - string GetUrl(string bundleName, string fileExtension, bool debug); + string GetUrl(string bundleName, string fileExtension, bool debug, ICacheBuster cacheBuster); - IEnumerable GetUrls(IEnumerable dependencies, string fileExtension); + IEnumerable GetUrls(IEnumerable dependencies, string fileExtension, ICacheBuster cacheBuster); ParsedUrlPath ParsePath(string input); } diff --git a/src/Smidge/Models/BundleExtensions.cs b/src/Smidge/Models/BundleExtensions.cs index c2b6d6c..96b94f1 100644 --- a/src/Smidge/Models/BundleExtensions.cs +++ b/src/Smidge/Models/BundleExtensions.cs @@ -19,5 +19,20 @@ public static BundleOptions GetBundleOptions(this Bundle bundle, IBundleManager return bundleOptions; } + + /// + /// Gets the default bundle options based on whether we're in debug or not + /// + /// + /// + /// + public static BundleOptions GetDefaultBundleOptions(this IBundleManager bundleMgr, bool debug) + { + var bundleOptions = debug + ? bundleMgr.DefaultBundleOptions.DebugOptions + : bundleMgr.DefaultBundleOptions.ProductionOptions; + + return bundleOptions; + } } } \ No newline at end of file diff --git a/src/Smidge/Options/BundleEnvironmentOptions.cs b/src/Smidge/Options/BundleEnvironmentOptions.cs index be23ae1..a184e41 100644 --- a/src/Smidge/Options/BundleEnvironmentOptions.cs +++ b/src/Smidge/Options/BundleEnvironmentOptions.cs @@ -1,4 +1,7 @@ -namespace Smidge.Options +using System; +using Smidge.Cache; + +namespace Smidge.Options { @@ -35,7 +38,7 @@ public BundleEnvironmentOptions() }; ProductionOptions = new BundleOptions(); } - + /// /// The options for debug mode /// diff --git a/src/Smidge/Options/BundleOptions.cs b/src/Smidge/Options/BundleOptions.cs index 0293cf3..50619f3 100644 --- a/src/Smidge/Options/BundleOptions.cs +++ b/src/Smidge/Options/BundleOptions.cs @@ -1,5 +1,10 @@ -namespace Smidge.Options +using System; +using Smidge.Cache; + +namespace Smidge.Options { + + /// /// Defines options for a particular bundle /// @@ -14,8 +19,41 @@ public BundleOptions() CacheControlOptions = new CacheControlOptions(); ProcessAsCompositeFile = true; CompressResult = true; + + } + + private Type _defaultCacheBuster; + + /// + /// Sets the default cache buster type + /// + /// + /// + /// This instance will be resolved from IoC at runtime + /// + public void SetCacheBusterType() + where T: ICacheBuster + { + _defaultCacheBuster = typeof(T); } + /// + /// Returns the default cache buster type + /// + /// + /// + /// By default this is the ConfigCacheBuster + /// + public Type GetCacheBusterType() + { + return _defaultCacheBuster ?? typeof(ConfigCacheBuster); + } + + ///// + ///// Gets/sets the cache buster + ///// + //public ICacheBuster CacheBuster { get; set; } + /// /// If set to true, will process the bundle as composite files and combine them into a single file /// diff --git a/src/Smidge/Options/BundleOptionsBuilder.cs b/src/Smidge/Options/BundleOptionsBuilder.cs index aeda176..021e07a 100644 --- a/src/Smidge/Options/BundleOptionsBuilder.cs +++ b/src/Smidge/Options/BundleOptionsBuilder.cs @@ -1,4 +1,6 @@ -namespace Smidge.Options +using Smidge.Cache; + +namespace Smidge.Options { /// /// Used to build up bundle options in fluent syntax @@ -12,6 +14,13 @@ public BundleOptionsBuilder(BundleOptions options) _options = options; } + public BundleOptionsBuilder SetCacheBusterType() + where T: ICacheBuster + { + _options.SetCacheBusterType(); + return this; + } + public BundleOptionsBuilder EnableCompositeProcessing() { _options.ProcessAsCompositeFile = true; diff --git a/src/Smidge/Options/SmidgeOptions.cs b/src/Smidge/Options/SmidgeOptions.cs index 23b10ad..ba4be2c 100644 --- a/src/Smidge/Options/SmidgeOptions.cs +++ b/src/Smidge/Options/SmidgeOptions.cs @@ -2,6 +2,7 @@ using Smidge.FileProcessors; using System; using System.Collections.Generic; +using Smidge.Cache; namespace Smidge.Options { @@ -14,7 +15,7 @@ public SmidgeOptions() { } - + /// /// Gets/sets the pipeline factory /// diff --git a/src/Smidge/Options/SmidgeOptionsSetup.cs b/src/Smidge/Options/SmidgeOptionsSetup.cs index dee1b44..05bc999 100644 --- a/src/Smidge/Options/SmidgeOptionsSetup.cs +++ b/src/Smidge/Options/SmidgeOptionsSetup.cs @@ -10,7 +10,7 @@ namespace Smidge.Options /// public sealed class SmidgeOptionsSetup : ConfigureOptions { - public SmidgeOptionsSetup(PreProcessPipelineFactory pipelineFactory) : base(ConfigureSmidge) + public SmidgeOptionsSetup(PreProcessPipelineFactory pipelineFactory, IServiceProvider serviceProvider) : base(ConfigureSmidge) { PipelineFactory = pipelineFactory; } diff --git a/src/Smidge/SmidgeHelper.cs b/src/Smidge/SmidgeHelper.cs index 956cbda..2ed59ca 100644 --- a/src/Smidge/SmidgeHelper.cs +++ b/src/Smidge/SmidgeHelper.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Http; +using Smidge.Cache; using Smidge.CompositeFiles; using Smidge.FileProcessors; using Smidge.Hashing; @@ -29,6 +30,7 @@ public class SmidgeHelper : ISmidgeRequire private readonly IUrlManager _urlManager; private readonly IRequestHelper _requestHelper; private readonly IHttpContextAccessor _httpContextAccessor; + private readonly CacheBusterResolver _cacheBusterResolver; /// @@ -44,6 +46,7 @@ public class SmidgeHelper : ISmidgeRequire /// /// /// + /// public SmidgeHelper( IBundleFileSetGenerator fileSetGenerator, DynamicallyRegisteredWebFiles dynamicallyRegisteredWebFiles, @@ -54,7 +57,8 @@ public class SmidgeHelper : ISmidgeRequire PreProcessPipelineFactory processorFactory, IUrlManager urlManager, IRequestHelper requestHelper, - IHttpContextAccessor httpContextAccessor) + IHttpContextAccessor httpContextAccessor, + CacheBusterResolver cacheBusterResolver) { if (fileSetGenerator == null) throw new ArgumentNullException(nameof(fileSetGenerator)); if (dynamicallyRegisteredWebFiles == null) throw new ArgumentNullException(nameof(dynamicallyRegisteredWebFiles)); @@ -65,11 +69,13 @@ public class SmidgeHelper : ISmidgeRequire if (urlManager == null) throw new ArgumentNullException(nameof(urlManager)); if (requestHelper == null) throw new ArgumentNullException(nameof(requestHelper)); if (httpContextAccessor == null) throw new ArgumentNullException(nameof(httpContextAccessor)); + if (cacheBusterResolver == null) throw new ArgumentNullException(nameof(cacheBusterResolver)); _fileSetGenerator = fileSetGenerator; _processorFactory = processorFactory; _urlManager = urlManager; _requestHelper = requestHelper; _httpContextAccessor = httpContextAccessor; + _cacheBusterResolver = cacheBusterResolver; _bundleManager = bundleManager; _preProcessManager = preProcessManager; _dynamicallyRegisteredWebFiles = dynamicallyRegisteredWebFiles; @@ -204,7 +210,7 @@ private async Task> GenerateBundleUrlsAsync(string bundleNam var compression = bundleOptions.CompressResult ? _requestHelper.GetClientCompression(_httpContextAccessor.HttpContext.Request.Headers) : CompressionType.none; - var url = _urlManager.GetUrl(bundleName, fileExt, debug); + var url = _urlManager.GetUrl(bundleName, fileExt, debug, _cacheBusterResolver.GetCacheBuster(bundleOptions.GetCacheBusterType())); //now we need to determine if these files have already been minified var compositeFilePath = _fileSystemHelper.GetCurrentCompositeFilePath(compression, bundleName); @@ -269,7 +275,10 @@ private async Task> GenerateBundleUrlsAsync(string bundleNam { //Get the URLs for the batch, this could be more than one resulting URL depending on how many // files are in the batch and the max url length - var compositeUrls = _urlManager.GetUrls(batch.Select(x => x.Hashed), fileType == WebFileType.Css ? ".css" : ".js"); + var compositeUrls = _urlManager.GetUrls( + batch.Select(x => x.Hashed), + fileType == WebFileType.Css ? ".css" : ".js", + _cacheBusterResolver.GetCacheBuster(_bundleManager.GetDefaultBundleOptions(debug).GetCacheBusterType())); foreach (var u in compositeUrls) { diff --git a/src/Smidge/SmidgeStartup.cs b/src/Smidge/SmidgeStartup.cs index 86c6b1d..ffe36b6 100644 --- a/src/Smidge/SmidgeStartup.cs +++ b/src/Smidge/SmidgeStartup.cs @@ -40,7 +40,7 @@ public static class SmidgeStartup services.AddTransient, SmidgeOptionsSetup>(); - services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); @@ -61,6 +61,10 @@ public static class SmidgeStartup } return new SmidgeConfig(smidgeConfiguration); }); + + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); services.AddScoped(); services.AddScoped(); diff --git a/test/Smidge.Tests/DefaultUrlManagerTests.cs b/test/Smidge.Tests/DefaultUrlManagerTests.cs index 65047c3..237251a 100644 --- a/test/Smidge.Tests/DefaultUrlManagerTests.cs +++ b/test/Smidge.Tests/DefaultUrlManagerTests.cs @@ -9,6 +9,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; +using Smidge.Cache; using Smidge.Options; using Smidge.Hashing; @@ -22,8 +23,7 @@ public void Parse_Path() var path = "c61531b5.2512be3b.bb1214f7.a21bd1fd.js.v1"; var options = new SmidgeOptions { UrlOptions = new UrlManagerOptions { CompositeFilePath = "sg" } }; var manager = new DefaultUrlManager( - Mock.Of>(x => x.Value == options), - Mock.Of(x => x.Version == "1"), + Mock.Of>(x => x.Value == options), Mock.Of(), Mock.Of()); @@ -47,11 +47,10 @@ public void Make_Bundle_Url() var options = new SmidgeOptions { UrlOptions = new UrlManagerOptions { BundleFilePath = "sg" } }; var creator = new DefaultUrlManager( Mock.Of>(x => x.Value == options), - Mock.Of(x => x.Version == "1"), hasher.Object, urlHelper); - var url = creator.GetUrl("my-bundle", ".js", false); + var url = creator.GetUrl("my-bundle", ".js", false, Mock.Of(buster => buster.GetValue() == "1")); Assert.Equal("/sg/my-bundle.js.v1", url); } @@ -69,11 +68,12 @@ public void Make_Composite_Url() var options = new SmidgeOptions { UrlOptions = new UrlManagerOptions { CompositeFilePath = "sg", MaxUrlLength = 100 } }; var creator = new DefaultUrlManager( Mock.Of>(x => x.Value == options), - Mock.Of(x => x.Version == "1"), hasher.Object, urlHelper); - var url = creator.GetUrls(new List { new JavaScriptFile("Test1.js"), new JavaScriptFile("Test2.js") }, ".js"); + var url = creator.GetUrls( + new List { new JavaScriptFile("Test1.js"), new JavaScriptFile("Test2.js") }, ".js", + Mock.Of(buster => buster.GetValue() == "1")); Assert.Equal(1, url.Count()); Assert.Equal("/sg/Test1.Test2.js.v1", url.First().Url); @@ -93,11 +93,12 @@ public void Make_Composite_Url_Splits() var options = new SmidgeOptions { UrlOptions = new UrlManagerOptions { CompositeFilePath = "sg", MaxUrlLength = 14 + 10 } }; var creator = new DefaultUrlManager( Mock.Of>(x => x.Value == options), - Mock.Of(x => x.Version == "1"), hasher.Object, urlHelper); - var url = creator.GetUrls(new List { new JavaScriptFile("Test1.js"), new JavaScriptFile("Test2.js") }, ".js"); + var url = creator.GetUrls( + new List { new JavaScriptFile("Test1.js"), new JavaScriptFile("Test2.js") }, ".js", + Mock.Of(buster => buster.GetValue() == "1")); Assert.Equal(2, url.Count()); Assert.Equal("/sg/Test1.js.v1", url.ElementAt(0).Url); @@ -119,11 +120,12 @@ public void Throws_When_Single_Dependency_Too_Long() var options = new SmidgeOptions { UrlOptions = new UrlManagerOptions { CompositeFilePath = "sg", MaxUrlLength = 10 } }; var creator = new DefaultUrlManager( Mock.Of>(x => x.Value == options), - Mock.Of(x => x.Version == "1"), hasher.Object, urlHelper); - Assert.Throws(() => creator.GetUrls(new List { new JavaScriptFile("Test1.js") }, ".js")); + Assert.Throws(() => creator.GetUrls( + new List { new JavaScriptFile("Test1.js") }, ".js", + Mock.Of(buster => buster.GetValue() == "1"))); } } diff --git a/test/Smidge.Tests/SmidgeHelperTests.cs b/test/Smidge.Tests/SmidgeHelperTests.cs index 1bda992..848f311 100644 --- a/test/Smidge.Tests/SmidgeHelperTests.cs +++ b/test/Smidge.Tests/SmidgeHelperTests.cs @@ -3,10 +3,12 @@ using Moq; using Smidge.CompositeFiles; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Options; using Smidge; +using Smidge.Cache; using Smidge.Hashing; using Smidge.FileProcessors; using Smidge.Options; @@ -21,7 +23,7 @@ public class SmidgeHelperTests private readonly IHostingEnvironment _hostingEnvironment = Mock.Of(); private readonly IFileProvider _fileProvider = Mock.Of(); private readonly IHasher _hasher = Mock.Of(); - private readonly IEnumerable _preProcessors = Mock.Of>(); + private readonly IEnumerable _preProcessors = new List(); private readonly IBundleFileSetGenerator _fileSetGenerator; private readonly DynamicallyRegisteredWebFiles _dynamicallyRegisteredWebFiles; private readonly FileSystemHelper _fileSystemHelper; @@ -64,7 +66,8 @@ public async Task Generate_Css_Urls_For_Non_Existent_Bundle_Throws_Exception() _fileSetGenerator, _dynamicallyRegisteredWebFiles, _preProcessManager, _fileSystemHelper, _hasher, _bundleManager, _processorFactory, _urlManager, _requestHelper, - _httpContextAccessor.Object); + _httpContextAccessor.Object, + new CacheBusterResolver(Enumerable.Empty())); var exception = await Assert.ThrowsAsync ( @@ -83,7 +86,8 @@ public async Task Generate_Js_Urls_For_Non_Existent_Bundle_Throws_Exception() _fileSetGenerator, _dynamicallyRegisteredWebFiles, _preProcessManager, _fileSystemHelper, _hasher, _bundleManager, _processorFactory, _urlManager, _requestHelper, - _httpContextAccessor.Object); + _httpContextAccessor.Object, + new CacheBusterResolver(Enumerable.Empty())); var exception = await Assert.ThrowsAsync ( @@ -101,7 +105,8 @@ public async Task CssHere_HtmlString_For_Non_Existent_Css_Bundle_Throws_Exceptio _fileSetGenerator, _dynamicallyRegisteredWebFiles, _preProcessManager, _fileSystemHelper, _hasher, _bundleManager, _processorFactory, _urlManager, _requestHelper, - _httpContextAccessor.Object); + _httpContextAccessor.Object, + new CacheBusterResolver(Enumerable.Empty())); var exception = await Assert.ThrowsAsync ( @@ -124,7 +129,8 @@ public async Task JsHere_HtmlString_For_Non_Existent_Css_Bundle_Throws_Exception _fileSetGenerator, _dynamicallyRegisteredWebFiles, _preProcessManager, _fileSystemHelper, _hasher, _bundleManager, _processorFactory, _urlManager, _requestHelper, - _httpContextAccessor.Object); + _httpContextAccessor.Object, + new CacheBusterResolver(Enumerable.Empty())); var exception = await Assert.ThrowsAsync (