Skip to content

Commit

Permalink
DYN-5791 Fixing Renaming Tool (#14812)
Browse files Browse the repository at this point in the history
* DYN-5791 Fixing Renaming Tool

If the md file name contains empty spaces then the img file name will also contain %20 instead of empty space so in this way will be loaded by DocumentationBrowser correctly.
So my fix is to remove the %20 when renaming the files and the md file content so matches the expected hashed img name.

* DYN-5791 Fixing Renaming Tool Code Review

Adding unit test that will validate that the files are correctly renamed in a folder and that the md file content is updated correctly.
  • Loading branch information
RobertGlobant20 authored Jan 10, 2024
1 parent 4ada933 commit 3ee9c88
Show file tree
Hide file tree
Showing 5 changed files with 577 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private static void RenameFile(string file)

private static void RenameFile(string file, string baseName, string shortName)
{
var content = File.ReadAllText(file);
var content = HttpUtility.UrlDecode(File.ReadAllText(file));
content = content.Replace(baseName, shortName);
var path = Path.GetDirectoryName(file);
var newFile = Path.Combine(path, shortName + ".md");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,62 @@ public void CanRenameFile()
Assert.IsTrue(content.Contains("CoreNodeModels.HigherOrder.Map"));
}

[Test]
public void CanRenameFileLongName()
{
// Arrange
var originalOutDirName = "fallback_docs";
var filesDirectory = "LongNameFiles";
var emptySpaceChar = "%20";
var originalOutDir = new DirectoryInfo(Path.Combine(toolsTestFilesDirectory, originalOutDirName, filesDirectory));

tempDirectory = CreateTempOutputDirectory();
Assert.That(tempDirectory.Exists);

CopyFilesRecursively(originalOutDir, tempDirectory);

var originalMdFile = tempDirectory.GetFiles("*.md", SearchOption.TopDirectoryOnly)
.Select(x => x.Name).FirstOrDefault();
Assert.IsNotNull(originalMdFile);

//Check that the original MD file contains space characters URL encoded
var originalMdFileContent = Path.Combine(tempDirectory.FullName, originalMdFile);
Assert.IsTrue(File.ReadAllText(originalMdFileContent).Contains(emptySpaceChar));

// Act
var opts = new RenameOptions
{
InputMdDirectory = tempDirectory.FullName,
MaxLength = 90
};

//Rename all the files in the temp directory
RenameCommand.HandleRename(opts);

// Assert
var finalMdFile = tempDirectory.GetFiles("*.md", SearchOption.TopDirectoryOnly)
.Select(x => x.Name).FirstOrDefault();
Assert.IsNotNull(finalMdFile);

var hashedName = Path.GetFileNameWithoutExtension(finalMdFile);

//Validates that all the renamed files start with the hashed name
var allFiles = tempDirectory.GetFiles("*.*", SearchOption.TopDirectoryOnly).Select(x => x.Name);
foreach(var file in allFiles)
{
Assert.IsTrue(file.StartsWith(hashedName));
}

//Get the image file name renamed
var imageFile = tempDirectory.GetFiles("*.jpg", SearchOption.TopDirectoryOnly)
.Select(x => x.Name).FirstOrDefault();
Assert.IsNotNull(imageFile);

//Validates that the image file name is present inside the md file content.
var finalMdFileContent = Path.Combine(tempDirectory.FullName, finalMdFile);
Assert.IsTrue(File.ReadAllText(finalMdFileContent).Contains(imageFile));
}

[Test]
public void CanRenameFilesInADirectory()
{
Expand Down
Loading

0 comments on commit 3ee9c88

Please sign in to comment.