Skip to content

Commit

Permalink
Format updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant-Archibald-MS committed Oct 12, 2024
1 parent 688d348 commit ba301e4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void IsValid(string usingStatements, string script, bool useTemplate, str
Assert.Equal(expected, result);
}


private string _functionTemplate = @"
#r ""Microsoft.PowerFx.Interpreter.dll""
using Microsoft.PowerFx;
Expand All @@ -124,11 +124,12 @@ public void IsValid(string usingStatements, string script, bool useTemplate, str
[InlineData("", "*", "public class FooFunction : ReflectionFunction { public FooFunction() : base(DPath.Root.Append(new DName(\"Other\")), \"Foo\", FormulaType.Blank) {} } public BlankValue Execute() { return FormulaValue.NewBlank(); }", false)] // Deny all and not in allow list
[InlineData("Other", "*", "public class FooFunction : ReflectionFunction { public FooFunction() : base(DPath.Root.Append(new DName(\"Other\")), \"Foo\", FormulaType.Blank) {} } public BlankValue Execute() { return FormulaValue.NewBlank(); }", true)] // Deny all and in allow list
[InlineData("", "", "public class OtherFunction : ReflectionFunction { public OtherFunction(int someNumber) : base(DPath.Root.Append(new DName(\"TestEngine\")), \"Other\", FormulaType.Blank) {} } public BlankValue Execute() { return FormulaValue.NewBlank(); }", true)] // Allow TestEngine namespace with a parameter
public void ValidPowerFxFunction(string allow, string deny, string code, bool valid) {
public void ValidPowerFxFunction(string allow, string deny, string code, bool valid)
{
// Arrange
var checker = new TestEngineExtensionChecker(MockLogger.Object);

var assembly = CompileScript(_functionTemplate.Replace("%CODE%",code));
var assembly = CompileScript(_functionTemplate.Replace("%CODE%", code));

var settings = new TestSettingExtensions();
settings.AllowPowerFxNamespaces.AddRange(allow.Split(','));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using System.IO;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.Metadata;
using Microsoft.Extensions.Logging;
using Microsoft.PowerApps.TestEngine.Config;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.CSharp;

using System.Reflection;
using System.Reflection.PortableExecutable;
using System.Reflection.Metadata;
using MethodBody = Mono.Cecil.Cil.MethodBody;
using ModuleDefinition = Mono.Cecil.ModuleDefinition;
using TypeDefinition = Mono.Cecil.TypeDefinition;
using TypeReference = Mono.Cecil.TypeReference;
using MethodBody = Mono.Cecil.Cil.MethodBody;
using System.IO;

namespace Microsoft.PowerApps.TestEngine.Modules
{
Expand Down Expand Up @@ -243,7 +242,7 @@ public virtual bool Validate(TestSettingExtensions settings, string file)

var valid = true;

if ( !VerifyContainsValidNamespacePowerFxFunctions(settings, contents) )
if (!VerifyContainsValidNamespacePowerFxFunctions(settings, contents))
{
Logger.LogInformation("Invalid Power FX Namespace");
valid = false;
Expand Down Expand Up @@ -286,7 +285,7 @@ public bool VerifyContainsValidNamespacePowerFxFunctions(TestSettingExtensions s

// Get the source code of the assembly as will be used to check Power FX Namespaces
var code = DecompileModuleToCSharp(assembly);

foreach (TypeDefinition type in module.GetAllTypes())
{
if (type.BaseType != null && type.BaseType.Name == "ReflectionFunction")
Expand All @@ -301,14 +300,14 @@ public bool VerifyContainsValidNamespacePowerFxFunctions(TestSettingExtensions s

var constructor = constructors.First();

if ( !constructor.HasBody )
if (!constructor.HasBody)
{
Logger.LogInformation($"No body defined for {type.Name}");
// Needs body for call to base constructor
return false;
}

var baseCall = constructor.Body.Instructions.FirstOrDefault(i => i.OpCode == OpCodes.Call && i.Operand is MethodReference && ((MethodReference)i.Operand).Name == ".ctor" );
var baseCall = constructor.Body.Instructions.FirstOrDefault(i => i.OpCode == OpCodes.Call && i.Operand is MethodReference && ((MethodReference)i.Operand).Name == ".ctor");

if (baseCall == null)
{
Expand All @@ -319,14 +318,14 @@ public bool VerifyContainsValidNamespacePowerFxFunctions(TestSettingExtensions s

MethodReference baseConstructor = (MethodReference)baseCall.Operand;

if ( baseConstructor.Parameters.Count() < 2 )
if (baseConstructor.Parameters.Count() < 2)
{
// Not enough parameters
Logger.LogInformation($"No not enough parameters for {type.Name}");
return false;
}

if (baseConstructor.Parameters[0].ParameterType.FullName != "Microsoft.PowerFx.Core.Utils.DPath" )
if (baseConstructor.Parameters[0].ParameterType.FullName != "Microsoft.PowerFx.Core.Utils.DPath")
{
// First argument should be Namespace
Logger.LogInformation($"No Power FX Namespace for {type.Name}");
Expand All @@ -336,7 +335,7 @@ public bool VerifyContainsValidNamespacePowerFxFunctions(TestSettingExtensions s
// Use the decompiled code to get the values of the base constructor, specifically look for the namespace
var name = GetPowerFxNamespace(type.Name, code);

if ( string.IsNullOrEmpty(name) )
if (string.IsNullOrEmpty(name))
{
// No Power FX Namespace found
Logger.LogInformation($"No Power FX Namespace found for {type.Name}");
Expand All @@ -361,7 +360,7 @@ public bool VerifyContainsValidNamespacePowerFxFunctions(TestSettingExtensions s
return false;
}

if ( !settings.AllowPowerFxNamespaces.Contains(name) && name != "TestEngine" )
if (!settings.AllowPowerFxNamespaces.Contains(name) && name != "TestEngine")
{
Logger.LogInformation($"Not allow Power FX Namespace {name} for {type.Name}");
// Not in allow list or the Reserved TestEngine namespace
Expand Down Expand Up @@ -400,7 +399,7 @@ public OtherFunction(int start)

var match = lines.Where(l => l.Contains($"public {name}(")).FirstOrDefault();

if ( match == null )
if (match == null)
{
return String.Empty;
}
Expand All @@ -413,7 +412,7 @@ public OtherFunction(int start)
// Search for the DName
var declaration = lines[index + 1].IndexOf(baseDeclaration);

if ( declaration >= 0 )
if (declaration >= 0)
{
// Found a match
var start = declaration + baseDeclaration.Length;
Expand Down

0 comments on commit ba301e4

Please sign in to comment.