简体   繁体   English

C#Directory.GetCurrentDirectory()返回带有NUnit的system32

[英]C# Directory.GetCurrentDirectory() returning system32 with NUnit

I am using a 3rd party nuget package for puppeteer. 我正在使用第3方nuget程序包进行伪装。 I cant get any code involving this library to work. 我无法获得与此库有关的任何代码。 I looked into the code and found the following line of code 我查看了代码,发现以下代码行

DownloadsFolder = Path.Combine(Directory.GetCurrentDirectory(), ".local-chromium");

The issue is that when I run a unit test Directory.GetCurrentDirectory() returns Windows/system32 and the code fails because it doesn't have permission to write to this directory. 问题是,当我运行单元测试Directory.GetCurrentDirectory()返回Windows/system32 ,代码失败,因为它没有写此目录的权限。

It would seem that this is intended behavior https://github.com/nunit/nunit/issues/1768 . 看来这是预期的行为https://github.com/nunit/nunit/issues/1768 The issue here is that I can't modify the source code of this dll so I am stuck. 这里的问题是我无法修改此dll的源代码,因此被卡住了。 Is there any way to modify nunit so that it returns a directory that I can define for Directory.GetCurrentDirectory() ? 有什么方法可以修改nunit,以便它返回我可以为Directory.GetCurrentDirectory()定义的Directory.GetCurrentDirectory()

If I understand correctly, the code that calls Directory.GetCurrentDirectory is in a library used by the application you are testing. 如果我理解正确,则调用Directory.GetCurrentDirectory的代码在您正在测试的应用程序使用的库中。 (If it were in your own NUnit tests, then this answer would have to be different.) This is a bad design choice for library code, since it assumes that the application calling the library has set (or left) the current directory to the appropriate location. (如果是在您自己的NUnit测试中,则答案必须有所不同。)对于库代码,这是一个错误的设计选择,因为它假定调用该库的应用程序已将当前目录设置(或保留)到该目录。适当的位置。 (IMO, it's OK for an application to do this, but not a library.) (IMO,应用程序可以执行此操作,但库不是可以的。)

I'm guessing that you are running under Visual Studio, since the current directory ends up being System32. 我猜您正在Visual Studio下运行,因为当前目录最终是System32。

By design, NUnit itself never changes the current directory, so it is left just as it was when the program was first run. 根据设计,NUnit本身永远不会更改当前目录,因此它会像程序第一次运行时一样保留。 You can change it in your tests, but there are risks in doing so. 您可以在测试中更改它,但是这样做存在风险。

That explains why you see the problem. 那解释了为什么您看到问题。 Here is a workaround. 这是一种解决方法。

  1. If you don't care (for test purposes) where the folder is located, create a temp folder in your test setup and remove it in teardown. 如果您不关心(出于测试目的)文件夹的位置,请在测试设置中创建一个临时文件夹,然后将其删除。 You can do this for each test (SetUp and TearDown) or for the entire fixture (OneTimeSetUp and OneTimeTearDown). 您可以对每个测试(SetUp和TearDown)或整个夹具(OneTimeSetUp和OneTimeTearDown)执行此操作。

  2. In the same setup location, set the current directory to that temporary folder, saving the original current directory. 在同一安装位置中,将当前目录设置为该临时文件夹,并保存原始当前目录。 Restore it in the appropriate teardown method. 用适当的拆卸方法将其还原。

  3. Ensure that no tests using the directory can run in parallel. 确保使用该目录的任何测试都不能并行运行。 If you don't use the ParallelizableAttribute at all you should be OK. 如果根本不使用ParallelizableAttribute ,则应该没问题。 But if you have set it to some value at the assembly or other higher level, mark the classes that contain these tests as [NonParallelizable] . 但是,如果您已在程序集或其他更高级别[NonParallelizable]其设置为某个值,请将包含这些测试的类标记为[NonParallelizable]

The last step is very important. 最后一步非常重要。 The current directory is set for the entire process, so it would affect all executing tests. 当前目录是为整个过程设置的,因此它将影响所有正在执行的测试。 It's important that no other tests run while the changed directory is in effect. 重要的是,在更改的目录生效时,不要再运行其他测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#Directory.GetCurrentDirectory和C以外的驱动器: - C# Directory.GetCurrentDirectory and drives other than C: C#:从浏览器启动时的Directory.getCurrentDirectory() - C#: Directory.getCurrentDirectory() when launching from Browser c#winforms GetCurrentDirectory返回C:\\ WINDOWS \\ System32 \\设计错误 - c# winforms GetCurrentDirectory returns C:\WINDOWS\System32\ design error Directory.GetCurrentDirectory - Directory.GetCurrentDirectory System.CurrentDomain.AppDomain.BaseDirectory和Directory.GetCurrentDirectory()之间的C#差异 - C#-Difference between System.CurrentDomain.AppDomain.BaseDirectory and Directory.GetCurrentDirectory() Directory.GetCurrentDirectory() 方法在不同类型的 C# 项目中给出不同的路径 - Directory.GetCurrentDirectory() method gives different path in different type of C# project Directory.GetCurrentDirectory 在单元测试中抛出 System.IO.DirectoryNotFoundException - Directory.GetCurrentDirectory Throwing System.IO.DirectoryNotFoundException in unit tests Directory.GetCurrentDirectory()不能在linux上运行? - Directory.GetCurrentDirectory() not working on linux? 将转义序列添加到Directory.GetCurrentDirectory() - adding escape sequence to Directory.GetCurrentDirectory() 无法将文件写入 Directory.GetCurrentDirectory() 中的路径 - Unable to write file to path in Directory.GetCurrentDirectory()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM