Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Mock.cs « ILLink.Tasks.Tests « test - github.com/mono/linker.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2eb3a879707afe9dc70df91d6d573254a6ed20e8 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Build.Framework;
using Mono.Linker;
using Mono.Linker.Steps;
using Xunit;

namespace ILLink.Tasks.Tests
{
	public class MockTask : ILLink
	{

		public List<(MessageImportance Importance, string Line)> Messages { get; } = new List<(MessageImportance Importance, string Line)> ();

		public MockTask ()
		{
			// Ensure that [Required] members are non-null
			AssemblyPaths = new ITaskItem[0];
			RootAssemblyNames = new ITaskItem[0];
		}

		public MockDriver CreateDriver ()
		{
			string[] responseFileLines = GenerateResponseFileCommands ().Split (Environment.NewLine);
			var arguments = new Queue<string> ();
			Driver.ParseResponseFileLines (responseFileLines, arguments);
			return new MockDriver (arguments);
		}

		public static string[] OptimizationNames {
			get {
				var field = typeof (ILLink).GetField ("_optimizationNames", BindingFlags.NonPublic | BindingFlags.Static);
				return (string[]) field.GetValue (null);
			}
		}

		public void SetOptimization (string optimization, bool enabled)
		{
			var property = typeof (ILLink).GetProperty (optimization);
			property.GetSetMethod ().Invoke (this, new object[] { enabled });
		}

		static readonly string[] nonOptimizationBooleanProperties = new string[] {
			"DumpDependencies",
			"RemoveSymbols",
			"TreatWarningsAsErrors"
		};

		public static IEnumerable<string> GetOptimizationPropertyNames ()
		{
			foreach (var property in typeof (ILLink).GetProperties (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)) {
				if (property.PropertyType != typeof (bool))
					continue;
				if (nonOptimizationBooleanProperties.Contains (property.Name))
					continue;
				yield return property.Name;
			}
		}

		protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance) => Messages.Add ((messageImportance, singleLine));
	}

	public class MockBuildEngine : IBuildEngine
	{
		public void LogErrorEvent (BuildErrorEventArgs e) { }
		public void LogWarningEvent (BuildWarningEventArgs e) { }
		public void LogMessageEvent (BuildMessageEventArgs e) { }
		public void LogCustomEvent (CustomBuildEventArgs e) { }
		public bool BuildProjectFile (string projectFileName, string[] targetNames, IDictionary globalProperties, IDictionary targetOutputs) => false;
		public bool ContinueOnError => false;
		public int LineNumberOfTaskNode => 0;
		public int ColumnNumberOfTaskNode => 0;
		public string ProjectFileOfTaskNode => null;
	}

	public class MockDriver : Driver
	{
		public MockDriver (Queue<string> arguments) : base (arguments)
		{
			// Always add a dummy root assembly for testing purposes (otherwise Driver fails without roots).
			arguments.Enqueue ("-a");
			arguments.Enqueue ("DummyRootAssembly");
			// Always set up the context early on.
			Assert.Equal (0, SetupContext ());
		}

		public LinkContext Context => context;

		public IEnumerable<string> GetRootAssemblies ()
		{
			foreach (var step in context.Pipeline.GetSteps ()) {
				if (!(step is ResolveFromAssemblyStep))
					continue;

				var assemblyName = (string) (typeof (ResolveFromAssemblyStep).GetField ("_file", BindingFlags.NonPublic | BindingFlags.Instance).GetValue (step));
				if (assemblyName == "DummyRootAssembly")
					continue;

				yield return assemblyName;
			}
		}

		public IEnumerable<string> GetRootDescriptors ()
		{
			foreach (var step in context.Pipeline.GetSteps ()) {
				if (!(step is ResolveFromXmlStep))
					continue;

				var descriptor = (string) (typeof (ResolveFromXmlStep).GetField ("_xmlDocumentLocation", BindingFlags.NonPublic | BindingFlags.Instance).GetValue (step));

				yield return descriptor;
			}
		}

		public IEnumerable<string> GetReferenceAssemblies ()
		{
			return (IEnumerable<string>) (typeof (AssemblyResolver).GetField ("_references", BindingFlags.NonPublic | BindingFlags.Instance).GetValue (context.Resolver));
		}

		protected override void AddResolveFromXmlStep (Pipeline pipeline, string file)
		{
			// Don't try to load an xml file - just pretend it exists.
			pipeline.PrependStep (new ResolveFromXmlStep (document: null, file));
		}

		protected override void AddXmlDependencyRecorder (LinkContext context, string file)
		{
			// Don't try to open the output file for writing - just pretend it exists.
			Context.Tracer.AddRecorder (MockXmlDependencyRecorder.Singleton);
		}

		public IEnumerable<IDependencyRecorder> GetDependencyRecorders ()
		{
			return (IEnumerable<IDependencyRecorder>) (typeof (Tracer).GetField ("recorders", BindingFlags.NonPublic | BindingFlags.Instance).GetValue (context.Tracer));
		}

		public new bool GetOptimizationName (string optimization, out CodeOptimizations codeOptimizations)
		{
			return base.GetOptimizationName (optimization, out codeOptimizations);
		}

		public CodeOptimizations GetDefaultOptimizations ()
		{
			var context = base.GetDefaultContext (null);
			return context.Optimizations.Global;
		}

		public Dictionary<string, string> GetCustomData ()
		{
			var field = typeof (LinkContext).GetField ("_parameters", BindingFlags.NonPublic | BindingFlags.Instance);
			return (Dictionary<string, string>) field.GetValue (this.context);
		}
	}

	public class MockXmlDependencyRecorder : IDependencyRecorder
	{
		public static MockXmlDependencyRecorder Singleton = new MockXmlDependencyRecorder ();
		public void RecordDependency (object source, object arget, bool marked) { }
		public void RecordDependency (object target, in DependencyInfo reason, bool marked) { }
	}

	public class MockCustomStep : IStep
	{
		public void Process (LinkContext context) { }
	}

}