public void TestOutputContainsSentDate() { SilentErrorHandler sh = new SilentErrorHandler(); AppenderSmtpPickup appender = CreateSmtpPickupDirAppender(sh); ILogger log = CreateLogger(appender); log.Log(GetType(), Level.Info, "This is a message", null); log.Log(GetType(), Level.Info, "This is a message 2", null); DestroyLogger(); Assert.AreEqual(1, Directory.GetFiles(_testPickupDir).Length); string[] fileContent = File.ReadAllLines((Directory.GetFiles(_testPickupDir)[0])); bool hasDateHeader = false; const string dateHeaderStart = "Date: "; foreach (string line in fileContent) { if (line.StartsWith(dateHeaderStart)) { string datePart = line.Substring(dateHeaderStart.Length); DateTime date = DateTime.ParseExact(datePart, "r", System.Globalization.CultureInfo.InvariantCulture); double diff = Math.Abs((DateTime.UtcNow - date).TotalMilliseconds); Assert.LessOrEqual(diff, 1000, "Times should be equal, allowing a diff of one second to make test robust"); hasDateHeader = true; } } Assert.IsTrue(hasDateHeader, "Output must contains a date header"); Assert.AreEqual("", sh.Message, "Unexpected error message"); }
public SilentErrorHandler PushSilentErrorHandler() { var errHandler = new SilentErrorHandler(); m_errorHandlerReplacements.Push(errHandler); return(errHandler); }
public void TestDefaultFileNameIsAGuid() { SilentErrorHandler sh = new SilentErrorHandler(); AppenderSmtpPickup appender = CreateSmtpPickupDirAppender(sh); ILogger log = CreateLogger(appender); log.Log(GetType(), Level.Info, "This is a message", null); log.Log(GetType(), Level.Info, "This is a message 2", null); DestroyLogger(); Assert.AreEqual(1, Directory.GetFiles(_testPickupDir).Length); FileInfo fileInfo = new FileInfo(Directory.GetFiles(_testPickupDir)[0]); Assert.IsEmpty(fileInfo.Extension); Assert.DoesNotThrow(delegate { new Guid(fileInfo.Name); }); // Assert that filename is a guid Assert.AreEqual("", sh.Message, "Unexpected error message"); }
public void TestDefaultFileNameIsAGuid() { SilentErrorHandler sh = new SilentErrorHandler(); SmtpPickupDirAppender appender = CreateSmtpPickupDirAppender(sh); ILogger log = CreateLogger(appender); log.Log(GetType(), Level.Info, "This is a message", null); log.Log(GetType(), Level.Info, "This is a message 2", null); DestroyLogger(); Assert.AreEqual(1, Directory.GetFiles(_testPickupDir).Length); var fileInfo = new FileInfo(Directory.GetFiles(_testPickupDir)[0]); Assert.IsEmpty(fileInfo.Extension); Guid tmpGuid; Assert.IsTrue(Guid.TryParse(fileInfo.Name, out tmpGuid)); // Assert that filename is a guid Assert.AreEqual("", sh.Message, "Unexpected error message"); }
public void TestConfigurableFileExtension() { const string fileExtension = "eml"; SilentErrorHandler sh = new SilentErrorHandler(); AppenderSmtpPickup appender = CreateSmtpPickupDirAppender(sh); appender.FileExtension = fileExtension; ILogger log = CreateLogger(appender); log.Log(GetType(), Level.Info, "This is a message", null); log.Log(GetType(), Level.Info, "This is a message 2", null); DestroyLogger(); Assert.AreEqual(1, Directory.GetFiles(_testPickupDir).Length); FileInfo fileInfo = new FileInfo(Directory.GetFiles(_testPickupDir)[0]); Assert.AreEqual("." + fileExtension, fileInfo.Extension); Assert.DoesNotThrow(delegate { new Guid(fileInfo.Name.Substring(0, fileInfo.Name.Length - fileInfo.Extension.Length)); }); // Assert that filename before extension is a guid Assert.AreEqual("", sh.Message, "Unexpected error message"); }
public void TestDefaultFileNameIsAGuid() { SilentErrorHandler sh = new SilentErrorHandler(); SmtpPickupDirAppender appender = CreateSmtpPickupDirAppender(sh); ILogger log = CreateLogger(appender); log.Log(GetType(), Level.Info, "This is a message", null); log.Log(GetType(), Level.Info, "This is a message 2", null); DestroyLogger(); Assert.AreEqual(1, Directory.GetFiles(_testPickupDir).Length); var fileInfo = new FileInfo(Directory.GetFiles(_testPickupDir)[0]); Assert.IsEmpty(fileInfo.Extension); Assert.DoesNotThrow(delegate { new Guid(fileInfo.Name); }); // Assert that filename is a guid Assert.AreEqual("", sh.Message, "Unexpected error message"); }
public void TestInterProcessLockRoll() { String filename = "test.log"; bool locked; SilentErrorHandler sh = new SilentErrorHandler(); ILogger log = CreateLogger(filename, new FileAppender.InterProcessLock(), sh, 1, 2); Assert.DoesNotThrow(delegate { log.Log(GetType(), Level.Info, "A", null); }); Assert.DoesNotThrow(delegate { log.Log(GetType(), Level.Info, "A", null); }); DestroyLogger(); AssertFileEquals(filename, "A" + Environment.NewLine); AssertFileEquals(filename + ".1", "A" + Environment.NewLine); Assert.IsEmpty(sh.Message); }
public void TestDefaultLockingModel() { String filename = "test.log"; SilentErrorHandler sh = new SilentErrorHandler(); ILogger log = CreateLogger(filename, null, sh); IAppender[] appenders = log.Repository.GetAppenders(); Assert.AreEqual(1, appenders.Length, "The wrong number of appenders are configured"); RollingFileAppender rfa = (RollingFileAppender)(appenders[0]); Assert.AreEqual(typeof(log4net.Appender.FileAppender.ExclusiveLock), rfa.LockingModel.GetType(), "The LockingModel is of an unexpected type"); DestroyLogger(); }
public void TestInterProcessLockUnlocks() { String filename = "test.log"; bool locked; SilentErrorHandler sh = new SilentErrorHandler(); ILogger log = CreateLogger(filename, new FileAppender.InterProcessLock(), sh); log.Log(GetType(), Level.Info, "This is a message", null); locked = true; FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); fs.Write(Encoding.ASCII.GetBytes("Test" + Environment.NewLine), 0, 4 + Environment.NewLine.Length); fs.Close(); log.Log(GetType(), Level.Info, "This is a message 2", null); DestroyLogger(); Assert.IsTrue(locked, "File was not locked"); AssertFileEquals(filename, "This is a message" + Environment.NewLine + "Test" + Environment.NewLine + "This is a message 2" + Environment.NewLine); Assert.AreEqual("", sh.Message, "Unexpected error message"); }
public void TestInterProcessLockRecovers() { String filename = "test.log"; FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None); fs.Write(Encoding.ASCII.GetBytes("Test"), 0, 4); SilentErrorHandler sh = new SilentErrorHandler(); ILogger log = CreateLogger(filename, new FileAppender.InterProcessLock(), sh); log.Log(GetType(), Level.Info, "This is a message", null); fs.Close(); log.Log(GetType(), Level.Info, "This is a message 2", null); DestroyLogger(); AssertFileEquals(filename, "This is a message 2" + Environment.NewLine); Assert.AreEqual("Unable to acquire lock on file", sh.Message.Substring(0, 30), "Expecting an error message"); }
public void TestExclusiveLockLocks() { String filename = "test.log"; bool locked = false; SilentErrorHandler sh = new SilentErrorHandler(); ILogger log = CreateLogger(filename, new FileAppender.ExclusiveLock(), sh); log.Log(GetType(), Level.Info, "This is a message", null); try { FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None); fs.Write(Encoding.ASCII.GetBytes("Test"), 0, 4); fs.Close(); } catch(IOException e1) { #if DOTNET Assert.AreEqual("The process cannot access the file ", e1.Message.Substring(0, 35), "Unexpected exception"); #else Assert.AreEqual("Sharing violation on path ", e1.Message.Substring(0, 26), "Unexpected exception"); #endif locked = true; } log.Log(GetType(), Level.Info, "This is a message 2", null); DestroyLogger(); Assert.IsTrue(locked, "File was not locked"); #if !MONO || !FRAMEWORK_3_5_OR_ABOVE // at least on Linux with Mono 2.4 exclusive locking doesn't work as one would expect AssertFileEquals(filename, "This is a message" + Environment.NewLine + "This is a message 2" + Environment.NewLine); #endif Assert.AreEqual("", sh.Message, "Unexpected error message"); }
public void TestLogOutput() { String filename = "test.log"; SilentErrorHandler sh = new SilentErrorHandler(); ILogger log = CreateLogger(filename, new FileAppender.ExclusiveLock(), sh); log.Log(GetType(), Level.Info, "This is a message", null); log.Log(GetType(), Level.Info, "This is a message 2", null); DestroyLogger(); AssertFileEquals(filename, "This is a message" + Environment.NewLine + "This is a message 2" + Environment.NewLine); Assert.AreEqual("", sh.Message, "Unexpected error message"); }
public void TestConfigurableFileExtension() { const string fileExtension = "eml"; SilentErrorHandler sh = new SilentErrorHandler(); SmtpPickupDirAppender appender = CreateSmtpPickupDirAppender(sh); appender.FileExtension = fileExtension; ILogger log = CreateLogger(appender); log.Log(GetType(), Level.Info, "This is a message", null); log.Log(GetType(), Level.Info, "This is a message 2", null); DestroyLogger(); Assert.AreEqual(1, Directory.GetFiles(_testPickupDir).Length); var fileInfo = new FileInfo(Directory.GetFiles(_testPickupDir)[0]); Assert.AreEqual("." + fileExtension, fileInfo.Extension); Assert.DoesNotThrow(delegate { new Guid(fileInfo.Name.Substring(0, fileInfo.Name.Length - fileInfo.Extension.Length)); }); // Assert that filename before extension is a guid Assert.AreEqual("", sh.Message, "Unexpected error message"); }
public void TestOutputContainsSentDate() { SilentErrorHandler sh = new SilentErrorHandler(); SmtpPickupDirAppender appender = CreateSmtpPickupDirAppender(sh); ILogger log = CreateLogger(appender); log.Log(GetType(), Level.Info, "This is a message", null); log.Log(GetType(), Level.Info, "This is a message 2", null); DestroyLogger(); Assert.AreEqual(1, Directory.GetFiles(_testPickupDir).Length); string[] fileContent = File.ReadAllLines((Directory.GetFiles(_testPickupDir)[0])); bool hasDateHeader = false; const string dateHeaderStart = "Date: "; foreach (string line in fileContent) { if(line.StartsWith(dateHeaderStart)) { var datePart = line.Substring(dateHeaderStart.Length); var date = DateTime.ParseExact(datePart, "r", System.Globalization.CultureInfo.InvariantCulture); var diff = Math.Abs( (DateTime.UtcNow - date).TotalMilliseconds); Assert.LessOrEqual(diff, 1000, "Times should be equal, allowing a diff of one second to make test robust"); hasDateHeader = true; } } Assert.IsTrue(hasDateHeader, "Output must contains a date header"); Assert.AreEqual("", sh.Message, "Unexpected error message"); }
private void LoadFile(Uri uri, string path, string text) { path = path.Replace('/', System.IO.Path.DirectorySeparatorChar); if (files.ContainsKey(path)) { return; } var eh = new SilentErrorHandler(); var lexer = Lexer.FromString(text, eh, path); SourceFile file = new SourceFile(uri, text); files[path] = file; var parser = new Parser(lexer, eh); void HandleImport(string baseFilePath, AstImportExpr import) { string SearchForModuleInPath(string basePath, AstIdExpr[] module) { var path = basePath; for (int i = 0; i < module.Length - 1; i++) { var combined = Path.Combine(path, module[i].Name); if (Directory.Exists(combined)) { path = combined; } else { return(null); } } path = Path.Combine(path, module.Last().Name); path += ".che"; if (File.Exists(path)) { return(path); } return(null); } IEnumerable <string> ModulePaths(string baseFilePath, AstIdExpr[] path) { yield return(Path.GetDirectoryName(baseFilePath)); foreach (var modulePath in modulePaths) { yield return(modulePath); } } string FindModule() { foreach (var modPath in ModulePaths(baseFilePath, import.Path)) { var p = SearchForModuleInPath(modPath, import.Path); if (p != null) { return(p); } } return(null); } string path = FindModule(); if (path == null || files.ContainsKey(path)) { return; } var uriPath = path.Replace("\\", "/").Replace("D:", "file:///d%3A"); var uri = new Uri(uriPath); var relative = _workerSpaceRoot.MakeRelativeUri(uri); LoadFile(new Uri(_workerSpaceRoot, relative), path, File.ReadAllText(path)); } while (true) { var s = parser.ParseStatement(); if (s == null) { break; } file.statements.Add(s); switch (s) { case AstConstantDeclaration d when d.Initializer is AstImportExpr import: HandleImport(path, import); break; case AstUsingStmt d when d.Value is AstImportExpr import: HandleImport(path, import); break; } } }
/// <summary> /// Verifies that attempting to log to a file with ExclusiveLock really locks the file /// </summary> [Test] public void TestExclusiveLockLocks() { String filename="test.log"; bool locked=false; SilentErrorHandler sh=new SilentErrorHandler(); ILogger log=CreateLogger(filename,new FileAppender.ExclusiveLock(),sh); log.Log(this.GetType(),Level.Info,"This is a message",null); try { FileStream fs=new FileStream(filename,FileMode.Create,FileAccess.Write,FileShare.None); fs.Write(System.Text.Encoding.ASCII.GetBytes("Test"),0,4); fs.Close(); } catch (System.IO.IOException e1) { Assert.AreEqual("The process cannot access the file ",e1.Message.Substring(0,35), "Unexpected exception"); locked=true; } log.Log(this.GetType(),Level.Info,"This is a message 2",null); DestroyLogger(); Assert.IsTrue(locked, "File was not locked"); AssertFileEquals(filename,"This is a message"+Environment.NewLine+"This is a message 2"+Environment.NewLine); Assert.AreEqual("",sh.Message, "Unexpected error message"); }
/// <summary> /// Verifies that attempting to log to a locked file fails gracefully /// </summary> [Test] public void TestExclusiveLockFails() { String filename="test.log"; FileStream fs=new FileStream(filename,FileMode.Create,FileAccess.Write,FileShare.None); fs.Write(System.Text.Encoding.ASCII.GetBytes("Test"),0,4); SilentErrorHandler sh=new SilentErrorHandler(); ILogger log=CreateLogger(filename,new FileAppender.ExclusiveLock(),sh); log.Log(this.GetType(),Level.Info,"This is a message",null); log.Log(this.GetType(),Level.Info,"This is a message 2",null); DestroyLogger(); fs.Close(); AssertFileEquals(filename,"Test"); Assert.AreEqual(sh.Message.Substring(0,30), "Unable to acquire lock on file", "Expecting an error message"); }
static CompilationResult Run(CompilerOptions options) { var result = new CompilationResult(); result.PrintTime = options.PrintTime; if (options.OutName == null) { options.OutName = Path.GetFileNameWithoutExtension(options.Files.First()); } Console.WriteLine(Parser.Default.FormatCommandLine(options)); IErrorHandler errorHandler = new ConsoleErrorHandler(0, 0, options.PrintSourceInErrorMessage); if (options.NoErrors) { errorHandler = new SilentErrorHandler(); } var stopwatch = new Stopwatch(); stopwatch.Start(); var compiler = new CheezCompiler(errorHandler, options.Stdlib, options.Preload); foreach (string mod in options.Modules) { compiler.ModulePaths.Add(mod); } // load additional module paths from modules.txt if existent { string exePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); void LoadModulesFile(string fileName) { string modulesFile = Path.Combine(exePath, fileName); if (File.Exists(modulesFile)) { foreach (var modulePath in File.ReadAllLines(modulesFile)) { if (!string.IsNullOrWhiteSpace(modulePath)) { compiler.ModulePaths.Add(modulePath); } } } } LoadModulesFile("modules.txt"); if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { LoadModulesFile("modules_linux.txt"); } if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { LoadModulesFile("modules_windows.txt"); } } foreach (var file in options.Files) { var v = compiler.AddFile(file); if (v == null) { result.ExitCode = 4; } } result.LexAndParse = stopwatch.Elapsed; if (options.PrintRawAst != null) { var dir = Path.GetDirectoryName(options.PrintRawAst); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } using (var file = File.Open(options.PrintRawAst, FileMode.Create)) using (var writer = new StreamWriter(file)) { var printer = new RawAstPrinter(writer); printer.PrintWorkspace(compiler.DefaultWorkspace); } } stopwatch.Restart(); compiler.DefaultWorkspace.CompileAll(); result.SemanticAnalysis = stopwatch.Elapsed; result.FrontEnd = result.LexAndParse + result.SemanticAnalysis; if (options.PrintAnalysedAst != null) { var dir = Path.GetDirectoryName(options.PrintAnalysedAst); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } var printer = new AnalysedAstPrinter(); using (var file = File.Open(options.PrintAnalysedAst, FileMode.Create)) using (var writer = new StreamWriter(file)) { printer.PrintWorkspace(compiler.DefaultWorkspace, writer); } } System.Console.WriteLine($"Global variables: {compiler.DefaultWorkspace.Variables.Count()}"); System.Console.WriteLine($" Impls: {compiler.DefaultWorkspace.Impls.Count()}"); System.Console.WriteLine($" Functions: {compiler.DefaultWorkspace.Functions.Count()}"); System.Console.WriteLine($" Structs: {compiler.DefaultWorkspace.Structs.Count()}"); System.Console.WriteLine($" Enums: {compiler.DefaultWorkspace.Enums.Count()}"); System.Console.WriteLine($" Traits: {compiler.DefaultWorkspace.Traits.Count()}"); if (errorHandler.HasErrors) { result.ExitCode = 3; return(result); } if (!options.DontEmitCode) { // generate code stopwatch.Restart(); bool codeGenOk = GenerateAndCompileCode(options, compiler.DefaultWorkspace, errorHandler); result.BackEnd = stopwatch.Elapsed; if (!codeGenOk) { result.ExitCode = 4; return(result); } if (options.RunCode && codeGenOk) { if (options.RunAsTest) { stopwatch.Restart(); string[] StringLiteralToString(AstExpression e) => (e as AstStringLiteral).StringValue.Split('\n'); IEnumerable <string> DirectiveToStrings(AstDirective d) => d.Arguments.SelectMany(StringLiteralToString); string[] expectedOutputs = compiler.TestOutputs.Select(DirectiveToStrings).SelectMany(x => x).ToArray(); int currentExpectedOutput = 0; int linesFailed = 0; string exeFileExtension = ""; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { exeFileExtension = ".exe"; } if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { exeFileExtension = ""; } var testProc = Utilities.StartProcess( Path.Combine(options.OutDir, options.OutName + exeFileExtension), "", workingDirectory: options.OutDir, stdout: (s, e) => { if (e.Data != null) { var expectedOutput = currentExpectedOutput < expectedOutputs.Length ? expectedOutputs[currentExpectedOutput] : ""; if (expectedOutput != e.Data) { Console.WriteLine($"[TEST] ({currentExpectedOutput}) Expected: '{expectedOutput}', got: '{e.Data}'"); linesFailed++; } currentExpectedOutput++; } }, stderr: (s, e) => { if (e.Data != null) { Console.Error.WriteLine(e.Data); } }); testProc.WaitForExit(); result.Execution = stopwatch.Elapsed; if (linesFailed == 0 && currentExpectedOutput == expectedOutputs.Length) { Console.WriteLine($"[TEST] {options.OutName} ok"); } else { if (linesFailed > 0) { Console.WriteLine($"[TEST] {linesFailed} error(s)."); } if (currentExpectedOutput != expectedOutputs.Length) { Console.WriteLine($"[TEST] Not enough output."); } result.ExitCode = 69; return(result); } } else { Console.WriteLine($"\nRunning code:"); Console.WriteLine("====================================="); stopwatch.Restart(); var testProc = Utilities.StartProcess( Path.Combine(options.OutDir, options.OutName + ".exe"), "", workingDirectory: options.OutDir, stdout: (s, e) => { if (e.Data != null) { Console.WriteLine(e.Data); } }, stderr: (s, e) => { if (e.Data != null) { Console.Error.WriteLine(e.Data); } }); //var testProc = Utilities.StartProcess( // Path.Combine(options.OutDir, options.OutName + ".exe"), // "", // workingDirectory: options.OutDir, // useShellExecute: true, // createNoWindow: true); testProc.WaitForExit(); result.Execution = stopwatch.Elapsed; Console.WriteLine("====================================="); Console.WriteLine("Program exited with code " + testProc.ExitCode); } } } return(result); }