public void CanResolveFile()
        {
            //resharper doesn't support the TestCase attribute
            var values = new Dictionary<string, string>()
                             {
                                 {@"C:\testfile.js", @"C:\testfile.js"},
                                 {@"C:\testfile.css", @"C:\testfile.css"},
                                 {@"C:\test\testfile.js", @"C:\test\testfile.js"},
                                 {@"C:\test\testfile.css", @"C:\test\testfile.css"},
                                 {@"D:\testfile.js", @"D:\testfile.js"},
                                 {@"\testfile.js", @"C:\testfile.js"},
                                 {@"\test\testfile.js", @"C:\test\testfile.js"},
                                 {@"\test\test3\testfile.js", @"C:\test\test3\testfile.js"},
                                 {@"testfile.js", Environment.CurrentDirectory + @"\testfile.js"},
                                 {@"..\testfile.js", Path.GetFullPath(Environment.CurrentDirectory + @"\..\testfile.js")},
                                 {@"..\..\testfile.js", Path.GetFullPath(Environment.CurrentDirectory + @"\..\..\testfile.js")}
                             };

            var fileResolver = new FileResolver();
            foreach (string key in values.Keys)
            {
                var resolvedFile = fileResolver.TryResolve(key).ToList();
                Assert.AreEqual(values[key], resolvedFile[0]);
            }
        }
示例#2
0
        public void CanResolveFile()
        {
            //dealing with working directory on a non-C location
            var currentDirectory = Environment.CurrentDirectory;
            var driveLetter = currentDirectory.Substring(0, currentDirectory.IndexOf("\\"));//ignoring UNC paths

            //resharper doesn't support the TestCase attribute
            var values = new Dictionary<string, string>
                             {
                                 {@"C:\testfile.js", @"C:\testfile.js"},
                                 {@"C:\test\testfile.js", @"C:\test\testfile.js"},
                                 {@"D:\testfile.js", @"D:\testfile.js"},
                                 {@"\testfile.js", driveLetter + @"\testfile.js"},
                                 {@"\test\testfile.js", driveLetter + @"\test\testfile.js"},
                                 {@"\test\test3\testfile.js", driveLetter + @"\test\test3\testfile.js"},
                                 {@"testfile.js", Environment.CurrentDirectory + @"\testfile.js"},
                                 {@"..\testfile.js", Path.GetFullPath(Environment.CurrentDirectory + @"\..\testfile.js")},
                                 {@"..\..\testfile.js", Path.GetFullPath(Environment.CurrentDirectory + @"\..\..\testfile.js")}
                             };

            var fileResolver = new FileResolver();
            foreach (string key in values.Keys)
            {
                var resolvedFile = fileResolver.TryResolve(key).ToList();
                Assert.AreEqual(values[key], resolvedFile[0], key);
            }
        }
示例#3
0
        public void CanAppendFileHashToRelativeUriWithAnExistingQueryString()
        {
            var hashQueryStringKeyName = "v";
            var hashValue = "hashValue";
            var hasher = new StubHasher(hashValue);
            var fileResolver = new FileResolver();
            var uri = Assembly.GetExecutingAssembly().CodeBase;
            var cssFilePath = Path.GetDirectoryName(uri) + TestUtilities.PreparePath(@"\subdirectory\output.css");
            var url = "../" + Path.GetFileName(uri) + "?test=value";
            var assetsFileHasher = new CssAssetsFileHasher(hashQueryStringKeyName, fileResolver, hasher);

            var expectedUrl = url + "&" + hashQueryStringKeyName + "=" + hashValue;

            var rewrittenUrl = assetsFileHasher.AppendFileHash(cssFilePath, url);

            Assert.That(rewrittenUrl, Is.EqualTo(expectedUrl));
        }
示例#4
0
        public void CanResolveFile_Unix()
        {
            var currentDirectory = Environment.CurrentDirectory;

            var values = new Dictionary<string, string>
                             {
                                 {@"testfile.js", Path.Combine(currentDirectory, "testfile.js")},
                                 {@"/testfile.js", @"/testfile.js"},
                                 {@"../testfile.js", Path.Combine(currentDirectory.Substring(0, currentDirectory.LastIndexOf("/")), "testfile.js")}
                             };

            var fileResolver = new FileResolver();
            foreach (string key in values.Keys)
            {
                var resolvedFile = fileResolver.TryResolve(key).ToList();
                Assert.AreEqual(values[key], resolvedFile[0], key);
            }
        }
示例#5
0
        public void DoesNotAppendHashIfFileDoesNotExists()
        {
            var hashQueryStringKeyName = "v";
            var fileResolver = new FileResolver();
            var hasher = new StubHasher("hash");
            var cssFilePath = TestUtilities.PreparePath(@"C:\somepath\output.css");
            var url = "/doesnotexist.jpg";

            var assetsFileHasher = new CssAssetsFileHasher(hashQueryStringKeyName, fileResolver, hasher);

            var rewrittenUrl = assetsFileHasher.AppendFileHash(cssFilePath, url);

            Assert.That(rewrittenUrl, Is.EqualTo(url));
        }
示例#6
0
        public void DoesNotAppendHashIfFileIsRemote()
        {
            var hashQueryStringKeyName = "v";
            var fileResolver = new FileResolver();
            var hasher = new StubHasher("hash");
            var cssFilePath = @"C:\somepath\output.css";
            var url = "http://www.test.com/image.jpg";

            var assetsFileHasher = new CssAssetsFileHasher(hashQueryStringKeyName, fileResolver, hasher);

            var rewrittenUrl = assetsFileHasher.AppendFileHash(cssFilePath, url);

            Assert.That(rewrittenUrl, Is.EqualTo(url));
        }
示例#7
0
        private string CompressCss(string outputFilePath, List<string> files, ICssCompressor compressor, bool appendHashToAssets)
        {
            var outputCss = new StringBuilder();
            foreach (string file in files)
            {
                string css;
                if (file.ToLower().EndsWith(".less") || file.ToLower().EndsWith(".less.css"))
                {
                    css = ProcessLess(file);
                }
                else
                {
                    css = ReadFile(file);
                }

                if (processImports)
                {
                    css = ProcessImport(css);
                }

                ICssAssetsFileHasher fileHasher = null;

                if (appendHashToAssets)
                {
                    var fileResolver = new FileResolver();
                    fileHasher = new CssAssetsFileHasher(hashQueryStringKeyName, fileResolver, hasher);
                }

                css = CssPathRewriter.RewriteCssPaths(outputFilePath, file, css, fileHasher);
                outputCss.Append(css + "\n");
            }
            return compressor.CompressContent(outputCss.ToString());
        }