2016-03-31 16:37:40 -07:00
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
2017-06-05 20:51:58 -07:00
using FluentAssertions ;
2016-04-01 13:41:13 -07:00
using Microsoft.DotNet.Cli ;
2017-06-05 20:51:58 -07:00
using Microsoft.DotNet.Cli.Telemetry ;
using Microsoft.DotNet.Cli.Utils ;
2016-03-31 16:37:40 -07:00
using Microsoft.DotNet.Tools.Test.Utilities ;
2017-06-05 20:51:58 -07:00
using System.Collections.Generic ;
2017-09-10 11:47:19 -07:00
using System ;
2016-03-31 16:37:40 -07:00
using Xunit ;
namespace Microsoft.DotNet.Tests
{
2017-06-05 20:51:58 -07:00
public class TelemetryCommandTests : TestBase
2016-03-31 16:37:40 -07:00
{
2017-06-05 20:51:58 -07:00
private readonly FakeRecordEventNameTelemetry _fakeTelemetry ;
2016-04-27 16:04:26 -07:00
public string EventName { get ; set ; }
2017-10-27 10:58:25 -07:00
2017-06-27 13:46:09 -07:00
public IDictionary < string , string > Properties { get ; set ; }
2017-10-27 10:58:25 -07:00
2017-06-05 20:51:58 -07:00
public TelemetryCommandTests ( )
{
_fakeTelemetry = new FakeRecordEventNameTelemetry ( ) ;
TelemetryEventEntry . Subscribe ( _fakeTelemetry . TrackEvent ) ;
2017-10-27 10:58:25 -07:00
TelemetryEventEntry . TelemetryFilter = new TelemetryFilter ( Sha256Hasher . HashWithNormalizedCasing ) ;
2017-06-05 20:51:58 -07:00
}
2016-04-27 16:04:26 -07:00
2017-09-10 11:47:19 -07:00
[Fact]
public void NoTelemetryIfCommandIsInvalid ( )
{
string [ ] args = { "publish" , "-r" } ;
Action a = ( ) = > { Cli . Program . ProcessArgs ( args ) ; } ;
a . ShouldNotThrow < ArgumentOutOfRangeException > ( ) ;
}
[Fact]
public void NoTelemetryIfCommandIsInvalid2 ( )
{
string [ ] args = { "restore" , "-v" } ;
Action a = ( ) = > { Cli . Program . ProcessArgs ( args ) ; } ;
a . ShouldNotThrow < ArgumentOutOfRangeException > ( ) ;
}
2017-06-05 20:51:58 -07:00
[Fact]
public void TopLevelCommandNameShouldBeSentToTelemetry ( )
2016-03-31 16:37:40 -07:00
{
2017-10-27 10:58:25 -07:00
string [ ] args = { "help" } ;
2017-06-05 20:51:58 -07:00
Cli . Program . ProcessArgs ( args ) ;
2017-10-27 10:58:25 -07:00
_fakeTelemetry . LogEntries . Should ( ) . Contain ( e = > e . EventName = = "toplevelparser/command" & &
e . Properties . ContainsKey ( "verb" ) & &
e . Properties [ "verb" ] = = Sha256Hasher . Hash ( "HELP" ) ) ;
2017-06-05 20:51:58 -07:00
}
[Fact]
public void DotnetNewCommandFirstArgumentShouldBeSentToTelemetry ( )
{
const string argumentToSend = "console" ;
2017-10-27 10:58:25 -07:00
string [ ] args = { "new" , argumentToSend } ;
2017-06-05 20:51:58 -07:00
Cli . Program . ProcessArgs ( args ) ;
_fakeTelemetry
. LogEntries . Should ( )
2017-10-27 10:58:25 -07:00
. Contain ( e = > e . EventName = = "sublevelparser/command" & &
e . Properties . ContainsKey ( "argument" ) & &
e . Properties [ "argument" ] = = Sha256Hasher . Hash ( argumentToSend . ToUpper ( ) ) & &
e . Properties . ContainsKey ( "verb" ) & &
e . Properties [ "verb" ] = = Sha256Hasher . Hash ( "NEW" ) ) ;
2016-03-31 16:37:40 -07:00
}
[Fact]
2017-06-05 20:51:58 -07:00
public void DotnetHelpCommandFirstArgumentShouldBeSentToTelemetry ( )
2016-03-31 16:37:40 -07:00
{
2017-06-05 20:51:58 -07:00
const string argumentToSend = "something" ;
2017-10-27 10:58:25 -07:00
string [ ] args = { "help" , argumentToSend } ;
2017-06-05 20:51:58 -07:00
Cli . Program . ProcessArgs ( args ) ;
_fakeTelemetry
. LogEntries . Should ( )
2017-10-27 10:58:25 -07:00
. Contain ( e = > e . EventName = = "sublevelparser/command" & &
e . Properties . ContainsKey ( "argument" ) & &
e . Properties [ "argument" ] = = Sha256Hasher . Hash ( argumentToSend . ToUpper ( ) ) & &
e . Properties . ContainsKey ( "verb" ) & &
e . Properties [ "verb" ] = = Sha256Hasher . Hash ( "HELP" ) ) ;
2017-06-05 20:51:58 -07:00
}
[Fact]
public void DotnetAddCommandFirstArgumentShouldBeSentToTelemetry ( )
{
const string argumentToSend = "package" ;
2017-10-27 10:58:25 -07:00
string [ ] args = { "add" , argumentToSend , "aPackageName" } ;
2017-06-05 20:51:58 -07:00
Cli . Program . ProcessArgs ( args ) ;
_fakeTelemetry
. LogEntries . Should ( )
2017-10-27 10:58:25 -07:00
. Contain ( e = > e . EventName = = "sublevelparser/command" & &
e . Properties . ContainsKey ( "argument" ) & &
e . Properties [ "argument" ] = = Sha256Hasher . Hash ( argumentToSend . ToUpper ( ) ) & &
e . Properties . ContainsKey ( "verb" ) & &
e . Properties [ "verb" ] = = Sha256Hasher . Hash ( "ADD" ) ) ;
2017-06-05 20:51:58 -07:00
}
[Fact]
public void DotnetAddCommandFirstArgumentShouldBeSentToTelemetry2 ( )
{
const string argumentToSend = "reference" ;
2017-10-27 10:58:25 -07:00
string [ ] args = { "add" , argumentToSend , "aPackageName" } ;
2017-06-05 20:51:58 -07:00
Cli . Program . ProcessArgs ( args ) ;
_fakeTelemetry
. LogEntries . Should ( )
2017-10-27 10:58:25 -07:00
. Contain ( e = > e . EventName = = "sublevelparser/command" & &
e . Properties . ContainsKey ( "argument" ) & &
e . Properties [ "argument" ] = = Sha256Hasher . Hash ( argumentToSend . ToUpper ( ) ) & &
e . Properties . ContainsKey ( "verb" ) & &
e . Properties [ "verb" ] = = Sha256Hasher . Hash ( "ADD" ) ) ;
2017-06-05 20:51:58 -07:00
}
[Fact]
public void DotnetRemoveCommandFirstArgumentShouldBeSentToTelemetry ( )
{
const string argumentToSend = "package" ;
2017-10-27 10:58:25 -07:00
string [ ] args = { "remove" , argumentToSend , "aPackageName" } ;
2017-06-05 20:51:58 -07:00
Cli . Program . ProcessArgs ( args ) ;
_fakeTelemetry
. LogEntries . Should ( )
2017-10-27 10:58:25 -07:00
. Contain ( e = > e . EventName = = "sublevelparser/command" & &
e . Properties . ContainsKey ( "argument" ) & &
e . Properties [ "argument" ] = = Sha256Hasher . Hash ( argumentToSend . ToUpper ( ) ) & &
e . Properties . ContainsKey ( "verb" ) & &
e . Properties [ "verb" ] = = Sha256Hasher . Hash ( "REMOVE" ) ) ;
2017-06-05 20:51:58 -07:00
}
[Fact]
public void DotnetListCommandFirstArgumentShouldBeSentToTelemetry ( )
{
const string argumentToSend = "reference" ;
2017-10-27 10:58:25 -07:00
string [ ] args = { "list" , argumentToSend , "aPackageName" } ;
2017-06-05 20:51:58 -07:00
Cli . Program . ProcessArgs ( args ) ;
_fakeTelemetry
. LogEntries . Should ( )
2017-10-27 10:58:25 -07:00
. Contain ( e = > e . EventName = = "sublevelparser/command" & & e . Properties . ContainsKey ( "argument" ) & &
e . Properties [ "argument" ] = = Sha256Hasher . Hash ( argumentToSend . ToUpper ( ) ) & &
e . Properties . ContainsKey ( "verb" ) & &
e . Properties [ "verb" ] = = Sha256Hasher . Hash ( "LIST" ) ) ;
2017-06-05 20:51:58 -07:00
}
[Fact]
public void DotnetSlnCommandFirstArgumentShouldBeSentToTelemetry ( )
{
const string argumentToSend = "list" ;
2017-10-27 10:58:25 -07:00
string [ ] args = { "sln" , "aSolution" , argumentToSend } ;
2017-06-05 20:51:58 -07:00
Cli . Program . ProcessArgs ( args ) ;
_fakeTelemetry
. LogEntries . Should ( )
2017-10-27 10:58:25 -07:00
. Contain ( e = > e . EventName = = "sublevelparser/command" & &
e . Properties . ContainsKey ( "argument" ) & &
e . Properties [ "argument" ] = = Sha256Hasher . Hash ( argumentToSend . ToUpper ( ) ) & &
e . Properties . ContainsKey ( "verb" ) & &
e . Properties [ "verb" ] = = Sha256Hasher . Hash ( "SLN" ) ) ;
2017-06-05 20:51:58 -07:00
}
[Fact]
public void DotnetNugetCommandFirstArgumentShouldBeSentToTelemetry ( )
{
const string argumentToSend = "push" ;
2017-10-31 10:15:11 -07:00
2017-10-31 13:37:21 -07:00
string [ ] args = { "nuget" , argumentToSend } ;
2017-10-31 10:15:11 -07:00
2017-06-05 20:51:58 -07:00
Cli . Program . ProcessArgs ( args ) ;
_fakeTelemetry
. LogEntries . Should ( )
2017-10-27 10:58:25 -07:00
. Contain ( e = > e . EventName = = "sublevelparser/command" & &
e . Properties . ContainsKey ( "argument" ) & &
e . Properties [ "argument" ] = = Sha256Hasher . Hash ( argumentToSend . ToUpper ( ) ) & &
e . Properties . ContainsKey ( "verb" ) & &
e . Properties [ "verb" ] = = Sha256Hasher . Hash ( "NUGET" ) ) ;
2017-06-05 20:51:58 -07:00
}
[Fact]
public void DotnetNewCommandLanguageOpinionShouldBeSentToTelemetry ( )
{
const string optionKey = "language" ;
const string optionValueToSend = "c#" ;
2017-10-27 10:58:25 -07:00
string [ ] args = { "new" , "console" , "--" + optionKey , optionValueToSend } ;
2017-06-05 20:51:58 -07:00
Cli . Program . ProcessArgs ( args ) ;
_fakeTelemetry
. LogEntries . Should ( )
2017-10-27 10:58:25 -07:00
. Contain ( e = > e . EventName = = "sublevelparser/command" & & e . Properties . ContainsKey ( optionKey ) & &
e . Properties [ optionKey ] = = Sha256Hasher . Hash ( optionValueToSend . ToUpper ( ) ) & &
e . Properties . ContainsKey ( "verb" ) & &
e . Properties [ "verb" ] = = Sha256Hasher . Hash ( "NEW" ) ) ;
2017-06-05 20:51:58 -07:00
}
[Fact]
public void AnyDotnetCommandVerbosityOpinionShouldBeSentToTelemetry ( )
{
const string optionKey = "verbosity" ;
const string optionValueToSend = "minimal" ;
2017-10-27 10:58:25 -07:00
string [ ] args = { "restore" , "--" + optionKey , optionValueToSend } ;
2017-06-05 20:51:58 -07:00
Cli . Program . ProcessArgs ( args ) ;
_fakeTelemetry
. LogEntries . Should ( )
2017-10-27 10:58:25 -07:00
. Contain ( e = > e . EventName = = "sublevelparser/command" & &
e . Properties . ContainsKey ( optionKey ) & &
e . Properties [ optionKey ] = = Sha256Hasher . Hash ( optionValueToSend . ToUpper ( ) ) & &
e . Properties . ContainsKey ( "verb" ) & &
e . Properties [ "verb" ] = = Sha256Hasher . Hash ( "RESTORE" ) ) ;
2017-06-05 20:51:58 -07:00
}
[Fact]
public void DotnetBuildAndPublishCommandOpinionsShouldBeSentToTelemetry ( )
{
const string optionKey = "configuration" ;
const string optionValueToSend = "Debug" ;
2017-10-27 10:58:25 -07:00
string [ ] args = { "build" , "--" + optionKey , optionValueToSend } ;
2017-06-05 20:51:58 -07:00
Cli . Program . ProcessArgs ( args ) ;
_fakeTelemetry
. LogEntries . Should ( )
2017-10-27 10:58:25 -07:00
. Contain ( e = > e . EventName = = "sublevelparser/command" & &
e . Properties . ContainsKey ( optionKey ) & &
e . Properties [ optionKey ] = = Sha256Hasher . Hash ( optionValueToSend . ToUpper ( ) ) & &
e . Properties . ContainsKey ( "verb" ) & &
e . Properties [ "verb" ] = = Sha256Hasher . Hash ( "BUILD" ) ) ;
2017-06-05 20:51:58 -07:00
}
[Fact]
public void DotnetPublishCommandRuntimeOpinionsShouldBeSentToTelemetry ( )
{
const string optionKey = "runtime" ;
const string optionValueToSend = "win10-x64" ;
string [ ] args = { "publish" , "--" + optionKey , optionValueToSend } ;
Cli . Program . ProcessArgs ( args ) ;
_fakeTelemetry
. LogEntries . Should ( )
2017-10-27 10:58:25 -07:00
. Contain ( e = > e . EventName = = "sublevelparser/command" & &
e . Properties . ContainsKey ( optionKey ) & &
e . Properties [ optionKey ] = = Sha256Hasher . Hash ( optionValueToSend . ToUpper ( ) ) & &
e . Properties . ContainsKey ( "verb" ) & &
e . Properties [ "verb" ] = = Sha256Hasher . Hash ( "PUBLISH" ) ) ;
2017-06-05 20:51:58 -07:00
}
[Fact]
public void DotnetBuildAndPublishCommandOpinionsShouldBeSentToTelemetryWhenThereIsMultipleOption ( )
{
2017-10-27 10:58:25 -07:00
string [ ] args = { "build" , "--configuration" , "Debug" , "--runtime" , "osx.10.11-x64" } ;
2017-06-05 20:51:58 -07:00
Cli . Program . ProcessArgs ( args ) ;
_fakeTelemetry
. LogEntries . Should ( )
2017-10-27 10:58:25 -07:00
. Contain ( e = > e . EventName = = "sublevelparser/command" & & e . Properties . ContainsKey ( "configuration" ) & &
e . Properties [ "configuration" ] = = Sha256Hasher . Hash ( "DEBUG" ) & &
e . Properties . ContainsKey ( "verb" ) & &
e . Properties [ "verb" ] = = Sha256Hasher . Hash ( "BUILD" ) ) ;
2017-06-05 20:51:58 -07:00
_fakeTelemetry
. LogEntries . Should ( )
2017-10-27 10:58:25 -07:00
. Contain ( e = > e . EventName = = "sublevelparser/command" & & e . Properties . ContainsKey ( "runtime" ) & &
e . Properties [ "runtime" ] = = Sha256Hasher . Hash ( "OSX.10.11-X64" ) & &
e . Properties . ContainsKey ( "verb" ) & &
e . Properties [ "verb" ] = = Sha256Hasher . Hash ( "BUILD" ) ) ;
2017-06-05 20:51:58 -07:00
}
[Fact]
public void DotnetRunCleanTestCommandOpinionsShouldBeSentToTelemetryWhenThereIsMultipleOption ( )
{
2017-10-27 10:58:25 -07:00
string [ ] args = { "clean" , "--configuration" , "Debug" , "--framework" , "netcoreapp1.0" } ;
2017-06-05 20:51:58 -07:00
Cli . Program . ProcessArgs ( args ) ;
_fakeTelemetry
. LogEntries . Should ( )
2017-10-27 10:58:25 -07:00
. Contain ( e = > e . EventName = = "sublevelparser/command" & & e . Properties . ContainsKey ( "configuration" ) & &
e . Properties [ "configuration" ] = = Sha256Hasher . Hash ( "DEBUG" ) & &
e . Properties . ContainsKey ( "verb" ) & &
e . Properties [ "verb" ] = = Sha256Hasher . Hash ( "CLEAN" ) ) ;
2017-06-05 20:51:58 -07:00
_fakeTelemetry
. LogEntries . Should ( )
2017-10-27 10:58:25 -07:00
. Contain ( e = > e . EventName = = "sublevelparser/command" & & e . Properties . ContainsKey ( "framework" ) & &
e . Properties [ "framework" ] = = Sha256Hasher . Hash ( "NETCOREAPP1.0" ) & &
e . Properties . ContainsKey ( "verb" ) & &
e . Properties [ "verb" ] = = Sha256Hasher . Hash ( "CLEAN" ) ) ;
2016-03-31 16:37:40 -07:00
}
2017-06-27 13:46:09 -07:00
2017-07-07 10:20:00 -07:00
[WindowsOnlyFact]
2017-06-27 13:46:09 -07:00
public void InternalreportinstallsuccessCommandCollectExeNameWithEventname ( )
{
2017-06-05 20:51:58 -07:00
FakeRecordEventNameTelemetry fakeTelemetry = new FakeRecordEventNameTelemetry ( ) ;
2017-06-27 13:46:09 -07:00
string [ ] args = { "c:\\mypath\\dotnet-sdk-latest-win-x64.exe" } ;
2017-06-05 20:51:58 -07:00
InternalReportinstallsuccess . ProcessInputAndSendTelemetry ( args , fakeTelemetry ) ;
2017-06-27 13:46:09 -07:00
2017-06-05 20:51:58 -07:00
fakeTelemetry
. LogEntries . Should ( )
2017-10-27 10:58:25 -07:00
. Contain ( e = > e . EventName = = "install/reportsuccess" & & e . Properties . ContainsKey ( "exeName" ) & &
e . Properties [ "exeName" ] = = Sha256Hasher . Hash ( "DOTNET-SDK-LATEST-WIN-X64.EXE" ) ) ;
2017-06-27 13:46:09 -07:00
}
[Fact]
public void InternalreportinstallsuccessCommandIsRegistedInBuiltIn ( )
{
BuiltInCommandsCatalog . Commands . Should ( ) . ContainKey ( "internal-reportinstallsuccess" ) ;
}
2016-03-31 16:37:40 -07:00
}
}