< Summary

Information
Class: Elsa.Expressions.Python.Activities.RunPython
Assembly: Elsa.Expressions.Python
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.Python/Activities/RunPython/RunPython.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 17
Coverable lines: 17
Total lines: 72
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
.ctor(...)100%210%
get_Script()100%210%
get_PossibleOutcomes()100%210%
ExecuteAsync()0%2040%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.Python/Activities/RunPython/RunPython.cs

#LineLine coverage
 1using System.Runtime.CompilerServices;
 2using Elsa.Extensions;
 3using Elsa.Expressions.Python.Contracts;
 4using Elsa.Expressions.Python.Models;
 5using Elsa.Workflows;
 6using Elsa.Workflows.Attributes;
 7using Elsa.Workflows.UIHints;
 8using Elsa.Workflows.Models;
 9
 10// ReSharper disable once CheckNamespace
 11namespace Elsa.Expressions.Python.Activities;
 12
 13/// <summary>
 14/// Executes Python code.
 15/// </summary>
 16[Activity("Elsa", "Scripting", "Executes Python code", DisplayName = "Run Python")]
 17public class RunPython : CodeActivity<object?>
 18{
 19    /// <inheritdoc />
 020    public RunPython([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line)
 21    {
 022    }
 23
 24    /// <inheritdoc />
 025    public RunPython(string script, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : this(
 26    {
 027        Script = new Input<string>(script);
 028    }
 29
 30    /// <summary>
 31    /// The script to run.
 32    /// </summary>
 33    [Input(
 34        Description = "The script to run.",
 35        DefaultSyntax = "Python",
 36        UIHint = InputUIHints.CodeEditor,
 37        UIHandler = typeof(RunPythonOptionsProvider)
 38    )]
 039    public Input<string> Script { get; set; } = new("");
 40
 41    /// <summary>
 42    /// A list of possible outcomes. Use "SetOutcome(string)" to set the outcome. Use "SetOutcomes(params string[])" to 
 43    /// </summary>
 44    [Input(Description = "A list of possible outcomes.", UIHint = InputUIHints.DynamicOutcomes)]
 045    public Input<ICollection<string>> PossibleOutcomes { get; set; } = null!;
 46
 47    /// <inheritdoc />
 48    protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
 49    {
 050        var script = context.Get(Script);
 51
 52        // If no script was specified, there's nothing to do.
 053        if (string.IsNullOrWhiteSpace(script))
 054            return;
 55
 56        // Get a Python evaluator.
 057        var evaluator = context.GetRequiredService<IPythonEvaluator>();
 58
 59        // Run the script.
 060        var result = await evaluator.EvaluateAsync(script, typeof(object), context.ExpressionExecutionContext, context.C
 61
 62        // Set the result as output, if any.
 063        if (result is not null)
 064            context.Set(Result, result);
 65
 66        // Get the outcome or outcomes set by the script, if any. If not set, use "Done".
 067        var outcomes = context.ExpressionExecutionContext.TransientProperties.GetValueOrDefault(OutcomeProxy.OutcomeProp
 68
 69        // Complete the activity with the outcome.
 070        await context.CompleteActivityWithOutcomesAsync(outcomes);
 071    }
 72}