public void InitializeLogMgrShouldFunctionCorrectly() { var appFileNameExt = ConfigurationManager.AppSettings["ApplicationLogFileNameExtension"]; var appLogFileNameOnly = PathHelper.ExtractFileNameOnlyComponent(appFileNameExt); var appLogFileExtWithNoLeadingDot = PathHelper.ExtractFileExtensionComponentWithoutLeadingDot(appFileNameExt); var appLogFileTimeStamp = DateHelper.NowYearMthDayHrsSecs(); var cmdOutputLogFilePath = ConfigurationManager.AppSettings["DefaultCommandOutputLogFilePathName"]; var dir1 = new DirectoryDto(cmdOutputLogFilePath); if (dir1.DirInfo.Exists) { dir1.DirInfo.Delete(true); } var logMgr = new ApplicationLogMgr(dir1.DirInfo.FullName, appLogFileNameOnly, appLogFileExtWithNoLeadingDot, appLogFileTimeStamp); logMgr.CreateApplicaitonLogDirectory(); Assert.IsTrue(logMgr.LogDirectoryDto.DirInfo.Exists); DirectoryHelper.DeleteADirectory(logMgr.LogDirectoryDto); logMgr.Dispose(); }
public static bool ChangeToNewCurrentDirectory(DirectoryDto targetDirDto) { if (targetDirDto?.DirInfo == null) { return false; } try { Directory.SetCurrentDirectory(targetDirDto.DirInfo.FullName); } catch (Exception ex) { var err = new FileOpsErrorMessageDto { DirectoryPath = String.Empty, ErrId = 4, ErrorMessage = "Directory Deletion Failed!", ErrSourceMethod = "ChangeToNewCurrentDirectory()", ErrException = ex, FileName = "Target Dir: " + targetDirDto.DirInfo.FullName, LoggerLevel = LogLevel.FATAL }; ErrorMgr.LoggingStatus = ErrorLoggingStatus.On; ErrorMgr.WriteErrorMsg(err); return false; } var newCurrentDirectory = new DirectoryDto(Directory.GetCurrentDirectory()); return targetDirDto == newCurrentDirectory; }
public void LogMgrShouldCorrectlyHandleInitializeToDifferentLogger() { var dir1Dto = new DirectoryDto(".\\logdir"); DirectoryHelper.DeleteADirectory(dir1Dto); var fileStamp = DateHelper.NowYearMthDayHrsSecs(); var defaultLogFileBaseName = ".\\logdir\\installLog.log"; var mgr = new ConsoleCommandLogMgr(defaultLogFileBaseName, fileStamp, string.Empty); Assert.IsTrue(mgr.DefaultConsoleLogPathFileBaseName == defaultLogFileBaseName); Assert.IsTrue(mgr.CurrentConsoleLogPathFileBaseName == defaultLogFileBaseName); var newLogFileBaseName = ".\\logdir\\install2Log.log"; mgr.InitializeCmdConsoleLog(newLogFileBaseName); Assert.IsTrue(mgr.DefaultConsoleLogPathFileBaseName == defaultLogFileBaseName); Assert.IsTrue(mgr.CurrentConsoleLogPathFileBaseName == newLogFileBaseName); mgr.Dispose(); DirectoryHelper.DeleteADirectory(dir1Dto); }
public WorkingDirectoryMgr(string targetDir) { SetCurrentWorkingDirectory(); if (string.IsNullOrWhiteSpace(targetDir) || !SetTargetDirectory(targetDir)) { TargetWorkingDirectory = new DirectoryDto(OriginalCurrentWorkingDirectory.DirInfo.FullName); } if (!DirectoryHelper.IsDirectoryDtoValid(TargetWorkingDirectory)) { var ex = new Exception("TargetWorkingDirectory Dto Invalid!"); var err = new FileOpsErrorMessageDto { DirectoryPath = OriginalCurrentWorkingDirectory.DirInfo.FullName, ErrId = 1, ErrorMessage = "Directory Deletion Failed!", ErrSourceMethod = "Constructor()", ErrException = ex, FileName = string.Empty, LoggerLevel = LogLevel.FATAL }; ErrorMgr.LoggingStatus = ErrorLoggingStatus.On; ErrorMgr.WriteErrorMsg(err); throw ex; } }
public static DirectoryDto GetAppDirWithTrailingSlash() { var appDir = DirectoryHelper.GetCurrentApplicationDirectoryLocation().DirInfo.FullName; var dirDto = new DirectoryDto(PathHelper.AddDefaultTrailingDelimiter(appDir)); return dirDto; }
public void Test001IncompletePathForDtoCreation() { var testStr = @"D:\Level1\Level2\SomefileName.txt"; var expectedResult = @"D:\Level1\Level2"; var dir1 = new DirectoryDto(testStr); Assert.IsTrue(dir1.DirInfo.FullName == expectedResult); }
public static FileDto GetCommandOutputLogFile(DirectoryDto targetDir) { var filePrefix = DateTime.Now.ToString(CultureInfo.InvariantCulture); return new FileDto(targetDir.DirInfo.FullName + "\\" + filePrefix + "_" + AppConstants.DefaultCommandOutputLogFilePathName); }
public void DotDirectoryShouldTranslateIntoFullPath() { var dir1 = new DirectoryDto("."); var dir2 = TestDirectories.GetTestExeDir(); var result = (dir1 == dir2); Assert.IsTrue(result); }
public void Test005CreateFileDtoFromFileAndDirectoryCombination() { var testStr = @"D:\Level1\Level2"; var testDirDto = new DirectoryDto(testStr); var fileComponent = @"SomefileName.txt"; var expectedResult = @"D:\Level1\Level2\SomefileName.txt"; var fileDto = new FileDto(testDirDto, fileComponent); Assert.IsTrue(FileHelper.IsFileDtoValid(fileDto)); Assert.IsTrue(fileDto.FileXinfo.FullName == expectedResult); }
public FileDto(DirectoryDto dirDto, string fileNameAndExtension) { if (dirDto?.DirInfo == null) { SetDtoToEmpty(); return; } ConfigureDto(Path.Combine(dirDto.DirInfo.FullName, StringHelper.TrimStringEnds(fileNameAndExtension))); }
public void T004_XmlCmdFileShouldParseHeaderSuccessfully() { var cmdExeDto = TestDirectories.GetConsoleExecutorDto(); var builder = new XmlParameterBuilder(cmdExeDto); builder.BuildParmsFromXml(); var expectedDirDto = new DirectoryDto(TestDirectories.GetTestExeDir().DirInfo.FullName + "\\installLog"); var dirDto = new DirectoryDto(cmdExeDto.DefaultCommandOutputLogFilePathName); Assert.IsTrue(cmdExeDto.AppLogRetentionInDays == 0); Assert.IsTrue(dirDto.DirInfo.FullName == expectedDirDto.DirInfo.FullName); }
public static FileDto CombineDirSubDirWithFile(DirectoryDto dirDto, string subdirFile) { if (!DirectoryHelper.IsDirectoryDtoValid(dirDto)) { return null; } if (string.IsNullOrWhiteSpace(subdirFile)) { return null; } var dir = PathHelper.AddTrailingDelimiter(dirDto); var subDir = PathHelper.RemovePrefixDelimiter(subdirFile); return new FileDto(dir + subDir); }
public void ShouldExecuteCommandsCorrectly() { var exeDir = TestDirectories.GetTestExeDir(); var logDir = new DirectoryDto(exeDir.DirInfo.FullName + "\\logdir"); DirectoryHelper.DeleteADirectory(logDir); var dirT3 = TestDirectories.GetTestDirectory003(); FileHelper.DeleteAllFilesInDirectoryTree(dirT3); var consoleExeDto = new ConsoleExecutorDto { DefaultConsoleCommandExecutor = TestDirectories.GetConsoleCommandExecutor(), DefaultConsoleCommandExeArgs = TestDirectories.GetConsoleCommandExeArguments(), CmdConsoleLogFileErrorSuffix = "Error", CmdConsoleLogFileTimeStamp = DateHelper.NowYearMthDayHrsSecs(), CommandDefaultTimeOutInMinutes = 5.0M, CommandMaxTimeOutInMinutes = 45.0M, CommandMinTimeOutInMinutes = 1.0M, DefaultCommandOutputLogFilePathName = ".\\logdir\\installLog.log", XmlCmdFileDto = TestDirectories.GetXmlCmdTest002FileDto(), DefaultConsoleCommandType = ConsoleCommandType.Console }; var jobs = TestDirectories.GetTestJobGroup(); var cmdExeMgr = new CommandExecutionMgr(jobs,consoleExeDto); var result = cmdExeMgr.ExecuteCommands(); Assert.IsTrue(result); var files = dirT3.DirInfo.GetFiles(); FileHelper.DeleteAllFilesInDirectoryTree(dirT3); Assert.IsTrue(files.Length == 4); }
private void ConfigureDto(string fileName) { var fName = StringHelper.TrimStringEnds(fileName); if (!ValidateFileInputString(fName)) { var err = new FileOpsErrorMessageDto { DirectoryPath = string.Empty, ErrId = 10, ErrorMessage = "Validation failed on FileDto input file path!", ErrSourceMethod = "ValidateFileInputString()", FileName = fileName, LoggerLevel = LogLevel.ERROR }; ErrorMgr.LoggingStatus = ErrorLoggingStatus.On; ErrorMgr.WriteErrorMsg(err); SetDtoToEmpty(); return; } try { FileXinfo = new FileInfo(fName); DirDto = new DirectoryDto(FileXinfo.DirectoryName); } catch(Exception ex) { var err = new FileOpsErrorMessageDto { DirectoryPath = string.Empty, ErrId = 30, ErrorMessage = "Failue creating FileInfo and DirectoryInfo objects!", ErrSourceMethod = "ConfigureDto()", ErrException = ex, FileName = fileName, LoggerLevel = LogLevel.ERROR }; ErrorMgr.LoggingStatus = ErrorLoggingStatus.On; ErrorMgr.WriteErrorMsg(err); SetDtoToEmpty(); return; } FileExtension = FileXinfo.Extension; FileNameAndExtension = FileXinfo.Name; FilePath = FileXinfo.DirectoryName; FullPathAndFileName = FileXinfo.FullName; IsFileDtoEmpty = false; }
public void TwoDotDirectoryShouldProcessCorrectly() { var dir1 = new DirectoryDto("..\\.."); var targetDir = "CmdrXTests"; var dir2 = TestDirectories.GetTestExeDir(); var idx = dir2.DirInfo.FullName.IndexOf(targetDir, StringComparison.Ordinal); var expectedResult = dir2.DirInfo.FullName.Substring(0, idx + targetDir.Length); Assert.IsTrue(dir1.DirInfo.FullName==expectedResult); }
public static bool IsDirectoryDtoValid(DirectoryDto dirDto) { if (dirDto?.DirInfo == null || string.IsNullOrWhiteSpace(dirDto.DirInfo.FullName) || dirDto.DirInfo.FullName.Length < 3) { return false; } return true; }
public static bool SetCurrentEnvironmentDirectory(DirectoryDto targetDto) { if (targetDto == null || targetDto.DirInfo == null) { return false; } Environment.CurrentDirectory = targetDto.DirInfo.FullName; return GetCurrentEnvironmentDirectory().DirInfo.FullName == targetDto.DirInfo.FullName; }
public static bool DeleteADirectory(DirectoryDto dirDto) { if (dirDto?.DirInfo == null) { return false; } return DeleteADirectory(dirDto.DirInfo.FullName); }
public void Test008DirectoryExtractionFromFileName() { var dirStr1 = @".\installLog\install.log"; // No trailing slash var dirTestExe = TestDirectories.GetTestExeDir(); var expectedDto = new DirectoryDto(dirTestExe.DirInfo.FullName + "\\installLog"); var result = new DirectoryDto(dirStr1); Assert.IsTrue(result.DirInfo.FullName == expectedDto.DirInfo.FullName); }
public static bool DeleteAllFilesInDirectoryTree(DirectoryDto dirDto) { if (!DirectoryHelper.IsDirectoryDtoValid(dirDto)) { return false; } try { foreach (var file in dirDto.DirInfo.GetFiles()) { file.Delete(); } foreach (var dir in dirDto.DirInfo.GetDirectories()) { dir.Delete(true); } } catch { return false; } return true; }
public static bool CreateDirectoryIfNecessary(DirectoryDto dirDto) { if (!IsDirectoryDtoValid(dirDto)) { return false; } if (CreateADirectory(dirDto.DirInfo.FullName)) { dirDto.DirInfo.Refresh(); return true; } return false; }
protected virtual void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!_disposed) { // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { // Dispose managed resources. if (OriginalCurrentWorkingDirectory != null) { OriginalCurrentWorkingDirectory.Dispose(); OriginalCurrentWorkingDirectory = null; } if (TargetWorkingDirectory != null) { TargetWorkingDirectory.Dispose(); TargetWorkingDirectory = null; } } // Note disposing has been done. _disposed = true; } }
public bool SetTargetDirectory(string targetDir) { if (string.IsNullOrWhiteSpace(targetDir)) { return SetTargetDirectory(OriginalCurrentWorkingDirectory); } var dirDto = new DirectoryDto(targetDir); var result = SetTargetDirectory(dirDto); dirDto.Dispose(); return result; }
public void WriteTextLinesToLogMgr() { var dir1Dto = new DirectoryDto(".\\logdir"); DirectoryHelper.DeleteADirectory(dir1Dto); var fileStamp = DateHelper.NowYearMthDayHrsSecs(); var mgr = new ConsoleCommandLogMgr(".\\logdir\\installLog.log", fileStamp, string.Empty); var currWrkDir = TestDirectories.GetTestExeDir(); var subDir = "\\logdir\\installLog" + "_" + fileStamp + ".log"; var expectedResult = FileHelper.CombineDirSubDirWithFile(currWrkDir, subDir); mgr.InitializeCmdConsoleLog(string.Empty); var textOutput = "Hello World!"; mgr.LogWriteLine(textOutput); var expectedText = textOutput+ Environment.NewLine; mgr.LogFlushStreamWriter(); mgr.Dispose(); StreamReader sReader = null; var sb = new StringBuilder(); try { sReader = new StreamReader(expectedResult.FileXinfo.FullName); sb.Append(sReader.ReadToEnd()); sReader.Close(); sReader.Dispose(); } catch { if (sReader != null) { sReader.Close(); sReader.Dispose(); } Assert.IsTrue(false); } var result = sb.ToString(); DirectoryHelper.DeleteADirectory(dir1Dto); Assert.IsTrue(result == expectedText); }
public void WriteLinesToTwoLoggers() { var dir1Dto = new DirectoryDto(".\\logdir"); DirectoryHelper.DeleteADirectory(dir1Dto); var fileStamp = DateHelper.NowYearMthDayHrsSecs(); var mgr = new ConsoleCommandLogMgr(".\\logdir\\installLog.log", fileStamp, string.Empty); var currWrkDir = TestDirectories.GetTestExeDir(); var subDir = "\\logdir\\installLog" + "_" + fileStamp + ".log"; var expectedResult = FileHelper.CombineDirSubDirWithFile(currWrkDir, subDir); mgr.InitializeCmdConsoleLog(string.Empty); var textOutput = "Hello World!"; mgr.LogWriteLine(textOutput); var expectedText = textOutput + Environment.NewLine; mgr.LogFlushStreamWriter(); StreamReader sReader = null; var sb = new StringBuilder(); FileStream fStream = null; try { fStream = new FileStream(expectedResult.FileXinfo.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); sReader = new StreamReader(fStream); sb.Append(sReader.ReadToEnd()); } catch (Exception ex) { // ReSharper disable once UnusedVariable var x = ex.Message; } finally { if (sReader != null) { sReader.Close(); sReader.Dispose(); sReader = null; } if (fStream != null) { fStream.Close(); fStream.Dispose(); fStream = null; } } var result = sb.ToString(); Assert.IsTrue(result == expectedText); subDir = ".\\logdir\\installLog2" + "_" + fileStamp + ".log"; var baseFile = ".\\logdir\\installLog2" +".log"; expectedResult = FileHelper.CombineDirSubDirWithFile(currWrkDir, subDir); mgr.InitializeCmdConsoleLog(baseFile); textOutput = "Hello2 World!"; mgr.LogWriteLine(textOutput); expectedText = textOutput + Environment.NewLine; mgr.LogFlushStreamWriter(); sb = new StringBuilder(); try { fStream = new FileStream(expectedResult.FileXinfo.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); sReader = new StreamReader(fStream); sb.Append(sReader.ReadToEnd()); } catch(Exception ex) { // ReSharper disable once UnusedVariable var y = ex.Message; } finally { if (sReader != null) { sReader.Close(); sReader.Dispose(); } if (fStream != null) { fStream.Close(); fStream.Dispose(); } } result = sb.ToString(); mgr.Dispose(); DirectoryHelper.DeleteADirectory(dir1Dto); Assert.IsTrue(result == expectedText); }
public void SetNewLogFileDirectory(string dirPath) { LogDirectoryDto?.Dispose(); LogDirectoryDto = new DirectoryDto(dirPath); if (!DirectoryHelper.IsDirectoryDtoValid(LogDirectoryDto)) { var msg = "Application Log Directory Dto Invalid!"; var ex = new ArgumentException(msg); var err = new FileOpsErrorMessageDto { ErrId = 35, ErrorMessage = "Application Log Directory Dto Invalid!", ErrSourceMethod = "CreateApplicaitonLogDirectory()", ErrException = ex, DirectoryPath = dirPath, LoggerLevel = LogLevel.FATAL }; ErrorMgr.LoggingStatus = ErrorLoggingStatus.On; ErrorMgr.WriteErrorMsg(err); throw ex; } SetLogFileDto(); }
public bool SetTargetDirectory(DirectoryDto targetDto) { if (!DirectoryHelper.IsDirectoryDtoValid(targetDto)) { return false; } TargetWorkingDirectory?.Dispose(); TargetWorkingDirectory = new DirectoryDto(targetDto.DirInfo.FullName); return true; }
public void SetDefaultCommandExeDirectory(string dir) { if (string.IsNullOrWhiteSpace(dir)) { DefaultCommandExeDirectoryDto = DirectoryHelper.GetCurrentDirectory(); if (!DirectoryHelper.IsDirectoryDtoValid(DefaultCommandExeDirectoryDto)) { var ex = new Exception("Default Command Execution Directory Dto is INVALID!"); var err = new FileOpsErrorMessageDto { DirectoryPath = string.Empty, ErrId = 1, ErrorMessage = ex.Message, ErrSourceMethod = "SetDefaultCommandExeDirectory()", ErrException = ex, FileName = string.Empty, LoggerLevel = LogLevel.FATAL }; ErrorMgr.LoggingStatus = ErrorLoggingStatus.On; ErrorMgr.WriteErrorMsg(err); throw ex; } } DefaultCommandExeDirectoryDto?.Dispose(); DefaultCommandExeDirectoryDto = new DirectoryDto(dir); if (!DirectoryHelper.IsDirectoryDtoValid(DefaultCommandExeDirectoryDto)) { DefaultCommandExeDirectoryDto = DirectoryHelper.GetCurrentDirectory(); if (!DirectoryHelper.IsDirectoryDtoValid(DefaultCommandExeDirectoryDto)) { var ex = new Exception("Default Command Execution Directory Dto is INVALID!"); var err = new FileOpsErrorMessageDto { DirectoryPath = string.Empty, ErrId = 2, ErrorMessage = ex.Message, ErrSourceMethod = "SetDefaultCommandExeDirectory()", ErrException = ex, FileName = string.Empty, LoggerLevel = LogLevel.FATAL }; ErrorMgr.LoggingStatus = ErrorLoggingStatus.On; ErrorMgr.WriteErrorMsg(err); throw ex; } } }
public static DirectoryDto GetMainLibLoadTestDir() { const string testLibDir = "CmdrXTests"; var appDir = DirectoryHelper.GetCurrentApplicationDirectoryLocation().DirInfo.FullName; var idx = appDir.IndexOf(testLibDir, StringComparison.Ordinal); if (idx < 0) { return null; } var testDir = PathHelper.AddDefaultTrailingDelimiter(appDir.Substring(0, idx + testLibDir.Length)); var dirDto = new DirectoryDto(testDir); return dirDto; }
public void SetDefaultCommandOutputLogFilePathName(string filePath) { var dirDto = new DirectoryDto(filePath); if (!DirectoryHelper.IsDirectoryDtoValid(dirDto)) { dirDto.Dispose(); return; } DefaultCommandOutputLogFilePathName = filePath; AppLogDirectory = PathHelper.ExtractDirectoryComponent(DefaultCommandOutputLogFilePathName); }