Skip to content

Commit

Permalink
Replaces all marked binding redirects with required only
Browse files Browse the repository at this point in the history
  • Loading branch information
mrinaldi committed Nov 18, 2015
1 parent 126a4db commit 14ce06f
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 12 deletions.
78 changes: 78 additions & 0 deletions integrationtests/Paket.IntegrationTests/BindingRedirect.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ open System
open System.IO
open NUnit.Framework
open FsUnit
open System.Text.RegularExpressions

[<Test>]
let ``install should redirect required assemblies only``() =
Expand Down Expand Up @@ -141,3 +142,80 @@ let ``#1218 install hard should replace all assembly redirects with required onl
config4.Contains ``xunit.extensions`` |> shouldEqual false
config4 |> shouldContainText ``Castle.Core``
config4.Contains ``Castle.Windsor`` |> shouldEqual false

[<Test>]
let ``#1218 install should replace paket's binding redirects with required only``() =
paket "install --redirects --createnewbindingfiles" "i001218-binding-redirect-replaces-all-redirects-with-required-only" |> ignore

let path = Path.Combine(scenarioTempPath "i001218-binding-redirect-replaces-all-redirects-with-required-only")
let config1Path = Path.Combine(path, "Project1", "app.config")
let config2Path = Path.Combine(path, "Project2", "app.config")
let config3Path = Path.Combine(path, "Project3", "app.config")
let config4Path = Path.Combine(path, "Project4", "app.config")

let config1 = File.ReadAllText(config1Path)
let config2 = File.ReadAllText(config2Path)
let config3 = File.ReadAllText(config3Path)
let config4 = File.ReadAllText(config4Path)

let paketMark = "<!--Paket-->\s+"

let Albedo = """<assemblyIdentity name="Ploeh.Albedo" publicKeyToken="179ef6dd03497bbd" culture="neutral" />"""
let AutoFixture = """<assemblyIdentity name="Ploeh.AutoFixture" publicKeyToken="b24654c590009d4f" culture="neutral" />"""
let ``AutoFixture.Idioms`` = """<assemblyIdentity name="Ploeh.AutoFixture.Idioms" publicKeyToken="b24654c590009d4f" culture="neutral" />"""
let ``AutoFixture.Xunit`` = """<assemblyIdentity name="Ploeh.AutoFixture.Xunit" publicKeyToken="b24654c590009d4f" culture="neutral" />"""
let log4net = """<assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" culture="neutral" />"""
let ``Newtonsoft.Json`` = """<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />"""
let ``Newtonsoft.Json.Schema`` = """<assemblyIdentity name="Newtonsoft.Json.Schema" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />"""
let xunit = """<assemblyIdentity name="xunit" publicKeyToken="8d05b1bb7a6fdb6c" culture="neutral" />"""
let ``xunit.extensions`` = """<assemblyIdentity name="xunit.extensions" publicKeyToken="8d05b1bb7a6fdb6c" culture="neutral" />"""
let ``Castle.Core`` = """<assemblyIdentity name="Castle.Core" publicKeyToken="407dd0808d44fbdc" culture="neutral" />"""
let ``Castle.Windsor`` = """<assemblyIdentity name="Castle.Windsor" publicKeyToken="407dd0808d44fbdc" culture="neutral" />"""

Regex.IsMatch(config1, paketMark + Albedo) |> shouldEqual true
Regex.IsMatch(config1, paketMark + AutoFixture) |> shouldEqual true
config1.Contains ``AutoFixture.Idioms`` |> shouldEqual false
config1.Contains ``AutoFixture.Xunit`` |> shouldEqual false
config1.Contains log4net |> shouldEqual false
Regex.IsMatch(config1, paketMark + ``Newtonsoft.Json``) |> shouldEqual true
config1.Contains ``Newtonsoft.Json.Schema`` |> shouldEqual false
config1.Contains xunit |> shouldEqual false
Regex.IsMatch(config1, paketMark + ``xunit.extensions``) |> shouldEqual true
Regex.IsMatch(config1, paketMark + ``Castle.Core``) |> shouldEqual true
config1.Contains ``Castle.Windsor`` |> shouldEqual false

config2.Contains Albedo |> shouldEqual false
config2.Contains AutoFixture |> shouldEqual false
config2.Contains ``AutoFixture.Idioms`` |> shouldEqual false
config2.Contains ``AutoFixture.Xunit`` |> shouldEqual false
config2.Contains log4net |> shouldEqual false
config2 |> shouldContainText ``Newtonsoft.Json``
config2.Contains ``Newtonsoft.Json.Schema`` |> shouldEqual false
config2.Contains xunit |> shouldEqual false
config2.Contains ``xunit.extensions`` |> shouldEqual false
config2.Contains ``Castle.Core`` |> shouldEqual false
config2.Contains ``Castle.Windsor`` |> shouldEqual false

config3.Contains Albedo |> shouldEqual false
config3 |> shouldContainText AutoFixture
config3.Contains ``AutoFixture.Idioms`` |> shouldEqual false
config3.Contains ``AutoFixture.Xunit`` |> shouldEqual false
config3.Contains log4net |> shouldEqual false
Regex.IsMatch(config3, paketMark + ``Newtonsoft.Json``) |> shouldEqual true
config3.Contains ``Newtonsoft.Json.Schema`` |> shouldEqual false
config3.Contains xunit |> shouldEqual false
config3.Contains ``xunit.extensions`` |> shouldEqual false
Regex.IsMatch(config3, paketMark + ``Castle.Core``) |> shouldEqual true
config3.Contains ``Castle.Windsor`` |> shouldEqual false

config4.Contains Albedo |> shouldEqual false
config4.Contains AutoFixture |> shouldEqual false
config4.Contains ``AutoFixture.Idioms`` |> shouldEqual false
config4.Contains ``AutoFixture.Xunit`` |> shouldEqual false
config4.Contains log4net |> shouldEqual false
config4 |> shouldContainText ``Newtonsoft.Json``
config4.Contains ``Newtonsoft.Json.Schema`` |> shouldEqual false
config4.Contains xunit |> shouldEqual false
config4.Contains ``xunit.extensions`` |> shouldEqual false
Regex.IsMatch(config4, paketMark + ``Castle.Core``) |> shouldEqual true
config4.Contains ``Castle.Windsor`` |> shouldEqual false
27 changes: 15 additions & 12 deletions src/Paket.Core/BindingRedirects.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ let private bindingNs = "urn:schemas-microsoft-com:asm.v1"
let private ensureAssemblyBinding doc =
doc |> ensurePathExists ("/configuration/runtime/assemblyBinding!" + bindingNs)

let private isMarked (e : XElement) =
e.Nodes()
|> Seq.filter (fun e -> e.NodeType = XmlNodeType.Comment)
|> Seq.map (fun e -> e :?> XComment)
|> Seq.exists (fun e -> String.Compare(e.Value.Trim(), "Paket", true) = 0)

/// Updates the supplied MSBuild document with the supplied binding redirect.
let internal setRedirect (doc:XDocument) bindingRedirect =
let createElementWithNs = createElement (Some bindingNs)
Expand Down Expand Up @@ -51,11 +57,8 @@ let internal setRedirect (doc:XDocument) bindingRedirect =
let newRedirect = createElementWithNs "bindingRedirect" [ "oldVersion", "0.0.0.0-999.999.999.999"
"newVersion", bindingRedirect.Version ]

dependentAssembly.Nodes()
|> Seq.filter (fun e -> e.NodeType = XmlNodeType.Comment)
|> Seq.map (fun e -> e :?> XComment)
|> Seq.exists (fun e -> String.Compare(e.Value.Trim(), "Paket", true) = 0)
|> function | true -> () | false -> dependentAssembly.AddFirst(XComment("Paket"))
if isMarked dependentAssembly |> not then
dependentAssembly.AddFirst(XComment("Paket"))

match dependentAssembly |> tryGetElementWithNs "bindingRedirect" with
| Some redirect -> redirect.ReplaceWith(newRedirect)
Expand Down Expand Up @@ -123,13 +126,13 @@ let private applyBindingRedirects cleanBindingRedirects bindingRedirects (config
with
| exn -> failwithf "Parsing of %s failed.%s%s" configFilePath Environment.NewLine exn.Message

if cleanBindingRedirects then
let nsManager = XmlNamespaceManager(NameTable());
nsManager.AddNamespace("bindings", bindingNs)
config.XPathSelectElements("//bindings:assemblyBinding", nsManager)
|> Seq.collect (fun e -> e.Elements(XName.Get("dependentAssembly", bindingNs)))
|> List.ofSeq
|> List.iter (fun e -> e.Remove())
let nsManager = XmlNamespaceManager(NameTable());
nsManager.AddNamespace("bindings", bindingNs)
config.XPathSelectElements("//bindings:assemblyBinding", nsManager)
|> Seq.collect (fun e -> e.Elements(XName.Get("dependentAssembly", bindingNs)))
|> List.ofSeq
|> List.filter (fun e -> cleanBindingRedirects || isMarked e)
|> List.iter (fun e -> e.Remove())

let config = Seq.fold setRedirect config bindingRedirects
indentAssemblyBindings config
Expand Down

0 comments on commit 14ce06f

Please sign in to comment.