Skip to content

Commit

Permalink
Restore write-resource-to-text tests (#4830)
Browse files Browse the repository at this point in the history
Closes #308
  • Loading branch information
rainersigwald authored Oct 24, 2019
2 parents 72f86ff + 2c99582 commit 455f5be
Show file tree
Hide file tree
Showing 19 changed files with 165 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<XunitOptions Condition="'$(OsEnvironment)'=='windows' and '$(MonoBuild)' == 'true'">$(XunitOptions) -notrait category=mono-windows-failing</XunitOptions>

<XunitOptions Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">$(XunitOptions) -notrait category=nonnetcoreapptests</XunitOptions>
<XunitOptions Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">$(XunitOptions) -notrait category=nonnetfxtests</XunitOptions>

<XunitOptions>$(XunitOptions) -notrait category=failing</XunitOptions>

Expand Down
59 changes: 41 additions & 18 deletions src/Tasks.UnitTests/ResourceHandling/GenerateResource_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1713,11 +1713,7 @@ public void DuplicateResourceNames()
/// <summary>
/// Non-string resource with text output
/// </summary>
#if RUNTIME_TYPE_NETCORE
[Fact (Skip = "https://github.com/Microsoft/msbuild/issues/308")]
#else
[Fact]
#endif
public void UnsupportedTextType()
{
string bitmap = Utilities.CreateWorldsSmallestBitmap();
Expand Down Expand Up @@ -1776,11 +1772,8 @@ public void InvalidStateFile()
/// <summary>
/// Cause failures in ResourceReader
/// </summary>
#if RUNTIME_TYPE_NETCORE
[Fact (Skip = "https://github.com/Microsoft/msbuild/issues/308")]
#else
[Fact]
#endif
[SkipOnTargetFramework(TargetFrameworkMonikers.Netcoreapp, ".NET Core MSBuild doesn't try to read binary input resources")]
public void FailedResourceReader()
{
GenerateResource t = Utilities.CreateTask(_output);
Expand All @@ -1807,6 +1800,28 @@ public void FailedResourceReader()
File.Delete(item.ItemSpec);
}

[Theory]
[InlineData(".resources")]
[InlineData(".dll")]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "This error is .NET Core only")]
public void ResourceReaderRejectsNonCoreCompatFormats(string inputExtension)
{
using var env = TestEnvironment.Create(_output);

GenerateResource t = Utilities.CreateTask(_output);
t.StateFile = new TaskItem(env.GetTempFile(".cache").Path);

// file contents aren't required since the extension is checked first
var resourcesFile = env.CreateFile(inputExtension).Path;

t.Sources = new ITaskItem[] { new TaskItem(resourcesFile) };
t.OutputResources = new ITaskItem[] { new TaskItem(env.GetTempFile(".resources").Path) };

t.Execute().ShouldBeFalse();

Utilities.AssertLogContains(t, "MSB3824");
}

/// <summary>
/// Invalid STR Class name
/// </summary>
Expand Down Expand Up @@ -1844,11 +1859,9 @@ public void FailedSTRProperty()
/// <summary>
/// Reference passed in that can't be loaded should error
/// </summary>
#if RUNTIME_TYPE_NETCORE
[Fact (Skip = "https://github.com/Microsoft/msbuild/issues/308")]
#else
[Fact]
#endif
[SkipOnTargetFramework(TargetFrameworkMonikers.Netcoreapp,
reason: ".NET Core MSBuild doesn't load refs so it pushes this failure to runtime")]
public void InvalidReference()
{
string txtFile = null;
Expand Down Expand Up @@ -3700,7 +3713,7 @@ public static string GetTestResXContent(bool useType, string linkedBitmap, strin
/// <param name="useType">Indicates whether to include an enum to test type-specific resource encoding with assembly references</param>
/// <param name="linkedBitmap">The name of a linked-in bitmap. use 'null' for no bitmap.</param>
/// <returns>The name of the resx file</returns>
public static string WriteTestResX(bool useType, string linkedBitmap, string extraToken, string resxFileToWrite = null)
public static string WriteTestResX(bool useType, string linkedBitmap, string extraToken, string resxFileToWrite = null, TestEnvironment env = null)
{
return WriteTestResX(useType, linkedBitmap, extraToken, useInvalidType: false, resxFileToWrite:resxFileToWrite);
}
Expand All @@ -3711,16 +3724,26 @@ public static string WriteTestResX(bool useType, string linkedBitmap, string ext
/// <param name="useType">Indicates whether to include an enum to test type-specific resource encoding with assembly references</param>
/// <param name="linkedBitmap">The name of a linked-in bitmap. use 'null' for no bitmap.</param>
/// <returns>The name of the resx file</returns>
public static string WriteTestResX(bool useType, string linkedBitmap, string extraToken, bool useInvalidType, string resxFileToWrite = null)
public static string WriteTestResX(bool useType, string linkedBitmap, string extraToken, bool useInvalidType, string resxFileToWrite = null, TestEnvironment env = null)
{
string resgenFile = resxFileToWrite;
if (string.IsNullOrEmpty(resgenFile))

string contents = GetTestResXContent(useType, linkedBitmap, extraToken, useInvalidType);

if (env == null)
{
if (string.IsNullOrEmpty(resgenFile))
{
resgenFile = GetTempFileName(".resx");
}

File.WriteAllText(resgenFile, contents);
}
else
{
resgenFile = GetTempFileName(".resx");
File.Delete(resgenFile);
resgenFile = env.CreateFile(".resx", contents).Path;
}

File.WriteAllText(resgenFile, GetTestResXContent(useType, linkedBitmap, extraToken, useInvalidType));
return resgenFile;
}

Expand Down
29 changes: 21 additions & 8 deletions src/Tasks/GenerateResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2603,6 +2603,12 @@ private bool ProcessFile(string inFile, string outFileOrDir)
{
ReadResources(inFile, _useSourcePath, outFileOrDir);
}
catch (InputFormatNotSupportedException)
{
_logger.LogErrorWithCodeFromResources(null, FileUtilities.GetFullPathNoThrow(inFile), 0, 0, 0, 0,
"GenerateResource.CoreSupportsLimitedScenarios");
return false;
}
catch (MSBuildResXException msbuildResXException)
{
_logger.LogErrorWithCodeFromResources(null, FileUtilities.GetFullPathNoThrow(inFile), 0, 0, 0, 0,
Expand Down Expand Up @@ -2988,7 +2994,7 @@ private void ReadResources(String filename, bool shouldUseSourcePath, String out
#if FEATURE_ASSEMBLY_LOADFROM
ReadAssemblyResources(filename, outFileOrDir);
#else
_logger.LogError("Reading resources from Assembly not supported on .NET Core MSBuild");
throw new InputFormatNotSupportedException("Reading resources from Assembly not supported on .NET Core MSBuild");
#endif
}
else
Expand Down Expand Up @@ -3046,10 +3052,10 @@ private void ReadResources(String filename, bool shouldUseSourcePath, String out
case Format.Binary:
#if FEATURE_RESX_RESOURCE_READER
ReadResources(reader, new ResourceReader(filename), filename); // closes reader for us
break;
#else
_logger.LogError("ResGen.exe not supported on .NET Core MSBuild");
throw new InputFormatNotSupportedException("Reading resources from binary .resources not supported on .NET Core MSBuild");
#endif
break;

default:
// We should never get here, we've already checked the format
Expand Down Expand Up @@ -3387,8 +3393,13 @@ private bool HaveSystemResourcesExtensionsReference

PopulateAssemblyNames();

foreach (var assemblyName in _assemblyNames)
foreach (AssemblyNameExtension assemblyName in _assemblyNames)
{
if (assemblyName == null)
{
continue;
}

if (string.Equals(assemblyName.Name, "System.Resources.Extensions", StringComparison.OrdinalIgnoreCase))
{
_haveSystemResourcesExtensionsReference = true;
Expand Down Expand Up @@ -3761,14 +3772,16 @@ private void WriteTextResources(ReaderInfo reader, String fileName)
{
using (StreamWriter writer = FileUtilities.OpenWrite(fileName, false, Encoding.UTF8))
{
foreach (LiveObjectResource entry in reader.resources)
foreach (IResource resource in reader.resources)
{
String key = entry.Name;
Object v = entry.Value;
LiveObjectResource entry = resource as LiveObjectResource;

String key = entry?.Name;
Object v = entry?.Value;
String value = v as String;
if (value == null)
{
_logger.LogErrorWithCodeFromResources(null, fileName, 0, 0, 0, 0, "GenerateResource.OnlyStringsSupported", key, v.GetType().FullName);
_logger.LogErrorWithCodeFromResources(null, fileName, 0, 0, 0, 0, "GenerateResource.OnlyStringsSupported", key, v?.GetType().FullName);
}
else
{
Expand Down
28 changes: 28 additions & 0 deletions src/Tasks/ResourceHandling/InputFormatNotSupportedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Runtime.Serialization;

namespace Microsoft.Build.Tasks.ResourceHandling
{
[Serializable]
internal class InputFormatNotSupportedException : Exception
{
public InputFormatNotSupportedException()
{
}

public InputFormatNotSupportedException(string message) : base(message)
{
}

public InputFormatNotSupportedException(string message, Exception innerException) : base(message, innerException)
{
}

protected InputFormatNotSupportedException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
4 changes: 4 additions & 0 deletions src/Tasks/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,10 @@
<value>MSB3823: Non-string resources require the property GenerateResourceUsePreserializedResources to be set to true.</value>
<comment>{StrBegin="MSB3823: "}</comment>
</data>
<data name="GenerateResource.CoreSupportsLimitedScenarios">
<value>MSB3824: In order to build with .NET Core, resource inputs must be in .txt or .resx format.</value>
<comment>{StrBegin="MSB3824: "}</comment>
</data>


<!--
Expand Down
5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.en.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 455f5be

Please sign in to comment.