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

Embeds preview notice message to CLI snippets #1678

Merged
merged 5 commits into from
Jul 24, 2023
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
52 changes: 26 additions & 26 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(expectedCommand, result);
Assert.Equal(notice + Environment.NewLine + expectedCommand, result);
}

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

// Then
Assert.Equal("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(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);
}

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

// Then
Assert.Equal(expectedCommand, result);
Assert.Equal(notice + 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("mgc tests count get", result);
Assert.Equal(notice + Environment.NewLine + "mgc tests count get", result);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// Then
Assert.Equal("mgc reports get-yammer-device-usage-user-detail-with-date get --date {date-id}", result);
Assert.Equal(notice + Environment.NewLine + "mgc reports get-yammer-device-usage-user-detail-with-date get --date {date-id}", result);
}

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

// Then
Assert.Equal("mgc reports get-yammer-device-usage-user-detail-with-date get --date '{date-id}'", result);
Assert.Equal(notice + Environment.NewLine + "mgc reports get-yammer-device-usage-user-detail-with-date get --date '{date-id}'", result);
}

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

// Then
Assert.Equal("mgc drives items delta-with-token get --token '{token-id}' --drive-id {drive-id} --drive-item-id {driveItem-id}", result);
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);
}

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

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

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

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

// Then
Assert.Equal("mgc users messages get --user-id {user-id} --message-id {message-id} --expand \"singleValueExtendedProperties(`$filter=id eq 'XXXX')\"", result);
Assert.Equal(notice + 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 @@ -509,7 +509,7 @@ public async Task GeneratesSnippetsWithFilterQueryOptions()
var result = _generator.GenerateCodeSnippet(snippetModel);

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

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

// Then
Assert.Equal("mgc teams members list --team-id {team-id} --filter \"(microsoft.graph.aadUserConversationMember/displayName eq 'Harry Johnson' or microsoft.graph.aadUserConversationMember/email eq 'admin@M365x987948.OnMicrosoft.com')\"", result);
Assert.Equal(notice + Environment.NewLine + "mgc teams members list --team-id {team-id} --filter \"(microsoft.graph.aadUserConversationMember/displayName eq 'Harry Johnson' or microsoft.graph.aadUserConversationMember/email eq 'admin@M365x987948.OnMicrosoft.com')\"", result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public string GenerateCodeSnippet(SnippetModel snippetModel)
// 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 @@ -47,7 +48,7 @@ public string GenerateCodeSnippet(SnippetModel snippetModel)
// parameters to the parameters dictionary.
ProcessCommandSegmentsAndParameters(snippetModel, ref commandSegments, ref operation, ref parameters);

return commandSegments.Aggregate("", (accum, val) => string.IsNullOrWhiteSpace(accum) ? val : $"{accum} {val}")
return notice + 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