VSOnline 自动测试 Runs/Builds 与本地有何不同?
How do VSOnline Automated Test Runs/Builds Differ From Local?
我有单元测试,通过确定项目路径在特定文件夹中查找配置文件:
/// <summary>
/// Gets the project path of the given type.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static string GetProjectPath(Type type)
{
var dll = new FileInfo(type.Assembly.Location);
if (dll.Directory == null || // ...\Solution\Project\bin\Build
dll.Directory.Parent == null || // ...\Solution\Project\bin
dll.Directory.Parent.Parent == null || // ...\Solution\Project
dll.Directory.Parent.Parent.Parent == null) // ...\Solution
{
throw new Exception("Unable to find Project Path for " + type.FullName);
}
// Class Name, Project Name, Version, Culture, PublicKeyTyoken
// ReSharper disable once PossibleNullReferenceException
var projectName = type.AssemblyQualifiedName.Split(',')[1].Trim();
var projectPath = Path.Combine(dll.Directory.Parent.Parent.Parent.FullName, projectName);
if (!Directory.Exists(projectPath))
{
throw new Exception(String.Format("Unable to find Project Path for {0} at {1}", type.FullName, projectPath));
}
return projectPath;
}
这在开发机器上效果很好,但是当 VSOnline Build 启动时,单元测试失败,说找不到项目路径。这让我相信要么 VSOnline 在一台机器上构建,而在另一台机器上进行单元测试,要么它的目录结构有所不同,要么单元测试没有读取文件系统的权限...
任何人都知道可能是什么问题?
当 VSOnline 执行构建时,源文件位于 c:\a\src\Branch Name\
,但当它构建时,它构建到 c:\a\bin
文件夹。我猜这样做是为了帮助减少任何长路径名错误。构建是在同一台服务器上执行的,但输出与 运行 在本地不同。
这需要对我的方法 GetProjectPath 方法进行一些更改:
string solutionFolder = null;
if (dll.Directory == null || // ...\Solution\Project\bin\Build
dll.Directory.Parent == null || // ...\Solution\Project\bin
dll.Directory.Parent.Parent == null || // ...\Solution\Project
dll.Directory.Parent.Parent.Parent == null) // ...\Solution
{
if (dll.DirectoryName == @"C:\a\bin")
{
// Build is on VSOnline. Redirect to other c:\a\src\Branch Name
var s = new System.Diagnostics.StackTrace(true);
for (var i = 0; i < s.FrameCount; i++)
{
var fileName = s.GetFrame(i).GetFileName();
if (!String.IsNullOrEmpty(fileName))
{
// File name will be in the form of c:\a\src\Branch Name\project\filename. Get everything up to and including the Branch Name
var parts = fileName.Split(Path.DirectorySeparatorChar);
solutionFolder = Path.Combine(parts[0] + Path.DirectorySeparatorChar + parts[1], parts[2], parts[3]);
break;
}
}
}
if (String.IsNullOrWhiteSpace(solutionFolder))
{
throw new Exception("Unable to find Project Path for " + type.FullName + ". Assembly Located at " + type.Assembly.Location + sb);
}
}
else
{
solutionFolder = dll.Directory.Parent.Parent.Parent.FullName;
}
我有单元测试,通过确定项目路径在特定文件夹中查找配置文件:
/// <summary>
/// Gets the project path of the given type.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static string GetProjectPath(Type type)
{
var dll = new FileInfo(type.Assembly.Location);
if (dll.Directory == null || // ...\Solution\Project\bin\Build
dll.Directory.Parent == null || // ...\Solution\Project\bin
dll.Directory.Parent.Parent == null || // ...\Solution\Project
dll.Directory.Parent.Parent.Parent == null) // ...\Solution
{
throw new Exception("Unable to find Project Path for " + type.FullName);
}
// Class Name, Project Name, Version, Culture, PublicKeyTyoken
// ReSharper disable once PossibleNullReferenceException
var projectName = type.AssemblyQualifiedName.Split(',')[1].Trim();
var projectPath = Path.Combine(dll.Directory.Parent.Parent.Parent.FullName, projectName);
if (!Directory.Exists(projectPath))
{
throw new Exception(String.Format("Unable to find Project Path for {0} at {1}", type.FullName, projectPath));
}
return projectPath;
}
这在开发机器上效果很好,但是当 VSOnline Build 启动时,单元测试失败,说找不到项目路径。这让我相信要么 VSOnline 在一台机器上构建,而在另一台机器上进行单元测试,要么它的目录结构有所不同,要么单元测试没有读取文件系统的权限...
任何人都知道可能是什么问题?
当 VSOnline 执行构建时,源文件位于 c:\a\src\Branch Name\
,但当它构建时,它构建到 c:\a\bin
文件夹。我猜这样做是为了帮助减少任何长路径名错误。构建是在同一台服务器上执行的,但输出与 运行 在本地不同。
这需要对我的方法 GetProjectPath 方法进行一些更改:
string solutionFolder = null;
if (dll.Directory == null || // ...\Solution\Project\bin\Build
dll.Directory.Parent == null || // ...\Solution\Project\bin
dll.Directory.Parent.Parent == null || // ...\Solution\Project
dll.Directory.Parent.Parent.Parent == null) // ...\Solution
{
if (dll.DirectoryName == @"C:\a\bin")
{
// Build is on VSOnline. Redirect to other c:\a\src\Branch Name
var s = new System.Diagnostics.StackTrace(true);
for (var i = 0; i < s.FrameCount; i++)
{
var fileName = s.GetFrame(i).GetFileName();
if (!String.IsNullOrEmpty(fileName))
{
// File name will be in the form of c:\a\src\Branch Name\project\filename. Get everything up to and including the Branch Name
var parts = fileName.Split(Path.DirectorySeparatorChar);
solutionFolder = Path.Combine(parts[0] + Path.DirectorySeparatorChar + parts[1], parts[2], parts[3]);
break;
}
}
}
if (String.IsNullOrWhiteSpace(solutionFolder))
{
throw new Exception("Unable to find Project Path for " + type.FullName + ". Assembly Located at " + type.Assembly.Location + sb);
}
}
else
{
solutionFolder = dll.Directory.Parent.Parent.Parent.FullName;
}