@@ -2,6 +2,7 @@ open Fake.DotNet
22open System
33open System.Linq
44open System.IO
5+ open System.Xml .Linq
56open Fun.Build
67open Fake.Core
78open Fake.Tools .Git
@@ -127,7 +128,6 @@ Target.create "Build" (fun _ ->
127128 slnPath)
128129
129130open System.Data .SqlClient
130- open System.Configuration
131131open System.IO .Compression
132132open Fake.DotNet .Testing
133133
@@ -141,29 +141,32 @@ Target.create "DeployTestDB" (fun _ ->
141141
142142 stage " adjust config file connection strings" {
143143 run ( fun ctx ->
144- let map = ExeConfigurationFileMap()
145- map.ExeConfigFilename <- testsSourceRoot @@ " app.config"
144+ let appConfigPath = testsSourceRoot @@ " app.config"
146145
147- let testConfigFile =
148- ConfigurationManager.OpenMappedExeConfiguration( map, ConfigurationUserLevel.None)
146+ let connStrValue =
147+ let gitHubActionSqlConnectionString =
148+ System.Environment.GetEnvironmentVariable " GITHUB_ACTION_SQL_SERVER_CONNECTION_STRING"
149149
150- let connStr =
151- let connStr =
152- let gitHubActionSqlConnectionString =
153- System.Environment.GetEnvironmentVariable " GITHUB_ACTION_SQL_SERVER_CONNECTION_STRING"
150+ if String.IsNullOrWhiteSpace gitHubActionSqlConnectionString then
151+ // Read current value directly from XML
152+ let doc = XDocument.Load( appConfigPath)
154153
155- if String.IsNullOrWhiteSpace gitHubActionSqlConnectionString then
156- testConfigFile.ConnectionStrings.ConnectionStrings .[ " AdventureWorks" ]. ConnectionString
157- else
158- // we run under Github Actions, update the test config file connection string.
159- testConfigFile.ConnectionStrings.ConnectionStrings .[ " AdventureWorks " ]. ConnectionString <-
160- gitHubActionSqlConnectionString
154+ doc.Root.Element ( " connectionStrings " ) .Elements ( " add " )
155+ |> Seq.find ( fun el -> el.Attribute ( XName.Get " name " ) .Value = " AdventureWorks" )
156+ |> fun el -> el.Attribute ( XName.Get " connectionString " ) .Value
157+ else
158+ // Write the new connection string directly into the XML file
159+ let doc = XDocument.Load ( appConfigPath )
161160
162- testConfigFile.Save()
163- gitHubActionSqlConnectionString
161+ let el =
162+ doc.Root.Element( " connectionStrings" ) .Elements( " add" )
163+ |> Seq.find ( fun el -> el.Attribute( XName.Get " name" ) .Value = " AdventureWorks" )
164164
165- SqlConnectionStringBuilder connStr
165+ el.SetAttributeValue( XName.Get " connectionString" , gitHubActionSqlConnectionString)
166+ doc.Save( appConfigPath)
167+ gitHubActionSqlConnectionString
166168
169+ let connStr = SqlConnectionStringBuilder connStrValue
167170 testConnStr <- Some connStr
168171 database <- Some connStr.InitialCatalog
169172
0 commit comments