public override bool Execute() { if (Manifests != null && Manifests.Length > 0) { // create the project default settings // with a default warning level of maxvalue so everything is reported unless otherwise changed // with a -warn switch var projectDefaultSettings = new UglifyCommandParser { WarningLevel = int.MaxValue }; if (!ProjectDefaultSwitches.IsNullOrWhiteSpace()) { projectDefaultSettings.Parse(ProjectDefaultSwitches); } // each task item represents an ajaxmin manifest file: an XML file that // has settings and one or more output files, each comprised of one or more // input files. To execute this process, we will read the XML manifest and // execute NUglify for each output group. // won't bother executing NUglify is the file time for the output file // is greater than all its inputs. foreach (var taskItem in Manifests) { ProcessManifest(taskItem, projectDefaultSettings); } } // we succeeded if there have been no errors logged return(!Log.HasLoggedErrors); }
public void RunErrorTest(string settingsSwitches, params JSError[] expectedErrorArray) { // open the stack trace for this call StackTrace stackTrace = new StackTrace(); string testClass = null; string testName = null; // save the name of the current method (RunTest) string currentMethodName = MethodInfo.GetCurrentMethod().Name; // loop from the previous frame up until we get a method name that is not the // same as the current method name for (int ndx = 1; ndx < stackTrace.FrameCount; ++ndx) { // get the frame StackFrame stackFrame = stackTrace.GetFrame(ndx); // we have different entry points with the same name -- we're interested // in the first one that ISN'T the same name as our method MethodBase methodBase = stackFrame.GetMethod(); if (methodBase.Name != currentMethodName) { // the calling method's name is the test name - we use this as-is for the output file name // and we use any portion before an underscore as the input file testName = methodBase.Name; // get the method's class - we use this as the subfolder under input/output/expected testClass = methodBase.DeclaringType.Name; break; } } // we definitely should be able to find a function on the stack frame that // has a different name than this function, but just in case... Debug.Assert(testName != null && testClass != null, "Couldn't locate calling stack frame"); // the input file is the portion of the test name before the underscore (if any) string inputFile = testName.Split('_')[0]; // get the input and output paths string inputPath = GetJsPath( InputFolder, testClass, inputFile, false); Assert.IsTrue(File.Exists(inputPath), "Input File does not exist: {0}", inputPath); var outputPath = GetJsPath( m_outputFolder, testClass, testName, false); if (File.Exists(outputPath)) { // if it exists already, delete it File.Delete(outputPath); } else { // otherwise make sure the directory exists Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); } /*int expectedErrorCode = (int)(0x800A0000 + (int)expectedError); * Trace.WriteLine(string.Empty); * Trace.WriteLine(string.Format("Expecting error 0x{0:X}", expectedErrorCode));*/ // if we were passed a string containing command-line settings... var switchParser = new UglifyCommandParser(); if (!string.IsNullOrEmpty(settingsSwitches)) { // parse the string now switchParser.Parse(settingsSwitches); } // read the input JS string jsSource; using (var reader = new StreamReader(inputPath, GetJSEncoding(switchParser.EncodingInputName))) { jsSource = reader.ReadToEnd(); } Trace.Write("INPUT FILE: "); Trace.WriteLine(inputPath); Trace.WriteLine(jsSource); var testPassed = true; var expectedErrorList = new List <JSError>(expectedErrorArray); var errorList = new List <UglifyError>(); var parser = new JSParser(); parser.CompilerError += (source, e) => { errorList.Add(e.Error); }; var sb = new StringBuilder(); using (var writer = new StringWriter(sb)) { if (switchParser.JSSettings.PreprocessOnly) { parser.EchoWriter = writer; } // normal -- just run it through the parser var block = parser.Parse(new DocumentContext(jsSource) { FileContext = inputPath }, switchParser.JSSettings); if (!switchParser.JSSettings.PreprocessOnly) { // look at the settings for the proper output visitor if (switchParser.JSSettings.Format == JavaScriptFormat.JSON) { { if (!JsonOutputVisitor.Apply(writer, block, switchParser.JSSettings)) { Trace.WriteLine("JSON OUTPUT ERRORS!"); } } } else { OutputVisitor.Apply(writer, block, switchParser.JSSettings); } } } var crunchedCode = sb.ToString(); // output the crunched code using the proper output encoding using (var outputStream = new StreamWriter(outputPath, false, GetJSEncoding(switchParser.EncodingOutputName))) { outputStream.Write(crunchedCode); } Trace.WriteLine(string.Empty); Trace.WriteLine("---ERRORS---"); foreach (var err in errorList) { Trace.WriteLine(((JSError)err.ErrorNumber).ToString()); } Trace.WriteLine(string.Empty); Trace.Indent(); foreach (var err in errorList) { // log the error Trace.WriteLine(string.Empty); Trace.WriteLine(string.Format("Error {0} at Line {1}, Column {2}: {3}", err.ErrorCode, err.StartLine, err.StartColumn, err.Message)); Trace.Indent(); Trace.WriteLine(err.Message); int index = expectedErrorList.IndexOf((JSError)err.ErrorNumber); if (index >= 0) { // expected error -- remove it from the list so we can tell what we're missing later expectedErrorList.RemoveAt(index); } else { // unexpected error testPassed = false; Trace.WriteLine("UNEXPECTED"); } Trace.Unindent(); } Trace.Unindent(); // the list should be empty now -- if it isn't, then there was an expected error that didn't happen if (expectedErrorList.Count > 0) { testPassed = false; Trace.WriteLine(string.Empty); Trace.WriteLine("---MISSING ERRORS---"); Trace.Indent(); foreach (JSError jsError in expectedErrorList) { Trace.WriteLine(jsError.ToString()); } Trace.Unindent(); } if (!testPassed) { Trace.WriteLine(""); Trace.WriteLine("UNEXPECTED ERROR RESULTS"); } // compute the path to the expected file string expectedPath = GetJsPath( m_expectedFolder, testClass, testName, false); Trace.WriteLine(string.Empty); Trace.WriteLine("odd \"" + expectedPath + "\" \"" + outputPath + "\""); Trace.WriteLine(string.Empty); Trace.WriteLine("---Expected Code---"); TraceFileContents(expectedPath); Trace.WriteLine(string.Empty); Trace.WriteLine("---Resulting Code---"); TraceFileContents(outputPath); AssertCompareTextFiles(outputPath, expectedPath); Assert.IsTrue(testPassed, "Test failed"); }
public void ToSettings() { var testData = new ArgumentsSettings[] { new ArgumentsSettings() { CommandLine = "-warn:4 -ei:utf-8 -enc:out utf-8 /g:jQuery,$,Msn -p", JSSettings = new CodeSettings() { OutputMode = OutputMode.MultipleLines, LocalRenaming = LocalRenaming.KeepAll, KnownGlobalNamesList = "jQuery,$,Msn", MinifyCode = false, KillSwitch = -2 }, CssSettings = new CssSettings() { OutputMode = OutputMode.MultipleLines, KillSwitch = -2 }, WarningLevel = 4, EncodingInputName = "utf-8", EncodingOutputName = "utf-8" }, new ArgumentsSettings() { CommandLine = "-minify:false -rename:none", JSSettings = new CodeSettings() { MinifyCode = false, LocalRenaming = LocalRenaming.KeepAll }, CssSettings = null, WarningLevel = 0 }, new ArgumentsSettings() { CommandLine = "-define:foo,bar,ack,gag,42", JSSettings = new CodeSettings() { PreprocessorDefineList = "foo,bar,ack,gag" }, CssSettings = new CssSettings() { PreprocessorDefineList = "foo,bar,ack,gag" } }, new ArgumentsSettings() { CommandLine = "-ignore:foo,bar,ack,gag", JSSettings = new CodeSettings() { IgnoreErrorList = "foo,bar,ack,gag" }, CssSettings = new CssSettings() { IgnoreErrorList = "foo,bar,ack,gag" } }, new ArgumentsSettings() { CommandLine = "/aspnet:T -pretty:8 -term:Yes", JSSettings = new CodeSettings() { AllowEmbeddedAspNetBlocks = true, TermSemicolons = true, LocalRenaming = LocalRenaming.KeepAll, OutputMode = OutputMode.MultipleLines, IndentSize = 8, MinifyCode = false, KillSwitch = -2 }, CssSettings = new CssSettings() { AllowEmbeddedAspNetBlocks = true, TermSemicolons = true, OutputMode = OutputMode.MultipleLines, IndentSize = 8, KillSwitch = -2 } }, new ArgumentsSettings() { CommandLine = "/aspnet:F -minify:1 -kill:0 -pretty:0 -term:N", JSSettings = new CodeSettings() { AllowEmbeddedAspNetBlocks = false, TermSemicolons = false, LocalRenaming = LocalRenaming.KeepAll, OutputMode = OutputMode.MultipleLines, IndentSize = 0 }, CssSettings = new CssSettings() { AllowEmbeddedAspNetBlocks = false, TermSemicolons = false, OutputMode = OutputMode.MultipleLines, IndentSize = 0 } }, new ArgumentsSettings() { CommandLine = "-expr:minify -colors:hex -comments:All", JSSettings = null, CssSettings = new CssSettings() { MinifyExpressions = true, ColorNames = CssColor.Hex, CommentMode = CssComment.All } }, new ArgumentsSettings() { CommandLine = "-expr:raw -colors:MAJOR /comments:HaCkS", JSSettings = null, CssSettings = new CssSettings() { MinifyExpressions = false, ColorNames = CssColor.Major, CommentMode = CssComment.Hacks } }, new ArgumentsSettings() { CommandLine = "-cc:1 -comments:None -debug:true -inline:yes -literals:keep -literals:evAL -mac:Y -minify:T -new:Keep -reorder:yes -unused:remove -rename:all", JSSettings = new CodeSettings() { IgnoreConditionalCompilation = false, PreserveImportantComments = false, StripDebugStatements = false, InlineSafeStrings = true, EvalLiteralExpressions = true, MacSafariQuirks = true, MinifyCode = true, CollapseToLiteral = false, ReorderScopeDeclarations = true, RemoveUnneededCode = true, LocalRenaming = LocalRenaming.CrunchAll, PreprocessorDefineList = "debug" }, CssSettings = null }, new ArgumentsSettings() { CommandLine = "-cc:0 -comments:ImportanT -debug:false -inline:no /literals:combine -literals:NoEval -mac:N -minify:F -new:Collapse -reorder:N -unused:keep -rename:localization", JSSettings = new CodeSettings() { IgnoreConditionalCompilation = true, PreserveImportantComments = true, StripDebugStatements = true, InlineSafeStrings = false, EvalLiteralExpressions = false, MacSafariQuirks = false, MinifyCode = false, CollapseToLiteral = true, ReorderScopeDeclarations = false, RemoveUnneededCode = false, LocalRenaming = LocalRenaming.KeepLocalizationVars }, CssSettings = null }, new ArgumentsSettings() { CommandLine = "–debug:,", JSSettings = new CodeSettings() { StripDebugStatements = false, DebugLookupList = "", PreprocessorDefineList = "debug" }, CssSettings = null }, new ArgumentsSettings() { CommandLine = "-debug:N,", JSSettings = new CodeSettings() { StripDebugStatements = true, DebugLookupList = "" }, CssSettings = null }, new ArgumentsSettings() { CommandLine = "-debug:N,Foo,Bar,Ack.Gag.Barf,14,Name.42.First", JSSettings = new CodeSettings() { StripDebugStatements = true, DebugLookupList = "Foo,Bar,Ack.Gag.Barf" }, CssSettings = null }, new ArgumentsSettings() { CommandLine = "-global:foo,bar,ack,gag,212", JSSettings = new CodeSettings() { KnownGlobalNamesList = "foo,bar,ack,gag" }, CssSettings = null }, new ArgumentsSettings() { CommandLine = "-norename:foo,bar,ack,gag,105 -rename:NoProps", JSSettings = new CodeSettings() { NoAutoRenameList = "foo,bar,ack,gag", ManualRenamesProperties = false }, CssSettings = null }, new ArgumentsSettings() { CommandLine = "-rename:foo=bar,ack=gag,105=106", JSSettings = new CodeSettings() { RenamePairs = "foo=bar,ack=gag", ManualRenamesProperties = true }, CssSettings = null }, new ArgumentsSettings() { CommandLine = "–fnames:lock -evals:ignore", JSSettings = new CodeSettings() { PreserveFunctionNames = true, RemoveFunctionExpressionNames = false, EvalTreatment = EvalTreatment.Ignore }, CssSettings = null }, new ArgumentsSettings() { CommandLine = "-fnames:keep -evals:immediate", JSSettings = new CodeSettings() { PreserveFunctionNames = false, RemoveFunctionExpressionNames = false, EvalTreatment = EvalTreatment.MakeImmediateSafe }, CssSettings = null }, new ArgumentsSettings() { CommandLine = "-fnames:onlyref -evals:safeall", JSSettings = new CodeSettings() { PreserveFunctionNames = false, RemoveFunctionExpressionNames = true, EvalTreatment = EvalTreatment.MakeAllSafe }, CssSettings = null }, new ArgumentsSettings() { CommandLine = "–kill:-1", JSSettings = new CodeSettings() { KillSwitch = -1 }, CssSettings = new CssSettings() { KillSwitch = -1, CommentMode = CssComment.None } }, new ArgumentsSettings() { CommandLine = "–kill:0x1", JSSettings = new CodeSettings() { KillSwitch = 1 }, CssSettings = new CssSettings() { KillSwitch = 1, CommentMode = CssComment.None } }, new ArgumentsSettings() { CommandLine = "-kill:2", JSSettings = new CodeSettings() { KillSwitch = 2 }, CssSettings = new CssSettings() { KillSwitch = 2 } }, new ArgumentsSettings() { CommandLine = "-kill:0xDAB0 -cc:BOOYAH! -warn:-1", JSSettings = new CodeSettings() { KillSwitch = 0xdab0 }, CssSettings = new CssSettings() { KillSwitch = 55984 }, WarningLevel = 0 }, new ArgumentsSettings() { CommandLine = "-enc:in ascii -EO:big5 -warn", JSSettings = null, CssSettings = null, EncodingInputName = "ascii", EncodingOutputName = "big5", WarningLevel = int.MaxValue }, new ArgumentsSettings() { CommandLine = "-css /js", JSSettings = new CodeSettings(), CssSettings = new CssSettings() }, new ArgumentsSettings() { CommandLine = "-rename:All -pretty:WHAM", JSSettings = new CodeSettings() { OutputMode = OutputMode.MultipleLines, IndentSize = 4, KillSwitch = -16777218 }, CssSettings = new CssSettings() { OutputMode = OutputMode.MultipleLines, IndentSize = 4, KillSwitch = -2 } }, new ArgumentsSettings() { CommandLine = "-rename:All -pretty:-10", JSSettings = new CodeSettings() { OutputMode = OutputMode.MultipleLines, IndentSize = 4, KillSwitch = -16777218 }, CssSettings = new CssSettings() { OutputMode = OutputMode.MultipleLines, IndentSize = 4, KillSwitch = -2 } }, new ArgumentsSettings() { CommandLine = "–rename:foo=bar,foo=ack", JSSettings = new CodeSettings() { RenamePairs = "foo=bar" }, CssSettings = null }, new ArgumentsSettings() { CommandLine = "–d -h -j -k -m", JSSettings = new CodeSettings(), CssSettings = null }, new ArgumentsSettings() { CommandLine = "-l -z -hCL", JSSettings = new CodeSettings() { CollapseToLiteral = false, TermSemicolons = true, LocalRenaming = LocalRenaming.KeepLocalizationVars }, CssSettings = null }, new ArgumentsSettings() { CommandLine = "/HC", JSSettings = new CodeSettings() { }, CssSettings = null }, new ArgumentsSettings() { CommandLine = "-define:debug", JSSettings = new CodeSettings() { StripDebugStatements = false, PreprocessorDefineList = "debug" }, CssSettings = null }, new ArgumentsSettings() { CommandLine = "-define:debug -debug:0", JSSettings = new CodeSettings() { StripDebugStatements = true }, CssSettings = null }, new ArgumentsSettings() { CommandLine = "-debug:0 -define:debug", JSSettings = new CodeSettings() { StripDebugStatements = false, PreprocessorDefineList = "debug" }, CssSettings = null }, new ArgumentsSettings() { CommandLine = "-define:foo=bar,bob,ack=gag,BaT=CrAzY", JSSettings = new CodeSettings() { PreprocessorDefineList = "foo=bar,bob,ack=gag,BaT=CrAzY" }, CssSettings = new CssSettings() { PreprocessorDefineList = "foo=bar,bob,ack=gag,BaT=CrAzY" } }, new ArgumentsSettings() { CommandLine = "-define:foo=", JSSettings = null, CssSettings = new CssSettings() { PreprocessorDefineList = "foo" } }, new ArgumentsSettings() { CommandLine = "-define:configuration=Debug -define:debug=Y,ralph=driver", JSSettings = new CodeSettings() { StripDebugStatements = false, PreprocessorDefineList = "configuration=Debug,debug=Y,ralph=driver" }, CssSettings = new CssSettings() { PreprocessorDefineList = "configuration=Debug,debug=Y,ralph=driver" } }, new ArgumentsSettings() { CommandLine = "-define:once=first -define:OnCE=Last", JSSettings = new CodeSettings() { PreprocessorDefineList = "once=Last" }, CssSettings = new CssSettings() { PreprocessorDefineList = "once=Last" } }, }; var ndxTest = 0; foreach (var test in testData) { Trace.Write(string.Format("Settings test {0}, command line: ", ++ndxTest)); Trace.WriteLine(test.CommandLine ?? "<null pointer>"); // parse the command line var switchParser = new UglifyCommandParser(); switchParser.Parse(test.CommandLine); // assume succesful unless proven otherwise var success = true; // test the top-level properties if (switchParser.WarningLevel == test.WarningLevel) { Trace.WriteLine(string.Format("\tParsed warning level {0} matches expectations", switchParser.WarningLevel)); } else { Trace.WriteLine(string.Format("\tFAIL: Parsed warning level is {0}, expected is {1}", switchParser.WarningLevel, test.WarningLevel)); success = false; } if (string.CompareOrdinal(switchParser.EncodingInputName, test.EncodingInputName) == 0) { Trace.WriteLine(string.Format("\tParsed input encoding {0} matches expectations", switchParser.EncodingInputName)); } else { Trace.WriteLine(string.Format("\tFAIL: Parsed input encoding is {0}, expected is {1}", switchParser.EncodingInputName, test.EncodingInputName)); success = false; } if (string.CompareOrdinal(switchParser.EncodingOutputName, test.EncodingOutputName) == 0) { Trace.WriteLine(string.Format("\tParsed output encoding {0} matches expectations", switchParser.EncodingOutputName)); } else { Trace.WriteLine(string.Format("\tFAIL: Parsed output encoding is {0}, expected is {1}", switchParser.EncodingOutputName, test.EncodingOutputName)); success = false; } // if we care about the JS settings.... if (test.JSSettings != null) { var jsSuccess = CheckSettings(switchParser.JSSettings, test.JSSettings); if (!jsSuccess) { success = false; } } // if we care about the CSS settings.... if (test.CssSettings != null) { var cssSuccess = CheckSettings(switchParser.CssSettings, test.CssSettings); if (!cssSuccess) { success = false; } } Assert.IsTrue(success, "\t****TEST {0} FAILED!", ndxTest); } }