Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio 更改TeamCity内app.config中的值_Visual Studio_Nunit_Teamcity_Nunit 3.0 - Fatal编程技术网

Visual studio 更改TeamCity内app.config中的值

Visual studio 更改TeamCity内app.config中的值,visual-studio,nunit,teamcity,nunit-3.0,Visual Studio,Nunit,Teamcity,Nunit 3.0,在包含所有单元测试的VisualStudio解决方案中,我们有一些文本文件。这些文本文件是根据单元测试生成的一些结果进行检查的 为了加载文件,我们有一个app.config,其中包含: <appSettings> <add key="BaseTestDataPath" value="D:\MyPath\MySolution\" /> </appSettings> 我知道代理工作文件夹中的物理布局,因此我需要知道的是: 如何在针对TeamCity

在包含所有单元测试的VisualStudio解决方案中,我们有一些文本文件。这些文本文件是根据单元测试生成的一些结果进行检查的

为了加载文件,我们有一个app.config,其中包含:

 <appSettings>
    <add key="BaseTestDataPath" value="D:\MyPath\MySolution\" />
 </appSettings>
我知道代理工作文件夹中的物理布局,因此我需要知道的是:

  • 如何在针对TeamCity的“我的构建步骤”中的解决方案运行Nunit之前更改app.config文件

有几种方法可以实现这一点

在运行NUnit步骤之前,只需选择以下脚本之一,将其添加到源代码管理中,并在构建配置中设置PowerShell构建运行程序,以运行传递所需参数的脚本。如果你选择了选项二,那么你也需要考虑变换DLL。 AppSettingReplace.ps1 如果您只想更改一个值,那么可以使用一些简单的PowerShell实现这一点,它将配置文件加载到xml文档中,迭代应用程序设置并更改匹配的设置

# -----------------------------------------------
# Config Transform
# -----------------------------------------------
#
# Ver   Who                     When      What
# 1.0   Evolve Software Ltd     13-05-16  Initial Version

# Script Input Parameters
param (
    [ValidateNotNullOrEmpty()]
    [string] $ConfigurationFile = $(throw "-ConfigurationFile is mandatory, please provide a value."),
    [ValidateNotNullOrEmpty()]
    [string] $ApplicationSetting = $(throw "-ApplicationSetting is mandatory, please provide a value."),
    [ValidateNotNullOrEmpty()]
    [string] $ApplicationSettingValue = $(throw "-ApplicationSettingValue is mandatory, please provide a value.")
)

function Main() 
{
    $CurrentScriptVersion = "1.0"

    Write-Host "================== Config Transform - Version"$CurrentScriptVersion": START =================="

    # Log input variables passed in
    Log-Variables
    Write-Host

    try {
        $xml = [xml](get-content($ConfigurationFile))
        $conf = $xml.configuration
        $conf.appSettings.add | foreach { if ($_.key -eq $ApplicationSetting) { $_.value = $ApplicationSettingValue } }
        $xml.Save($ConfigurationFile)
    } 
    catch [System.Exception] {
        Write-Output $_
        Exit 1
    }

    Write-Host "================== Config Transform - Version"$CurrentScriptVersion": END =================="
}

function Log-Variables
{
    Write-Host "ConfigurationFile: " $ConfigurationFile
    Write-Host "ApplicationSetting: " $ApplicationSetting
    Write-Host "ApplicationSettingValue: " $ApplicationSettingValue
    Write-Host "Computername:" (gc env:computername)
}

Main
用法

AppSettingReplace.ps1“D:\MyPath\app.config”“BaseTestDataPath”“%teamcity.build.workingDir%”


XDTConfigtTransform.ps1 另一种方法是使用XDT提供完整的配置转换支持—这确实需要Microsoft.Web.XmlTransform.dll以某种方式在服务器上结束(我通常将其放入源代码管理中)

下面的脚本将用另一个配置文件转换一个配置文件

# -----------------------------------------------
# Xdt Config Transform
# -----------------------------------------------
#
# Ver   Who                     When      What
# 1.0   Evolve Software Ltd     14-05-16  Initial Version

# Script Input Parameters
param (
    [ValidateNotNullOrEmpty()]
    [string] $ConfigurationFile = $(throw "-ConfigurationFile is mandatory, please provide a value."),
    [ValidateNotNullOrEmpty()]
    [string] $TransformFile = $(throw "-TransformFile is mandatory, please provide a value."),
    [ValidateNotNullOrEmpty()]
    [string] $LibraryPath = $(throw "-LibraryPath is mandatory, please provide a value.")
)

function Main() 
{
    $CurrentScriptVersion = "1.0"

    Write-Host "================== Xdt Config Transform - Version"$CurrentScriptVersion": START =================="

    # Log input variables passed in
    Log-Variables
    Write-Host

    if (!$ConfigurationFile -or !(Test-Path -path $ConfigurationFile -PathType Leaf)) {
        throw "File not found. $ConfigurationFile";
        Exit 1
    }
    if (!$TransformFile -or !(Test-Path -path $TransformFile -PathType Leaf)) {
        throw "File not found. $TransformFile";
        Exit 1
    }

    try {

        Add-Type -LiteralPath "$LibraryPath\Microsoft.Web.XmlTransform.dll"
        $xml = New-Object Microsoft.Web.XmlTransform.XmlTransformableDocument;
        $xml.PreserveWhitespace = $true
        $xml.Load($ConfigurationFile);

        $xmlTransform = New-Object Microsoft.Web.XmlTransform.XmlTransformation($TransformFile);
        if ($xmlTransform.Apply($xml) -eq $false)
        {
            throw "Transformation failed."
        }
        $xml.Save($ConfigurationFile)
    } 
    catch [System.Exception] {
        Write-Output $_
        Exit 1
    }

    Write-Host "================== Xdt Config Transform - Version"$CurrentScriptVersion": END =================="
}

function Log-Variables
{
    Write-Host "ConfigurationFile: " $ConfigurationFile
    Write-Host "TransformFile: " $TransformFile
    Write-Host "LibraryPath: " $LibraryPath
    Write-Host "Computername:" (gc env:computername)
}

Main
用法

XDTConfigtTransform.ps1“D:\MyPath\app.config”D:\MyPath\app.transform.config”“%teamcity.build.workingDir%\Library”

注意:最后一个参数是包含Microsoft.Web.XmlTransform.dll的目录的路径

Github存储库-


希望这有帮助

您可以在生成之前使用生成功能在文本文件中执行正则表达式替换。生成后,它会将文件内容恢复到原始状态。

您也可以选择使用nuget package id=“SlowCheetah”。这为app.config添加了转换。
在构建时进行转换,因此不需要额外的脚本或DLL。

谢谢,这正是我要寻找的,您需要更多+1的答案;)虽然您可以添加这个NuGet包,但它最终只对TC是必需的,所以在解决方案中使用它本身并不是必需的。因此,这仍然意味着要搞乱特定的TC实现。这似乎与运行单独的脚本没有什么不同?它有助于在pc上进行测试。我有app.config.Debug、app.config.UAT、app.config.Release等。通过构建解决方案,它得到了转换,因此可以测试不同的服务器、数据库、,etc开箱即用,不需要任何额外的脚本。你有一个例子说明如何使用FCR来解决这个问题吗?
# -----------------------------------------------
# Xdt Config Transform
# -----------------------------------------------
#
# Ver   Who                     When      What
# 1.0   Evolve Software Ltd     14-05-16  Initial Version

# Script Input Parameters
param (
    [ValidateNotNullOrEmpty()]
    [string] $ConfigurationFile = $(throw "-ConfigurationFile is mandatory, please provide a value."),
    [ValidateNotNullOrEmpty()]
    [string] $TransformFile = $(throw "-TransformFile is mandatory, please provide a value."),
    [ValidateNotNullOrEmpty()]
    [string] $LibraryPath = $(throw "-LibraryPath is mandatory, please provide a value.")
)

function Main() 
{
    $CurrentScriptVersion = "1.0"

    Write-Host "================== Xdt Config Transform - Version"$CurrentScriptVersion": START =================="

    # Log input variables passed in
    Log-Variables
    Write-Host

    if (!$ConfigurationFile -or !(Test-Path -path $ConfigurationFile -PathType Leaf)) {
        throw "File not found. $ConfigurationFile";
        Exit 1
    }
    if (!$TransformFile -or !(Test-Path -path $TransformFile -PathType Leaf)) {
        throw "File not found. $TransformFile";
        Exit 1
    }

    try {

        Add-Type -LiteralPath "$LibraryPath\Microsoft.Web.XmlTransform.dll"
        $xml = New-Object Microsoft.Web.XmlTransform.XmlTransformableDocument;
        $xml.PreserveWhitespace = $true
        $xml.Load($ConfigurationFile);

        $xmlTransform = New-Object Microsoft.Web.XmlTransform.XmlTransformation($TransformFile);
        if ($xmlTransform.Apply($xml) -eq $false)
        {
            throw "Transformation failed."
        }
        $xml.Save($ConfigurationFile)
    } 
    catch [System.Exception] {
        Write-Output $_
        Exit 1
    }

    Write-Host "================== Xdt Config Transform - Version"$CurrentScriptVersion": END =================="
}

function Log-Variables
{
    Write-Host "ConfigurationFile: " $ConfigurationFile
    Write-Host "TransformFile: " $TransformFile
    Write-Host "LibraryPath: " $LibraryPath
    Write-Host "Computername:" (gc env:computername)
}

Main