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
57 changes: 57 additions & 0 deletions CosmosDBShell.Tests/CommandTests/ConnectCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace CosmosShell.Tests.CommandTests;

using Azure.Data.Cosmos.Shell.Commands;
using Azure.Data.Cosmos.Shell.Core;
using Azure.Data.Cosmos.Shell.Lsp.Semantics;
using Azure.Data.Cosmos.Shell.Parser;
using Microsoft.Azure.Cosmos;

public class ConnectCommandTests
Expand All @@ -21,4 +24,58 @@ await Assert.ThrowsAnyAsync<OperationCanceledException>(() => shell.ConnectAsync
mode: ConnectionMode.Gateway,
token: cancellationTokenSource.Token));
}

[Fact]
public async Task ConnectCommand_VSCodeCredentialOption_BindsHiddenInteractiveFlag()
{
var command = await BindConnectCommandAsync("connect https://example.documents.azure.com:443/ -vscode-credential");

Assert.Equal("https://example.documents.azure.com:443/", command.ConnectionString);
Assert.True(command.UseVSCodeCredential);
}

[Fact]
public async Task ConnectCommand_StartupVSCodeCredentialOptionAlias_BindsHiddenInteractiveFlag()
{
var command = await BindConnectCommandAsync("connect https://example.documents.azure.com:443/ --connect-vscode-credential");

Assert.Equal("https://example.documents.azure.com:443/", command.ConnectionString);
Assert.True(command.UseVSCodeCredential);
}

[Fact]
public void ConnectCommand_VSCodeCredentialOption_IsHiddenButKnownToCommandMetadata()
{
Assert.True(CommandFactory.TryCreateFactory(typeof(ConnectCommand), out var factory));

Assert.DoesNotContain(factory.Options, option => option.MatchesArgument("vscode-credential"));
Assert.Contains(factory.AllOptions, option => option.MatchesArgument("vscode-credential"));
Assert.True(factory.HasOption("vscode-credential"));

using var shell = ShellInterpreter.CreateInstance();
Assert.True(shell.App.IsOptionPrefix("connect", "vscode-credential"));
}

[Fact]
public void ConnectCommand_VSCodeCredentialOption_DoesNotProduceUnknownOptionDiagnostic()
{
const string CommandText = "connect https://example.documents.azure.com:443/ -vscode-credential";
var parser = new StatementParser(CommandText);
var statements = parser.ParseStatements();

var model = new SemanticAnalyzer().Analyze(statements, CommandText);

Assert.DoesNotContain(model.Diagnostics, diagnostic => diagnostic.Code == "SEM002");
}

private static async Task<ConnectCommand> BindConnectCommandAsync(string commandText)
{
var parser = new StatementParser(commandText);
var statement = Assert.IsType<CommandStatement>(Assert.Single(parser.ParseStatements()));

Assert.True(CommandFactory.TryCreateFactory(typeof(ConnectCommand), out var factory));
using var shell = ShellInterpreter.CreateInstance();
var command = await statement.CreateCommandAsync(factory, shell, new CommandState(), CancellationToken.None);
return Assert.IsType<ConnectCommand>(command);
}
}
15 changes: 13 additions & 2 deletions CosmosDBShell/Azure.Data.Cosmos.Shell.Commands/CommandFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public McpAnnotationAttribute? McpAnnotation
/// </summary>
public List<Option> Options { get; } = [];

/// <summary>
/// Gets all options for the command, including options hidden from public surfaces.
/// </summary>
internal List<Option> AllOptions { get; } = [];

/// <summary>
/// Gets a value indicating whether the command is external.
/// </summary>
Expand Down Expand Up @@ -112,7 +117,13 @@ public static bool TryCreateFactory(Type type, out CommandFactory factory)
var optattr = p.GetCustomAttribute<CosmosOptionAttribute>();
if (optattr != null)
{
factory.Options.Add(new Option(p, optattr));
var option = new Option(p, optattr);
factory.AllOptions.Add(option);

if (!optattr.Hidden)
{
factory.Options.Add(option);
}
}
}

Expand Down Expand Up @@ -141,7 +152,7 @@ public CosmosCommand CreateCommand()
/// <returns>True if the option exists; otherwise, false.</returns>
internal bool HasOption(string optionName)
{
foreach (var opt in this.Options)
foreach (var opt in this.AllOptions)
{
if (opt.MatchesArgument(optionName))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ internal partial class ConnectCommand : CosmosCommand
[CosmosOption("managed-identity")]
public string? ManagedIdentityClientId { get; set; }

[CosmosOption("vscode-credential", "connect-vscode-credential", Hidden = true)]
public bool UseVSCodeCredential { get; init; }

public async override Task<CommandState> ExecuteAsync(ShellInterpreter shell, CommandState commandState, string commandText, CancellationToken token)
{
// If no connection string provided, show current connection info
Expand Down Expand Up @@ -82,7 +85,7 @@ public async override Task<CommandState> ExecuteAsync(ShellInterpreter shell, Co

try
{
await shell.ConnectAsync(this.ConnectionString, this.LoginHint, connectionMode, tenantId: this.TenantId, authorityHost: this.AuthorityHost, managedIdentityClientId: this.ManagedIdentityClientId, token: token);
await shell.ConnectAsync(this.ConnectionString, this.LoginHint, connectionMode, tenantId: this.TenantId, authorityHost: this.AuthorityHost, managedIdentityClientId: this.ManagedIdentityClientId, useVSCodeCredential: this.UseVSCodeCredential, token: token);
var returnState = new CommandState
{
IsPrinted = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal bool IsOptionPrefix(string? currentCommand, string name)
return true;
}

return factory.Options.Any(o => o.Name.Any(optionName => optionName.StartsWith(name, StringComparison.OrdinalIgnoreCase)));
return factory.AllOptions.Any(o => o.Name.Any(optionName => optionName.StartsWith(name, StringComparison.OrdinalIgnoreCase)));
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public CosmosOptionAttribute(params string[] name)
public string[] Names { get; }

public object? DefaultValue { get; set; }

public bool Hidden { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private void ValidateCommandOptions(CommandStatement cmd, CommandFactory factory
{
// Collect valid option names (case-insensitive) including all aliases
var valid = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var opt in factory.Options)
foreach (var opt in factory.AllOptions)
{
foreach (var n in opt.Name)
{
Expand Down Expand Up @@ -186,7 +186,7 @@ private void ValidateCommandOptions(CommandStatement cmd, CommandFactory factory
continue;
}

var optDef = factory.Options.FirstOrDefault(o => o.MatchesArgument(optName));
var optDef = factory.AllOptions.FirstOrDefault(o => o.MatchesArgument(optName));
if (optDef != null)
{
if (optDef.IsBool && optExpr.Value != null)
Expand Down
Loading