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

Remove 'CLI in preview' warning in cli snippets #2038

Merged
merged 1 commit into from
May 3, 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
54 changes: 27 additions & 27 deletions CodeSnippetsReflection.OpenAPI.Test/GraphCliGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace CodeSnippetsReflection.OpenAPI.LanguageGenerators;
public class GraphCliGeneratorTests : OpenApiSnippetGeneratorTestBase
{
private readonly GraphCliGenerator _generator = new();
private static string notice = "// THE CLI IS IN PREVIEW. NON-PRODUCTION USE ONLY";

public static IEnumerable<object[]> GetSnippetData()
{
return new[] {
Expand All @@ -39,7 +39,7 @@ public async Task GeneratesSnippetsForCommands(HttpMethod method, string url, st
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + expectedCommand, result);
Assert.Equal(Environment.NewLine + expectedCommand, result);
}

[Fact]
Expand All @@ -55,7 +55,7 @@ public async Task GeneratesSnippetsForDeepSubCommand()
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + "mgc users todo lists tasks attachments get --user-id {user-id} --todo-task-list-id {todoTaskList-id} --todo-task-id {todoTask-id} --attachment-base-id {attachmentBase-id}", result);
Assert.Equal(Environment.NewLine + "mgc users todo lists tasks attachments get --user-id {user-id} --todo-task-list-id {todoTaskList-id} --todo-task-id {todoTask-id} --attachment-base-id {attachmentBase-id}", result);
}

[Theory]
Expand All @@ -78,7 +78,7 @@ public void GeneratesSnippetsForConflictingIndexerNavSubCommand(string url, stri
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + expectedCommand, result);
Assert.Equal(Environment.NewLine + expectedCommand, result);
}

// Powershell metadata doesn't have /$count endpoints
Expand All @@ -98,7 +98,7 @@ public void GeneratesSnippetsForCountCommand()
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + "mgc tests count get", result);
Assert.Equal(Environment.NewLine + "mgc tests count get", result);
}

[Fact]
Expand All @@ -114,7 +114,7 @@ public async Task GeneratesSnippetsForBetaCommand()

// Then
// TODO: Check snippet generation support after beta releases.
Assert.Equal(notice + Environment.NewLine + "mgc-beta users list", result);
Assert.Equal(Environment.NewLine + "mgc-beta users list", result);
}

[Fact]
Expand All @@ -130,7 +130,7 @@ public async Task GeneratesSnippetsForRefCommand()
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + "mgc users manager ref get --user-id {user-id}", result);
Assert.Equal(Environment.NewLine + "mgc users manager ref get --user-id {user-id}", result);
}

[Fact]
Expand All @@ -146,7 +146,7 @@ public async Task GeneratesSnippetsForContentCommand()
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + "mgc users photo content get --user-id {user-id}", result);
Assert.Equal(Environment.NewLine + "mgc users photo content get --user-id {user-id}", result);
}

[Fact]
Expand All @@ -161,7 +161,7 @@ public async Task GeneratesSnippetsForGetListCommand()
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + "mgc users list", result);
Assert.Equal(Environment.NewLine + "mgc users list", result);
}

[Theory]
Expand All @@ -186,7 +186,7 @@ public void GeneratesSnippetsForCommandWithConflictingParameterName(string query

// Then
// TODO: What should happen to the query parameter?
Assert.Equal(notice + Environment.NewLine + $"mgc tests results get {commandSuffix}", result);
Assert.Equal(Environment.NewLine + $"mgc tests results get {commandSuffix}", result);
}

[Fact]
Expand All @@ -208,7 +208,7 @@ public void GeneratesSnippetsForCommandWithConflictingParameterNameAndNoLocation

// Then
// TODO: What should happen to the query parameter?
Assert.Equal(notice + Environment.NewLine + "mgc tests results get --id {id}", result);
Assert.Equal(Environment.NewLine + "mgc tests results get --id {id}", result);
}

[Fact]
Expand All @@ -228,7 +228,7 @@ public void GeneratesSnippetsForCommandWithConflictingPathAndCookieParameterName
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + "mgc tests results get --id {id}", result);
Assert.Equal(Environment.NewLine + "mgc tests results get --id {id}", result);
}

[Fact]
Expand All @@ -249,7 +249,7 @@ public void GeneratesSnippetsForCommandWithConflictingPathAndHeaderParameterName
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + "mgc tests results get --id {id} --id-header test-header", result);
Assert.Equal(Environment.NewLine + "mgc tests results get --id {id} --id-header test-header", result);
}

[Theory]
Expand All @@ -266,7 +266,7 @@ public async Task GeneratesSnippetsForCommandWithODataParameters(string queryStr
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + $"mgc users list{commandOptions}", result);
Assert.Equal(Environment.NewLine + $"mgc users list{commandOptions}", result);
}

[Fact]
Expand All @@ -286,7 +286,7 @@ public void GeneratesSnippetsForCommandWithEmptyParameterNames()

// Then
// TODO: What should happen to the query parameter?
Assert.Equal(notice + Environment.NewLine + "mgc tests get", result);
Assert.Equal(Environment.NewLine + "mgc tests get", result);
}

[Fact]
Expand All @@ -306,7 +306,7 @@ public void GeneratesSnippetsForCommandWithQueryParameters()

// Then
// TODO: What should happen to the query parameter?
Assert.Equal(notice + Environment.NewLine + "mgc tests get --id \"10\"", result);
Assert.Equal(Environment.NewLine + "mgc tests get --id \"10\"", result);
}

[Theory]
Expand All @@ -328,7 +328,7 @@ public async Task GeneratesSnippetsForCreateInListCommandWithBody(string content
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + expectedCommand, result);
Assert.Equal(Environment.NewLine + expectedCommand, result);
}

[Theory]
Expand Down Expand Up @@ -364,7 +364,7 @@ public async Task GeneratesSnippetsForCreateInListCommandWithInvalidContent(stri
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + "mgc users create", result);
Assert.Equal(Environment.NewLine + "mgc users create", result);
}

[Fact]
Expand Down Expand Up @@ -405,7 +405,7 @@ public async Task GeneratesEscapedSnippetsForMultilineCommand()
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + "mgc users create --body '{\\\n \"name\": \"test\"\\\n}'", result);
Assert.Equal(Environment.NewLine + "mgc users create --body '{\\\n \"name\": \"test\"\\\n}'", result);
}

[Fact]
Expand All @@ -421,7 +421,7 @@ public async Task GeneratesEscapedBetaSnippetsForMultilineCommand()
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + "mgc-beta users create --body '{\\\n \"name\": \"test\"\\\n}'", result);
Assert.Equal(Environment.NewLine + "mgc-beta users create --body '{\\\n \"name\": \"test\"\\\n}'", result);
}
[Fact]
public async Task GeneratesSnippetsContainingOverLoadedBoundFunctionsWithSingleParameter()
Expand All @@ -435,7 +435,7 @@ public async Task GeneratesSnippetsContainingOverLoadedBoundFunctionsWithSingleP
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + "mgc drives items delta-with-token get --token {token-id} --drive-id {drive-id} --drive-item-id {driveItem-id}", result);
Assert.Equal(Environment.NewLine + "mgc drives items delta-with-token get --token {token-id} --drive-id {drive-id} --drive-item-id {driveItem-id}", result);
}

[Fact]
Expand All @@ -450,7 +450,7 @@ public async Task GeneratesSnippetsContainingOverLoadedBoundFunctionsWithMultipl
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + "mgc drives items get-activities-by-interval-with-start-date-time-with-end-date-time-with-interval get --start-date-time {start-date-time-id} --end-date-time {end-date-time-id} --interval {interval-id} --drive-id {drive-id} --drive-item-id {driveItem-id}", result);
Assert.Equal(Environment.NewLine + "mgc drives items get-activities-by-interval-with-start-date-time-with-end-date-time-with-interval get --start-date-time {start-date-time-id} --end-date-time {end-date-time-id} --interval {interval-id} --drive-id {drive-id} --drive-item-id {driveItem-id}", result);
}

[Fact]
Expand All @@ -465,7 +465,7 @@ public async Task GeneratesSnippetsContainingUnBoundedFunctions()
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + "mgc identity identity-providers available-provider-types get", result);
Assert.Equal(Environment.NewLine + "mgc identity identity-providers available-provider-types get", result);
}

[Fact]
Expand All @@ -480,7 +480,7 @@ public async Task GeneratesSnippetsWithSlashMeEndpoints()
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + "mgc users calendar events list --user-id {user-id} --filter \"startsWith(subject,'All')\"", result);
Assert.Equal(Environment.NewLine + "mgc users calendar events list --user-id {user-id} --filter \"startsWith(subject,'All')\"", result);
}
[Fact]
public async Task GeneratesSnippetsWithExpandQueryOptions()
Expand All @@ -494,7 +494,7 @@ public async Task GeneratesSnippetsWithExpandQueryOptions()
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + "mgc users messages get --user-id {user-id} --message-id {message-id} --expand \"singleValueExtendedProperties(\\$filter=id eq 'XXXX')\"", result);
Assert.Equal(Environment.NewLine + "mgc users messages get --user-id {user-id} --message-id {message-id} --expand \"singleValueExtendedProperties(\\$filter=id eq 'XXXX')\"", result);
}
[Fact]
public async Task GeneratesSnippetsWithFilterQueryOptions()
Expand All @@ -508,7 +508,7 @@ public async Task GeneratesSnippetsWithFilterQueryOptions()
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + "mgc identity-governance access-reviews definitions list --filter \"contains(scope/microsoft.graph.accessReviewQueryScope/query, './members')\"", result);
Assert.Equal(Environment.NewLine + "mgc identity-governance access-reviews definitions list --filter \"contains(scope/microsoft.graph.accessReviewQueryScope/query, './members')\"", result);
}

[Fact]
Expand All @@ -523,6 +523,6 @@ public async Task GeneratesSnippetsForHttpSnippetsWithUrlEncodedValuesForSystemQ
var result = _generator.GenerateCodeSnippet(snippetModel);

// Then
Assert.Equal(notice + Environment.NewLine + "mgc teams members list --team-id {team-id} --filter \"(microsoft.graph.aadUserConversationMember/displayName%20eq%20'Harry%20Johnson'%20or%20microsoft.graph.aadUserConversationMember/email%20eq%20'[email protected]')\"", result);
Assert.Equal(Environment.NewLine + "mgc teams members list --team-id {team-id} --filter \"(microsoft.graph.aadUserConversationMember/displayName%20eq%20'Harry%20Johnson'%20or%20microsoft.graph.aadUserConversationMember/email%20eq%20'[email protected]')\"", result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
// List has an initial capacity of 4. Reserve more based on the number of nodes.
// Reduces reallocations at the expense of more memory used.
var initialCapacity = Math.Max(snippetModel.PathNodes.Count, 20);
var notice = "// THE CLI IS IN PREVIEW. NON-PRODUCTION USE ONLY";
var commandSegments = new List<string>(initialCapacity)
{
GetCommandName(snippetModel)
Expand All @@ -46,7 +45,7 @@
// parameters to the parameters dictionary.
ProcessCommandSegmentsAndParameters(snippetModel, ref commandSegments, ref operation, ref parameters);

return notice + Environment.NewLine + commandSegments.Aggregate("", (accum, val) => string.IsNullOrWhiteSpace(accum) ? val : $"{accum} {val}")
return Environment.NewLine + commandSegments.Aggregate("", (accum, val) => string.IsNullOrWhiteSpace(accum) ? val : $"{accum} {val}")
.Replace("\n", "\\\n", StringComparison.Ordinal)
.Replace("\r\n", "\\\r\n", StringComparison.Ordinal);
}
Expand Down Expand Up @@ -90,7 +89,7 @@
// Handle path operation conflicts
// GET /users/{user-id}/directReports/graph.orgContact
// GET /users/{user-id}/directReports/{directoryObject-id}/graph.orgContact
if (prevPrevNode is not null && prevNode is not null && prevNode.IsParameter &&

Check warning on line 92 in CodeSnippetsReflection.OpenAPI/LanguageGenerators/GraphCliGenerator.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)

Check warning on line 92 in CodeSnippetsReflection.OpenAPI/LanguageGenerators/GraphCliGenerator.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)
prevPrevNode.Children.TryGetValue(node.Segment, out var prevPrevNodeMatch) &&
node.PathItems.TryGetValue(PathItemsKey, out var nodeDefaultItem) &&
prevPrevNodeMatch.PathItems.TryGetValue(PathItemsKey, out var prevPrevNodeDefaultItem) &&
Expand Down Expand Up @@ -134,7 +133,7 @@

FetchUnBoundFunctions(commandSegments);
ProcessMeSegments(commandSegments, operationName);
if (commandSegments.Any(static u => u.Contains("=")))

Check warning on line 136 in CodeSnippetsReflection.OpenAPI/LanguageGenerators/GraphCliGenerator.cs

View workflow job for this annotation

GitHub Actions / Build

Collection-specific "Exists" method should be used instead of the "Any" extension. (https://rules.sonarsource.com/csharp/RSPEC-6605)

Check warning on line 136 in CodeSnippetsReflection.OpenAPI/LanguageGenerators/GraphCliGenerator.cs

View workflow job for this annotation

GitHub Actions / Build

Collection-specific "Exists" method should be used instead of the "Any" extension. (https://rules.sonarsource.com/csharp/RSPEC-6605)
FetchOverLoadedBoundFunctions(commandSegments, operationName, snippetModel);

}
Expand Down Expand Up @@ -162,7 +161,7 @@
/// </summary>
/// <param name="commandSegments"></param>
/// <param name="operationName"></param>
private static void FetchOverLoadedBoundFunctions(List<string> commandSegments, string operationName, SnippetModel snippetModel)

Check warning on line 164 in CodeSnippetsReflection.OpenAPI/LanguageGenerators/GraphCliGenerator.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this unused method parameter 'snippetModel'. (https://rules.sonarsource.com/csharp/RSPEC-1172)

Check warning on line 164 in CodeSnippetsReflection.OpenAPI/LanguageGenerators/GraphCliGenerator.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this unused method parameter 'snippetModel'. (https://rules.sonarsource.com/csharp/RSPEC-1172)
{
int boundedFunctionIndex = commandSegments.FindIndex(static u => overloadedBoundedFunctionWithSingleOrMultipleParameters.IsMatch(u) || overloadedBoundedHyphenatedFunctionWithSingleOrMultipleParameters.IsMatch(u));

Expand Down Expand Up @@ -409,7 +408,7 @@
return null;
}

var payload = (snippetModel.ContentType?.Split(';').First().ToLowerInvariant()) switch

Check warning on line 411 in CodeSnippetsReflection.OpenAPI/LanguageGenerators/GraphCliGenerator.cs

View workflow job for this annotation

GitHub Actions / Build

Indexing at 0 should be used instead of the "Enumerable" extension method "First" (https://rules.sonarsource.com/csharp/RSPEC-6608)

Check warning on line 411 in CodeSnippetsReflection.OpenAPI/LanguageGenerators/GraphCliGenerator.cs

View workflow job for this annotation

GitHub Actions / Build

Indexing at 0 should be used instead of the "Enumerable" extension method "First" (https://rules.sonarsource.com/csharp/RSPEC-6608)
{
// Do other types of content exist that can be handled by the body parameter? Currently, JSON, plain text are supported
"application/json" or "text/plain" => $"--body '{snippetModel.RequestBody}'",
Expand Down
Loading