Skip to content

Commit

Permalink
feat(ruby analyzer) add fallback mechanism for ruby analyzers when bu…
Browse files Browse the repository at this point in the history
…ndler fails
  • Loading branch information
microsoftly committed Sep 11, 2018
2 parents d9afc8f + a1bcdc9 commit ca66f70
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 32 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ dev: docker-test-base
-v $$GOPATH/bin:/home/fossa/go/bin \
quay.io/fossa/fossa-cli-test-base /bin/bash

.PHONY: dev-osx
dev-osx: docker-test-base
docker run --rm -it \
-v $$GOPATH/src/github.com/fossas/fossa-cli:/home/fossa/go/src/github.com/fossas/fossa-cli \
quay.io/fossa/fossa-cli-test-base /bin/bash

.PHONY: install
install: $(PREFIX)/fossa

Expand All @@ -71,6 +77,7 @@ vendor: $(DEP)
.PHONY: clean
clean:
rm -f $(BIN)/fossa
find -name *_generated.go | grep -v vendor | xargs rm -f

# Testing tasks.
.PHONY: test
Expand Down
103 changes: 75 additions & 28 deletions analyzers/ruby/ruby.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,47 +127,94 @@ func (a *Analyzer) IsBuilt() (bool, error) {
}

func (a *Analyzer) Analyze() (graph.Deps, error) {
lockfilePath := filepath.Join(a.Module.Dir, "Gemfile.lock")
if a.Options.LockfilePath != "" {
lockfilePath = a.Options.LockfilePath
strategy := "list-lockfile"

if a.Options.Strategy != "" {
strategy = a.Options.Strategy
}

switch a.Options.Strategy {
lockfilePath := a.lockfilePath()

switch strategy {
case "list":
gems, err := a.Bundler.List()
if err != nil {
return graph.Deps{}, err
}
imports, deps := FromGems(gems)
return graph.Deps{
Direct: imports,
Transitive: deps,
}, nil
return a.bundlerListAnalyzerStrategy()
case "lockfile":
lockfile, err := bundler.FromLockfile(lockfilePath)
if err != nil {
return graph.Deps{}, err
}
imports, deps := FromLockfile(lockfile)
return graph.Deps{
Direct: imports,
Transitive: deps,
}, nil
return a.lockfileAnalyzerStrategy(lockfilePath)
case "list-lockfile":
fallthrough
default:
lockfile, err := bundler.FromLockfile(lockfilePath)
if err != nil {
return graph.Deps{}, err
}
gems, err := a.Bundler.List()
if err != nil {
return a.bundlerListLockfileAnalyzerStrategy(lockfilePath)
}
}

func (a *Analyzer) bundlerListLockfileAnalyzerStrategy(lockfilePath string) (graph.Deps, error) {
lockfile, err := bundler.FromLockfile(lockfilePath)
if err != nil {
if a.Options.Strategy != "" {
return graph.Deps{}, err
}

return a.lockfileAnalyzerStrategy(lockfilePath)
}

gems, err := a.Bundler.List()
if err == nil {
imports, deps := FilteredLockfile(gems, lockfile)

return graph.Deps{
Direct: imports,
Transitive: deps,
}, nil
}

if a.Options.Strategy != "" {
return graph.Deps{}, err
}

deps, err := a.lockfileAnalyzerStrategy(lockfilePath)

if err == nil {
return deps, err
}

if a.Options.Strategy != "" {
return graph.Deps{}, err
}

return a.bundlerListAnalyzerStrategy()
}

func (a *Analyzer) bundlerListAnalyzerStrategy() (graph.Deps, error) {
gems, err := a.Bundler.List()
if err != nil {
return graph.Deps{}, err
}

imports, deps := FromGems(gems)

return graph.Deps{
Direct: imports,
Transitive: deps,
}, nil
}

func (a *Analyzer) lockfileAnalyzerStrategy(lockfilePath string) (graph.Deps, error) {
lockfile, err := bundler.FromLockfile(lockfilePath)
if err != nil {
return graph.Deps{}, err
}

imports, deps := FromLockfile(lockfile)

return graph.Deps{
Direct: imports,
Transitive: deps,
}, nil
}

func (a *Analyzer) lockfilePath() string {
if a.Options.LockfilePath != "" {
return a.Options.LockfilePath
}
return filepath.Join(a.Module.Dir, "Gemfile.lock")
}
53 changes: 50 additions & 3 deletions analyzers/ruby/ruby_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@ import (
"github.com/stretchr/testify/assert"

"github.com/fossas/fossa-cli/analyzers/ruby"
"github.com/fossas/fossa-cli/buildtools/bundler"
"github.com/fossas/fossa-cli/module"
"github.com/fossas/fossa-cli/pkg"
)

func TestCustomGemfileLockPath(t *testing.T) {
buildTarget := "github.com/fossas/fossa-cli/cmd/fossa"
buildTarget := "testdata"
m := module.Module{
Name: "test",
Type: pkg.Ruby,
BuildTarget: buildTarget,
Dir: buildTarget,
}

gemModule := m
gemModule.Options = map[string]interface{}{
"strategy": "lockfile",
"gemfile-lock-path": filepath.Join("testdata", "Gemfile.lock"),
"strategy": "lockfile",
}
analyzer, err := ruby.New(gemModule)
assert.NoError(t, err)
Expand All @@ -43,3 +44,49 @@ func TestCustomGemfileLockPath(t *testing.T) {

assert.NotEqual(t, analyzed, analyzedCustom)
}

func TestFallbackOnMissingBundler(t *testing.T) {
buildTarget := "testdata"
useLockfileOptions := map[string]interface{}{
"strategy": "lockfile",
}

useBundlerWithLockfilePathOptions := map[string]interface{}{}

gemModuleUsingLockfile := module.Module{
Name: "test",
Type: pkg.Ruby,
BuildTarget: buildTarget,
Dir: buildTarget,
Options: useLockfileOptions,
}

gemModuleUsingFallbackToLockfile := module.Module{
Name: "test",
Type: pkg.Ruby,
BuildTarget: buildTarget,
Options: useBundlerWithLockfilePathOptions,
Dir: buildTarget,
}

lockfileBasedAnalyzer, lockfileBasedAnalyzerErr := ruby.New(gemModuleUsingLockfile)
assert.NoError(t, lockfileBasedAnalyzerErr)

fallbackBasedAnalyzer, fallbackBasedAnalyzerErr := ruby.New(gemModuleUsingFallbackToLockfile)
assert.NoError(t, fallbackBasedAnalyzerErr)

fallbackBasedAnalyzer.Bundler = bundler.Bundler{
Cmd: "doesntWork",
}

lockfileBasedAnalyze, lockfileBasedAnalyzerErr := lockfileBasedAnalyzer.Analyze()
assert.NoError(t, lockfileBasedAnalyzerErr)

fallbackBasedAnalyze, fallbackBasedAnalyzerErr := fallbackBasedAnalyzer.Analyze()
assert.NoError(t, fallbackBasedAnalyzerErr)

// ensure that the arrays are actually populated and we aren't comparing equivalent empty results
assert.NotEmpty(t, fallbackBasedAnalyze.Direct)
assert.NotEmpty(t, lockfileBasedAnalyze.Direct)
assert.Equal(t, fallbackBasedAnalyze, lockfileBasedAnalyze)
}
2 changes: 1 addition & 1 deletion docker/test-base/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,4 @@ RUN git clone --depth=1 https://github.com/MacDownApp/macdown
RUN git clone --depth=1 https://github.com/fossas/ant-example-project

# Raw projects:
RUN git clone --depth=1 https://github.com/jelaas/bifrost-framework
RUN git clone --depth=1 https://github.com/jelaas/bifrost-framework

0 comments on commit ca66f70

Please sign in to comment.