Skip to content

Commit

Permalink
Fix paket.lock file parser
Browse files Browse the repository at this point in the history
Description
===========

The `paket.lock` file parser has issues with counting leading
whitespaces. The value from the old logic `def newLeadingWhitespaces =
(line =~ /\s/).size()` returns the number of all white space characters
in the current line.

The second part:

```groovy
if (newLeadingWhitespaces > currentLeadingWhitespaces) {
  currentIndent++
} else if (newLeadingWhitespaces < currentLeadingWhitespaces) {
  currentIndent--
}
```

didn't take into account when the indention level suddenly jumped from 3
to 1. I also made the method `List<String>
getAllDependencies(List<String> references)` recursive.
I added a new test covering the failing behavior

Changes
=======

![FIX] `paket.lock` file parsing
![IMPROVE] `getAllDependencies` method by making it recursive
![ADD] new test example
  • Loading branch information
Larusso committed Apr 17, 2018
1 parent 5635348 commit 54e6b45
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ class PaketLock {

def currentSourceType
def currentPackageName
def currentIndent = 0
def currentLeadingWhitespaces = 0
int currentIndent = 0
def currentLineData

lockContent.eachLine { line ->
Expand All @@ -52,13 +51,10 @@ class PaketLock {
return
}

def newLeadingWhitespaces = (line =~ /\s/).size()
if (newLeadingWhitespaces > currentLeadingWhitespaces) {
currentIndent++
} else if (newLeadingWhitespaces < currentLeadingWhitespaces) {
currentIndent--
if((line =~ /^[\s]+/)) {
currentIndent = (line =~ /^[\s]+/)[0].size() / 2
}
currentLeadingWhitespaces = newLeadingWhitespaces

currentLineData = line.trim()

if (currentIndent == LineType.TYPE.value && isValidSourceType(currentLineData)) {
Expand Down Expand Up @@ -89,22 +85,14 @@ class PaketLock {
}

List<String> getDependencies(SourceType source, String id) {
content[source.getValue()] && content[source.getValue()][id] ? content[source.getValue()][id] as List<String> : null
content[source.getValue()] && content[source.getValue()][id] ? content[source.getValue()][id] as List<String> : []
}

List<String> getAllDependencies(List<String> references) {
def result = []
for (def referenceDependency in references) {
result.add(referenceDependency)
def referenceDependencies = getDependencies(SourceType.NUGET, referenceDependency)
if (referenceDependencies) {
result.addAll(referenceDependencies)
def dependencies = getAllDependencies(referenceDependencies)
if (dependencies) {
result.addAll(dependencies)
}
}
def ref = references.collect { reference ->
[reference, getAllDependencies(getDependencies(SourceType.NUGET, reference))]
}
result.unique()

ref.flatten().unique()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,33 @@ class PaketLockSpec extends Specification {
Wooga.JsonDotNetNode (0.1.1-master00001)
""".stripIndent()

static String LOCK_CONTENT_2 = """
NUGET
remote: https://wooga.artifactoryonline.com//wooga/api/nuget/atlas-nuget
Substance (0.0.10-beta)
Wooga.AssetBundleManager (3.0.0-rc00001)
Wooga.Services (>= 3.0.0-rc)
Wooga.EditorSpotlight (1.0.0)
Wooga.NativeShare (0.1.0)
Wooga.Services (3.0.1)
Wooga.JsonDotNetNode (>= 0.1.0-prerelease < 0.2.0-prerelease)
Wooga.Lambda (>= 0.7 < 1.0)
remote: https://www.nuget.org/api/v2
Substance.Collections (0.0.10-beta)
Substance (>= 0.0.10-beta)
Substance.Collections.Generic (0.0.10-beta)
Substance (>= 0.0.10-beta)
Substance.Collections (>= 0.0.10-beta)
Substance.Collections.Immutable (0.0.10-beta)
Substance (>= 0.0.10-beta)
Substance.Collections (>= 0.0.10-beta)
Substance.Collections.Generic (>= 0.0.10-beta)
Wooga.Lambda (0.7)
Substance.Collections.Immutable (>= 0.0.0-beta)
remote: https://wooga.artifactoryonline.com//wooga/api/nuget/atlas-nuget-snapshot
Wooga.JsonDotNetNode (0.1.1-master00001)
""".stripIndent()

@Shared
File lockFile = File.createTempFile("paket", ".lock")

Expand Down Expand Up @@ -59,4 +86,27 @@ class PaketLockSpec extends Specification {
"File" | lockFile << LOCK_CONTENT
}

@Unroll
def "parses all nuget dependencies from paket.lock with #objectType"() {
when:
def lock = new PaketLock(content)

then:
def nugets = lock.getAllDependencies(references)

expectedDependencies.every {
nugets.contains(it)
}

nugets.size() == expectedDependencies.size()

where:
objectType | content
"String" | LOCK_CONTENT_2
"File" | lockFile << LOCK_CONTENT_2

references = ["Wooga.Services", "Wooga.AssetBundleManager", "Wooga.NativeShare", "Wooga.EditorSpotlight"]
expectedDependencies = references + ["Substance","Substance.Collections","Substance.Collections.Immutable", "Substance.Collections.Generic", "Wooga.Lambda", "Wooga.JsonDotNetNode"]
}

}

0 comments on commit 54e6b45

Please sign in to comment.