Skip to content

Commit

Permalink
Add and fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ngyukman committed Sep 8, 2018
1 parent cbab505 commit 4ec6373
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/test/groovy/com/ullink/MSBuildTaskBuilder.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ class MSBuildTaskBuilder {
public MSBuildTaskBuilder withProjectFile(File file) {
withProjectFile(file.path)
}

public MSBuildTaskBuilder withMainProjectProperty(String name, String value) {
mainProjectProperties[name] = value
this
}
}
44 changes: 44 additions & 0 deletions src/test/groovy/com/ullink/NuGetSpecTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -557,4 +557,48 @@ class NuGetSpecTest {
assertXMLEqual(expected, nuspecGenerated)
}
}

@Test
public void generateNuspec_defaultDependenciesFromProjectPackageReferences() {
def project = newNugetProject()

project.nugetSpec {
nuspec {}
}

File.createTempDir().with { projectFolder ->
deleteOnExit()

def csproj = new File(getClass().getResource('packageparser/packagereference.csproj').toURI())
def msbuildTask = new MSBuildTaskBuilder()
.withAssemblyName('bar')
.withProjectFile(csproj)
.withMainProjectProperty('MSBuildProjectFile', 'packagereference.csproj')
.build()

project.tasks.add(msbuildTask)

def expected =
'''
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>foo</id>
<version>2.1</version>
<description>fooDescription</description>
<dependencies>
<dependency id="Microsoft.AspNetCore.Mvc.Abstractions" version="1.1.2"/>
<dependency id="Microsoft.AspNetCore.Mvc.Core" version="1.1.2"/>
<dependency id="Microsoft.Extensions.Caching.Abstractions" version="1.1.1"/>
<dependency id="Microsoft.Extensions.Configuration.Binder" version="1.1.1"/>
<dependency id="Newtonsoft.Json" version="9.0.1"/>
<dependency id="StyleCop.Analyzers" version="1.0.0"/>
<dependency id="NuGet.Versioning" version="3.6.0" include="build" exclude="none"/>
<dependency id="System.Xml.XDocument" version="4.3.0"/>
</dependencies>
</metadata>
</package>'''
def nuspecGenerated = project.tasks.nugetSpec.generateNuspec()
assertXMLEqual(expected, nuspecGenerated)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package com.ullink.packageparser

import com.ullink.packagesparser.PackageReferenceParser
import groovy.xml.MarkupBuilder
import spock.lang.Specification

class PackageReferenceParserSpec extends Specification {
Expand All @@ -23,14 +24,24 @@ class PackageReferenceParserSpec extends Specification {
def result = new PackageReferenceParser().getDependencies(csproj)

then:
result == [
[dependency: [id: 'Microsoft.AspNetCore.Mvc.Abstractions', version: '1.1.2']],
[dependency: [id: 'Microsoft.AspNetCore.Mvc.Core', version: '1.1.2']],
[dependency: [id: 'Microsoft.Extensions.Caching.Abstractions', version: '1.1.1']],
[dependency: [id: 'Microsoft.Extensions.Configuration.Binder', version: '1.1.1']],
[dependency: [id: 'Newtonsoft.Json', version: '9.0.1']],
[dependency: [id: 'StyleCop.Analyzers', version: '1.0.0']],
[dependency: [id: 'NuGet.Versioning', version: '3.6.0', include: 'build', exclude: 'none']],
[dependency: [id: 'System.Xml.XDocument', version: '4.3.0']]]
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.dependencies() {
result.each {
it.resolveStrategy = DELEGATE_FIRST
it.delegate = delegate
it.call()
}
}
writer.toString() == '''<dependencies>
<dependency id='Microsoft.AspNetCore.Mvc.Abstractions' version='1.1.2' />
<dependency id='Microsoft.AspNetCore.Mvc.Core' version='1.1.2' />
<dependency id='Microsoft.Extensions.Caching.Abstractions' version='1.1.1' />
<dependency id='Microsoft.Extensions.Configuration.Binder' version='1.1.1' />
<dependency id='Newtonsoft.Json' version='9.0.1' />
<dependency id='StyleCop.Analyzers' version='1.0.0' />
<dependency id='NuGet.Versioning' version='3.6.0' include='build' exclude='none' />
<dependency id='System.Xml.XDocument' version='4.3.0' />
</dependencies>'''
}
}

0 comments on commit 4ec6373

Please sign in to comment.