Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DYN-5791 Fixing Renaming Tool #14812

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Copy link
Member

@mjkkirschner mjkkirschner Jan 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why have you only modified this overload of RenameFile / Directory - now the others are inconsistent in behavior? Is there a good reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is only happening when is trying to replace the image name inside the .md file with the hashed file name and this RenameFile(string file, string baseName, string shortName) method is doing that task,

e.g. for ByOriginVectors node the next values will be replaced.
baseName = "Autodesk.DesignScript.Geometry.CoordinateSystem.ByOriginVectors(origin, xAxis, yAxis, zAxis)"
shortName = "KLFVJ5DI7APVNODARMSE3JM5C27DG62OXUUQRS3HCDTZYO6IBOZA"

The other RenameFile(string file) function is calling RenameFile(string file, string baseName, string shortName), then I don't understand what you mean by "inconsistent in behavior".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation- that makes sense, so now all replace methods will eventually call down to this one and have the same behavior with decoding?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if with "now all replace methods" you refer to calling "string.Replace", this line updated just applies to the case of replacing the md file content, there are other calls to "string.Replace" that won't need to use HttpUtility.UrlDecode (like the image below) due that is replacing the file name and file names doesn't have the "%20" char.

There are two test cases for using "NodeDocumentationMarkdownGenerator rename" command, one is when renaming just a file and the other when renaming all the files in a folder. If you have a specific case in mind in which this code could not work, please let me know and I can test it. Thanks

image

Copy link
Member

@mjkkirschner mjkkirschner Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, typo, I meant Rename not replace.
RenameFile
RenameDirectory

I want to understand if this tool will now always do this decoding whenever it replaces the content inside a file while renaming no matter what code path is used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it will do the decoding whenever replaces the content inside a md file.
Here you can see a flow diagram showing that when using "rename" option in both cases will be decoding, I hope this diagram shows the behavior in a more clear way:
image

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
Loading