public void CanMoveDirectory() { string dir1 = dllPath.CombineAssert("a"); string dir2 = dllPath.Combine("b"); Assert.That(Directory.Exists(dir2), Is.False); Assert.That(File.Exists(dir2), Is.False, "Lingering files should not be allowed to disrupt the testing."); string aFile = dir1.Combine("file"); File.WriteAllText(aFile, "I should also be moved."); infosCreated.Add(aFile); using (var t = new FileTransaction("moving tx")) { t.Begin(); (t as IDirectoryAdapter).Move(dir1, dir2); Assert.IsFalse(Directory.Exists(dir2), "The directory should not yet exist."); t.Commit(); Assert.That(Directory.Exists(dir2), "Now after committing it should."); infosCreated.Add(dir2); Assert.That(File.Exists(dir2.Combine(Path.GetFileName(aFile))), "And so should the file in the directory."); } }
public void CreateFileTranscationally_Commit() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } string filepath = testFixturePath.CombineAssert("temp").Combine("test"); if (File.Exists(filepath)) { File.Delete(filepath); } infosCreated.Add(filepath); using (var tx = new FileTransaction("Commit TX")) { tx.Begin(); tx.WriteAllText(filepath, "Transactioned file."); tx.Commit(); Assert.That(tx.Status == TransactionStatus.Committed); } Assert.That(File.Exists(filepath), "The file should exists after the transaction."); Assert.That(File.ReadAllLines(filepath)[0], Is.EqualTo("Transactioned file.")); }
public void CreateFileAndReplaceContents() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } string filePath = testFixturePath.CombineAssert("temp").Combine("temp__"); infosCreated.Add(filePath); // simply write something to to file. using (StreamWriter wr = File.CreateText(filePath)) wr.WriteLine("Hello"); using (var tx = new FileTransaction()) { tx.Begin(); using (FileStream fs = (tx as IFileAdapter).Create(filePath)) { byte[] str = new UTF8Encoding().GetBytes("Goodbye"); fs.Write(str, 0, str.Length); fs.Flush(); } tx.Commit(); } Assert.That(File.ReadAllLines(filePath)[0], Is.EqualTo("Goodbye")); }
public void CanDelete_Recursively() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } // 1. Create directory string pr = dllPath.Combine("testing"); Directory.CreateDirectory(pr); Directory.CreateDirectory(pr.Combine("one")); Directory.CreateDirectory(pr.Combine("two")); Directory.CreateDirectory(pr.Combine("three")); // 2. Write contents File.WriteAllLines(Exts.Combine(pr, "one").Combine("fileone"), new[] { "Hello world", "second line" }); File.WriteAllLines(Exts.Combine(pr, "one").Combine("filetwo"), new[] { "two", "second line" }); File.WriteAllLines(Exts.Combine(pr, "two").Combine("filethree"), new[] { "three", "second line" }); // 3. test using (var t = new FileTransaction()) { t.Begin(); Assert.IsTrue((t as IDirectoryAdapter).Delete(pr, true)); t.Commit(); } }
public void CanNotDelete_NonRecursively_NonEmptyDir() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } // 1. create dir and file string dir = dllPath.CombineAssert("testing"); string file = dir.Combine("file"); File.WriteAllText(file, "hello"); // 2. test it using (var t = new FileTransaction("Can not delete non-empty directory")) { IDirectoryAdapter da = t; t.Begin(); Assert.That(da.Delete(dir, false), Is.False, "Did not delete non-empty dir."); IFileAdapter fa = t; fa.Delete(file); Assert.That(da.Delete(dir, false), "After deleting the file in the folder, the folder is also deleted."); t.Commit(); } }
/// <summary> /// use using here? /// </summary> public void CommitTransaction() { if (FileTransaction != null) { FileTransaction.Commit(); } }
private void button1_Click(object sender, System.EventArgs e) { FileTransaction myFileTransaction = new FileTransaction("C:\\Temp\\Path.vii", "TEST"); myFileTransaction.BeginSection(true); myFileTransaction.WriteKey("Key001", "MasterValue001"); myFileTransaction.WriteKey("Key003", "MasterValue003"); myFileTransaction.WriteKey("Key005", "Value005"); myFileTransaction.Commit(); }
public void CanMove_File() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } string folder = dllPath.CombineAssert("testing"); Console.WriteLine(string.Format("Directory \"{0}\"", folder)); string toFolder = dllPath.CombineAssert("testing2"); string file = folder.Combine("file"); Assert.That(File.Exists(file), Is.False); string file2 = folder.Combine("file2"); Assert.That(File.Exists(file2), Is.False); File.WriteAllText(file, "hello world"); File.WriteAllText(file2, "hello world 2"); infosCreated.Add(file); infosCreated.Add(file2); infosCreated.Add(toFolder.Combine("file2")); infosCreated.Add(toFolder.Combine("file")); infosCreated.Add(toFolder); using (var t = new FileTransaction("moving file")) { t.Begin(); Assert.That(File.Exists(toFolder.Combine("file")), Is.False, "Should not exist before move"); Assert.That(File.Exists(toFolder.Combine("file2")), Is.False, "Should not exist before move"); (t as IFileAdapter).Move(file, toFolder); // moving file to folder (t as IFileAdapter).Move(file2, toFolder.Combine("file2")); // moving file to folder+new file name. Assert.That(File.Exists(toFolder.Combine("file")), Is.False, "Should not be visible to the outside"); Assert.That(File.Exists(toFolder.Combine("file2")), Is.False, "Should not be visible to the outside"); t.Commit(); Assert.That(File.Exists(toFolder.Combine("file")), Is.True, "Should be visible to the outside now and since we tried to move it to an existing folder, it should put itself in that folder with its current name."); Assert.That(File.Exists(toFolder.Combine("file2")), Is.True, "Should be visible to the outside now."); } Assert.That(File.ReadAllText(toFolder.Combine("file2")), Is.EqualTo("hello world 2"), "Make sure we moved the contents."); }
/// <summary> /// Installs provided packages /// </summary> /// <param name="packages">Packages to install</param> /// <returns>Task</returns> public async Task InstallPackages(IEnumerable <PackageId> packages) { Logger.Info("Installing packages..."); var productPackages = DeploymentContext.ProductConfigurationProvider .GetPackageConfigurations(DependencyOrdering.BottomToTop) .Select(x => x.Id); var topLevelProductPackages = DeploymentContext.ProductConfigurationProvider .GetPackageConfigurations(DependencyOrdering.TopPackagesOnly) .Select(x => x.Id); var workflowContext = new WorkflowContext(packages, productPackages, topLevelProductPackages); Logger.Info( $"{workflowContext.InputProductPackages.Count} product package(s) discovered and {workflowContext.InputPackages.Count} new package(s) requested"); var workflow = GetDeploymentWorkflow(DeploymentContext, workflowContext, NuGetEngine); await workflow.EnsureAllPackagesAreVersioned(); Logger.Info("Versions are verified and updated when needed"); await workflow.ResolvePackages(); Logger.Info("Packages are resolved"); Logger.Info( $"\t{workflowContext.PackagesForInstallation.Count(x => !workflowContext.PackagesAffectedByUpdate.Contains(x.Id.Id))} package(s) to install"); Logger.Info($"\t{workflowContext.PackagesAffectedByUpdate.Count} package(s) to update"); DeploymentContext.RaiseDeploymentEvent(new BeforeMaintenanceEvent()); workflow.DownloadPackages(PackagesFolderPath); Logger.Info($"All resolved packages are unpacked to {PackagesFolderPath}"); using (var transaction = new FileTransaction()) { workflow.DeletePackages(transaction, workflowContext.ProductPackagesForDeletion, DeploymentContext.ProductConfigurationProvider); Logger.Info($"Uninstalled {workflowContext.ProductPackagesForDeletion.Count} package(s)"); workflow.InstallPackages(transaction, workflowContext.PackagesForInstallation, DeploymentContext.ProductConfigurationProvider, DeploymentContext.PackageConfigurationProvider, DeploymentStartegyProvider); Logger.Info($"Installed {workflowContext.PackagesForInstallation.Count} package(s)"); DeploymentContext.ProductConfigurationProvider.Save(); transaction.Commit(); } DeploymentContext.RaiseDeploymentEvent(new AfterMaintenanceEvent()); Logger.Info("Packages installation complete"); }
public override void RegisterPlug(ITransactionContext context) { Console.WriteLine("Installing AbstractUI.Context"); FileTransaction trans = new FileTransaction(); trans.BeginTransaction(); PlugLocationFactory plugLocFactory = PlugLocationFactory.GetInstance(); plugLocFactory.Context = context as ISqlTransactionContext; string EXEC_DIR = new PlugLocationController(context).LoadByName("EXECUTABLE_DIR").PlugLocationPath; PlugIn plug = new PlugIn(); plug.PlugName = "Interlogic.Trainings.Plugs.AbstractUI.Context"; plug.PlugVersion = "0.0.0.1"; plug.PlugFriendlyName = "AbstractUI.Context"; plug.PlugDescription = "AbstractUI.Context Interfaces"; plug.Active = true; PlugFile file = new PlugFile(); file.PlugFileName = "Interlogic.Trainings.Plugs.AbstractUI.Context.dll"; file.RelativeIncomingPath = @"..\..\..\Interlogic.Trainings.Plugs.AbstractUI.Context\bin\Debug"; file.DestinationPath = "EXECUTABLE_DIR"; plug.Files.Add(file); trans.AddAction(new CopyFileAction(Path.Combine(file.RelativeIncomingPath, file.PlugFileName), Path.Combine(EXEC_DIR, file.PlugFileName), true)); file = new PlugFile(); file.PlugFileName = "Interlogic.Trainings.Plugs.AbstractUI.Context.pdb"; file.RelativeIncomingPath = @"..\..\..\Interlogic.Trainings.Plugs.AbstractUI.Context\bin\Debug"; file.DestinationPath = "EXECUTABLE_DIR"; plug.Files.Add(file); trans.AddAction(new CopyFileAction(Path.Combine(file.RelativeIncomingPath, file.PlugFileName), Path.Combine(EXEC_DIR, file.PlugFileName), true)); try { trans.Execute(); trans.Commit(); PlugInController plugController = new PlugInController(context); plugController.InsertAll(plug); } catch (Exception e) { Console.WriteLine("AbstractUI.Context installation failed"); trans.RollBack(); throw new Exception("AbstractUI.Context Installation Process Failed!", e); } Console.WriteLine("AbstractUI.Context successfully installed"); }
public void NotUsingTransactionScope_IsNotDistributed_AboveNegated() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } using (var tx = new FileTransaction("Not distributed transaction")) { tx.Begin(); Assert.That(tx.IsAmbient, Is.False); tx.Commit(); } }
public void Using_NormalStates() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } using (var tx = new FileTransaction()) { Assert.That(tx.Status, Is.EqualTo(TransactionStatus.NoTransaction)); tx.Begin(); Assert.That(tx.Status, Is.EqualTo(TransactionStatus.Active)); tx.Commit(); Assert.That(tx.Status, Is.EqualTo(TransactionStatus.Committed)); } }
public void CanDelete_NonRecursively_EmptyDir() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } // 1. create dir string dir = dllPath.CombineAssert("testing"); // 2. test it using (var t = new FileTransaction("Can delete empty directory")) { IDirectoryAdapter da = t; t.Begin(); Assert.That(da.Delete(dir, false), "Successfully deleted."); t.Commit(); } }
public void CanCreateDirectory_NLengths_DownInNonExistentDirectory() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } string directoryPath = "testing/apa/apa2"; Assert.That(Directory.Exists(directoryPath), Is.False); using (var t = new FileTransaction()) { t.Begin(); (t as IDirectoryAdapter).Create(directoryPath); t.Commit(); } Assert.That(Directory.Exists(directoryPath)); Directory.Delete(directoryPath); }
public void CreatingFolder_InTransaction_AndCommitting_MeansExistsAfter() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } string directoryPath = "testing"; Assert.That(Directory.Exists(directoryPath), Is.False); using (var tx = new FileTransaction()) { tx.Begin(); (tx as IDirectoryAdapter).Create(directoryPath); tx.Commit(); } Assert.That(Directory.Exists(directoryPath)); Directory.Delete(directoryPath); }
/// <summary> /// Uninstalls provided packages /// </summary> /// <param name="packages">Packages to uninstall</param> /// <returns>Task</returns> public async Task UninstallPackages(IEnumerable <PackageId> packages) { Logger.Info("Uninstalling packages"); var productPackages = DeploymentContext.ProductConfigurationProvider .GetPackageConfigurations(DependencyOrdering.TopToBottom) .Select(x => x.Id); var topLevelProductPackages = DeploymentContext.ProductConfigurationProvider .GetPackageConfigurations(DependencyOrdering.TopPackagesOnly) .Select(x => x.Id); var workflowContext = new WorkflowContext(new PackageId[] {}, productPackages, topLevelProductPackages); var workflow = GetDeploymentWorkflow(DeploymentContext, workflowContext, NuGetEngine); using (var transaction = new FileTransaction()) { workflow.DeletePackages(transaction, packages, DeploymentContext.ProductConfigurationProvider); transaction.Commit(); Logger.Info("Packages uninstalled"); } await SuccessTask; }
public override void RegisterPlug(ITransactionContext context) { FileTransaction trans = new FileTransaction(); trans.BeginTransaction(); PlugLocationFactory plugLocFactory = PlugLocationFactory.GetInstance(); plugLocFactory.Context = context as ISqlTransactionContext; string EXEC_DIR = new PlugLocationController(context).LoadByName("EXECUTABLE_DIR").PlugLocationPath; PlugIn plug = new PlugIn(); plug.PlugName = "Interlogic.Trainings.Plugs.AbstractUI"; plug.PlugVersion = "0.0.0.1"; plug.PlugFriendlyName = "AbstractUI"; plug.PlugDescription = "AbstractUI Interfaces"; plug.Active = true; PlugFile file = new PlugFile(); file.PlugFileName = "Interlogic.Trainings.Plugs.AbstractUI.dll"; file.RelativeIncomingPath = @"..\..\..\Interlogic.Trainings.Plugs.AbstractUI\bin\Debug"; file.DestinationPath = "EXECUTABLE_DIR"; plug.Files.Add(file); trans.AddAction(new CopyFileAction(Path.Combine(file.RelativeIncomingPath, file.PlugFileName), Path.Combine(EXEC_DIR, file.PlugFileName), true)); file = new PlugFile(); file.PlugFileName = "Interlogic.Trainings.Plugs.AbstractUI.pdb"; file.RelativeIncomingPath = @"..\..\..\Interlogic.Trainings.Plugs.AbstractUI\bin\Debug"; file.DestinationPath = "EXECUTABLE_DIR"; plug.Files.Add(file); trans.AddAction(new CopyFileAction(Path.Combine(file.RelativeIncomingPath, file.PlugFileName), Path.Combine(EXEC_DIR, file.PlugFileName), true)); ClassDefinition classDef = new ClassDefinition(); classDef.Active = true; classDef.ClassName = "Interlogic.Trainings.Plugs.AbstractUI.IAbstractComponent"; classDef.FileName = "Interlogic.Trainings.Plugs.AbstractUI.dll"; classDef.ClassDefinitionDescription = "IAbstractComponent public interface"; plug.ClassDefinitions.Add(classDef); classDef = new ClassDefinition(); classDef.Active = true; classDef.ClassName = "Interlogic.Trainings.Plugs.AbstractUI.IAbstractContainer"; classDef.FileName = "Interlogic.Trainings.Plugs.AbstractUI.dll"; classDef.ClassDefinitionDescription = "IAbstractContainer public interface"; plug.ClassDefinitions.Add(classDef); classDef = new ClassDefinition(); classDef.Active = true; classDef.ClassName = "Interlogic.Trainings.Plugs.AbstractUI.INavigationComponent"; classDef.FileName = "Interlogic.Trainings.Plugs.AbstractUI.dll"; classDef.ClassDefinitionDescription = "INavigationComponent public interface"; plug.ClassDefinitions.Add(classDef); classDef = new ClassDefinition(); classDef.Active = true; classDef.ClassName = "Interlogic.Trainings.Plugs.AbstractUI.INavigationListenerComponent"; classDef.FileName = "Interlogic.Trainings.Plugs.AbstractUI.dll"; classDef.ClassDefinitionDescription = "INavigationListenerComponent public interface"; plug.ClassDefinitions.Add(classDef); BindablePointDefinition bpd = new BindablePointDefinition(); bpd.BindablePointName = AbstractUiConstants.IAbstractUiContainer_Controls_BindingPointName; bpd.BindablePointFriendlyName = AbstractUiConstants.IAbstractUiContainer_Controls_BindingPointName; bpd.ClassDefinitionName = "Interlogic.Trainings.Plugs.AbstractUI.IAbstractContainer"; plug.BindablePointDefinitions.Add(bpd); bpd = new BindablePointDefinition(); bpd.BindablePointName = AbstractUiConstants.IAbstractUiNavigationControl_NavigationListeners_BindingPointName; bpd.BindablePointFriendlyName = AbstractUiConstants.IAbstractUiNavigationControl_NavigationListeners_BindingPointName; bpd.ClassDefinitionName = "Interlogic.Trainings.Plugs.AbstractUI.INavigationComponent"; plug.BindablePointDefinitions.Add(bpd); try { trans.Execute(); trans.Commit(); PlugInController plugController = new PlugInController(context); plugController.InsertAll(plug); } catch (Exception e) { trans.RollBack(); throw new Exception("AbstractUI Installation Process Failed!", e); } }
public void TwoTransactions_SameName_FirstSleeps() { var t1_started = new ManualResetEvent(false); var t2_started = new ManualResetEvent(false); var t2_done = new ManualResetEvent(false); Exception e = null; // non transacted thread var t1 = new Thread(() => { try { // modifies the file using (var fs = File.OpenWrite("abb")) { Console.WriteLine("t2 start"); Console.Out.Flush(); t2_started.Set(); // before the transacted thread does Console.WriteLine("t2 wait for t1 to start"); Console.Out.Flush(); t1_started.WaitOne(); fs.Write(new byte[] { 0x1 }, 0, 1); fs.Close(); } } catch (Exception ee) { e = ee; } finally { Console.WriteLine("t2 finally"); Console.Out.Flush(); t2_started.Set(); } }); t1.Start(); using (var t = new FileTransaction()) { t.Begin(); Console.WriteLine("t1 wait for t2 to start"); Console.Out.Flush(); t2_started.WaitOne(); try { Console.WriteLine("t1 started"); // the transacted thread should receive ERROR_TRANSACTIONAL_CONFLICT, but it gets permission denied. using (var fs = (t as IFileAdapter).Create("abb")) { fs.WriteByte(0x2); } } finally { Console.WriteLine("t1 finally"); Console.Out.Flush(); t1_started.Set(); } t.Commit(); } if (e != null) { Console.WriteLine(e); Assert.Fail(e.Message); } }
public void CreateFileTranscationally_Commit() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } string filepath = testFixturePath.CombineAssert("temp").Combine("test"); if (File.Exists(filepath)) File.Delete(filepath); infosCreated.Add(filepath); using (var tx = new FileTransaction("Commit TX")) { tx.Begin(); tx.WriteAllText(filepath, "Transactioned file."); tx.Commit(); Assert.That(tx.Status == TransactionStatus.Committed); } Assert.That(File.Exists(filepath), "The file should exists after the transaction."); Assert.That(File.ReadAllLines(filepath)[0], Is.EqualTo("Transactioned file.")); }