Ejemplo n.º 1
0
        public void Css_Paths_Are_Rewritten_Properly(string path, string url, string expected)
        {
            var env = new MockHostEnvironment();

            env.AddWebFile(path, "url(" + url + ")");

            var container = new ServiceCollection();

            container.AddSingleton <IWebHostEnvironment>(env);
            container.AddSingleton <IPermissionService, MockPermissions>();
            var cssUrl = "~/" + PathHelper.ToUrl(path);

            container.AddCssBundling(options =>
            {
                options.Enabled         = true;
                options.Bundles["Test"] = new string[]
                {
                    cssUrl
                };
            });
            var services         = container.BuildServiceProvider();
            var scriptManager    = services.GetRequiredService <IDynamicScriptManager>();
            var cssBundleManager = services.GetRequiredService <ICssBundleManager>();

            cssBundleManager.GetCssBundle(cssUrl);
            var actual = scriptManager.GetScriptText("CssBundle.Test")?.Trim();

            Assert.Equal("url(" + expected + ")", actual);
        }
Ejemplo n.º 2
0
        public void When_Css_File_Changes_It_Reloads_Bundle()
        {
            var env      = new MockHostEnvironment();
            var testFile = env.Path.Combine(env.WebRootPath, "test.css");

            env.AddWebFile(testFile, "before");
            var fileWatcherFactory = new MockFileWatcherFactory(env.FileSystem);
            var container          = new ServiceCollection();

            container.AddSingleton <IWebHostEnvironment>(env);
            container.AddSingleton <IPermissionService, MockPermissions>();
            container.AddSingleton <IFileWatcherFactory>(fileWatcherFactory);
            container.AddCssBundling(options =>
            {
                options.Enabled         = true;
                options.Bundles["Test"] = new string[]
                {
                    "~/" + env.Path.GetFileName(testFile)
                };
            });
            var services = container.BuildServiceProvider();

            services.UseCssWatching(env.Path.GetDirectoryName(testFile));
            var scriptManager = services.GetRequiredService <IDynamicScriptManager>();
            var before        = scriptManager.GetScriptText("CssBundle.Test");

            Assert.Equal("before", before?.Replace(";", "").Trim());
            env.File.WriteAllText(testFile, "after");
            fileWatcherFactory.Watchers.Single().RaiseChanged(env.Path.GetFileName(testFile));
            var after = scriptManager.GetScriptText("CssBundle.Test");

            Assert.Equal("after", after?.Replace(";", "").Trim());
        }