From bac88855e93ccaeab2f7517d5472d69aa74c869c Mon Sep 17 00:00:00 2001 From: "Erik A. Brandstadmoen" Date: Thu, 13 Dec 2018 00:22:26 +0100 Subject: [PATCH] Issue #355: Replace invalid chars in path (#356) --- .../infrastructure/filesystem/KnownFolders.cs | 29 +++++++++++++++++++ .../builders/KnownFoldersBuilder.cs | 9 ++++-- .../filesystem/DotNetFileSystemAccess.cs | 11 +++++++ .../filesystem/FileSystemAccess.cs | 8 +++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 product/roundhouse.tests/infrastructure/filesystem/KnownFolders.cs diff --git a/product/roundhouse.tests/infrastructure/filesystem/KnownFolders.cs b/product/roundhouse.tests/infrastructure/filesystem/KnownFolders.cs new file mode 100644 index 00000000..9d5a3dc6 --- /dev/null +++ b/product/roundhouse.tests/infrastructure/filesystem/KnownFolders.cs @@ -0,0 +1,29 @@ +using NUnit.Framework; +using roundhouse.consoles; +using roundhouse.infrastructure.app; +using roundhouse.infrastructure.app.builders; +using roundhouse.infrastructure.filesystem; + +namespace roundhouse.tests.infrastructure.filesystem +{ + [TestFixture()] + public class KnownFolders + { + [Test()] + public void should_never_contain_colons() + { + var config = new DefaultConfiguration + { + DatabaseName = "DaDatabase", + OutputPath = "/tmp/rh", + ServerName = "tcp:database.domain.domain" + }; + var fileSystem = new DotNetFileSystemAccess(config); + + var known_folders = KnownFoldersBuilder.build(fileSystem, config); + + StringAssert.DoesNotContain(":", known_folders.change_drop.folder_full_path); + } + + } +} \ No newline at end of file diff --git a/product/roundhouse/infrastructure.app/builders/KnownFoldersBuilder.cs b/product/roundhouse/infrastructure.app/builders/KnownFoldersBuilder.cs index 0d25b6bf..d26517df 100644 --- a/product/roundhouse/infrastructure.app/builders/KnownFoldersBuilder.cs +++ b/product/roundhouse/infrastructure.app/builders/KnownFoldersBuilder.cs @@ -28,8 +28,8 @@ public static KnownFolders build(FileSystemAccess file_system, ConfigurationProp Folder change_drop_folder = new DefaultFolder(file_system, combine_items_into_one_path(file_system, configuration_property_holder.OutputPath, "migrations", - remove_paths_from(configuration_property_holder.DatabaseName,file_system), - remove_paths_from(configuration_property_holder.ServerName,file_system)), + remove_invalid_characters_from(configuration_property_holder.DatabaseName,file_system), + remove_invalid_characters_from(configuration_property_holder.ServerName,file_system)), get_run_date_time_string()); return new DefaultKnownFolders( @@ -59,6 +59,11 @@ private static string remove_paths_from(string name, FileSystemAccess file_syste { return file_system.get_file_name_without_extension_from(name); } + + private static string remove_invalid_characters_from(string path_segment, FileSystemAccess file_system) + { + return file_system.remove_invalid_characters_from(path_segment); + } private static string get_run_date_time_string() { diff --git a/product/roundhouse/infrastructure/filesystem/DotNetFileSystemAccess.cs b/product/roundhouse/infrastructure/filesystem/DotNetFileSystemAccess.cs index ef3a3f6e..97717329 100644 --- a/product/roundhouse/infrastructure/filesystem/DotNetFileSystemAccess.cs +++ b/product/roundhouse/infrastructure/filesystem/DotNetFileSystemAccess.cs @@ -25,6 +25,7 @@ public DotNetFileSystemAccess(ConfigurationPropertyHolder configuration) } private ConfigurationPropertyHolder configuration; + private static readonly char[] InvalidPathCharacters = Path.GetInvalidPathChars().Append(':').ToArray(); #region File @@ -249,6 +250,16 @@ public string get_file_name_without_extension_from(string file_path) return Path.GetFileNameWithoutExtension(file_path); } + public string remove_invalid_characters_from(string path_segment) + { + foreach (var c in InvalidPathCharacters) + { + path_segment = path_segment.Replace(c, '_'); + } + + return path_segment; + } + /// /// Determines the file extension for a given path to a file /// diff --git a/product/roundhouse/infrastructure/filesystem/FileSystemAccess.cs b/product/roundhouse/infrastructure/filesystem/FileSystemAccess.cs index 28a42dd9..80985463 100644 --- a/product/roundhouse/infrastructure/filesystem/FileSystemAccess.cs +++ b/product/roundhouse/infrastructure/filesystem/FileSystemAccess.cs @@ -109,6 +109,14 @@ public interface FileSystemAccess /// Full path to file including file name /// Returns only the file name minus extensions from the filepath string get_file_name_without_extension_from(string file_path); + + /// + /// Removes invalid characters from a path segment + /// + /// segment of path to clean + /// The path with all illegal characters stripped away + string remove_invalid_characters_from(string path_segment); + /// /// Determines the file extension for a given path to a file