Skip to content

Commit 8ac1445

Browse files
imadityaaAditya Abhishek
andauthored
add stateDir in profile expression evaluators (#643)
Co-authored-by: Aditya Abhishek <adityaa@microsoft.com>
1 parent e1657ba commit 8ac1445

3 files changed

Lines changed: 100 additions & 1 deletion

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.1.55
1+
2.1.56

src/VirtualClient/VirtualClient.Core.UnitTests/ProfileExpressionEvaluatorTests.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,75 @@ public async Task ProfileExpressionEvaluatorLogPathLocationReferenceExpressionsA
9999
}
100100
}
101101

102+
[Test]
103+
public async Task ProfileExpressionEvaluatorSupportsWellKnownExpressionStatePathReferencesOnUnixSystems()
104+
{
105+
this.SetupDefaults(PlatformID.Unix);
106+
string stateDirectory = this.mockFixture.GetStatePath();
107+
string stateFilePath = this.mockFixture.GetStatePath("anylogs", "file1.log");
108+
109+
Dictionary<string, string> expressions = new Dictionary<string, string>
110+
{
111+
{ "{StatePath}", stateDirectory },
112+
{ "{StateDir}", stateDirectory },
113+
{ "--any-path={StatePath}/anylogs/file1.log", $"--any-path={stateFilePath}" },
114+
{ "--any-path={StateDir}/anylogs/file1.log", $"--any-path={stateFilePath}" }
115+
};
116+
117+
foreach (var entry in expressions)
118+
{
119+
string expectedExpression = entry.Value;
120+
string actualExpression = await ProfileExpressionEvaluator.Instance.EvaluateAsync(this.mockFixture.Dependencies, entry.Key);
121+
Assert.AreEqual(expectedExpression, actualExpression);
122+
}
123+
}
124+
125+
[Test]
126+
public async Task ProfileExpressionEvaluatorSupportsWellKnownExpressionStatePathReferencesOnWindowsSystems()
127+
{
128+
this.SetupDefaults(PlatformID.Win32NT);
129+
string stateDirectory = this.mockFixture.GetStatePath();
130+
string stateFilePath = this.mockFixture.GetStatePath("anylogs", "file1.log");
131+
132+
Dictionary<string, string> expressions = new Dictionary<string, string>
133+
{
134+
{ "{StatePath}", stateDirectory },
135+
{ "{StateDir}", stateDirectory },
136+
{ "--any-path={StatePath}/anylogs/file1.log", $"--any-path={stateFilePath}" },
137+
{ "--any-path={StateDir}/anylogs/file1.log", $"--any-path={stateFilePath}" }
138+
};
139+
140+
foreach (var entry in expressions)
141+
{
142+
string expectedExpression = entry.Value;
143+
string actualExpression = await ProfileExpressionEvaluator.Instance.EvaluateAsync(this.mockFixture.Dependencies, entry.Key);
144+
Assert.AreEqual(expectedExpression, actualExpression);
145+
}
146+
}
147+
148+
[Test]
149+
public async Task ProfileExpressionEvaluatorStatePathLocationReferenceExpressionsAreNotCaseSensitive()
150+
{
151+
this.SetupDefaults(PlatformID.Unix);
152+
string stateDirectory = this.mockFixture.GetStatePath();
153+
154+
Dictionary<string, string> expressions = new Dictionary<string, string>
155+
{
156+
{ "{StatePath}", stateDirectory },
157+
{ "{statepath}", stateDirectory },
158+
{ "{statedir}", stateDirectory },
159+
{ "{STATEPATH}", stateDirectory },
160+
{ "{STATEDIR}", stateDirectory }
161+
};
162+
163+
foreach (var entry in expressions)
164+
{
165+
string expectedExpression = entry.Value;
166+
string actualExpression = await ProfileExpressionEvaluator.Instance.EvaluateAsync(this.mockFixture.Dependencies, entry.Key);
167+
Assert.AreEqual(expectedExpression, actualExpression);
168+
}
169+
}
170+
102171
[Test]
103172
public async Task ProfileExpressionEvaluatorSupportsWellKnownExpressionTempPathReferencesOnUnixSystems()
104173
{

src/VirtualClient/VirtualClient.Core/ProfileExpressionEvaluator.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ public class ProfileExpressionEvaluator : IExpressionEvaluator
6161
@"\{(?:PackagePath|PackageDir)/Platform\:([a-z0-9-_\. ]+)\}",
6262
RegexOptions.Compiled | RegexOptions.IgnoreCase);
6363

64+
// e.g.
65+
// {StatePath}, {StateDir}
66+
private static readonly Regex StatePathExpression = new Regex(
67+
@"\{(?:StatePath|StateDir)\}",
68+
RegexOptions.Compiled | RegexOptions.IgnoreCase);
69+
6470
// e.g.
6571
// {TempPath}, {TempDir}
6672
private static readonly Regex TempPathExpression = new Regex(
@@ -154,6 +160,30 @@ public class ProfileExpressionEvaluator : IExpressionEvaluator
154160
Outcome = evaluatedExpression
155161
});
156162
}),
163+
// Expression: {StatePath|StateDir}
164+
// Resolves to the path to the state folder location (e.g. /home/users/virtualclient/state).
165+
new Func<IServiceCollection, IDictionary<string, IConvertible>, string, Task<EvaluationResult>>((dependencies, parameters, expression) =>
166+
{
167+
bool isMatched = false;
168+
string evaluatedExpression = expression;
169+
MatchCollection matches = ProfileExpressionEvaluator.StatePathExpression.Matches(expression);
170+
171+
if (matches?.Any() == true)
172+
{
173+
isMatched = true;
174+
PlatformSpecifics platformSpecifics = dependencies.GetService<PlatformSpecifics>();
175+
foreach (Match match in matches)
176+
{
177+
evaluatedExpression = Regex.Replace(evaluatedExpression, match.Value, platformSpecifics.StateDirectory);
178+
}
179+
}
180+
181+
return Task.FromResult(new EvaluationResult
182+
{
183+
IsMatched = isMatched,
184+
Outcome = evaluatedExpression
185+
});
186+
}),
157187
// Expression: {OS}
158188
// Resolves to the current operating system (e.g. linux, windows).
159189
new Func<IServiceCollection, IDictionary<string, IConvertible>, string, Task<EvaluationResult>>((dependencies, parameters, expression) =>

0 commit comments

Comments
 (0)