Skip to content

Commit

Permalink
Issue chucknorris#355: Replace invalid chars in path (chucknorris#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikbra authored Dec 12, 2018
1 parent c4dcdc0 commit bac8885
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
29 changes: 29 additions & 0 deletions product/roundhouse.tests/infrastructure/filesystem/KnownFolders.cs
Original file line number Diff line number Diff line change
@@ -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);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public DotNetFileSystemAccess(ConfigurationPropertyHolder configuration)
}

private ConfigurationPropertyHolder configuration;
private static readonly char[] InvalidPathCharacters = Path.GetInvalidPathChars().Append(':').ToArray();


#region File
Expand Down Expand Up @@ -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;
}

/// <summary>
/// Determines the file extension for a given path to a file
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ public interface FileSystemAccess
/// <param name="file_path">Full path to file including file name</param>
/// <returns>Returns only the file name minus extensions from the filepath</returns>
string get_file_name_without_extension_from(string file_path);

/// <summary>
/// Removes invalid characters from a path segment
/// </summary>
/// <param name="path_segment">segment of path to clean</param>
/// <returns>The path with all illegal characters stripped away</returns>
string remove_invalid_characters_from(string path_segment);


/// <summary>
/// Determines the file extension for a given path to a file
Expand Down

0 comments on commit bac8885

Please sign in to comment.