public void DeleteFileAndEmptyDirectories_Will_Only_Delete_File_If_Sibling_Content_Exists() { var pathLimit = PartialProjectSetupFixture.TestingAreaPath; var basePath = Path.Combine(pathLimit, "Dir1"); var filePath = Path.Combine(basePath, "testfile.txt"); var siblingFilePath = Path.Combine(basePath, "testfile2.txt"); var contentBytes = Encoding.UTF8.GetBytes("namespace SomeNS { public class NewClass {} }"); _projectBuilder.WriteFileBytesToProject(filePath, contentBytes); _projectBuilder.WriteFileBytesToProject(siblingFilePath, contentBytes); _projectBuilder.DeleteFileAndEmptyDirectories(filePath, pathLimit); Assert.False(File.Exists(filePath)); Assert.True(Directory.Exists(basePath)); Assert.True(File.Exists(siblingFilePath)); Directory.Delete(basePath, true); }
public async Task <WebFormsPortingResult> PerformMigration() { LogHelper.LogInformation(string.Format( Constants.StartedOfLogTemplate, GetType().Name, Constants.ProjectMigrationLogAction, _inputProjectPath)); // Order is important here InitializeProjectManagementStructures(); InitializeServices(); InitializeFactories(); // Pass workspace build manager to factory constructor var fileConverterCollection = _fileConverterFactory.BuildMany(_webFormsProjectAnalyzer.GetProjectFileInfo()); var ignorableFileInfo = _webFormsProjectAnalyzer.GetProjectIgnoredFiles(); foreach (var fileInfo in ignorableFileInfo) { _blazorProjectBuilder.DeleteFileAndEmptyDirectories(fileInfo.FullName, _inputProjectPath); } var migrationTasks = fileConverterCollection.Select(fileConverter => // ContinueWith specifies the action to be run after each task completes, // in this case it sends each generated file to the project builder fileConverter.MigrateFileAsync().ContinueWith(generatedFiles => { try { _blazorProjectBuilder.DeleteFileAndEmptyDirectories(fileConverter.FullPath, _inputProjectPath); // It's ok to use Task.Result here because the lambda within // the ContinueWith block only executes once the original task // is complete. Task.Result is also preferred because await // would force our lambda expression to be async foreach (FileInformation generatedFile in generatedFiles.Result) { _blazorProjectBuilder.WriteFileInformationToProject(generatedFile); } } catch (Exception e) { LogHelper.LogError( e, Rules.Config.Constants.WebFormsErrorTag + string.Format(Constants.OperationFailedLogTemplate, GetType().Name, Constants.FileMigrationLogAction)); } } )); // Combines migration tasks into a single task we can await await Task.WhenAll(migrationTasks).ConfigureAwait(false); LogHelper.LogInformation(string.Format(Constants.GenericInformationLogTemplate, GetType().Name, MigrationTasksCompletedLogAction)); WriteServiceDerivedFiles(); var result = new WebFormsPortingResult() { Metrics = _metricsContext.Transform() }; // TODO: Any necessary cleanup or last checks on new project LogHelper.LogInformation(string.Format( Constants.EndedOfLogTemplate, GetType().Name, Constants.ProjectMigrationLogAction, _inputProjectPath)); return(result); }