mirror of
https://github.com/actions/runner.git
synced 2025-12-13 10:05:23 +00:00
switch hashFiles to extension function (#362)
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using GitHub.DistributedTask.ObjectTemplating.Tokens;
|
||||
using GitHub.DistributedTask.Expressions2;
|
||||
using GitHub.DistributedTask.ObjectTemplating.Tokens;
|
||||
using GitHub.DistributedTask.Pipelines.ContextData;
|
||||
using GitHub.DistributedTask.WebApi;
|
||||
using GitHub.Runner.Common.Util;
|
||||
using GitHub.Runner.Worker;
|
||||
@@ -1600,6 +1602,8 @@ runs:
|
||||
_ec = new Mock<IExecutionContext>();
|
||||
_ec.Setup(x => x.CancellationToken).Returns(_ecTokenSource.Token);
|
||||
_ec.Setup(x => x.Variables).Returns(new Variables(_hc, new Dictionary<string, VariableValue>()));
|
||||
_ec.Setup(x => x.ExpressionValues).Returns(new DictionaryContextData());
|
||||
_ec.Setup(x => x.ExpressionFunctions).Returns(new List<IFunctionInfo>());
|
||||
_ec.Setup(x => x.Write(It.IsAny<string>(), It.IsAny<string>())).Callback((string tag, string message) => { _hc.GetTrace().Info($"[{tag}]{message}"); });
|
||||
_ec.Setup(x => x.AddIssue(It.IsAny<Issue>(), It.IsAny<string>())).Callback((Issue issue, string message) => { _hc.GetTrace().Info($"[{issue.Type}]{issue.Message ?? message}"); });
|
||||
_ec.Setup(x => x.GetGitHubContext("workspace")).Returns(Path.Combine(_workFolder, "actions", "actions"));
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
using GitHub.DistributedTask.Expressions2;
|
||||
using GitHub.DistributedTask.ObjectTemplating.Tokens;
|
||||
using GitHub.DistributedTask.Pipelines.ContextData;
|
||||
using GitHub.DistributedTask.WebApi;
|
||||
using GitHub.Runner.Worker;
|
||||
using GitHub.Runner.Worker.Expressions;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -533,26 +535,26 @@ namespace GitHub.Runner.Common.Tests.Worker
|
||||
var actionManifest = new ActionManifestManager();
|
||||
actionManifest.Initialize(_hc);
|
||||
|
||||
var githubContext = new DictionaryContextData();
|
||||
githubContext.Add("ref", new StringContextData("refs/heads/master"));
|
||||
|
||||
var evaluateContext = new Dictionary<string, PipelineContextData>(StringComparer.OrdinalIgnoreCase);
|
||||
evaluateContext["github"] = githubContext;
|
||||
evaluateContext["strategy"] = new DictionaryContextData();
|
||||
evaluateContext["matrix"] = new DictionaryContextData();
|
||||
evaluateContext["steps"] = new DictionaryContextData();
|
||||
evaluateContext["job"] = new DictionaryContextData();
|
||||
evaluateContext["runner"] = new DictionaryContextData();
|
||||
evaluateContext["env"] = new DictionaryContextData();
|
||||
_ec.Object.ExpressionValues["github"] = new DictionaryContextData
|
||||
{
|
||||
{ "ref", new StringContextData("refs/heads/master") },
|
||||
};
|
||||
_ec.Object.ExpressionValues["strategy"] = new DictionaryContextData();
|
||||
_ec.Object.ExpressionValues["matrix"] = new DictionaryContextData();
|
||||
_ec.Object.ExpressionValues["steps"] = new DictionaryContextData();
|
||||
_ec.Object.ExpressionValues["job"] = new DictionaryContextData();
|
||||
_ec.Object.ExpressionValues["runner"] = new DictionaryContextData();
|
||||
_ec.Object.ExpressionValues["env"] = new DictionaryContextData();
|
||||
_ec.Object.ExpressionFunctions.Add(new FunctionInfo<HashFilesFunction>("hashFiles", 1, 255));
|
||||
|
||||
//Act
|
||||
var result = actionManifest.EvaluateDefaultInput(_ec.Object, "testInput", new StringToken(null, null, null, "defaultValue"), evaluateContext);
|
||||
var result = actionManifest.EvaluateDefaultInput(_ec.Object, "testInput", new StringToken(null, null, null, "defaultValue"));
|
||||
|
||||
//Assert
|
||||
Assert.Equal("defaultValue", result);
|
||||
|
||||
//Act
|
||||
result = actionManifest.EvaluateDefaultInput(_ec.Object, "testInput", new BasicExpressionToken(null, null, null, "github.ref"), evaluateContext);
|
||||
result = actionManifest.EvaluateDefaultInput(_ec.Object, "testInput", new BasicExpressionToken(null, null, null, "github.ref"));
|
||||
|
||||
//Assert
|
||||
Assert.Equal("refs/heads/master", result);
|
||||
@@ -575,6 +577,8 @@ namespace GitHub.Runner.Common.Tests.Worker
|
||||
_ec.Setup(x => x.WriteDebug).Returns(true);
|
||||
_ec.Setup(x => x.CancellationToken).Returns(_ecTokenSource.Token);
|
||||
_ec.Setup(x => x.Variables).Returns(new Variables(_hc, new Dictionary<string, VariableValue>()));
|
||||
_ec.Setup(x => x.ExpressionValues).Returns(new DictionaryContextData());
|
||||
_ec.Setup(x => x.ExpressionFunctions).Returns(new List<IFunctionInfo>());
|
||||
_ec.Setup(x => x.Write(It.IsAny<string>(), It.IsAny<string>())).Callback((string tag, string message) => { _hc.GetTrace().Info($"{tag}{message}"); });
|
||||
_ec.Setup(x => x.AddIssue(It.IsAny<Issue>(), It.IsAny<string>())).Callback((Issue issue, string message) => { _hc.GetTrace().Info($"[{issue.Type}]{issue.Message ?? message}"); });
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using GitHub.DistributedTask.ObjectTemplating.Tokens;
|
||||
using GitHub.DistributedTask.Expressions2;
|
||||
using GitHub.DistributedTask.ObjectTemplating.Tokens;
|
||||
using GitHub.DistributedTask.Pipelines;
|
||||
using GitHub.DistributedTask.Pipelines.ContextData;
|
||||
using GitHub.DistributedTask.WebApi;
|
||||
@@ -322,6 +323,7 @@ namespace GitHub.Runner.Common.Tests.Worker
|
||||
|
||||
_ec = new Mock<IExecutionContext>();
|
||||
_ec.Setup(x => x.ExpressionValues).Returns(_context);
|
||||
_ec.Setup(x => x.ExpressionFunctions).Returns(new List<IFunctionInfo>());
|
||||
_ec.Setup(x => x.IntraActionState).Returns(new Dictionary<string, string>());
|
||||
_ec.Setup(x => x.EnvironmentVariables).Returns(new Dictionary<string, string>());
|
||||
_ec.Setup(x => x.SetGitHubContext(It.IsAny<string>(), It.IsAny<string>()));
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using GitHub.DistributedTask.Expressions2;
|
||||
using GitHub.DistributedTask.ObjectTemplating;
|
||||
using GitHub.DistributedTask.Pipelines.ObjectTemplating;
|
||||
using GitHub.DistributedTask.WebApi;
|
||||
using GitHub.Runner.Worker;
|
||||
using GitHub.Runner.Worker.Expressions;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using GitHub.DistributedTask.Expressions2;
|
||||
using GitHub.DistributedTask.Pipelines.ContextData;
|
||||
|
||||
namespace GitHub.Runner.Common.Tests.Worker
|
||||
namespace GitHub.Runner.Common.Tests.Worker.Expressions
|
||||
{
|
||||
public sealed class ExpressionManagerL0
|
||||
public sealed class ConditionFunctionsL0
|
||||
{
|
||||
private Mock<IExecutionContext> _ec;
|
||||
private ExpressionManager _expressionManager;
|
||||
private DictionaryContextData _expressions;
|
||||
private TemplateContext _templateContext;
|
||||
private JobContext _jobContext;
|
||||
|
||||
[Fact]
|
||||
@@ -38,7 +38,7 @@ namespace GitHub.Runner.Common.Tests.Worker
|
||||
_jobContext.Status = variableSet.JobStatus;
|
||||
|
||||
// Act.
|
||||
bool actual = _expressionManager.Evaluate(_ec.Object, "always()").Value;
|
||||
bool actual = Evaluate("always()");
|
||||
|
||||
// Assert.
|
||||
Assert.Equal(variableSet.Expected, actual);
|
||||
@@ -68,7 +68,7 @@ namespace GitHub.Runner.Common.Tests.Worker
|
||||
_jobContext.Status = variableSet.JobStatus;
|
||||
|
||||
// Act.
|
||||
bool actual = _expressionManager.Evaluate(_ec.Object, "cancelled()").Value;
|
||||
bool actual = Evaluate("cancelled()");
|
||||
|
||||
// Assert.
|
||||
Assert.Equal(variableSet.Expected, actual);
|
||||
@@ -97,7 +97,7 @@ namespace GitHub.Runner.Common.Tests.Worker
|
||||
_jobContext.Status = variableSet.JobStatus;
|
||||
|
||||
// Act.
|
||||
bool actual = _expressionManager.Evaluate(_ec.Object, "failure()").Value;
|
||||
bool actual = Evaluate("failure()");
|
||||
|
||||
// Assert.
|
||||
Assert.Equal(variableSet.Expected, actual);
|
||||
@@ -126,37 +126,7 @@ namespace GitHub.Runner.Common.Tests.Worker
|
||||
_jobContext.Status = variableSet.JobStatus;
|
||||
|
||||
// Act.
|
||||
bool actual = _expressionManager.Evaluate(_ec.Object, "success()").Value;
|
||||
|
||||
// Assert.
|
||||
Assert.Equal(variableSet.Expected, actual);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ContextNamedValue()
|
||||
{
|
||||
using (TestHostContext hc = CreateTestContext())
|
||||
{
|
||||
// Arrange.
|
||||
var variableSets = new[]
|
||||
{
|
||||
new { Condition = "github.ref == 'refs/heads/master'", VariableName = "ref", VariableValue = "refs/heads/master", Expected = true },
|
||||
new { Condition = "github['ref'] == 'refs/heads/master'", VariableName = "ref", VariableValue = "refs/heads/master", Expected = true },
|
||||
new { Condition = "github.nosuch || '' == ''", VariableName = "ref", VariableValue = "refs/heads/master", Expected = true },
|
||||
new { Condition = "github['ref'] == 'refs/heads/release'", VariableName = "ref", VariableValue = "refs/heads/master", Expected = false },
|
||||
new { Condition = "github.ref == 'refs/heads/release'", VariableName = "ref", VariableValue = "refs/heads/master", Expected = false },
|
||||
};
|
||||
foreach (var variableSet in variableSets)
|
||||
{
|
||||
InitializeExecutionContext(hc);
|
||||
_ec.Object.ExpressionValues["github"] = new GitHubContext() { { variableSet.VariableName, new StringContextData(variableSet.VariableValue) } };
|
||||
|
||||
// Act.
|
||||
bool actual = _expressionManager.Evaluate(_ec.Object, variableSet.Condition).Value;
|
||||
bool actual = Evaluate("success()");
|
||||
|
||||
// Assert.
|
||||
Assert.Equal(variableSet.Expected, actual);
|
||||
@@ -166,21 +136,34 @@ namespace GitHub.Runner.Common.Tests.Worker
|
||||
|
||||
private TestHostContext CreateTestContext([CallerMemberName] String testName = "")
|
||||
{
|
||||
var hc = new TestHostContext(this, testName);
|
||||
_expressionManager = new ExpressionManager();
|
||||
_expressionManager.Initialize(hc);
|
||||
return hc;
|
||||
return new TestHostContext(this, testName);
|
||||
}
|
||||
|
||||
private void InitializeExecutionContext(TestHostContext hc)
|
||||
{
|
||||
_expressions = new DictionaryContextData();
|
||||
_jobContext = new JobContext();
|
||||
|
||||
_ec = new Mock<IExecutionContext>();
|
||||
_ec.SetupAllProperties();
|
||||
_ec.Setup(x => x.ExpressionValues).Returns(_expressions);
|
||||
_ec.Setup(x => x.JobContext).Returns(_jobContext);
|
||||
var executionContext = new Mock<IExecutionContext>();
|
||||
executionContext.SetupAllProperties();
|
||||
executionContext.Setup(x => x.JobContext).Returns(_jobContext);
|
||||
|
||||
_templateContext = new TemplateContext();
|
||||
_templateContext.State[nameof(IExecutionContext)] = executionContext.Object;
|
||||
}
|
||||
|
||||
private bool Evaluate(string expression)
|
||||
{
|
||||
var parser = new ExpressionParser();
|
||||
var functions = new IFunctionInfo[]
|
||||
{
|
||||
new FunctionInfo<AlwaysFunction>(PipelineTemplateConstants.Always, 0, 0),
|
||||
new FunctionInfo<CancelledFunction>(PipelineTemplateConstants.Cancelled, 0, 0),
|
||||
new FunctionInfo<FailureFunction>(PipelineTemplateConstants.Failure, 0, 0),
|
||||
new FunctionInfo<SuccessFunction>(PipelineTemplateConstants.Success, 0, 0),
|
||||
};
|
||||
var tree = parser.CreateTree(expression, null, null, functions);
|
||||
var result = tree.Evaluate(null, null, _templateContext, null);
|
||||
return result.IsTruthy;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,6 @@ namespace GitHub.Runner.Common.Tests.Worker
|
||||
private Mock<IJobServerQueue> _jobServerQueue;
|
||||
private Mock<IConfigurationStore> _config;
|
||||
private Mock<IPagingLogger> _logger;
|
||||
private Mock<IExpressionManager> _express;
|
||||
private Mock<IContainerOperationProvider> _containerProvider;
|
||||
private Mock<IDiagnosticLogManager> _diagnosticLogManager;
|
||||
|
||||
@@ -35,7 +34,6 @@ namespace GitHub.Runner.Common.Tests.Worker
|
||||
_jobServerQueue = new Mock<IJobServerQueue>();
|
||||
_config = new Mock<IConfigurationStore>();
|
||||
_logger = new Mock<IPagingLogger>();
|
||||
_express = new Mock<IExpressionManager>();
|
||||
_containerProvider = new Mock<IContainerOperationProvider>();
|
||||
_diagnosticLogManager = new Mock<IDiagnosticLogManager>();
|
||||
_directoryManager = new Mock<IPipelineDirectoryManager>();
|
||||
@@ -108,7 +106,6 @@ namespace GitHub.Runner.Common.Tests.Worker
|
||||
hc.SetSingleton(_actionManager.Object);
|
||||
hc.SetSingleton(_config.Object);
|
||||
hc.SetSingleton(_jobServerQueue.Object);
|
||||
hc.SetSingleton(_express.Object);
|
||||
hc.SetSingleton(_containerProvider.Object);
|
||||
hc.SetSingleton(_directoryManager.Object);
|
||||
hc.SetSingleton(_diagnosticLogManager.Object);
|
||||
|
||||
@@ -53,9 +53,6 @@ namespace GitHub.Runner.Common.Tests.Worker
|
||||
}
|
||||
|
||||
_tokenSource = new CancellationTokenSource();
|
||||
var expressionManager = new ExpressionManager();
|
||||
expressionManager.Initialize(hc);
|
||||
hc.SetSingleton<IExpressionManager>(expressionManager);
|
||||
|
||||
_jobRunner = new JobRunner();
|
||||
_jobRunner.Initialize(hc);
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
using GitHub.DistributedTask.WebApi;
|
||||
using GitHub.Runner.Worker;
|
||||
using Moq;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using GitHub.DistributedTask.Expressions2;
|
||||
using GitHub.DistributedTask.Pipelines.ContextData;
|
||||
using GitHub.DistributedTask.ObjectTemplating.Tokens;
|
||||
using GitHub.DistributedTask.WebApi;
|
||||
using GitHub.Runner.Common.Util;
|
||||
using GitHub.Runner.Worker;
|
||||
|
||||
namespace GitHub.Runner.Common.Tests.Worker
|
||||
{
|
||||
@@ -27,9 +27,6 @@ namespace GitHub.Runner.Common.Tests.Worker
|
||||
private TestHostContext CreateTestContext([CallerMemberName] String testName = "")
|
||||
{
|
||||
var hc = new TestHostContext(this, testName);
|
||||
var expressionManager = new ExpressionManager();
|
||||
expressionManager.Initialize(hc);
|
||||
hc.SetSingleton<IExpressionManager>(expressionManager);
|
||||
Dictionary<string, VariableValue> variablesToCopy = new Dictionary<string, VariableValue>();
|
||||
_variables = new Variables(
|
||||
hostContext: hc,
|
||||
@@ -49,6 +46,7 @@ namespace GitHub.Runner.Common.Tests.Worker
|
||||
_contexts["runner"] = new DictionaryContextData();
|
||||
_contexts["job"] = _jobContext;
|
||||
_ec.Setup(x => x.ExpressionValues).Returns(_contexts);
|
||||
_ec.Setup(x => x.ExpressionFunctions).Returns(new List<IFunctionInfo>());
|
||||
_ec.Setup(x => x.JobContext).Returns(_jobContext);
|
||||
|
||||
_stepContext = new StepsContext();
|
||||
@@ -383,16 +381,11 @@ namespace GitHub.Runner.Common.Tests.Worker
|
||||
{
|
||||
using (TestHostContext hc = CreateTestContext())
|
||||
{
|
||||
var expressionManager = new Mock<IExpressionManager>();
|
||||
expressionManager.Object.Initialize(hc);
|
||||
hc.SetSingleton<IExpressionManager>(expressionManager.Object);
|
||||
expressionManager.Setup(x => x.Evaluate(It.IsAny<IExecutionContext>(), It.IsAny<string>(), It.IsAny<bool>())).Throws(new Exception());
|
||||
|
||||
// Arrange.
|
||||
var variableSets = new[]
|
||||
{
|
||||
new[] { CreateStep(hc, TaskResult.Succeeded, "success()") },
|
||||
new[] { CreateStep(hc, TaskResult.Succeeded, "success()") },
|
||||
new[] { CreateStep(hc, TaskResult.Succeeded, "fromJson('not json')") },
|
||||
new[] { CreateStep(hc, TaskResult.Succeeded, "fromJson('not json')") },
|
||||
};
|
||||
foreach (var variableSet in variableSets)
|
||||
{
|
||||
@@ -610,6 +603,7 @@ namespace GitHub.Runner.Common.Tests.Worker
|
||||
stepContext.Setup(x => x.Variables).Returns(_variables);
|
||||
stepContext.Setup(x => x.EnvironmentVariables).Returns(_env);
|
||||
stepContext.Setup(x => x.ExpressionValues).Returns(_contexts);
|
||||
stepContext.Setup(x => x.ExpressionFunctions).Returns(new List<IFunctionInfo>());
|
||||
stepContext.Setup(x => x.JobContext).Returns(_jobContext);
|
||||
stepContext.Setup(x => x.StepsContext).Returns(_stepContext);
|
||||
stepContext.Setup(x => x.ContextName).Returns(step.Object.Action.ContextName);
|
||||
|
||||
Reference in New Issue
Block a user