-
Notifications
You must be signed in to change notification settings - Fork 1k
/
MockContentAssetPreprocessor.cs
59 lines (48 loc) · 2.14 KB
/
MockContentAssetPreprocessor.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using NuGet.Common;
namespace Microsoft.NET.Build.Tasks.UnitTests
{
public class MockContentAssetPreprocessor : IContentAssetPreprocessor
{
private Dictionary<string, string> _preprocessorValues = new Dictionary<string, string>();
private string _preprocessedOutputDirectory = null;
private readonly Func<string, bool> _exists;
public string MockReadContent { get; set; }
public string MockWrittenContent { get; set; }
public MockContentAssetPreprocessor(Func<string,bool> exists)
{
_exists = exists;
}
public void ConfigurePreprocessor(string outputDirectoryBase, Dictionary<string, string> preprocessorValues)
{
_preprocessorValues = preprocessorValues ?? new Dictionary<string, string>();
_preprocessedOutputDirectory = Path.Combine(outputDirectoryBase, "test");
}
public bool Process(string originalAssetPath, string relativeOutputPath, out string pathToFinalAsset)
{
bool fileWritten = false;
pathToFinalAsset = Path.Combine(_preprocessedOutputDirectory, relativeOutputPath);
if (!_exists(pathToFinalAsset))
{
// Mock reading, processing and writing content
var inputBytes = Encoding.ASCII.GetBytes(MockReadContent);
using (MemoryStream input = new MemoryStream(inputBytes))
{
string result = Preprocessor.Process(input, (token) =>
{
string value;
if (!_preprocessorValues.TryGetValue(token, out value))
{
throw new InvalidDataException($"The token '${token}$' is unrecognized");
}
return value;
});
MockWrittenContent = result;
fileWritten = true;
}
}
return fileWritten;
}
}
}