【问题标题】:Tests folder not found when test zip package is built programmatically in .NET在 .NET 中以编程方式构建测试 zip 包时找不到测试文件夹
【发布时间】:2018-02-12 06:34:13
【问题描述】:

我正在使用适用于 Python 的 AWS Appium。由于我们的项目要求,测试将在 Python 中进行,但我们需要使用 .NET 在 AWS 中以编程方式构建、上传和安排测试。

根据 AWS 文档,测试包 zip 文件的根目录中应包含以下项目:“tests”文件夹、“wheelhouse”文件夹和“requirements.txt”文件。

当我在 Windows 机器上手动构建包时(只需选择文件和文件夹,右键单击并添加到 zip 存档),包被 AWS 接受并且测试正常运行。

但是,当我在 .NET 中以编程方式构建包时,即使输出 zip 文件与手动构建的 zip 文件具有相同的文件夹结构,AWS 也会通过以下错误消息拒绝它:

“我们在您的测试包的 root 中找不到测试目录”

我尝试了 3 种不同的方法在 .NET 中以编程方式构建包。两种方法都会导致相同的错误。简单来说,方法如下:

1/ 创建一个包含“tests”文件夹、“wheelhouse”文件夹和“requirements.txt”文件的“TestPackage”文件夹。使用以下代码压缩整个 TestPackage 文件夹:

ZipFile.CreateFromDirectory("TestPackage", zipPath, CompressionLevel.Optimal, includeBaseDirectory: false);

includeBaseDirectory 标志设置为false,这样所有文件夹和文件都将位于 zip 文件的根目录中。

2/ 创建一个空的 zip 文件。然后使用下面的伪代码将所有文件(“requirements.txt”和文件夹中的所有文件)添加到 zip 文件中:

using (var zipStream = File.Create(zipPath))
{
  using (var zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Update))
  {
    // entryName is the relative path to the file
    // For eg., a file in the tests folder will have this path:
    // @"\tests\test_sample.py"
    zipArchive.CreateEntryFromFile(sourceFileName, entryName)
  }
}

3/ 使用以下 PowerShell 脚本:

# Get the path to the AWSDeviceTests folder
# Assumption: the script file is in this folder
$ProjectDir = Split-Path -Path $MyInvocation.MyCommand.Path

# Get the paths of the test folders and file
$RequirementsFileDir = Join-Path -Path $ProjectDir -ChildPath "requirements.txt"
$TestsFolderDir = Join-Path -Path $ProjectDir -ChildPath "tests"
$WheelhouseFolderDir = Join-Path -Path $ProjectDir -ChildPath "wheelhouse"

# Zip the test folders and file
$ZipDestination = Join-Path -Path $ProjectDir -ChildPath "TestPackage.zip"
Compress-Archive -LiteralPath $RequirementsFileDir, $TestsFolderDir, $WheelhouseFolderDir -CompressionLevel Optimal -DestinationPath $ZipDestination

以上所有方法都失败了。 ZipFileZipArchive 来自 .NET 中的 System.IO.Compression

我想再次强调,所有方法的输出 zip 文件与我手动构建的 zip 文件具有完全相同的文件夹结构,即所有必需的文件夹和文件都在 zip 文件的根目录中。虽然手动构建的文件有效,但以编程方式构建的文件却不行。

我做错了吗?在 .NET 或 Windows 中构建 AWS 设备测试包是否存在已知问题?还是无法以编程方式构建测试包并将其上传到 AWS Device Farm? zip 文件在 AWS Device Farm 中如何解析/验证?

任何帮助或建议将不胜感激。提前谢谢你。

【问题讨论】:

  • 我曾经在 Mac 中使用存档器时遇到过类似的问题。在那种情况下,我发现如果我没有事先删除 zip,则 zip 不会更新。所以我不得不先把它删掉,然后再把所有东西都压缩起来。不确定这是否是这里发生的事情,但我希望这会有所帮助。听起来像是 .NET SDK 的一个非常细化的问题。
  • @jmp 感谢您的建议。在我的代码中,我确实检查了 zip 文件是否已经存在,如果存在,我会在从头开始创建新文件之前将其删除。

标签: amazon-web-services zip zipfile aws-device-farm


【解决方案1】:

过了一会儿,我注意到了一些有趣的事情。每次我手动构建 zip 文件时,我都是使用 7zip:选择文件和文件夹 => 右键单击​​ => 7zip => 添加到“archiveName.zip”

相比之下,在我用于自动化构建过程的所有方法中,我使用的似乎是 Windows 内置的 zip 功能(不管是什么;也许是 WinZip??)。

所以我决定再试一次,但在我的代码中使用了 7zip,它成功了。下面是代码:

// Execute command line to zip the build file and folder using 7-zip
// Assumption: 7zip.exe is installed in this directory
Directory.SetCurrentDirectory(@"C:\Program Files\7-Zip");
string sevenZipExecutable = @"7z.exe";
string requirementsFile = projectDirectory + @"\requirements.txt";
string testsFolder = projectDirectory + @"\tests";
string wheelhouseFolder = projectDirectory + @"\wheelhouse";
string arguments = $"/C {sevenZipExecutable} a -tzip \"{zipPath}\" \"{requirementsFile}\" \"{testsFolder}\" \"{wheelhouseFolder}\"";
var startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = arguments;
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(startInfo);

由此,我想问题一定是由于 7zip 和 Windows 内置 zip 构建 zip 文件的方式之间存在一些模糊的差异。我想我的“答案”并没有真正解释这是如何发生或为什么发生的,但至少现在有一个可行的解决方案可供可能遇到此问题的其他人使用。

【讨论】:

    猜你喜欢
    • 2011-05-30
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多