Skip to content

Commit

Permalink
Adds Go Module support
Browse files Browse the repository at this point in the history
  • Loading branch information
osis committed Sep 29, 2018
1 parent 279bd25 commit 8a20210
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM ubuntu:xenial

# Versioning
ENV PIP_INSTALL_VERSION 10.0.1
ENV GO_LANG_VERSION 1.10.2
ENV GO_LANG_VERSION 1.11
ENV MAVEN_VERSION 3.5.3
ENV SBT_VERSION 1.1.1
ENV GRADLE_VERSION 4.2
Expand Down
1 change: 1 addition & 0 deletions features/features/package_managers/dep_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
let(:go_developer) { LicenseFinder::TestingDSL::User.new }

specify 'are shown in reports for a project' do
ENV['DEPNOLOCK'] = '1'
project = LicenseFinder::TestingDSL::DepProject.create
ENV['GOPATH'] = "#{project.project_dir}/gopath_dep"

Expand Down
13 changes: 13 additions & 0 deletions features/features/package_managers/go_modules_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require_relative '../../support/feature_helper'

describe 'Go Modules Dependencies' do
let(:go_developer) { LicenseFinder::TestingDSL::User.new }

specify 'are shown in reports for a project' do
LicenseFinder::TestingDSL::GoModulesProject.create
go_developer.run_license_finder('go_modules')

expect(go_developer).to be_seeing_line 'gopkg.in/check.v1, v0.0.0-20161208181325-20d25e280405, "Apache 2.0, MIT"'
expect(go_developer).to be_seeing_line 'gopkg.in/yaml.v2, v2.2.1, "Apache 2.0, MIT"'
end
end
3 changes: 3 additions & 0 deletions features/fixtures/go_modules/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module foo

require gopkg.in/yaml.v2 v2.2.1
4 changes: 4 additions & 0 deletions features/fixtures/go_modules/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
5 changes: 5 additions & 0 deletions features/fixtures/go_modules/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package foo

import (
"gopkg.in/yaml.v2"
)
14 changes: 14 additions & 0 deletions features/support/testing_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,20 @@ def shell_out(command)
end
end

class GoModulesProject < Project
def add_dep
clone('go_modules')
end

def install
shell_out('go mod vendor')
end

def shell_out(command)
ProjectDir.new(Paths.root.join('tmp', 'projects', 'my_app', 'go_modules')).shell_out(command)
end
end

class GlideProject < Project
def add_dep
clone('gopath_glide')
Expand Down
1 change: 1 addition & 0 deletions lib/license_finder/package_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def log_to_file(contents)
require 'license_finder/package_managers/gvt'
require 'license_finder/package_managers/glide'
require 'license_finder/package_managers/govendor'
require 'license_finder/package_managers/go_modules'
require 'license_finder/package_managers/bundler'
require 'license_finder/package_managers/npm'
require 'license_finder/package_managers/yarn'
Expand Down
52 changes: 52 additions & 0 deletions lib/license_finder/package_managers/go_modules.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module LicenseFinder
class GoModules < PackageManager
PACKAGES_FILE = 'go.sum'.freeze

class << self
def takes_priority_over
Go15VendorExperiment
end

def prepare_command
'go mod vendor'
end
end

def active?
sum_files?
end

def current_packages
sum_file_paths.uniq.map do |file_path|
read_sum(file_path)
end.flatten
end

private

def sum_files?
sum_file_paths.any?
end

def sum_file_paths
Dir[project_path.join(PACKAGES_FILE)]
end

def read_sum(file_path)
contents = File.read(file_path)
contents.each_line.map do |line|
line.include?('go.mod') ? nil : read_package(file_path, line)
end.compact
end

def read_package(file_path, line)
parts = line.split(' ')
install_path = File.dirname(file_path)

name = parts[0]
version = parts[1]

Package.new(name, version, install_path: install_path)
end
end
end
2 changes: 1 addition & 1 deletion lib/license_finder/scanner.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module LicenseFinder
class Scanner
PACKAGE_MANAGERS = [GoDep, GoWorkspace, Go15VendorExperiment, Glide, Gvt, Govendor, Dep, Bundler, NPM, Pip,
PACKAGE_MANAGERS = [GoModules, GoDep, GoWorkspace, Go15VendorExperiment, Glide, Gvt, Govendor, Dep, Bundler, NPM, Pip,
Yarn, Bower, Maven, Gradle, CocoaPods, Rebar, Nuget, Carthage, Mix, Conan, Sbt, Cargo].freeze

def initialize(config = { project_path: Pathname.new('') })
Expand Down
Empty file added spec/fixtures/all_pms/go.sum
Empty file.
4 changes: 4 additions & 0 deletions spec/fixtures/config/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
60 changes: 60 additions & 0 deletions spec/lib/license_finder/package_managers/go_modules_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'spec_helper'
require 'fakefs/spec_helpers'

module LicenseFinder
describe GoModules do
it_behaves_like 'a PackageManager'

let(:src_path) { '/workspace/code' }
let(:sum_path) { "#{src_path}/go.sum" }
let(:vendor_path) { "#{src_path}/vendor" }

subject { GoModules.new(project_path: Pathname(src_path), logger: double(:logger, active: nil)) }

describe '#current_packages' do
before do
FakeFS.activate!
end

after do
FakeFS.deactivate!
end

let(:src_path) { '/workspace/code' }
let(:sum_path) { "#{src_path}/go.sum" }

let(:content) do
FakeFS.without do
fixture_from('go.sum')
end
end

it 'finds all the packages all go.sum files' do
FileUtils.mkdir_p(vendor_path)
File.write(sum_path, content)

packages = subject.current_packages

expect(packages.length).to eq 2

expect(packages.first.name).to eq 'gopkg.in/check.v1'
expect(packages.first.version).to eq 'v0.0.0-20161208181325-20d25e280405'

expect(packages.last.name).to eq 'gopkg.in/yaml.v2'
expect(packages.last.version).to eq 'v2.2.1'
end
end

describe '.prepare_command' do
it 'returns the correct package management command' do
expect(described_class.prepare_command).to eq('go mod vendor')
end
end

describe '.takes_priority_over' do
it 'returns the package manager it takes priority over' do
expect(described_class.takes_priority_over).to eq(Go15VendorExperiment)
end
end
end
end

0 comments on commit 8a20210

Please sign in to comment.