Skip to content

Improve Az.Tool.Predictor #12922

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

Merged
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<AssemblyName>Microsoft.Azure.PowerShell.AzPredictor.Test</AssemblyName>
<RootNamespace>Microsoft.Azure.PowerShell.AzPredictor.Test</RootNamespace>
<AssemblyName>Microsoft.Azure.PowerShell.Tools.AzPredictor.Test</AssemblyName>
<RootNamespace>Microsoft.Azure.PowerShell.Tools.AzPredictor.Test</RootNamespace>
<IsPackable>false</IsPackable>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<RepoArtifacts>$(MSBuildThisFileDirectory)</RepoArtifacts>
<OutputPath>$(RepoArtifacts)..\..\..\artifacts\Tools\Az.Tools.Predictor.Test\</OutputPath>
<DocumentationFile>$(OutputPath)\Microsoft.Azure.PowerShell.AzPredictor.Tools.Test.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Management.Automation" Version="7.1.0-preview.9" />
<PackageReference Include="System.Management.Automation" Version="7.1.0-preview.7" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
Expand All @@ -22,7 +25,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<ProjectReference Include="..\AzPredictor\AzPredictor.csproj" />
<ProjectReference Include="..\Az.Tools.Predictor\Az.Tools.Predictor.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.PowerShell.AzPredictor.Test.Mocks;
using Microsoft.Azure.PowerShell.Tools.AzPredictor.Test.Mocks;
using System.Management.Automation.Subsystem;
using System.Threading;
using Xunit;

namespace Microsoft.Azure.PowerShell.AzPredictor.Test
namespace Microsoft.Azure.PowerShell.Tools.AzPredictor.Test
{
/// <summary>
/// Tests for <see cref="AzPredictorService"/>
Expand All @@ -41,7 +41,7 @@ public AzPredictorServiceTests(ModelFixture fixture)
this._suggestionsPredictor = new Predictor(this._fixture.PredictionCollection[startHistory]);
this._commandsPredictor = new Predictor(this._fixture.CommandCollection);

this._service = new MockAzPredictorService(this._fixture.PredictionCollection[startHistory], this._fixture.CommandCollection);
this._service = new MockAzPredictorService(startHistory, this._fixture.PredictionCollection[startHistory], this._fixture.CommandCollection);
}


Expand All @@ -63,11 +63,12 @@ public void VerifyUsingSuggestion(string userInput)
var predictionContext = PredictionContext.Create(userInput);
var expected = this._suggestionsPredictor.Query(predictionContext.InputAst, CancellationToken.None);
var actual = this._service.GetSuggestion(predictionContext.InputAst, CancellationToken.None);
Assert.Equal(expected, actual);
Assert.NotNull(actual);
Assert.NotNull(actual.Item1);
Assert.Equal(expected, actual.Item1);
Assert.Equal(PredictionSource.CurrentHistory, actual.Item2);
}


/// <summary>
/// Verifies that when no prediction is in the suggestion list, we'll use the command list.
/// </summary>
Expand All @@ -79,8 +80,10 @@ public void VerifyUsingCommand(string userInput)
var predictionContext = PredictionContext.Create(userInput);
var expected = this._commandsPredictor.Query(predictionContext.InputAst, CancellationToken.None);
var actual = this._service.GetSuggestion(predictionContext.InputAst, CancellationToken.None);
Assert.Equal(expected, actual);
Assert.NotNull(actual);
Assert.NotNull(actual.Item1);
Assert.Equal(expected, actual.Item1);
Assert.Equal(PredictionSource.Commands, actual.Item2);
}

/// <summary>
Expand All @@ -98,7 +101,8 @@ public void VerifyNoPrediction(string userInput)
{
var predictionContext = PredictionContext.Create(userInput);
var actual = this._service.GetSuggestion(predictionContext.InputAst, CancellationToken.None);
Assert.Null(actual);
Assert.Null(actual.Item1);
Assert.Equal(PredictionSource.None, actual.Item2);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.PowerShell.AzPredictor.Test.Mocks;
using Microsoft.Azure.PowerShell.Tools.AzPredictor.Test.Mocks;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation.Subsystem;
using System.Threading;
using Xunit;

namespace Microsoft.Azure.PowerShell.AzPredictor.Test
namespace Microsoft.Azure.PowerShell.Tools.AzPredictor.Test
{
/// <summary>
/// Tests for <see cref="AzPredictor"/>
Expand All @@ -40,7 +40,7 @@ public AzPredictorTests(ModelFixture modelFixture)
this._fixture = modelFixture;
var startHistory = $"{AzPredictorConstants.CommandHistoryPlaceholder}{AzPredictorConstants.CommandConcatenator}{AzPredictorConstants.CommandHistoryPlaceholder}";

this._service = new MockAzPredictorService(this._fixture.PredictionCollection[startHistory], this._fixture.CommandCollection);
this._service = new MockAzPredictorService(startHistory, this._fixture.PredictionCollection[startHistory], this._fixture.CommandCollection);
this._telemetryClient = new MockAzPredictorTelemetryClient();
this._azPredictor = new AzPredictor(this._service, this._telemetryClient);
}
Expand Down Expand Up @@ -142,12 +142,12 @@ public void VerifySuggestion(string userInput)
var actual = this._azPredictor.GetSuggestion(predictionContext, CancellationToken.None);
if (actual == null)
{
Assert.Null(expected);
Assert.Null(expected?.Item1);
}
else
{
Assert.Single(actual);
Assert.Equal(expected, actual.First().SuggestionText);
Assert.Equal(expected.Item1, actual.First().SuggestionText);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

using System.Collections.Generic;

namespace Microsoft.Azure.PowerShell.AzPredictor.Test.Mocks
namespace Microsoft.Azure.PowerShell.Tools.AzPredictor.Test.Mocks
{
/// <summary>
/// Mock <see cref="AzPredictorService"/> so that it doesn't do httpd request to get the commands and predictions.
/// </summary>
public sealed class MockAzPredictorService : AzPredictorService
sealed class MockAzPredictorService : AzPredictorService
{
/// <summary>
/// Gets or sets if a predictions is requested.
Expand All @@ -29,12 +29,14 @@ public sealed class MockAzPredictorService : AzPredictorService
/// <summary>
/// Constructs a new instance of <see cref="MockAzPredictorService"/>
/// </summary>
/// <param name="history">The history that the suggestion is for</param>
/// <param name="suggestions">The suggestions collection</param>
/// <param name="commands">The commands collection</param>
public MockAzPredictorService(IList<string> suggestions, IList<string> commands)
public MockAzPredictorService(string history, IList<string> suggestions, IList<string> commands)
{
SetHistory(history);
SetCommandsPredictor(commands);
SetSuggestionPredictor(suggestions);
SetSuggestionPredictor(history, suggestions);
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

using System.Collections.Generic;

namespace Microsoft.Azure.PowerShell.AzPredictor.Test.Mocks
namespace Microsoft.Azure.PowerShell.Tools.AzPredictor.Test.Mocks
{
sealed class MockAzPredictorTelemetryClient : ITelemetryClient
{
Expand Down Expand Up @@ -46,5 +46,10 @@ public void OnSuggestionAccepted(string acceptedSuggestion)
{
++this.SuggestionAccepted;
}

/// <inheritdoc/>
public void OnGetSuggestion(PredictionSource predictionSource)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
using System.IO.Compression;
using Xunit;

namespace Microsoft.Azure.PowerShell.AzPredictor.Test
namespace Microsoft.Azure.PowerShell.Tools.AzPredictor.Test
{
/// <summary>
/// The class to prepare model for all the tests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
using System.Threading;
using Xunit;

namespace Microsoft.Azure.PowerShell.AzPredictor.Test
namespace Microsoft.Azure.PowerShell.Tools.AzPredictor.Test
{
/// <summary>
/// Test cases for <see cref="Predictor" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30426.262
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AzPredictor", "AzPredictor\AzPredictor.csproj", "{E4A5F697-086C-4908-B90E-A31EE47ECF13}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Az.Tools.Predictor", "Az.Tools.Predictor\Az.Tools.Predictor.csproj", "{E4A5F697-086C-4908-B90E-A31EE47ECF13}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AzPredictor.Test", "AzPredictor.Test\AzPredictor.Test.csproj", "{C7A3ED31-8F41-4643-ADCF-85DF032BD8AC}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Az.Tools.Predictor.Test", "Az.Tools.Predictor.Test\Az.Tools.Predictor.Test.csproj", "{C7A3ED31-8F41-4643-ADCF-85DF032BD8AC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<AssemblyName>Microsoft.Azure.PowerShell.AzPredictor</AssemblyName>
<AssemblyName>Microsoft.Azure.PowerShell.Tools.AzPredictor</AssemblyName>
<RootNamespace>Microsoft.Azure.PowerShell.Tools.AzPredictor</RootNamespace>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<RootNamespace>Microsoft.Azure.PowerShell.AzPredictor</RootNamespace>
<RepoArtifacts>$(MSBuildThisFileDirectory)</RepoArtifacts>
<OutputPath>$(RepoArtifacts)..\..\..\artifacts\Tools\Az.Tools.Predictor\</OutputPath>
</PropertyGroup>
Expand All @@ -15,13 +15,21 @@
</PropertyGroup>

<PropertyGroup>
<DocumentationFile>$(OutputPath)\Microsoft.Azure.PowerShell.AzPredictor.xml</DocumentationFile>
<PackageId>Az.Tools.Predictor</PackageId>
<Version>0.1.0-preview</Version>
<Author>Microsoft Corporation</Author>
<Company>Microsoft Corporation</Company>
<Description>Microsoft Azure PowerShell Predictor: Provide prediction while user types Azure PowerShell commands</Description>
</PropertyGroup>

<PropertyGroup>
<DocumentationFile>$(OutputPath)\Microsoft.Azure.PowerShell.Tools.AzPredictor.xml</DocumentationFile>
<GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.14.0" />
<PackageReference Include="System.Management.Automation" Version="7.1.0-preview.9" />
<PackageReference Include="System.Management.Automation" Version="7.1.0-preview.7" />
<PackageReference Include="Microsoft.Azure.PowerShell.Common" Version="1.3.23-preview" />
<PackageReference Include="Microsoft.Azure.PowerShell.Authentication.Abstractions" Version="1.3.23-preview" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Module manifest for module 'AzPredictor'
# Module manifest for module 'Az.Tools.Predictor'
#
# Generated by: Microsoft Corporation
#
Expand All @@ -8,7 +8,7 @@
@{

# Script module or binary module file associated with this manifest.
#RootModule = './AzPredictor.psm1'
RootModule = ''

# Version number of this module.
ModuleVersion = '0.1.0'
Expand All @@ -29,15 +29,15 @@ CompanyName = 'Microsoft Corporation'
Copyright = 'Microsoft Corporation. All rights reserved.'

# Description of the functionality provided by this module
Description = 'Microsoft Azure PowerShell: Provide prediction while user typing Azure PowerShell commands'
Description = 'Microsoft Azure PowerShell Predictor: Provide prediction while user types Azure PowerShell commands'

# Minimum version of the PowerShell engine required by this module
PowerShellVersion = '7.1'

# Modules that must be imported into the global environment prior to importing this module
#RequiredModules = @(@{ModuleName = 'PSReadLine'; ModuleVersion = '2.0'; })
RequiredModules = @(@{ModuleName = 'Az'; ModuleVersion = '3.0.0'; })

NestedModules = @("Microsoft.Azure.PowerShell.AzPredictor.dll")
NestedModules = @("Microsoft.Azure.PowerShell.Tools.AzPredictor.dll")

# Format files (.ps1xml) to be loaded when importing this module

Expand All @@ -62,7 +62,7 @@ PrivateData = @{
ReleaseNotes = '* the first preview release'

# Prerelease string of this module
# Prerelease = 'beta'
Prerelease = 'beta1'

# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenseAcceptance = $false
Expand All @@ -77,10 +77,4 @@ PrivateData = @{
# HelpInfo URI of this module
# HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''

FunctionsToExport = 'PSConsoleHostPredictor'

}

Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
using System.Management.Automation;
using System.Management.Automation.Language;
using System.Management.Automation.Subsystem;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;

namespace Microsoft.Azure.PowerShell.AzPredictor
[assembly:InternalsVisibleTo("Microsoft.Azure.PowerShell.Tools.AzPredictor.Test")]

namespace Microsoft.Azure.PowerShell.Tools.AzPredictor
{
/// <summary>
/// The implementation of a <see cref="ICommandPredictor"/> to provide suggestion in PSReadLine.
Expand Down Expand Up @@ -132,13 +135,15 @@ public List<PredictiveSuggestion> GetSuggestion(PredictionContext context, Cance
var userInput = context.InputAst.Extent.Text;
var result = _service.GetSuggestion(context.InputAst, cancellationToken);

if (result != null)
if (result?.Item1 != null)
{
cancellationToken.ThrowIfCancellationRequested();
var fullSuggestion = MergeStrings(userInput, result);
var fullSuggestion = MergeStrings(userInput, result.Item1);
return new List<PredictiveSuggestion>() { new PredictiveSuggestion(fullSuggestion) };
}

this._telemetryClient.OnGetSuggestion(result?.Item2 ?? PredictionSource.None);

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.PowerShell.AzPredictor
namespace Microsoft.Azure.PowerShell.Tools.AzPredictor
{
/// <summary>
/// The constants shared in the project.
/// </summary>
public static class AzPredictorConstants
internal static class AzPredictorConstants
{
/// <summary>
/// The value to use when the command isn't an Az command.
Expand Down
Loading