-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathPowerFxEngine.cs
More file actions
547 lines (462 loc) · 22.1 KB
/
PowerFxEngine.cs
File metadata and controls
547 lines (462 loc) · 22.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System.ComponentModel.Design;
using System.Globalization;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging;
using Microsoft.PowerApps.TestEngine.Config;
using Microsoft.PowerApps.TestEngine.Helpers;
using Microsoft.PowerApps.TestEngine.PowerFx.Functions;
using Microsoft.PowerApps.TestEngine.Providers;
using Microsoft.PowerApps.TestEngine.System;
using Microsoft.PowerApps.TestEngine.TestInfra;
using Microsoft.PowerFx;
using Microsoft.PowerFx.Core.Utils;
using Microsoft.PowerFx.Dataverse;
using Microsoft.PowerFx.Syntax;
using Microsoft.PowerFx.Types;
using Microsoft.PowerPlatform.Dataverse.Client;
using Microsoft.Xrm.Sdk;
namespace Microsoft.PowerApps.TestEngine.PowerFx
{
/// <summary>
/// Wrapper for Power FX interpreter
/// </summary>
public class PowerFxEngine : IPowerFxEngine
{
public const string ENABLE_DATAVERSE_FUNCTIONS = "enableDataverseFunctions";
public const string ENABLE_AI_FUNCTIONS = "enableAIFunctions";
private readonly ITestInfraFunctions TestInfraFunctions;
private readonly ITestWebProvider _testWebProvider;
private readonly IFileSystem _fileSystem;
private readonly ISingleTestInstanceState SingleTestInstanceState;
private readonly ITestState TestState;
private readonly IEnvironmentVariable _environmentVariable;
private IOrganizationService _orgService;
private DataverseConnection _dataverseConnection;
private DataverseAIPredictHelper _dataverseAIPredictHelper;
private bool enableAIFunctions = false;
private int _retryLimit = 2;
public RecalcEngine Engine { get; private set; }
private ILogger Logger { get { return SingleTestInstanceState.GetLogger(); } }
public Func<AzureCliHelper> GetAzureCliHelper = () => new AzureCliHelper();
public PowerFxEngine(ITestInfraFunctions testInfraFunctions,
ITestWebProvider testWebProvider,
ISingleTestInstanceState singleTestInstanceState,
ITestState testState,
IFileSystem fileSystem,
IEnvironmentVariable environmentVariable)
{
TestInfraFunctions = testInfraFunctions;
_testWebProvider = testWebProvider;
SingleTestInstanceState = singleTestInstanceState;
TestState = testState;
_fileSystem = fileSystem;
_environmentVariable = environmentVariable;
}
/// <summary>
/// Setup the Power Fx state for test execution
/// </summary>
public void Setup(TestSettings settings)
{
var features = Features.PowerFxV1;
var powerFxConfig = new PowerFxConfig(features);
var vals = new SymbolValues();
var symbols = (SymbolTable)vals.SymbolTable;
symbols.EnableMutationFunctions();
var testSettings = TestState.GetTestSettings();
powerFxConfig.SymbolTable = symbols;
ConditionallyRegisterTestTypes(testSettings, powerFxConfig);
// Enabled to allow ability to set variable and collection state that can be used with providers and as test variables
powerFxConfig.EnableSetFunction();
powerFxConfig.EnableJsonFunctions();
// Perform any provider specific configuration
if (_testWebProvider is IExtendedPowerFxProvider extendedPowerFxProvider)
{
extendedPowerFxProvider.ConfigurePowerFx(powerFxConfig);
}
powerFxConfig.AddFunction(new SelectOneParamFunction(_testWebProvider, async () => await UpdatePowerFxModelAsync(), Logger));
powerFxConfig.AddFunction(new SelectTwoParamsFunction(_testWebProvider, async () => await UpdatePowerFxModelAsync(), Logger));
powerFxConfig.AddFunction(new SelectThreeParamsFunction(_testWebProvider, async () => await UpdatePowerFxModelAsync(), Logger));
powerFxConfig.AddFunction(new SelectFileTwoParamsFunction(_testWebProvider, async () => await UpdatePowerFxModelAsync(), Logger));
powerFxConfig.AddFunction(new ScreenshotFunction(TestInfraFunctions, SingleTestInstanceState, _fileSystem, Logger));
powerFxConfig.AddFunction(new AssertWithoutMessageFunction(Logger));
powerFxConfig.AddFunction(new AssertFunction(Logger));
powerFxConfig.AddFunction(new AssertNotErrorFunction(Logger));
powerFxConfig.AddFunction(new SetPropertyFunction(_testWebProvider, Logger));
powerFxConfig.AddFunction(new IsMatchFunction(Logger));
if (settings != null && settings.ExtensionModules != null && settings.ExtensionModules.Enable)
{
var modules = TestState.GetTestEngineModules();
if (modules.Count == 0)
{
Logger.LogError("Extension enabled, none loaded");
}
foreach (var module in modules)
{
// Register all modules including pause module
module.RegisterPowerFxFunction(powerFxConfig, TestInfraFunctions, _testWebProvider, SingleTestInstanceState, TestState, _fileSystem);
}
}
else
{
if (TestState.GetTestEngineModules().Count > 0)
{
Logger.LogInformation("Extension loaded but not enabled");
}
}
WaitRegisterExtensions.RegisterAll(powerFxConfig, TestState.GetTimeout(), Logger);
var provider = TestState.TestProvider;
if (provider is IExtendedPowerFxProvider powerFxProvider)
{
powerFxProvider.Setup(powerFxConfig, TestInfraFunctions, SingleTestInstanceState, TestState, _fileSystem);
}
Engine = new RecalcEngine(powerFxConfig);
// Add any provider specific functions or state
if (_testWebProvider is IExtendedPowerFxProvider extendedProviderAfter)
{
extendedProviderAfter.ConfigurePowerFxEngine(Engine);
}
ConditionallySetupDataverse(testSettings, powerFxConfig);
ConditionallyRegisterTestFunctions(testSettings, powerFxConfig);
var symbolValues = new SymbolValues(powerFxConfig.SymbolTable);
foreach (var val in powerFxConfig.SymbolTable.SymbolNames.ToList())
{
if (powerFxConfig.SymbolTable.TryLookupSlot(val.Name, out ISymbolSlot slot))
{
Engine.UpdateVariable(val.Name, symbolValues.Get(slot));
powerFxConfig.SymbolTable.RemoveVariable(val.Name);
}
}
}
/// <summary>
/// Register Power Fx types that aid and simplify testing
/// </summary>
/// <param name="testSettings">The settings to obtain the test functions from</param>
/// <param name="powerFxConfig">The Power Fx context that the functions should be registered with</param>
private void ConditionallyRegisterTestTypes(TestSettings testSettings, PowerFxConfig powerFxConfig)
{
if (testSettings == null || testSettings.PowerFxTestTypes == null || testSettings.PowerFxTestTypes.Count == 0)
{
return;
}
var engine = new RecalcEngine(new PowerFxConfig(Features.PowerFxV1));
foreach (PowerFxTestType type in testSettings.PowerFxTestTypes)
{
var result = engine.Parse(type.Value);
RegisterPowerFxType(type.Name, result.Root, powerFxConfig);
}
}
private void RegisterPowerFxType(string name, TexlNode result, PowerFxConfig powerFxConfig)
{
switch (result.Kind)
{
case NodeKind.Table:
var table = TableType.Empty();
var tableRecord = RecordType.Empty();
var first = true;
TableNode tableNode = result as TableNode;
foreach (var child in tableNode.ChildNodes)
{
if (child is RecordNode recordNode && first)
{
first = false;
tableRecord = GetRecordType(recordNode);
foreach (var field in tableRecord.GetFieldTypes())
{
table = table.Add(field);
}
}
}
powerFxConfig.SymbolTable.AddType(new DName(name), table);
break;
case NodeKind.Record:
var record = GetRecordType(result as RecordNode);
powerFxConfig.SymbolTable.AddType(new DName(name), record);
break;
}
}
private RecordType GetRecordType(RecordNode recordNode)
{
var record = RecordType.Empty();
int index = 0;
foreach (var child in recordNode.ChildNodes)
{
if (child is DottedNameNode dottedNameNode)
{
var fieldName = dottedNameNode.Right.Name.Value;
var fieldType = GetFormulaTypeFromNode(dottedNameNode.Right);
record = record.Add(new NamedFormulaType(fieldName, fieldType));
}
if (child is FirstNameNode firstNameNode)
{
var fieldName = recordNode.Ids[index].Name.Value;
index++;
var fieldType = GetFormulaTypeFromNode(firstNameNode.Ident);
record = record.Add(new NamedFormulaType(fieldName, fieldType));
}
}
return record;
}
private FormulaType GetFormulaTypeFromNode(Identifier right)
{
switch (right.Name.Value)
{
case "Boolean":
return FormulaType.Boolean;
case "Number":
return FormulaType.Number;
case "Text":
return FormulaType.String;
case "Date":
return FormulaType.Date;
case "DateTime":
return FormulaType.DateTime;
case "Time":
return FormulaType.Time;
default:
throw new InvalidOperationException($"Unsupported node type: {right.Name.Value}");
}
}
/// <summary>
/// Register Power Fx funtions that aid and simplify testing
/// </summary>
/// <param name="testSettings">The settings to obtain the test functions from</param>
/// <param name="powerFxConfig">The Power Fx context that the functions should be registered with</param>
private void ConditionallyRegisterTestFunctions(TestSettings testSettings, PowerFxConfig powerFxConfig)
{
if (testSettings == null)
{
return;
}
if (testSettings.TestFunctions.Count > 0)
{
var culture = GetLocaleFromTestSettings(testSettings.Locale);
foreach (var function in testSettings.TestFunctions)
{
var code = function.Code.TrimEnd();
if (!code.EndsWith(";"))
{
code += ";";
}
var registerResult = Engine.AddUserDefinedFunction(code, culture, powerFxConfig.SymbolTable, true);
if (!registerResult.IsSuccess)
{
foreach (var error in registerResult.Errors)
{
var msg = error.ToString();
if (error.IsWarning)
{
Logger.LogWarning(msg);
}
else
{
Logger.LogError(msg);
}
}
}
}
}
}
private CultureInfo GetLocaleFromTestSettings(string strLocale)
{
var locale = CultureInfo.CurrentCulture;
try
{
if (string.IsNullOrEmpty(strLocale))
{
Logger.LogDebug($"Locale property not specified in testSettings. Using current system locale: {locale.Name}");
}
else
{
locale = new CultureInfo(strLocale);
Logger.LogDebug($"Locale: {locale.Name}");
}
return locale;
}
catch (CultureNotFoundException)
{
Logger.LogError($"Locale from test suite definition {strLocale} unrecognized.");
throw new UserInputException(UserInputException.ErrorMapping.UserInputExceptionInvalidTestSettings.ToString());
}
}
/// <summary>
/// Attach dataverse state to the test session if we are testing a Model Driven Application or have a dataverse URL we are testing
/// </summary>
private void ConditionallySetupDataverse(TestSettings testSettings, PowerFxConfig powerFxConfig)
{
if (testSettings == null || !testSettings.ExtensionModules.Parameters.ContainsKey(ENABLE_DATAVERSE_FUNCTIONS) || (testSettings != null && testSettings.ExtensionModules.Parameters[ENABLE_DATAVERSE_FUNCTIONS].ToString().ToLower() != "true"))
{
return;
}
// Must have dataverse enabled to enable AI Functions
if (testSettings != null && testSettings.ExtensionModules.Parameters.ContainsKey(ENABLE_AI_FUNCTIONS) && testSettings.ExtensionModules.Parameters[ENABLE_AI_FUNCTIONS].ToString().ToLower() == "true")
{
enableAIFunctions = true;
}
DataverseConnection dataverse = null;
var domainUrl = TestState.GetDomain();
if (domainUrl.Contains("dynamics.com"))
{
domainUrl = "https://" + new Uri(domainUrl).Host;
}
else
{
domainUrl = "";
}
// Fallback to environment to check if a DATAVERSE_URL value has been configured.
var dataverseUrl = !string.IsNullOrEmpty(domainUrl) ? domainUrl : _environmentVariable.GetVariable("DATAVERSE_URL");
if (!string.IsNullOrEmpty(dataverseUrl) && Uri.TryCreate(dataverseUrl, UriKind.Absolute, out Uri dataverseUri))
{
// Attempt to retreive OAuath access token. Assume logged in Azure CLI session
string token = GetAzureCliHelper()?.GetAccessToken(dataverseUri);
if (!string.IsNullOrEmpty(token))
{
// Establish a collection to Dataverse
Logger.LogInformation($"Loading dataverse state for {dataverseUri}");
Func<string, Task<string>> callback = async (string item) =>
{
return token;
};
var svcClient = new ServiceClient(dataverseUri, callback) { UseWebApi = false };
svcClient.Connect();
_orgService = svcClient;
dataverse = SingleOrgPolicy.New(svcClient);
_dataverseConnection = dataverse;
if (enableAIFunctions)
{
_dataverseAIPredictHelper = new DataverseAIPredictHelper(dataverseUri, token);
#if DEBUG
powerFxConfig.AddFunction(new AIExecutePromptFunction(Logger, svcClient, _dataverseAIPredictHelper));
#endif
}
}
}
}
public async Task<FormulaValue> ExecuteWithRetryAsync(string testSteps, CultureInfo culture)
{
int currentRetry = 0;
FormulaValue result = FormulaValue.NewBlank();
while (currentRetry <= _retryLimit)
{
try
{
result = await ExecuteAsync(testSteps, culture);
break;
}
catch (Exception e) when (e.Message.Contains("locale"))
{
Logger.LogDebug($"Got {e.Message} in attempt No.{currentRetry + 1} to run");
currentRetry++;
if (currentRetry > _retryLimit)
{
// Re-throw the exception.
throw;
}
// Wait to retry the operation.
Thread.Sleep(1000);
await UpdatePowerFxModelAsync();
}
}
return result;
}
public async Task<FormulaValue> ExecuteAsync(string testSteps, CultureInfo culture)
{
if (Engine == null)
{
Logger.LogError("Engine is null, make sure to call Setup first");
throw new InvalidOperationException();
}
// Remove the leading = sign
if (testSteps.StartsWith("="))
{
testSteps = testSteps.Remove(0, 1);
}
var goStepByStep = TestState.ExecuteStepByStep;
var parseResult = Engine.Parse(testSteps);
// Check if the syntax is correct
var checkResult = Engine.Check(testSteps, null, GetPowerFxParserOptions(culture));
if (!checkResult.IsSuccess)
{
// If it isn't, we have to go step by step as the object model isn't fully loaded
goStepByStep = true;
Logger.LogDebug($"Syntax check failed. Now attempting to execute lines step by step");
}
var runtimeConfig = new RuntimeConfig();
// Check if a connection to Dataverse has been established
if (_orgService != null)
{
// Add in symbols from Dataverse
runtimeConfig = new RuntimeConfig(_dataverseConnection.SymbolValues);
// And enable dataverse execution as part of this test session
runtimeConfig.AddDataverseExecute(_orgService);
}
if (TestState.TestProvider is IExtendedPowerFxProvider extendedProvider && extendedProvider.ProviderExecute)
{
return extendedProvider.ExecutePowerFx(testSteps, culture);
}
var parseOption = new ParserOptions() { AllowsSideEffects = true, Culture = culture, NumberIsFloat = true };
if (goStepByStep)
{
var splitSteps = PowerFxHelper.ExtractFormulasSeparatedByChainingOperator(Engine, checkResult, culture);
FormulaValue result = FormulaValue.NewBlank();
int stepNumber = 0;
foreach (var step in splitSteps)
{
TestState.OnBeforeTestStepExecuted(new TestStepEventArgs { TestStep = step, StepNumber = stepNumber, Engine = Engine });
Logger.LogTrace($"Attempting:{step.Replace("\n", "").Replace("\r", "")}");
result = await Engine.EvalAsync(
step
, CancellationToken.None
, parseOption, null, runtimeConfig);
TestState.OnAfterTestStepExecuted(new TestStepEventArgs { TestStep = step, Result = result, StepNumber = stepNumber, Engine = Engine });
stepNumber++;
}
return result;
}
else
{
var values = new SymbolValues();
Logger.LogTrace($"Attempting:\n\n{{\n{testSteps}}}");
TestState.OnBeforeTestStepExecuted(new TestStepEventArgs { TestStep = testSteps, StepNumber = null, Engine = Engine });
var result = await Engine.EvalAsync(
testSteps
, CancellationToken.None
, parseOption, null, runtimeConfig);
TestState.OnAfterTestStepExecuted(new TestStepEventArgs { TestStep = testSteps, Result = result, StepNumber = 1 });
return result;
}
}
public async Task UpdatePowerFxModelAsync()
{
if (Engine == null)
{
Logger.LogError("Engine is null, make sure to call Setup first");
throw new InvalidOperationException();
}
await PollingHelper.PollAsync<bool>(false, (x) => !x, () => _testWebProvider.CheckIsIdleAsync(), TestState.GetTestSettings().Timeout, SingleTestInstanceState.GetLogger(), "Something went wrong when Test Engine tried to get App status.");
var controlRecordValues = await _testWebProvider.LoadObjectModelAsync();
foreach (var control in controlRecordValues)
{
Engine.UpdateVariable(control.Key, control.Value);
}
}
private static ParserOptions GetPowerFxParserOptions(CultureInfo culture)
{
// Currently support for decimal is in progress for PowerApps
// Power Fx by default treats number as decimal. Hence setting NumberIsFloat config to true in our case
// TODO: Evuate culture evaluate across languages
return new ParserOptions() { AllowsSideEffects = true, Culture = culture, NumberIsFloat = true };
}
public ITestWebProvider GetWebProvider()
{
return _testWebProvider;
}
public async Task RunRequirementsCheckAsync()
{
await _testWebProvider.CheckProviderAsync();
await _testWebProvider.TestEngineReady();
}
public bool PowerAppIntegrationEnabled { get; set; } = true;
}
}