Skip to content

Commit

Permalink
fix(sbt): Add ignored lines for SBT output parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
elldritch committed Jul 18, 2018
1 parent dafbf91 commit 3a59e35
Show file tree
Hide file tree
Showing 7 changed files with 1,519,150 additions and 40 deletions.
10 changes: 1 addition & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,7 @@ test:

.PHONY: unit-test
unit-test:
# TODO: this should be go test ./... once all packages (particularly,
# analyzers) work.
go test \
./analyzers/nodejs \
./analyzers/golang \
./config \
./cmd/fossa/version \
./buildtools/bundler \
./buildtools/gocmd
go test ./...

.PHONY: acceptance-test
acceptance-test: docker-test
Expand Down
75 changes: 44 additions & 31 deletions buildtools/sbt/sbt.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,33 @@ func (s *SBT) DependencyTree(dir, project, configuration string) ([]Dependency,
return nil, nil, errors.Wrap(err, "could not get dependency tree from SBT")
}

return ParseDependencyTree(output, project == "root" || project == "")
}

func (s *SBT) DependencyList(dir, project, configuration string) (string, error) {
output, _, err := exec.Run(exec.Cmd{
Dir: dir,
Name: s.Bin,
Argv: []string{"-no-colors", Task(project, configuration, "dependencyList")},
})
if err != nil {
return "", errors.Wrap(err, "could not get dependency list from SBT")
}

// Filter lines to only include dependency list.
var depLines []string
for _, line := range strings.Split(output, "\n") {
if FilterLine(line) {
log.Logger.Debugf("Matched line: %#v", line)
depLines = append(depLines, line)
} else {
log.Logger.Debugf("Ignoring line: %#v", line)
}
}
return strings.Join(depLines, "\n"), err
}

func ParseDependencyTree(output string, rootBuild bool) ([]Dependency, map[Dependency][]Dependency, error) {
// Filter lines to only include dependency tree.
spacerRegex := regexp.MustCompile("^\\[info\\] ([ `+\\\\|-]*)(\\s*?)$")
var depLines []string
Expand All @@ -95,7 +122,6 @@ func (s *SBT) DependencyTree(dir, project, configuration string) ([]Dependency,

// Non-root builds only have one sub-project, so we collapse the first layer
// of imports.
rootBuild := project == "root" || project == ""
if !rootBuild {
// Remove the sub-project line.
depLines = depLines[1:]
Expand Down Expand Up @@ -136,37 +162,24 @@ func (s *SBT) DependencyTree(dir, project, configuration string) ([]Dependency,
})
}

func (s *SBT) DependencyList(dir, project, configuration string) (string, error) {
output, _, err := exec.Run(exec.Cmd{
Dir: dir,
Name: s.Bin,
Argv: []string{"-no-colors", Task(project, configuration, "dependencyList")},
})
if err != nil {
return "", errors.Wrap(err, "could not get dependency list from SBT")
}

// Filter lines to only include dependency list.
var depLines []string
for _, line := range strings.Split(output, "\n") {
if FilterLine(line) {
log.Logger.Debugf("Matched line: %#v", line)
depLines = append(depLines, line)
} else {
log.Logger.Debugf("Ignoring line: %#v", line)
}
}
return strings.Join(depLines, "\n"), err
}

func FilterLine(line string) bool {
return !(strings.HasPrefix(line, "[info] Loading ") ||
strings.HasPrefix(line, "[info] Resolving ") ||
strings.HasPrefix(line, "[info] Set ") ||
strings.HasPrefix(line, "[info] In file:") ||
strings.HasPrefix(line, "[info] Updating ") ||
strings.HasPrefix(line, "[info] Done ")) &&
strings.HasPrefix(line, "[info] ")
trimmed := strings.TrimSpace(line)
if !strings.HasPrefix(trimmed, "[info ]") {
return false
}
infoMsg := strings.TrimPrefix(trimmed, "[info] ")
return !(strings.HasPrefix(infoMsg, "Loading ") ||
strings.HasPrefix(infoMsg, "Compiling ") ||
strings.HasPrefix(infoMsg, "Non-compiled module ") ||
strings.HasPrefix(strings.TrimSpace(infoMsg), "Compilation ") ||
strings.HasPrefix(infoMsg, "Resolving ") ||
strings.HasPrefix(infoMsg, "Resolved ") ||
strings.HasPrefix(infoMsg, "Set ") ||
strings.HasPrefix(infoMsg, "In file:") ||
strings.HasPrefix(infoMsg, "Updating ") ||
strings.HasPrefix(infoMsg, "Done ") ||
strings.HasPrefix(infoMsg, "downloading ") ||
strings.HasPrefix(strings.TrimSpace(infoMsg), "[SUCCESSFUL ]"))
}

func Task(project, configuration, task string) string {
Expand Down
78 changes: 78 additions & 0 deletions buildtools/sbt/sbt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package sbt_test

import (
"io/ioutil"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"

"github.com/fossas/fossa-cli/buildtools/sbt"
"github.com/fossas/fossa-cli/log"
)

func TestSanityCheckParseDependencyTree(t *testing.T) {
// This is a hack so that logging from this test does not swamp all other log
// output.
log.Init(false, false)

fixture, err := ioutil.ReadFile(filepath.Join("testdata", "sbt_dependencytree_nocolor-prisma-stdout"))
assert.NoError(t, err)

_, _, err = sbt.ParseDependencyTree(string(fixture), true)
assert.NoError(t, err)
}

func TestFilterLines(t *testing.T) {
// This is a sampling of example disallowed lines from `sbt_dependencytree-prisma-stdout_and_stderr`
disallowed := []string{
`Getting org.scala-sbt sbt 1.0.4 (this may take some time)...`,
`downloading https://repo1.maven.org/maven2/org/scala-sbt/sbt/1.0.4/sbt-1.0.4.jar ...`,
` [SUCCESSFUL ] org.scala-sbt#sbt;1.0.4!sbt.jar (111ms)`,
`:: retrieving :: org.scala-sbt#boot-app`,
` confs: [default]`,
` 69 artifacts copied, 0 already retrieved (22031kB/75ms)`,
`[info] Loading settings from plugins.sbt ...`,
`[info] Loading global plugins from /home/fossa/.sbt/1.0/plugins`,
`[info] Updating {file:/home/fossa/.sbt/1.0/plugins/}global-plugins...`,
`[info] Done updating.`,
`[info] Loading settings from plugins.sbt ...`,
`[info] Loading project definition from /home/fossa/prisma/server/project`,
`[info] Updating {file:/home/fossa/prisma/server/project/}server-build...`,
`[info] downloading https://repo1.maven.org/maven2/commons-logging/commons-logging/1.2/commons-logging-1.2.jar ...`,
`[info] [SUCCESSFUL ] commons-logging#commons-logging;1.2!commons-logging.jar (103ms)`,
`[info] Done updating.`,
`[warn] Found version conflict(s) in library dependencies; some are suspected to be binary incompatible:`,
`[warn] Run 'evicted' to see detailed eviction warnings`,
`[info] Compiling 2 Scala sources to /home/fossa/prisma/server/project/target/scala-2.12/sbt-1.0/classes ...`,
`[info] Non-compiled module 'compiler-bridge_2.12' for Scala 2.12.4. Compiling...`,
`[info] Compilation completed in 9.09s.`,
`[info] Done compiling.`,
`[info] Loading settings from build.sbt ...`,
`[info] Loading settings from build.sbt,version.sbt ...`,
`[info] Resolving key references (27245 settings) ...`,
`[info] Set current project to server (in build file:/home/fossa/prisma/server/)`,
`[info] Updating gc-values`,
`https://repo1.maven.org/maven2/org/scalatest/scalatest_2.12/3.0.4/scalatest_2.12-3.0.4.pom`,
` 100.0% [##########] 5.2 KiB (29.7 KiB / s)`,
`[info] Resolved gc-values dependencies`,
`[info] Updating gc-values`,
`[info] Resolved gc-values dependencies`,
`[info] Updating authorg/maven2/com/pauldijou/jwt-core_2.12/0.14.1/jwt-core_2.12-0.14.1.pom`,
`https://repo1.maven.org/maven2/com/pauldijou/jwt-core_2.12/0.14.1/jwt-core_2.12-0.14.1.pom`,
` 100.0% [##########] 2.3 KiB (90.6 KiB / s)`,
`[info] Fetched artifacts of gc-values`,
`[info] Updating prisma-config`,
`https://repo1.maven.org/maven2/org/yaml/snakeyaml/1.19/snakeyaml-1.19.pom`,
` 100.0% [##########] 34.4 KiB (649.6 KiB / s)`,
`[info] Resolved prisma-config dependencies`,
`[info] Updating {file:/home/fossa/prisma/server/}shared-models...`,
` https://repo1.maven.org/maven2/com/rabbitmq/amqp-client/4.1.0/amqp-client-4.1.0.jar`,
`[info] Fetching artifacts of api-connector-postgresql`,
}

for _, line := range disallowed {
actual := sbt.FilterLine(line)
assert.False(t, actual, line)
}
}
Loading

0 comments on commit 3a59e35

Please sign in to comment.