public void GetAllPathsMoreThanXAndLessThanYCharacters() { // Get a length that doesn't include all paths, and then get the list of paths that should match the length condition. int minPathLength = _filesFixture.AllPaths.Min(p => p.Length) + 1; int maxPathLength = _filesFixture.AllPaths.Max(p => p.Length) - 1; var expectedPaths = _filesFixture.AllPaths.Where(p => p.Length >= minPathLength && p.Length <= maxPathLength); // Setup the Search Options var searchOptions = new PathLengthSearchOptions() { RootDirectory = _filesFixture.RootPath, SearchOption = SearchOption.AllDirectories, TypesToGet = FileSystemTypes.All, SearchPattern = string.Empty, RootDirectoryReplacement = null, MinimumPathLength = minPathLength, MaximumPathLength = maxPathLength }; // Because we try and get a list of paths. var paths = PathLengthChecker.GetPathsWithLengths(searchOptions, CancellationToken.None); // We should have all of the paths. paths.Should().Contain(expectedPaths); // And we should not have any extra paths. paths.Should().OnlyContain(p => expectedPaths.Contains(p)); }
/// <summary> /// Gets the paths and displays them on the UI. /// </summary> private void GetPaths() { var rootDirectory = txtRootDirectory.Text.Trim(); var rootDirectoryReplacement = txtReplaceRootDirectory.Text.Trim(); var searchPattern = txtSearchPattern.Text; int minPathLength = numMinPathLength.Value ?? 0; int maxPathLength = numMaxPathLength.Value ?? 999999; if (!Directory.Exists(rootDirectory)) { MessageBox.Show(string.Format("The Root Directory \"{0}\" does not exist. Please specify a valid directory.", rootDirectory), "Invalid Root Directory"); return; } // Clear any previous paths out. this.Paths = new List <PathInfo>(); txtNumberOfPaths.Text = string.Empty; txtMinAndMaxPathLengths.Text = string.Empty; // If we should NOT be replacing the Root Directory text, make sure we don't pass anything in for that parameter. if (!(chkReplaceRootDirectory.IsChecked ?? false)) { rootDirectoryReplacement = null; } // Build the options to search with. var searchOptions = new PathLengthSearchOptions() { RootDirectory = rootDirectory, SearchPattern = searchPattern, SearchOption = (chkIncludeSubdirectories.IsChecked ?? false) ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly, TypesToGet = (FileSystemTypes)cmbTypesToInclude.SelectedValue, RootDirectoryReplacement = rootDirectoryReplacement, MinimumPathLength = minPathLength, MaximumPathLength = maxPathLength }; try { // Get and display the new paths. Paths = PathLengthChecker.PathLengthChecker.GetPathsWithLengths(searchOptions).ToList(); // Display the number of paths found. txtNumberOfPaths.Text = Paths.Count + " Paths Found"; // Display the shortest and longest path lengths. int shortestPathLength = Paths.Count > 0 ? Paths.Min(p => p.Length) : 0; int longestPathLength = Paths.Count > 0 ? Paths.Max(p => p.Length) : 0; txtMinAndMaxPathLengths.Text = string.Format("Shortest Path: {0}, Longest Path: {1} characters", shortestPathLength, longestPathLength); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error Occurred"); } }
public void MinimumPathLengthGreaterThanMaximumPathLengthSoShouldThrowException() { // Setup the Search Options var searchOptions = new PathLengthSearchOptions() { RootDirectory = _filesFixture.RootPath, SearchOption = SearchOption.AllDirectories, TypesToGet = FileSystemTypes.All, SearchPattern = string.Empty, RootDirectoryReplacement = null, MinimumPathLength = 2, MaximumPathLength = 1 }; // Because we try and get a list of paths from a directory that does not exist. Action act = () => PathLengthChecker.GetPathsWithLengths(searchOptions, CancellationToken.None).Count(); // A DirectoryNotFound exception should be thrown. act.Should().Throw <MinPathLengthGreaterThanMaxPathLengthException>(); }
public void GetAllFilesFromEmptyDirectory() { // Setup the Search Options var searchOptions = new PathLengthSearchOptions() { RootDirectory = _filesFixture.EmptyDirectoryPath, SearchOption = SearchOption.AllDirectories, TypesToGet = FileSystemTypes.All, SearchPattern = string.Empty, RootDirectoryReplacement = null, MinimumPathLength = -1, MaximumPathLength = -1 }; // Because we try and get a list of paths from an empty directory. var paths = PathLengthChecker.GetPathsWithLengths(searchOptions, CancellationToken.None); // There shouldn't be any paths found. paths.Should().HaveCount(0); }
public void InvalidDirectorySoShouldThrowException() { // Setup the Search Options var searchOptions = new PathLengthSearchOptions() { RootDirectory = Path.Combine(RootPath, "ADirectoryThatDoesNotExist"), SearchOption = SearchOption.AllDirectories, TypesToGet = FileSystemTypes.All, SearchPattern = string.Empty, RootDirectoryReplacement = null, MinimumPathLength = -1, MaximumPathLength = -1 }; // Because we try and get a list of paths from a directory that does not exist. Action act = () => PathLengthChecker.GetPathsWithLengths(searchOptions).Count(); // A DirectoryNotFound exception should be thrown. act.ShouldThrow <DirectoryNotFoundException>(); }
public void ReplacingTheStartingDirectoryAndUsingUrlEncodingShouldAlterThePathsProperly() { // Setup the Search Options var newRootDirectoryName = "NewRootDirectory"; var searchOptions = new PathLengthSearchOptions() { RootDirectory = _filesFixture.RootPath, SearchOption = SearchOption.AllDirectories, TypesToGet = FileSystemTypes.All, SearchPattern = string.Empty, RootDirectoryReplacement = newRootDirectoryName, MinimumPathLength = -1, MaximumPathLength = -1, UrlEncodePaths = true }; var expectedPaths = _filesFixture.AllPaths.Select(p => { return(new PathInfo() { Path = p.Path.Replace(_filesFixture.RootPath, newRootDirectoryName) .Replace(" ", "%20") .Replace(@"\", "%5C") .Replace(":", "%3A") }); }); // Act. var paths = PathLengthChecker.GetPathsWithLengths(searchOptions, CancellationToken.None); // We should not have any of the original paths. paths.Should().NotContain(_filesFixture.AllPaths); // We should have the expected transformed paths. paths.Should().Contain(expectedPaths); // And we should not have any extra paths. paths.Should().OnlyContain(p => expectedPaths.Contains(p)); }
public void GetAllFilesInTopLevelDirectory() { // Setup the Search Options var searchOptions = new PathLengthSearchOptions() { RootDirectory = _filesFixture.RootPath, SearchOption = SearchOption.TopDirectoryOnly, TypesToGet = FileSystemTypes.Files, SearchPattern = string.Empty, RootDirectoryReplacement = null, MinimumPathLength = -1, MaximumPathLength = -1 }; // Because we try and get a list of paths. var paths = PathLengthChecker.GetPathsWithLengths(searchOptions, CancellationToken.None); // We should have all of the file paths directly off of the Root Path. paths.Should().Contain(p => string.Equals(Path.GetDirectoryName(p.Path), _filesFixture.RootPath)); // And we should not have any extra paths. paths.Should().OnlyContain(p => string.Equals(Path.GetDirectoryName(p.Path), _filesFixture.RootPath)); }
public void GetAllDirectories() { // Setup the Search Options var searchOptions = new PathLengthSearchOptions() { RootDirectory = _filesFixture.RootPath, SearchOption = SearchOption.AllDirectories, TypesToGet = FileSystemTypes.Directories, SearchPattern = string.Empty, RootDirectoryReplacement = null, MinimumPathLength = -1, MaximumPathLength = -1 }; // Because we try and get a list of directory paths. var paths = PathLengthChecker.GetPathsWithLengths(searchOptions, CancellationToken.None); // We should have all of the directory paths. paths.Should().Contain(_filesFixture.Directories); // And we should not have any extra paths. paths.Should().OnlyContain(p => _filesFixture.Directories.Contains(p)); }