示例#1
0
        public void GivenContainerCacheHasFileReferenceThatIsModifiedSinceCacheWrite_WhenInitializeBundlesFromCacheIfUpToDate_ThenReturnFalse()
        {
            using (var cacheDir = new TempDirectory())
                using (var sourceDir = new TempDirectory())
                {
                    File.WriteAllText(
                        Path.Combine(cacheDir, "container.xml"),
                        "<?xml version=\"1.0\"?><Container Version=\"VERSION\" AssetCount=\"0\">" +
                        "<Bundle Path=\"~/test\" Hash=\"01\">" +
                        "  <File Path=\"~/file.png\"/>" +
                        "</Bundle>" +
                        "</Container>"
                        );
                    Thread.Sleep(10); // Brief pause, otherwise the file write times are the same.
                    File.WriteAllText(Path.Combine(sourceDir, "file.png"), "");
                    var bundle        = new TestableBundle("~/test");
                    var sourceBundles = new[] { bundle };

                    var settings = new CassetteSettings("")
                    {
                        SourceDirectory = new FileSystemDirectory(sourceDir),
                        CacheDirectory  = new FileSystemDirectory(cacheDir)
                    };
                    var cache  = new BundleCache("VERSION", settings);
                    var result = cache.InitializeBundlesFromCacheIfUpToDate(sourceBundles);

                    result.ShouldBeFalse();
                }
        }
示例#2
0
        public void GivenContainerCacheHasBundleReferences_WhenInitializeBundlesFromCacheIfUpToDate_ThenBundleReferencesAreSet()
        {
            using (var cacheDir = new TempDirectory())
            {
                File.WriteAllText(
                    Path.Combine(cacheDir, "container.xml"),
                    "<?xml version=\"1.0\"?><Container Version=\"VERSION\" AssetCount=\"0\">" +
                    "<Bundle Path=\"~/test1\" Hash=\"01\">" +
                    "  <Reference Path=\"~/test2\"/>" +
                    "</Bundle>" +
                    "<Bundle Path=\"~/test2\" Hash=\"01\"/>" +
                    "</Container>"
                    );
                var bundle1       = new TestableBundle("~/test1");
                var bundle2       = new TestableBundle("~/test2");
                var sourceBundles = new[] { bundle1, bundle2 };

                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of <IDirectory>(),
                    CacheDirectory  = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                cache.InitializeBundlesFromCacheIfUpToDate(sourceBundles);

                bundle1.References.First().ShouldEqual("~/test2");
            }
        }
示例#3
0
        public void GivenContainerCacheHasFileReferenceToMissingFile_WhenInitializeBundlesFromCacheIfUpToDate_ThenReturnFalse()
        {
            using (var cacheDir = new TempDirectory())
                using (var sourceDir = new TempDirectory())
                {
                    File.WriteAllText(
                        Path.Combine(cacheDir, "container.xml"),
                        "<?xml version=\"1.0\"?><Container Version=\"VERSION\" AssetCount=\"0\">" +
                        "<Bundle Path=\"~/test\" Hash=\"01\">" +
                        "  <File Path=\"~/file.png\"/>" +
                        "</Bundle>" +
                        "</Container>"
                        );
                    var bundle        = new TestableBundle("~/test");
                    var sourceBundles = new[] { bundle };


                    var settings = new CassetteSettings
                    {
                        SourceDirectory = new FileSystemDirectory(sourceDir),
                        CacheDirectory  = new FileSystemDirectory(cacheDir)
                    };
                    var cache  = new BundleCache("VERSION", settings);
                    var result = cache.InitializeBundlesFromCacheIfUpToDate(sourceBundles);

                    result.ShouldBeFalse();
                }
        }
示例#4
0
        public void GivenCacheIsUpToDate_WhenInitializeBundlesFromCacheIfUpToDate_ThenBundleAssetsReplacedWithCachedAsset()
        {
            using (var cacheDir = new TempDirectory())
            {
                File.WriteAllText(
                    Path.Combine(cacheDir, "container.xml"),
                    "<?xml version=\"1.0\"?><Container Version=\"VERSION\" AssetCount=\"1\"><Bundle Path=\"~/test\" Hash=\"01\"/></Container>"
                    );
                File.WriteAllText(
                    Path.Combine(cacheDir, "test.bundle"),
                    "asset"
                    );
                var bundleWithAsset = new TestableBundle("~/test");
                var asset           = StubAsset();
                bundleWithAsset.Assets.Add(asset.Object);
                var sourceBundles = new[] { bundleWithAsset };

                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of <IDirectory>(),
                    CacheDirectory  = new FileSystemDirectory(cacheDir)
                };
                var cache  = new BundleCache("VERSION", settings);
                var result = cache.InitializeBundlesFromCacheIfUpToDate(sourceBundles);

                result.ShouldBeTrue();
                bundleWithAsset.Assets[0].OpenStream().ReadToEnd().ShouldEqual("asset");
            }
        }
示例#5
0
        public void GivenCachedFileThrowsWhenDeleted_WhenSaveContainer_ThenSecondCachedFileIsStillDeleted()
        {
            var firstFile = new Mock <IFile>();

            firstFile.Setup(f => f.Delete()).Throws <IOException>();
            var secondFile     = new Mock <IFile>();
            var cacheDirectory = new Mock <IDirectory>();

            cacheDirectory.Setup(d => d.GetFiles("*", SearchOption.AllDirectories))
            .Returns(new[] { firstFile.Object, secondFile.Object });
            var containerFile = new Mock <IFile>();

            cacheDirectory.Setup(d => d.GetFile("container.xml"))
            .Returns(containerFile.Object);
            containerFile.Setup(f => f.Open(It.IsAny <FileMode>(), It.IsAny <FileAccess>(), It.IsAny <FileShare>()))
            .Returns(Stream.Null);

            var settings = new CassetteSettings
            {
                SourceDirectory = Mock.Of <IDirectory>(),
                CacheDirectory  = cacheDirectory.Object
            };

            var cache = new BundleCache("VERSION", settings);

            cache.SaveBundleContainer(new BundleContainer(Enumerable.Empty <Bundle>()));

            secondFile.Verify(f => f.Delete());
        }
示例#6
0
        public void GivenCacheIsUpToDate_WhenInitializeBundlesFromCacheIfUpToDate_ThenReturnTrue()
        {
            using (var cacheDir = new TempDirectory())
            {
                File.WriteAllText(
                    Path.Combine(cacheDir, "container.xml"),
                    "<?xml version=\"1.0\"?><Container Version=\"VERSION\" AssetCount=\"0\"><Bundle Path=\"~/test\" Hash=\"01\"/></Container>"
                    );
                File.WriteAllText(
                    Path.Combine(cacheDir, "test.bundle"),
                    "asset"
                    );

                var bundle        = new TestableBundle("~/test");
                var sourceBundles = new[] { bundle };

                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of <IDirectory>(),
                    CacheDirectory  = new FileSystemDirectory(cacheDir)
                };
                var cache  = new BundleCache("VERSION", settings);
                var result = cache.InitializeBundlesFromCacheIfUpToDate(sourceBundles);

                result.ShouldBeTrue();
            }
        }
示例#7
0
        public void GivenCacheMissingSecondFile_WhenInitializeBundlesFromCacheIfUpToDate_ThenFirstBundleAssetsAreNotModified()
        {
            using (var cacheDir = new TempDirectory())
            {
                File.WriteAllText(
                    Path.Combine(cacheDir, "container.xml"),
                    "<?xml version=\"1.0\"?><Container Version=\"VERSION\" AssetCount=\"2\"><Bundle Path=\"~/test1\" Hash=\"01\"/><Bundle Path=\"~/test2\" Hash=\"01\"/></Container>"
                    );
                File.WriteAllText(
                    Path.Combine(cacheDir, "test1.bundle"),
                    "asset"
                    );
                var bundle1 = new TestableBundle("~/test1");
                var bundle2 = new TestableBundle("~/test2");
                var asset1  = StubAsset();
                bundle1.Assets.Add(asset1.Object);
                var asset2 = StubAsset();
                bundle2.Assets.Add(asset2.Object);
                var sourceBundles = new[] { bundle1, bundle2 };

                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of <IDirectory>(),
                    CacheDirectory  = new FileSystemDirectory(cacheDir)
                };
                var cache  = new BundleCache("VERSION", settings);
                var result = cache.InitializeBundlesFromCacheIfUpToDate(sourceBundles);

                result.ShouldBeFalse();
                bundle1.Assets[0].ShouldNotBeType <CachedAsset>();
            }
        }
示例#8
0
        public void GivenAssetWithReferenceToAssetInSameBundle_WhenSaveContainer_ThenXmlHasNoReferenceElements()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of <IDirectory>(),
                    CacheDirectory  = new FileSystemDirectory(cacheDir)
                };
                var cache     = new BundleCache("VERSION", settings);
                var bundle    = new TestableBundle("~/bundle-1");
                var asset1    = StubAsset("~/bundle-1/asset1.js");
                var asset2    = StubAsset("~/bundle-1/asset2.js");
                var reference = new AssetReference("~/bundle-1/asset2.js", asset1.Object, -1, AssetReferenceType.SameBundle);
                asset1.SetupGet(a => a.References)
                .Returns(new[] { reference });
                bundle.Assets.Add(new ConcatenatedAsset(new[]
                {
                    asset1.Object,
                    asset2.Object
                }));

                cache.SaveBundleContainer(new BundleContainer(new[] { bundle }));

                var xml = File.ReadAllText(Path.Combine(cacheDir, "container.xml"));
                xml.ShouldNotContain("<Reference ");
            }
        }
示例#9
0
        public void GivenAssetWithTwoReferencesToAssetsInAnotherBundle_WhenSaveContainer_ThenXmlHasOneReferenceElementWithReferencedBundlePath()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of <IDirectory>(),
                    CacheDirectory  = new FileSystemDirectory(cacheDir)
                };
                var cache      = new BundleCache("VERSION", settings);
                var bundle1    = new TestableBundle("~/bundle-1");
                var bundle2    = new TestableBundle("~/bundle-2");
                var asset1     = StubAsset();
                var reference1 = new AssetReference("~/bundle-2/asset2.js", asset1.Object, -1, AssetReferenceType.DifferentBundle);
                var reference2 = new AssetReference("~/bundle-2/asset3.js", asset1.Object, -1, AssetReferenceType.DifferentBundle);
                asset1.SetupGet(a => a.References)
                .Returns(new[] { reference1, reference2 });
                bundle1.Assets.Add(asset1.Object);

                bundle2.Assets.Add(
                    new ConcatenatedAsset(new[]
                {
                    StubAsset("~/bundle-2/asset2.js").Object,
                    StubAsset("~/bundle-2/asset3.js").Object
                })
                    );

                cache.SaveBundleContainer(new BundleContainer(new[] { bundle1, bundle2 }));

                var xml = File.ReadAllText(Path.Combine(cacheDir, "container.xml"));
                Regex.Matches(xml, Regex.Escape("<Reference Path=\"~/bundle-2\" />")).Count.ShouldEqual(1);
            }
        }
示例#10
0
        public void GivenContainerFileIsOlderThanAnAssetFile_WhenInitializeBundlesFromCacheIfUpToDate_ThenReturnFalse()
        {
            using (var cacheDir = new TempDirectory())
            {
                File.WriteAllText(
                    Path.Combine(cacheDir, "container.xml"),
                    "<?xml version=\"1.0\"?><Container Version=\"VERSION\" AssetCount=\"0\"></Container>"
                    );

                var bundleWithAsset = new TestableBundle("~/test");
                var asset           = StubAsset();
                asset.Setup(a => a.SourceFile.LastWriteTimeUtc).Returns(DateTime.UtcNow);
                bundleWithAsset.Assets.Add(asset.Object);
                var sourceBundles = new[] { bundleWithAsset };

                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of <IDirectory>(),
                    CacheDirectory  = new FileSystemDirectory(cacheDir)
                };
                var cache  = new BundleCache("VERSION", settings);
                var result = cache.InitializeBundlesFromCacheIfUpToDate(sourceBundles);

                result.ShouldBeFalse();
            }
        }
示例#11
0
        public void GivenCacheIsUpToDate_WhenInitializeBundlesFromCacheIfUpToDate_ThenBundleAssetsReplacedWithCachedAsset()
        {
            using (var cacheDir = new TempDirectory())
            {
                File.WriteAllText(
                    Path.Combine(cacheDir, "container.xml"),
                    "<?xml version=\"1.0\"?><Container Version=\"VERSION\" AssetCount=\"1\"><Bundle Path=\"~/test\" Hash=\"01\"/></Container>"
                    );
                File.WriteAllText(
                    Path.Combine(cacheDir, "test.bundle"),
                    "asset"
                    );
                var bundleWithAsset = new TestableBundle("~/test");
                var asset = StubAsset();
                bundleWithAsset.Assets.Add(asset.Object);
                var sourceBundles = new[] { bundleWithAsset };

                var settings = new CassetteSettings("")
                {
                    SourceDirectory = Mock.Of<IDirectory>(),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                var result = cache.InitializeBundlesFromCacheIfUpToDate(sourceBundles);

                result.ShouldBeTrue();
                bundleWithAsset.Assets[0].OpenStream().ReadToEnd().ShouldEqual("asset");
            }
        }
示例#12
0
        public void WhenSaveContainer_ThenXmlManifestContainsVersion()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of <IDirectory>(),
                    CacheDirectory  = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                cache.SaveBundleContainer(new BundleContainer(new TestableBundle[0]));

                var xml = File.ReadAllText(Path.Combine(cacheDir, "container.xml"));
                xml.ShouldContain("Version=\"VERSION\"");
            }
        }
示例#13
0
        public void GivenContainerFileDoesNotExist_WhenInitializeBundlesFromCacheIfUpToDate_ThenReturnFalse()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of <IDirectory>(),
                    CacheDirectory  = new FileSystemDirectory(cacheDir)
                };
                var cache         = new BundleCache("VERSION", settings);
                var sourceBundles = new TestableBundle[0];

                var result = cache.InitializeBundlesFromCacheIfUpToDate(sourceBundles);

                result.ShouldBeFalse();
            }
        }
示例#14
0
        public void GivenBundleWithNoAssets_WhenSaveContainer_ThenNoBundleFileCreated()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of <IDirectory>(),
                    CacheDirectory  = new FileSystemDirectory(cacheDir)
                };
                var cache  = new BundleCache("VERSION", settings);
                var bundle = new TestableBundle("~/test");

                cache.SaveBundleContainer(new BundleContainer(new[] { bundle }));

                File.Exists(Path.Combine(cacheDir, "test.bundle")).ShouldBeFalse();
            }
        }
示例#15
0
        public void GivenBundlePathIsUrl_WhenSaveContainer_ThenThrowException()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of <IDirectory>(),
                    CacheDirectory  = new FileSystemDirectory(cacheDir)
                };
                var cache  = new BundleCache("VERSION", settings);
                var bundle = new TestableBundle("http://test.com/api.js");
                bundle.Assets.Add(StubAsset().Object);

                Assert.Throws <ArgumentException>(
                    () => cache.SaveBundleContainer(new BundleContainer(new[] { bundle }))
                    );
            }
        }
示例#16
0
        public void GivenBundleWithTwoAssets_WhenSaveContainer_ThenExceptionThrownBecauseAssetsMustBeConcatenatedBeforeCaching()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of <IDirectory>(),
                    CacheDirectory  = new FileSystemDirectory(cacheDir)
                };
                var cache  = new BundleCache("VERSION", settings);
                var bundle = new TestableBundle("~/test");
                bundle.Assets.Add(StubAsset().Object);
                bundle.Assets.Add(StubAsset().Object);

                Assert.Throws <InvalidOperationException>(
                    () => cache.SaveBundleContainer(new BundleContainer(new[] { bundle }))
                    );
            }
        }
示例#17
0
        public void GivenBundleWithReference_WhenSaveContainer_ThenXmlHasReferenceElement()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of <IDirectory>(),
                    CacheDirectory  = new FileSystemDirectory(cacheDir)
                };
                var cache  = new BundleCache("VERSION", settings);
                var bundle = new TestableBundle("~/test");
                bundle.AddReference("~/other");

                cache.SaveBundleContainer(new BundleContainer(new[] { bundle, new TestableBundle("~/other") }));

                var xml = File.ReadAllText(Path.Combine(cacheDir, "container.xml"));
                xml.ShouldContain("<Reference Path=\"~/other\"");
            }
        }
示例#18
0
        public void GivenBundle_WhenSaveContainer_ThenXmlHasBundleElementWithPathAttribute()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of <IDirectory>(),
                    CacheDirectory  = new FileSystemDirectory(cacheDir)
                };
                var cache  = new BundleCache("VERSION", settings);
                var bundle = new TestableBundle("~/test");
                var asset1 = StubAsset();
                bundle.Assets.Add(asset1.Object);

                cache.SaveBundleContainer(new BundleContainer(new[] { bundle }));

                var xml = File.ReadAllText(Path.Combine(cacheDir, "container.xml"));
                xml.ShouldContain("<Bundle Path=\"~/test\"");
            }
        }
示例#19
0
        public void GivenBundleHasConcatenatedAsset_WhenSaveContainer_ThenXmlManifestHasAssetCountOfChildAssets()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of <IDirectory>(),
                    CacheDirectory  = new FileSystemDirectory(cacheDir)
                };
                var cache  = new BundleCache("VERSION", settings);
                var bundle = new TestableBundle("~/test");
                var asset1 = StubAsset();
                var asset2 = StubAsset();
                bundle.Assets.Add(new ConcatenatedAsset(new[] { asset1.Object, asset2.Object }));

                cache.SaveBundleContainer(new BundleContainer(new[] { bundle }));

                var xml = File.ReadAllText(Path.Combine(cacheDir, "container.xml"));
                xml.ShouldContain("AssetCount=\"2\"");
            }
        }
示例#20
0
        public void GivenBundleWithAsset_WhenSaveContainer_ThenBundleFileSavedWithContentsOfAsset()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of <IDirectory>(),
                    CacheDirectory  = new FileSystemDirectory(cacheDir)
                };
                var cache  = new BundleCache("VERSION", settings);
                var bundle = new TestableBundle("~/test");
                var asset  = StubAsset();
                asset.Setup(a => a.OpenStream())
                .Returns(() => "ASSET-DATA".AsStream());
                bundle.Assets.Add(asset.Object);

                cache.SaveBundleContainer(new BundleContainer(new[] { bundle }));

                var savedData = File.ReadAllText(Path.Combine(cacheDir, "test.bundle"));
                savedData.ShouldEqual("ASSET-DATA");
            }
        }
示例#21
0
        public void GivenCacheIsUpToDate_WhenInitializeBundlesFromCacheIfUpToDateWithZeroAssetBundle_ThenReturnTrue()
        {
            using (var cacheDir = new TempDirectory())
            {
                File.WriteAllText(
                    Path.Combine(cacheDir, "container.xml"),
                    "<?xml version=\"1.0\"?><Container Version=\"VERSION\" AssetCount=\"0\"><Bundle Path=\"~/test\" Hash=\"\"/></Container>"
                    );
                var bundle = new TestableBundle("~/test");
                var sourceBundles = new[] { bundle };

                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of<IDirectory>(),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                var result = cache.InitializeBundlesFromCacheIfUpToDate(sourceBundles);

                result.ShouldBeTrue();
            }
        }
示例#22
0
        public void GivenContainerFileWithDifferentVersion_WhenInitializeBundlesFromCacheIfUpToDate_ThenReturnFalse()
        {
            using (var cacheDir = new TempDirectory())
            {
                File.WriteAllText(
                    Path.Combine(cacheDir, "container.xml"),
                    "<?xml version=\"1.0\"?><Container Version=\"VERSION-1\" AssetCount=\"0\"></Container>"
                    );

                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of <IDirectory>(),
                    CacheDirectory  = new FileSystemDirectory(cacheDir)
                };
                var cache         = new BundleCache("VERSION-2", settings);
                var sourceBundles = new TestableBundle[0];

                var result = cache.InitializeBundlesFromCacheIfUpToDate(sourceBundles);

                result.ShouldBeFalse();
            }
        }
示例#23
0
        public void GivenAssetWithReferenceToUrl_WhenSaveContainer_ThenXmlHasReferenceElementWithUrlAsPath()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of <IDirectory>(),
                    CacheDirectory  = new FileSystemDirectory(cacheDir)
                };
                var cache     = new BundleCache("VERSION", settings);
                var bundle    = new TestableBundle("~/bundle-1");
                var asset     = StubAsset();
                var reference = new AssetReference("http://test.com", asset.Object, -1, AssetReferenceType.Url);
                asset.SetupGet(a => a.References)
                .Returns(new[] { reference });
                bundle.Assets.Add(asset.Object);

                cache.SaveBundleContainer(new BundleContainer(new Bundle[] { bundle, new ExternalScriptBundle("http://test.com"), }));

                var xml = File.ReadAllText(Path.Combine(cacheDir, "container.xml"));
                xml.ShouldContain("<Reference Path=\"http://test.com\" />");
            }
        }
示例#24
0
        public void GivenAssetWithRawFilenameReference_WhenSaveContainer_ThenXmlHasFileElement()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of <IDirectory>(),
                    CacheDirectory  = new FileSystemDirectory(cacheDir)
                };
                var cache     = new BundleCache("VERSION", settings);
                var bundle    = new TestableBundle("~/bundle-1");
                var asset     = StubAsset();
                var reference = new AssetReference("~/images/test.png", asset.Object, -1, AssetReferenceType.RawFilename);
                asset.SetupGet(a => a.References)
                .Returns(new[] { reference });
                bundle.Assets.Add(asset.Object);

                cache.SaveBundleContainer(new BundleContainer(new[] { bundle }));

                var xml = File.ReadAllText(Path.Combine(cacheDir, "container.xml"));
                xml.ShouldContain("<File Path=\"~/images/test.png\" />");
            }
        }
示例#25
0
        public void GivenAssetWithReferenceToAnotherBundle_WhenSaveContainer_ThenXmlHasReferenceElementWithReferencedBundlePath()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of <IDirectory>(),
                    CacheDirectory  = new FileSystemDirectory(cacheDir)
                };
                var cache     = new BundleCache("VERSION", settings);
                var bundle1   = new TestableBundle("~/bundle-1");
                var bundle2   = new TestableBundle("~/bundle-2");
                var asset1    = StubAsset();
                var reference = new AssetReference("~/bundle-2", asset1.Object, -1, AssetReferenceType.DifferentBundle);
                asset1.SetupGet(a => a.References)
                .Returns(new[] { reference });
                bundle1.Assets.Add(asset1.Object);

                cache.SaveBundleContainer(new BundleContainer(new[] { bundle1, bundle2 }));

                var xml = File.ReadAllText(Path.Combine(cacheDir, "container.xml"));
                xml.ShouldContain("<Reference Path=\"~/bundle-2\" />");
            }
        }
示例#26
0
        public void GivenAssetWithRawFilenameReference_WhenSaveContainer_ThenXmlHasFileElement()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of<IDirectory>(),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                var bundle = new TestableBundle("~/bundle-1");
                var asset = StubAsset();
                var reference = new AssetReference("~/images/test.png", asset.Object, -1, AssetReferenceType.RawFilename);
                asset.SetupGet(a => a.References)
                     .Returns(new[] { reference });
                bundle.Assets.Add(asset.Object);

                cache.SaveBundleContainer(new BundleContainer(new[] { bundle }));

                var xml = File.ReadAllText(Path.Combine(cacheDir, "container.xml"));
                xml.ShouldContain("<File Path=\"~/images/test.png\" />");
            }
        }
示例#27
0
        public void GivenBundleWithTwoAssets_WhenSaveContainer_ThenExceptionThrownBecauseAssetsMustBeConcatenatedBeforeCaching()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of<IDirectory>(),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                var bundle = new TestableBundle("~/test");
                bundle.Assets.Add(StubAsset().Object);
                bundle.Assets.Add(StubAsset().Object);

                Assert.Throws<InvalidOperationException>(
                    () => cache.SaveBundleContainer(new BundleContainer(new[] { bundle }))
                );
            }
        }
示例#28
0
        public void GivenBundleWithNoAssets_WhenSaveContainer_ThenNoBundleFileCreated()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of<IDirectory>(),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                var bundle = new TestableBundle("~/test");

                cache.SaveBundleContainer(new BundleContainer(new[] { bundle }));

                File.Exists(Path.Combine(cacheDir, "test.bundle")).ShouldBeFalse();
            }
        }
示例#29
0
        public void GivenBundlePathIsUrl_WhenSaveContainer_ThenThrowException()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of<IDirectory>(),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                var bundle = new TestableBundle("http://test.com/api.js");
                bundle.Assets.Add(StubAsset().Object);

                Assert.Throws<ArgumentException>(
                    () => cache.SaveBundleContainer(new BundleContainer(new[] { bundle }))
                );
            }
        }
示例#30
0
        public void GivenAssetWithTwoReferencesToAssetsInAnotherBundle_WhenSaveContainer_ThenXmlHasOneReferenceElementWithReferencedBundlePath()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of<IDirectory>(),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                var bundle1 = new TestableBundle("~/bundle-1");
                var bundle2 = new TestableBundle("~/bundle-2");
                var asset1 = StubAsset();
                var reference1 = new AssetReference("~/bundle-2/asset2.js", asset1.Object, -1, AssetReferenceType.DifferentBundle);
                var reference2 = new AssetReference("~/bundle-2/asset3.js", asset1.Object, -1, AssetReferenceType.DifferentBundle);
                asset1.SetupGet(a => a.References)
                      .Returns(new[] { reference1, reference2 });
                bundle1.Assets.Add(asset1.Object);

                bundle2.Assets.Add(
                    new ConcatenatedAsset(new[]
                    {
                        StubAsset("~/bundle-2/asset2.js").Object,
                        StubAsset("~/bundle-2/asset3.js").Object
                    })
                );

                cache.SaveBundleContainer(new BundleContainer(new[] { bundle1, bundle2 }));

                var xml = File.ReadAllText(Path.Combine(cacheDir, "container.xml"));
                Regex.Matches(xml, Regex.Escape("<Reference Path=\"~/bundle-2\" />")).Count.ShouldEqual(1);
            }
        }
示例#31
0
        public void GivenCachedFileThrowsWhenDeleted_WhenSaveContainer_ThenSecondCachedFileIsStillDeleted()
        {
            var firstFile = new Mock<IFile>();
            firstFile.Setup(f => f.Delete()).Throws<IOException>();
            var secondFile = new Mock<IFile>();
            var cacheDirectory = new Mock<IDirectory>();
            cacheDirectory.Setup(d => d.GetFiles("*", SearchOption.AllDirectories))
                          .Returns(new[] { firstFile.Object, secondFile.Object });
            var containerFile = new Mock<IFile>();
            cacheDirectory.Setup(d => d.GetFile("container.xml"))
                          .Returns(containerFile.Object);
            containerFile.Setup(f => f.Open(It.IsAny<FileMode>(), It.IsAny<FileAccess>(), It.IsAny<FileShare>()))
                         .Returns(Stream.Null);

            var settings = new CassetteSettings
            {
                SourceDirectory = Mock.Of<IDirectory>(),
                CacheDirectory = cacheDirectory.Object
            };

            var cache = new BundleCache("VERSION", settings);
            cache.SaveBundleContainer(new BundleContainer(Enumerable.Empty<Bundle>()));

            secondFile.Verify(f => f.Delete());
        }
示例#32
0
        public void GivenAssetWithReferenceToAnotherBundle_WhenSaveContainer_ThenXmlHasReferenceElementWithReferencedBundlePath()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of<IDirectory>(),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                var bundle1 = new TestableBundle("~/bundle-1");
                var bundle2 = new TestableBundle("~/bundle-2");
                var asset1 = StubAsset();
                var reference = new AssetReference("~/bundle-2", asset1.Object, -1, AssetReferenceType.DifferentBundle);
                asset1.SetupGet(a => a.References)
                      .Returns(new[] { reference });
                bundle1.Assets.Add(asset1.Object);

                cache.SaveBundleContainer(new BundleContainer(new[] { bundle1, bundle2 }));

                var xml = File.ReadAllText(Path.Combine(cacheDir, "container.xml"));
                xml.ShouldContain("<Reference Path=\"~/bundle-2\" />");
            }
        }
示例#33
0
        public void GivenAssetWithReferenceToAssetInSameBundle_WhenSaveContainer_ThenXmlHasNoReferenceElements()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of<IDirectory>(),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                var bundle = new TestableBundle("~/bundle-1");
                var asset1 = StubAsset("~/bundle-1/asset1.js");
                var asset2 = StubAsset("~/bundle-1/asset2.js");
                var reference = new AssetReference("~/bundle-1/asset2.js", asset1.Object, -1, AssetReferenceType.SameBundle);
                asset1.SetupGet(a => a.References)
                      .Returns(new[] { reference });
                bundle.Assets.Add(new ConcatenatedAsset(new[]
                {
                    asset1.Object,
                    asset2.Object
                }));

                cache.SaveBundleContainer(new BundleContainer(new[] { bundle }));

                var xml = File.ReadAllText(Path.Combine(cacheDir, "container.xml"));
                xml.ShouldNotContain("<Reference ");
            }
        }
示例#34
0
        public void GivenCacheMissingSecondFile_WhenInitializeBundlesFromCacheIfUpToDate_ThenFirstBundleAssetsAreNotModified()
        {
            using (var cacheDir = new TempDirectory())
            {
                File.WriteAllText(
                    Path.Combine(cacheDir, "container.xml"),
                    "<?xml version=\"1.0\"?><Container Version=\"VERSION\" AssetCount=\"2\"><Bundle Path=\"~/test1\" Hash=\"01\"/><Bundle Path=\"~/test2\" Hash=\"01\"/></Container>"
                    );
                File.WriteAllText(
                    Path.Combine(cacheDir, "test1.bundle"),
                    "asset"
                    );
                var bundle1 = new TestableBundle("~/test1");
                var bundle2 = new TestableBundle("~/test2");
                var asset1 = StubAsset();
                bundle1.Assets.Add(asset1.Object);
                var asset2 = StubAsset();
                bundle2.Assets.Add(asset2.Object);
                var sourceBundles = new[] { bundle1, bundle2 };

                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of<IDirectory>(),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                var result = cache.InitializeBundlesFromCacheIfUpToDate(sourceBundles);

                result.ShouldBeFalse();
                bundle1.Assets[0].ShouldNotBeType<CachedAsset>();
            }
        }
示例#35
0
        public void GivenAssetWithReferenceToUrl_WhenSaveContainer_ThenXmlHasReferenceElementWithUrlAsPath()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of<IDirectory>(),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                var bundle = new TestableBundle("~/bundle-1");
                var asset = StubAsset();
                var reference = new AssetReference("http://test.com", asset.Object, -1, AssetReferenceType.Url);
                asset.SetupGet(a => a.References)
                     .Returns(new[] { reference });
                bundle.Assets.Add(asset.Object);

                cache.SaveBundleContainer(new BundleContainer(new Bundle[] { bundle, new ExternalScriptBundle("http://test.com"),  }));

                var xml = File.ReadAllText(Path.Combine(cacheDir, "container.xml"));
                xml.ShouldContain("<Reference Path=\"http://test.com\" />");
            }
        }
示例#36
0
        public void GivenContainerCacheHasBundleReferences_WhenInitializeBundlesFromCacheIfUpToDate_ThenBundleReferencesAreSet()
        {
            using (var cacheDir = new TempDirectory())
            {
                File.WriteAllText(
                    Path.Combine(cacheDir, "container.xml"),
                    "<?xml version=\"1.0\"?><Container Version=\"VERSION\" AssetCount=\"0\">" +
                    "<Bundle Path=\"~/test1\" Hash=\"01\">" +
                    "  <Reference Path=\"~/test2\"/>" +
                    "</Bundle>" +
                    "<Bundle Path=\"~/test2\" Hash=\"01\"/>" +
                    "</Container>"
                    );
                var bundle1 = new TestableBundle("~/test1");
                var bundle2 = new TestableBundle("~/test2");
                var sourceBundles = new[] { bundle1, bundle2 };

                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of<IDirectory>(),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                cache.InitializeBundlesFromCacheIfUpToDate(sourceBundles);

                bundle1.References.First().ShouldEqual("~/test2");
            }
        }
示例#37
0
        public void GivenBundleHasConcatenatedAsset_WhenSaveContainer_ThenXmlManifestHasAssetCountOfChildAssets()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of<IDirectory>(),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                var bundle = new TestableBundle("~/test");
                var asset1 = StubAsset();
                var asset2 = StubAsset();
                bundle.Assets.Add(new ConcatenatedAsset(new[] { asset1.Object, asset2.Object }));

                cache.SaveBundleContainer(new BundleContainer(new[] { bundle }));

                var xml = File.ReadAllText(Path.Combine(cacheDir, "container.xml"));
                xml.ShouldContain("AssetCount=\"2\"");
            }
        }
示例#38
0
        public void GivenContainerCacheHasFileReferenceToMissingFile_WhenInitializeBundlesFromCacheIfUpToDate_ThenReturnFalse()
        {
            using (var cacheDir = new TempDirectory())
            using (var sourceDir = new TempDirectory())
            {
                File.WriteAllText(
                    Path.Combine(cacheDir, "container.xml"),
                    "<?xml version=\"1.0\"?><Container Version=\"VERSION\" AssetCount=\"0\">" +
                    "<Bundle Path=\"~/test\" Hash=\"01\">" +
                    "  <File Path=\"~/file.png\"/>" +
                    "</Bundle>" +
                    "</Container>"
                );
                var bundle = new TestableBundle("~/test");
                var sourceBundles = new[] { bundle };

                var settings = new CassetteSettings
                {
                    SourceDirectory = new FileSystemDirectory(sourceDir),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                var result = cache.InitializeBundlesFromCacheIfUpToDate(sourceBundles);

                result.ShouldBeFalse();
            }
        }
示例#39
0
        public void GivenBundleWithAsset_WhenSaveContainer_ThenBundleFileSavedWithContentsOfAsset()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of<IDirectory>(),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                var bundle = new TestableBundle("~/test");
                var asset = StubAsset();
                asset.Setup(a => a.OpenStream())
                     .Returns(() => "ASSET-DATA".AsStream());
                bundle.Assets.Add(asset.Object);

                cache.SaveBundleContainer(new BundleContainer(new[] { bundle }));

                var savedData = File.ReadAllText(Path.Combine(cacheDir, "test.bundle"));
                savedData.ShouldEqual("ASSET-DATA");
            }
        }
示例#40
0
        public void GivenContainerFileDoesNotExist_WhenInitializeBundlesFromCacheIfUpToDate_ThenReturnFalse()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of<IDirectory>(),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                var sourceBundles = new TestableBundle[0];

                var result = cache.InitializeBundlesFromCacheIfUpToDate(sourceBundles);

                result.ShouldBeFalse();
            }
        }
示例#41
0
        public void GivenBundleWithReference_WhenSaveContainer_ThenXmlHasReferenceElement()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of<IDirectory>(),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                var bundle = new TestableBundle("~/test");
                bundle.AddReference("~/other");

                cache.SaveBundleContainer(new BundleContainer(new[] { bundle, new TestableBundle("~/other")  }));

                var xml = File.ReadAllText(Path.Combine(cacheDir, "container.xml"));
                xml.ShouldContain("<Reference Path=\"~/other\"");
            }
        }
示例#42
0
        public void GivenContainerFileIsOlderThanAnAssetFile_WhenInitializeBundlesFromCacheIfUpToDate_ThenReturnFalse()
        {
            using (var cacheDir = new TempDirectory())
            {
                File.WriteAllText(
                    Path.Combine(cacheDir, "container.xml"),
                    "<?xml version=\"1.0\"?><Container Version=\"VERSION\" AssetCount=\"0\"></Container>"
                    );

                var bundleWithAsset = new TestableBundle("~/test");
                var asset = StubAsset();
                asset.Setup(a => a.SourceFile.LastWriteTimeUtc).Returns(DateTime.UtcNow);
                bundleWithAsset.Assets.Add(asset.Object);
                var sourceBundles = new[] { bundleWithAsset };

                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of<IDirectory>(),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                var result = cache.InitializeBundlesFromCacheIfUpToDate(sourceBundles);

                result.ShouldBeFalse();
            }
        }
示例#43
0
        public void GivenBundle_WhenSaveContainer_ThenXmlHasBundleElementWithPathAttribute()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of<IDirectory>(),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                var bundle = new TestableBundle("~/test");
                var asset1 = StubAsset();
                bundle.Assets.Add(asset1.Object);

                cache.SaveBundleContainer(new BundleContainer(new[] { bundle }));

                var xml = File.ReadAllText(Path.Combine(cacheDir, "container.xml"));
                xml.ShouldContain("<Bundle Path=\"~/test\"");
            }
        }
示例#44
0
        public void GivenContainerFileWithDifferentVersion_WhenInitializeBundlesFromCacheIfUpToDate_ThenReturnFalse()
        {
            using (var cacheDir = new TempDirectory())
            {
                File.WriteAllText(
                    Path.Combine(cacheDir, "container.xml"),
                    "<?xml version=\"1.0\"?><Container Version=\"VERSION-1\" AssetCount=\"0\"></Container>"
                    );

                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of<IDirectory>(),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION-2", settings);
                var sourceBundles = new TestableBundle[0];

                var result = cache.InitializeBundlesFromCacheIfUpToDate(sourceBundles);

                result.ShouldBeFalse();
            }
        }
示例#45
0
        public void WhenSaveContainer_ThenXmlManifestContainsVersion()
        {
            using (var cacheDir = new TempDirectory())
            {
                var settings = new CassetteSettings
                {
                    SourceDirectory = Mock.Of<IDirectory>(),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                cache.SaveBundleContainer(new BundleContainer(new TestableBundle[0]));

                var xml = File.ReadAllText(Path.Combine(cacheDir, "container.xml"));
                xml.ShouldContain("Version=\"VERSION\"");
            }
        }
示例#46
0
        public void GivenContainerCacheHasFileReferenceThatIsModifiedSinceCacheWrite_WhenInitializeBundlesFromCacheIfUpToDate_ThenReturnFalse()
        {
            using (var cacheDir = new TempDirectory())
            using (var sourceDir = new TempDirectory())
            {
                File.WriteAllText(
                    Path.Combine(cacheDir, "container.xml"),
                    "<?xml version=\"1.0\"?><Container Version=\"VERSION\" AssetCount=\"0\">" +
                    "<Bundle Path=\"~/test\" Hash=\"01\">" +
                    "  <File Path=\"~/file.png\"/>" +
                    "</Bundle>" +
                    "</Container>"
                );
                Thread.Sleep(10); // Brief pause, otherwise the file write times are the same.
                File.WriteAllText(Path.Combine(sourceDir, "file.png"), "");
                var bundle = new TestableBundle("~/test");
                var sourceBundles = new[] { bundle };

                var settings = new CassetteSettings("")
                {
                    SourceDirectory = new FileSystemDirectory(sourceDir),
                    CacheDirectory = new FileSystemDirectory(cacheDir)
                };
                var cache = new BundleCache("VERSION", settings);
                var result = cache.InitializeBundlesFromCacheIfUpToDate(sourceBundles);

                result.ShouldBeFalse();
            }
        }