Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests with MS Test fail with bad System.Text.Json reference #43

Open
Martin4Kostal opened this issue Jan 30, 2024 · 5 comments
Open

Comments

@Martin4Kostal
Copy link

Martin4Kostal commented Jan 30, 2024

  • I have written a small wrapper with some XLIFF handling using DeepL.NET.

    • When creating unit test with MS Test, the test failed with a System.IO.FileLoad.Exception for System.Text.Json.
    • When I use the same code in an executable, it worked fine.
  • Next, I have created some code which uses DeepL.NET directly.

    • Again, it works in the executable:
namespace Test_DeepLLibExe
{
  internal class Program
  {
    static async Task Main(string[] args)
    {

      const string ApiKey = "xxxxx";
      try
      {
        using (Translator client = new Translator(ApiKey))
        {
          var languages = await client.GetTargetLanguagesAsync();
        }

      }
      catch (Exception ex)
      {
        Console.WriteLine($"ERROR: {ex.Message}");
      }
. . .
+ => list of 31 languages
  • And fails, when called from a unit test with MS Test:
    [TestMethod]
    public async Task Test_DeepLDirect()
    {
      _logger.Info($"--- Starting {GetCurrentMethod()} ...");
      const string ApiKey = "xxxxx";
      try
      {
        using (Translator client = new Translator(ApiKey))
        {
          var languages = await client.GetTargetLanguagesAsync();
        }
      }
      catch (Exception ex)
      {
        _logger.Error($"Global error in {GetCurrentMethod()}: {ex.Message}");
      }
    }
+ => System.IO.FileLoad.Exception for System.Text.Json 
  + Message: "Could not load file or assembly 'System.Text.Json, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)":"System.Text.Json, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"
  + Source: "DeepL.net"
  + StackTrace: "   at DeepL.Internal.DeepLClient.<CheckStatusCodeAsync>d__10.MoveNext()\r\n   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine)\r\n   at DeepL.Internal.DeepLClient.CheckStatusCodeAsync(HttpResponseMessage responseMessage, Boolean usingGlossary, Boolean downloadingDocument)\r\n   at DeepL.Translator.<GetLanguagesAsync>d__30`1.MoveNext()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at DeepL.Translator.<GetTargetLanguagesAsync>d__19.MoveNext()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at Test_Kostal.ActivePresenter.DeepLLib.Test_DeepLRunner.<Test_DeepLDirect>d__28.MoveNext() in C:\\Users\\wagner06\\source\\APH\\activepresenterlib\\Test_DeepLLib\\Test_DeepLRunner.cs:line 277"

(screenshots have been inserted and uploaded, but that seems to be blocked)

  • In all cases, the binary folder contains System.Text.Json.dll in version 8.0.123.58001, so I do not understand the reference failure.

Expected behaviour: The code should be testable with MSTest without problems.

  • I have been using
    • VisualStudio 2022 (Version 17.7.0)
    • .NET Framework 4.8.1,
    • MSTest.TestAdapter/TestFramework (V3.2.0),
    • DeepL.net (Version 1.8.0) loaded with nuget and referenced with package references.

Additional info: A colleague from IT meant, that an improper nuget package definition might cause this problem.

@JanEbbing
Copy link
Member

Hi, thanks for this detailed report.

from what I can tell, your project is picking up System.Text.Json in Version 5.0.0, while DeepL.net requires 5.0.2 or higher. Presumably your executable uses the 8.0.1 version, while your Test for some reason uses 5.0.0. I can create a unit test with your provided code and steps that works on my machine. If you need further troubleshooting why your tests pick up the wrong version, I would need the csproj file for your unit tests.

Please also see this stackoverflow issue, it also links to the relevant MS documentation

@Martin4Kostal
Copy link
Author

Hi Jan,
thanks for your support. It still doesn't work.

  • Sorry, the stackoverflow issue with its referenced material is beyond my head.
  • From what I have checked in the meantime, it looks fine to me:
    • The .nuget\packages folder holds deepl.net with versions 1.3.0, 1.7.0, 1.7.1, 1.8.0 and system.text.json with versions 472., 5.0.2, 7.0.3, 8.0.1.
    • The nuspec of deepl.net, e.g. V1.8.0:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata>
    <id>DeepL.net</id>
    <version>1.8.0</version>
    <authors>DeepL SE</authors>
    <license type="expression">MIT</license>
    <licenseUrl>https://licenses.nuget.org/MIT</licenseUrl>
    <icon>icon.png</icon>
    <projectUrl>https://www.deepl.com/pro-api</projectUrl>
    <description>DeepL.net is the official DeepL .NET client library.</description>
    <releaseNotes>Release notes can be found at https://github.com/DeepLcom/deepl-dotnet/blob/main/CHANGELOG.md</releaseNotes>
    <tags>deepl translation api i18n language translator</tags>
    <readme>README.md</readme>
    <repository url="https://github.com/DeepLcom/deepl-dotnet" />
    <dependencies>
      <group targetFramework="net5.0">
        <dependency id="Microsoft.Extensions.Http.Polly" version="5.0.1" exclude="Build,Analyzers" />
        <dependency id="System.Text.Json" version="5.0.2" exclude="Build,Analyzers" />
      </group>
      <group targetFramework=".NETStandard2.0">
        <dependency id="Microsoft.Extensions.Http.Polly" version="5.0.1" exclude="Build,Analyzers" />
        <dependency id="System.Text.Json" version="5.0.2" exclude="Build,Analyzers" />
      </group>
    </dependencies>
  </metadata>
</package>
  • The .csproj of my DeepL-Unit-Tests:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{83CC30F3-A1DD-4FFB-A418-5CCD9A195EB2}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>Test_DeepLLib</RootNamespace>
    <AssemblyName>Test_DeepLLib</AssemblyName>
    <TargetFrameworkVersion>v4.8.1</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
    <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
    <IsCodedUITest>False</IsCodedUITest>
    <TestProjectType>UnitTest</TestProjectType>
    <TargetFrameworkProfile />
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
  </ItemGroup>
  <Choose>
    <When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
      <ItemGroup>
        <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
      </ItemGroup>
    </When>
    <Otherwise />
  </Choose>
  <ItemGroup>
    <Compile Include="Properties\AssemblyInfo.cs" />
    <Compile Include="Test_DeepLRunner.cs" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="DeepL.net">
      <Version>1.8.0</Version>
    </PackageReference>
    <PackageReference Include="MSTest.TestAdapter">
      <Version>3.2.0</Version>
    </PackageReference>
    <PackageReference Include="MSTest.TestFramework">
      <Version>3.2.0</Version>
    </PackageReference>
    <PackageReference Include="NLog">
      <Version>5.2.8</Version>
    </PackageReference>
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\DeepLLib\DeepLLib.csproj">
      <Project>{ff9e6c31-f06a-456b-afb3-4a867ff2ff8e}</Project>
      <Name>DeepLLib</Name>
    </ProjectReference>
    <ProjectReference Include="..\Test_APFileLib\Test_APFileLib.csproj">
      <Project>{67d67031-6f57-4798-aca5-cfe71d51efff}</Project>
      <Name>Test_APFileLib</Name>
    </ProjectReference>
  </ItemGroup>
  <Choose>
    <When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
      <ItemGroup>
        <Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
          <Private>False</Private>
        </Reference>
        <Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
          <Private>False</Private>
        </Reference>
        <Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
          <Private>False</Private>
        </Reference>
        <Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
          <Private>False</Private>
        </Reference>
      </ItemGroup>
    </When>
  </Choose>
  <Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>
  • I cannot find a "manifest" file in the bin folder structure.
  • The files in the obj folder structure (FileListAbsolute.txt, AssemblyReference.cache, ...) only contain references to System.Text.Json files which do exist

Anything else that I could check?
BR Martin

@Martin4Kostal
Copy link
Author

I also did the following:

  • Create new class library solution (Framework 4.8.1)
    • Create class with method GetDate() (which returns the current date)
    • Create unit test via VisualStudio wizzard (rightclick - create unit tests)
    • Debug GetDateTest => runs fine
  • Update MSTest from 2.10 to 3.2.0 (using Manage nuget packages for solution)
    • Debug GetDateTest => runs fine
  • Add reference to DeepL.net (V1.8 using nuget) and create method GetLanguages() (same code as above).
    • Debug GetDateTest => runs fine
  • Create unit test GetLanguagesTest()
    • Debug GetLanguageTest() => known exception (System.IO.FileLoad System.Text.Json V5.0.0.0). Although both folders (class\bin\debug and test\bin\debug) contain System.Text.Json in V5.0.5 !

@Martin4Kostal
Copy link
Author

Just retested with V1.9.0. Still the same problems.
Any news on this? Can I help somehow / provide more information / test something?
I am still stuck on the obsolete inofficial DeepL lib and I am getting problems with our IT security. But I cannot change to DeeL.net without successfull unit tests for our custom functionalities.

@Martin4Kostal
Copy link
Author

  • Tried to investigate further with the following setup
    • DeepL.Net V1.9.0
    • MSTest.TestFramework/TestAdapter V3.4.1/V3.1.1
    • VisualStudio V17.10.0
  • Calling the same routine (GetSourceLanguagesAsync) from executable and MSTest
    • exe: Method runs successfully
    • MSTest: fails at the same internal method with no exception (or complaining about missing System.Test.JSON). Other behaviour than before!
      • Up to this point, only okay messages in debug output
        image
      • Stepping over line 154, causes first thread exit
        image
      • Stepping over the using HttpRequestMessage ..., causes the next thread exit
        image
      • execution of or stepping into line 167, causes two more threads to exit, multiple ThreadAbortionExceptions are thrown, dlls are unloaded, the program stops.
        image
      • The try/catch around the GetSourceLanguagesAsync does not catch any of the exceptions.
        image
  • I hope this information helps you to solve the problem or allows you to point me at a work-around.
    Best regards,
    Martin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants