Add support for ToolsVersion and correctly build msbuild+xbuild assemblies
[mcs.git] / class / Microsoft.Build.Tasks / Test / Microsoft.Build.Tasks / CreateCSharpManifestResourceNameTest.cs
blob47f2b75e14f354a54e36a80fe769c70523d96964
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Text;
6 using NUnit.Framework;
7 using Microsoft.Build.BuildEngine;
9 namespace MonoTests.Microsoft.Build.Tasks
11 [TestFixture]
12 public class CreateCSharpManifestResourceNameTest
15 string [,] resx_no_culture_files, resx_with_culture_files;
16 string [,] non_resx_no_culture_files, non_resx_with_culture_files;
18 public CreateCSharpManifestResourceNameTest ()
20 string sample_cs_path = Path.Combine ("Test", Path.Combine ("resources", "Sample.cs"));
21 string junk_file = Path.Combine ("Test", Path.Combine ("resources", "junk.txt"));
22 string curdir = Path.GetDirectoryName (Environment.CurrentDirectory);
24 /* {Include, LogicalName, DependentUpon, TargetPath} */
25 resx_no_culture_files = new string [,] {
26 // With dependent file
27 { "foo with space.resx", null, sample_cs_path, null },
28 { "foo with space.resx", "RandomName", sample_cs_path, null },
29 { "foo with space.resx", "RandomName", sample_cs_path, "bar with space.resx" },
31 // can't find a C# class in the .vb file
32 { "foo with space.resx", "RandomName", junk_file, "bar with space.resx" },
34 { "Test/resources/foo with space.resx", null, "Sample.cs", null },
35 { "Test/resources/foo with space.resx", "RandomName", "Sample.cs", null },
36 { "Test/resources/foo with space.resx", "RandomName", "Sample.cs", "bar with space.resx"},
38 // W/o dependent file
39 { "foo with space.resx", null, null, null },
40 { "foo with space.resx", "RandomName", null, null },
42 { "Test/resources folder/foo with space.resx", null, null, null },
43 { "Test/resources folder/foo with space.resx", "RandomName", null, null },
46 resx_with_culture_files = new string [,] {
47 // With dependent file
48 { "foo with space.de.resx", null, sample_cs_path, null },
49 { "foo with space.de.resx", "RandomName", sample_cs_path, null },
50 { "foo with space.de.resx", "RandomName", sample_cs_path, "bar with space.fr.resx" },
52 // can't find a C# class in the .vb file
53 { "foo with space.de.resx", "RandomName", junk_file, "bar with space.fr.resx" },
55 { "Test/resources/foo with space.de.resx", null, "Sample.cs", null },
56 { "Test/resources/foo with space.de.resx", "RandomName", "Sample.cs", null},
58 // W/o dependent file
59 { "foo with space.de.resx", null, null, null },
60 { "foo with space.de.resx", "RandomName", null, null },
62 { "Test/resources folder/foo with space.de.resx", null, null, null },
63 { "Test/resources folder/foo with space.de.resx", "RandomName", null, null }
66 non_resx_no_culture_files = new string [,] {
67 { "foo with space.txt", null, null },
68 { "foo with space.txt", "RandomName", null },
70 { "Test/resources folder/foo with space.txt", null, null },
71 { "Test/resources folder/foo with space.txt", "RandomName", null }
74 non_resx_with_culture_files = new string [,] {
75 { "foo with space.de.txt", null, null },
76 { "foo with space.de.txt", "RandomName", null },
78 { "Test/resources folder/foo with space.de.txt", null, null },
79 { "Test/resources folder/foo with space.de.txt", "RandomName", null }
84 [Test]
85 public void TestNoRootNamespaceNoCulture ()
87 CheckResourceNames (resx_no_culture_files, new string [] {
88 // w/ dependent file
89 "Mono.Tests.Sample", "Mono.Tests.Sample",
90 "Mono.Tests.Sample", "bar with space",
91 "Mono.Tests.Sample", "Mono.Tests.Sample",
93 // W/o dependent file
94 "Mono.Tests.Sample", "foo with space" ,
95 "foo with space", "Test.resources_folder.foo with space",
96 "Test.resources_folder.foo with space",
97 }, null);
100 [Test]
101 public void TestWithRootNamespaceNoCulture ()
103 //FIXME: How does LogicalName affect things??
104 CheckResourceNames (resx_no_culture_files, new string [] {
105 // With dependent file
106 "Mono.Tests.Sample", "Mono.Tests.Sample",
107 "Mono.Tests.Sample", "RN1.RN2.bar with space",
108 "Mono.Tests.Sample", "Mono.Tests.Sample",
110 // W/o dependent file
111 "Mono.Tests.Sample", "RN1.RN2.foo with space",
112 "RN1.RN2.foo with space", "RN1.RN2.Test.resources_folder.foo with space",
113 "RN1.RN2.Test.resources_folder.foo with space"},
114 "RN1.RN2");
117 [Test]
118 public void TestNoRootNamespaceWithCulture ()
120 CheckResourceNames (resx_with_culture_files, new string [] {
121 // With dependent file
122 "Mono.Tests.Sample.de", "Mono.Tests.Sample.de",
123 "Mono.Tests.Sample.fr", "bar with space.fr",
125 "Mono.Tests.Sample.de", "Mono.Tests.Sample.de",
127 // W/o dependent file
128 "foo with space.de", "foo with space.de",
129 "Test.resources_folder.foo with space.de", "Test.resources_folder.foo with space.de" }, null);
132 [Test]
133 public void TestWithRootNamespaceWithCulture ()
135 CheckResourceNames (resx_with_culture_files, new string [] {
136 // With dependent file
137 "Mono.Tests.Sample.de", "Mono.Tests.Sample.de",
138 "Mono.Tests.Sample.fr", "RN1.RN2.bar with space.fr",
139 "Mono.Tests.Sample.de", "Mono.Tests.Sample.de",
140 // W/o dependent file
141 "RN1.RN2.foo with space.de", "RN1.RN2.foo with space.de",
142 "RN1.RN2.Test.resources_folder.foo with space.de", "RN1.RN2.Test.resources_folder.foo with space.de"},
143 "RN1.RN2");
146 [Test]
147 public void TestNonResxNoRootNamespaceWithCulture ()
149 CheckResourceNames (non_resx_with_culture_files, new string [] {
150 Path.Combine ("de", "foo with space.txt"), Path.Combine ("de", "foo with space.txt"),
151 Path.Combine ("de", "Test.resources_folder.foo with space.txt"), Path.Combine ("de", "Test.resources_folder.foo with space.txt")}, null);
155 [Test]
156 public void TestNonResxWithRootNamespaceWithCulture ()
158 CheckResourceNames (non_resx_with_culture_files, new string [] {
159 Path.Combine ("de", "RN1.RN2.foo with space.txt"), Path.Combine ("de", "RN1.RN2.foo with space.txt"),
160 Path.Combine ("de", "RN1.RN2.Test.resources_folder.foo with space.txt"), Path.Combine ("de", "RN1.RN2.Test.resources_folder.foo with space.txt")},
161 "RN1.RN2");
164 [Test]
165 public void TestNonResxNoRootNamespaceNoCulture ()
167 CheckResourceNames (non_resx_no_culture_files, new string [] {
168 "foo with space.txt", "foo with space.txt",
169 "Test.resources_folder.foo with space.txt", "Test.resources_folder.foo with space.txt"}, null);
172 [Test]
173 public void TestNonResxWithRootNamespaceNoCulture ()
175 CheckResourceNames (non_resx_no_culture_files, new string [] {
176 // With dependent file
177 "RN1.RN2.foo with space.txt", "RN1.RN2.foo with space.txt",
178 "RN1.RN2.Test.resources_folder.foo with space.txt", "RN1.RN2.Test.resources_folder.foo with space.txt"},
179 "RN1.RN2");
182 [Test]
183 public void TestExternalResourcesNoRootNamespaceWithTargetPath ()
185 CheckResourceNames (new string[,] {
186 {"../folder/foo.txt", null, null, "abc.txt"},
187 {"../folder/foo.de.txt", null, null, "xyz.txt"}},
188 new string[] { "abc.txt", "xyz.txt" }, null);
191 [Test]
192 public void TestExternalResourcesWithRootNamespaceWithTargetPath ()
194 CheckResourceNames (new string[,] {
195 {"../folder/foo.txt", null, null, "abc.txt"},
196 {"../folder/foo.de.txt", null, null, "xyz.txt"}},
197 new string[] { "RN.abc.txt", "RN.xyz.txt" }, "RN");
200 [Test]
201 public void TestExternalResourcesNoRootNamespaceNoTargetPath ()
203 CheckResourceNames (new string[,] {
204 {"../folder/foo.txt", null, null},
205 {"../folder/foo.de.txt", null, null}},
206 new string[] { "...folder.foo.txt", Path.Combine ("de", "...folder.foo.txt") }, null);
209 [Test]
210 public void TestExternalResourcesWithRootNamespaceNoTargetPath ()
212 CheckResourceNames (new string[,] {
213 {"../folder/foo.txt", null, null},
214 {"../folder/foo.de.txt", null, null}},
215 new string[] { "RN....folder.foo.txt", Path.Combine ("de", "RN....folder.foo.txt") }, "RN");
218 [Test]
219 public void TestInvalidCulture ()
221 string [,] files = new string [,] {
222 { "Foo.invalid.txt", null, null },
223 { "Foo.invalid.resx", null, null }
225 CheckResourceNames (files, new string [] {"RN1.RN2.Foo.invalid.txt", "RN1.RN2.Foo.invalid"},
226 "RN1.RN2");
229 void CheckResourceNames (string [,] files, string [] names, string rootNamespace)
231 Assert.AreEqual (files.GetUpperBound (0) + 1, names.Length, "Number of files and names must match");
232 string projectText = CreateProject (files, rootNamespace);
234 Engine engine = new Engine (Consts.BinPath);
235 Project project = engine.CreateNewProject ();
236 TestMessageLogger logger = new TestMessageLogger ();
237 engine.RegisterLogger (logger);
238 project.LoadXml (projectText);
239 if (!project.Build ("1")) {
240 Console.WriteLine (projectText);
241 logger.DumpMessages ();
242 Assert.Fail ("Build failed");
245 bool has_targetpaths = files.GetUpperBound (1) == 3;
246 BuildItemGroup group = project.GetEvaluatedItemsByName ("ResourceNames");
247 Assert.AreEqual (names.Length, group.Count, "A2");
248 for (int i = 0; i <= files.GetUpperBound (0); i++) {
249 Assert.AreEqual (names [i], group [i].FinalItemSpec, "A3 #" + (i + 1));
250 Assert.AreEqual (files [i, 1] != null, group [i].HasMetadata ("LogicalName"), "A4 #" + (i + 1));
251 if (files [i, 1] != null)
252 Assert.AreEqual (files [i, 1], group [i].GetMetadata ("LogicalName"), "A5 #" + (i + 1));
253 Assert.AreEqual (files [i, 2] != null, group [i].HasMetadata ("DependentUpon"), "A6 #" + (i + 1));
254 if (files [i, 2] != null)
255 Assert.AreEqual (files [i, 2], group [i].GetMetadata ("DependentUpon"), "A7 #" + (i + 1));
256 if (has_targetpaths && files [i, 3] != null)
257 Assert.AreEqual (files [i, 3], group [i].GetMetadata ("TargetPath"), "A8 #" + (i + 1));
261 string CreateProject (string [,] files, string rootNamespace)
263 StringBuilder sb = new StringBuilder ();
264 sb.Append ("<Project xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n");
266 bool has_targetpaths = files.GetUpperBound (1) == 3;
267 sb.Append ("\t<ItemGroup>\n");
268 for (int i = 0; i <= files.GetUpperBound (0); i ++) {
269 sb.AppendFormat ("\t\t<ResourceFiles Include = \"{0}\">\n", files [i, 0]);
270 if (has_targetpaths && files [i, 3] != null)
271 sb.AppendFormat ("\t\t\t<TargetPath>{0}</TargetPath>\n", files [i, 3]);
272 if (files [i, 1] != null)
273 sb.AppendFormat ("\t\t\t<LogicalName>{0}</LogicalName>\n", files [i, 1]);
274 if (files [i, 2] != null)
275 sb.AppendFormat ("\t\t\t<DependentUpon>{0}</DependentUpon>\n", files [i, 2]);
276 sb.AppendFormat ("\t\t</ResourceFiles>\n");
278 sb.Append ("\t</ItemGroup>\n");
280 sb.Append ("\t<Target Name=\"1\">\n");
281 sb.Append ("\t\t<CreateCSharpManifestResourceName ResourceFiles=\"@(ResourceFiles)\" ");
282 if (rootNamespace != null)
283 sb.AppendFormat (" RootNamespace = \"{0}\"", rootNamespace);
284 sb.Append (">\n \t\t\t<Output TaskParameter=\"ManifestResourceNames\" ItemName=\"ResourceNames\" />\n");
285 sb.Append ("\t\t</CreateCSharpManifestResourceName>\n\t</Target>\n");
286 sb.Append ("\t" + GetUsingTask ("CreateCSharpManifestResourceName"));
287 sb.Append ("</Project>");
289 return sb.ToString ();
292 string GetUsingTask (string taskName)
294 return "<UsingTask TaskName='Microsoft.Build.Tasks." + taskName + "' AssemblyFile='" + Consts.GetTasksAsmPath () + "' />";