Skip to content
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -953,4 +953,10 @@ The header is followed by the list of parameters and their errors (might be seve
<data name="PostAction_ModifyJson_Verbose_AttemptingToFindJsonFile" xml:space="preserve">
<value>Attempting to find json file '{0}' in '{1}'</value>
</data>
</root>
<data name="PostAction_ModifyJson_Error_NullJson" xml:space="preserve">
<value>The result of parsing the following JSON was 'null':

{0}</value>
<comment>{0} is the JSON that is being parsed.</comment>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ internal class AddJsonPropertyPostActionProcessor : PostActionProcessorBase
private const string ParentPropertyPathArgument = "parentPropertyPath";
private const string NewJsonPropertyNameArgument = "newJsonPropertyName";
private const string NewJsonPropertyValueArgument = "newJsonPropertyValue";
private const string DetectRepoRootForFileCreation = "detectRepositoryRootForFileCreation";
private const string DetectRepoRoot = "detectRepositoryRoot";
private const string IncludeAllDirectoriesInSearch = "includeAllDirectoriesInSearch";
private const string IncludeAllParentDirectoriesInSearch = "includeAllParentDirectoriesInSearch";

private static readonly JsonSerializerOptions SerializerOptions = new()
{
Expand Down Expand Up @@ -87,7 +89,33 @@ protected override bool ProcessInternal(
return false;
}

IReadOnlyList<string> jsonFiles = FindFilesInCurrentFolderOrParentFolder(environment.Host.FileSystem, outputBasePath, jsonFileName);
if (!bool.TryParse(action.Args.GetValueOrDefault(DetectRepoRoot, "false"), out bool detectRepoRoot))
{
Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, DetectRepoRoot));
return false;
}

if (!bool.TryParse(action.Args.GetValueOrDefault(IncludeAllDirectoriesInSearch, "true"), out bool includeAllDirectories))
{
Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, IncludeAllDirectoriesInSearch));
return false;
}

string? repoRoot = detectRepoRoot ? GetRootDirectory(environment.Host.FileSystem, outputBasePath) : null;

if (!bool.TryParse(action.Args.GetValueOrDefault(IncludeAllParentDirectoriesInSearch, "false"), out bool includeAllParentDirectories))
{
Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, IncludeAllParentDirectoriesInSearch));
return false;
}

IReadOnlyList<string> jsonFiles = FindFilesInCurrentFolderOrParentFolder(
environment.Host.FileSystem,
outputBasePath,
jsonFileName,
includeAllDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly,
includeAllParentDirectories ? int.MaxValue : 1,
repoRoot);

if (jsonFiles.Count == 0)
{
Expand All @@ -103,13 +131,7 @@ protected override bool ProcessInternal(
return false;
}

if (!bool.TryParse(action.Args.GetValueOrDefault(DetectRepoRootForFileCreation, "false"), out bool detectRepoRoot))
{
Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, DetectRepoRootForFileCreation));
return false;
}

string newJsonFilePath = Path.Combine(detectRepoRoot ? GetRootDirectory(environment.Host.FileSystem, outputBasePath) : outputBasePath, jsonFileName);
string newJsonFilePath = Path.Combine(repoRoot ?? outputBasePath, jsonFileName);
environment.Host.FileSystem.WriteAllText(newJsonFilePath, "{}");
jsonFiles = new List<string> { newJsonFilePath };
}
Expand Down Expand Up @@ -150,17 +172,19 @@ protected override bool ProcessInternal(

private static JsonNode? AddElementToJson(IPhysicalFileSystem fileSystem, string targetJsonFile, string? propertyPath, string propertyPathSeparator, string newJsonPropertyName, string newJsonPropertyValue, IPostAction action)
{
JsonNode? jsonContent = JsonNode.Parse(fileSystem.ReadAllText(targetJsonFile), nodeOptions: null, documentOptions: DeserializerOptions);
var fileContent = fileSystem.ReadAllText(targetJsonFile);
JsonNode? jsonContent = JsonNode.Parse(fileContent, nodeOptions: null, documentOptions: DeserializerOptions);

if (jsonContent == null)
{
Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_NullJson, fileContent));
return null;
}

if (!bool.TryParse(action.Args.GetValueOrDefault(AllowPathCreationArgument, "false"), out bool createPath))
{
Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, AllowPathCreationArgument));
return false;
return null;
}

JsonNode? parentProperty = FindJsonNode(jsonContent, propertyPath, propertyPathSeparator, createPath);
Expand Down Expand Up @@ -216,7 +240,10 @@ protected override bool ProcessInternal(
private static string[] FindFilesInCurrentFolderOrParentFolder(
IPhysicalFileSystem fileSystem,
string startPath,
string matchPattern)
string matchPattern,
SearchOption searchOption,
int maxUpLevels,
string? repoRoot)
{
string? directory = fileSystem.DirectoryExists(startPath) ? startPath : Path.GetDirectoryName(startPath);

Expand All @@ -230,17 +257,24 @@ private static string[] FindFilesInCurrentFolderOrParentFolder(
do
{
Reporter.Verbose.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Verbose_AttemptingToFindJsonFile, matchPattern, directory));
string[] filesInDir = fileSystem.EnumerateFileSystemEntries(directory, matchPattern, SearchOption.AllDirectories).ToArray();
string[] filesInDir = fileSystem.EnumerateFileSystemEntries(directory, matchPattern, searchOption).ToArray();

if (filesInDir.Length > 0)
{
return filesInDir;
}

if (repoRoot is not null && directory == repoRoot)
{
// The post action wants to detect the "repo root".
// We have already processed up to the repo root and didn't find any matching files, so we shouldn't go up any further.
return Array.Empty<string>();
}

directory = Path.GetPathRoot(directory) != directory ? Directory.GetParent(directory)?.FullName : null;
numberOfUpLevels++;
}
while (directory != null && numberOfUpLevels <= 1);
while (directory != null && numberOfUpLevels <= maxUpLevels);

return Array.Empty<string>();
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@
"parentPropertyPath": "test",
"newJsonPropertyName": "runner",
"newJsonPropertyValue": "Microsoft.Testing.Platform",
"detectRepositoryRootForFileCreation": true
"detectRepositoryRoot": true,
"includeAllDirectoriesInSearch": false,
"includeAllParentDirectoriesInSearch": true
},
"continueOnError": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@
"parentPropertyPath": "test",
"newJsonPropertyName": "runner",
"newJsonPropertyValue": "Microsoft.Testing.Platform",
"detectRepositoryRootForFileCreation": true
"detectRepositoryRoot": true,
"includeAllDirectoriesInSearch": false,
"includeAllParentDirectoriesInSearch": true
},
"continueOnError": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@
"parentPropertyPath": "test",
"newJsonPropertyName": "runner",
"newJsonPropertyValue": "Microsoft.Testing.Platform",
"detectRepositoryRootForFileCreation": true
"detectRepositoryRoot": true,
"includeAllDirectoriesInSearch": false,
"includeAllParentDirectoriesInSearch": true
},
"continueOnError": true
},
Expand Down
Loading
Loading