public void TestRemove() { using var flagFile = new TemporaryFile("0install-unit-tests"); File.WriteAllText(flagFile, "/dir1/file1\n/dir2/file2\n"); FlagUtils.Remove(flagFile, "dir"); File.ReadAllText(flagFile).Should().Be("/dir1/file1\n/dir2/file2\n", because: "Partial match should not change anything"); FlagUtils.Remove(flagFile, Path.Combine("dir1", "file1")); File.ReadAllText(flagFile).Should().Be("/dir2/file2\n"); FlagUtils.Remove(flagFile, "dir2"); File.ReadAllText(flagFile).Should().Be(""); }
/// <summary> /// Applies a <see cref="RemoveStep"/> to a <see cref="TemporaryDirectory"/>. /// </summary> /// <param name="step">The <see cref="RemoveStep"/> to apply.</param> /// <param name="workingDir">The <see cref="TemporaryDirectory"/> to apply the changes to.</param> /// <exception cref="IOException">A path specified in <paramref name="step"/> is illegal.</exception> public static void Apply(this RemoveStep step, TemporaryDirectory workingDir) { #region Sanity checks if (step == null) { throw new ArgumentNullException(nameof(step)); } if (workingDir == null) { throw new ArgumentNullException(nameof(workingDir)); } #endregion if (string.IsNullOrEmpty(step.Path)) { throw new IOException(string.Format(Resources.RecipeInvalidPath, "(empty)")); } string path = FileUtils.UnifySlashes(step.Path); if (FileUtils.IsBreakoutPath(path)) { throw new IOException(string.Format(Resources.RecipeInvalidPath, path)); } string absolutePath = Path.Combine(workingDir, path); if (Directory.Exists(absolutePath)) { Directory.Delete(absolutePath, recursive: true); } else { File.Delete(absolutePath); } // Update in flag files as well FlagUtils.Remove(Path.Combine(workingDir, FlagUtils.XbitFile), path); FlagUtils.Remove(Path.Combine(workingDir, FlagUtils.SymlinkFile), path); }