Skip to content

Commit

Permalink
[Added] composer support
Browse files Browse the repository at this point in the history
fixes and specs

fix spec

added composer features

updated readme, features test

fixing indentation

fixed rubocop errors

fixed specs, added logic for specs to run properly

fix rubocop errors

fix broken tests, rubocop errors, incorrect fixtures, extra code
  • Loading branch information
zk-kb4 committed Jun 3, 2019
1 parent d412c86 commit 13ecaab
Show file tree
Hide file tree
Showing 15 changed files with 193 additions and 2 deletions.
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ ENV GRADLE_VERSION 4.10.3
ENV RUBY_VERSION 2.6.1
ENV MIX_VERSION 1.0
ENV JDK_VERISON 8u211
ENV COMPOSER_ALLOW_SUPERUSER 1

# programs needed for building
RUN apt-get update && apt-get install -y \
Expand Down Expand Up @@ -142,6 +143,16 @@ RUN wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsof
sudo apt-get update &&\
sudo apt-get install -y dotnet-runtime-2.1 dotnet-sdk-2.1

RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 4F4EA0AAE5267A6C &&\
echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu xenial main" | sudo tee /etc/apt/sources.list.d/php.list &&\
apt-get update &&\
apt-get install -y php7.1-cli &&\
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" &&\
php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" &&\
php composer-setup.php &&\
php -r "unlink('composer-setup.php');" &&\
mv composer.phar /usr/bin/composer

# install license_finder
COPY . /LicenseFinder
RUN bash -lc "cd /LicenseFinder && bundle install -j4 && rake install"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ report.
* Scala (via `sbt`)
* Rust (via `cargo`)
* Go Modules (via `go mod`)
* PHP (via `composer`)

## Installation

Expand Down Expand Up @@ -187,6 +188,7 @@ languages, as long as that language has a package definition in the project dire
* `conanfile.txt` file (for `conan`)
* `build.sbt` file (for `sbt`)
* `Cargo.lock` file (for `cargo`)
* `composer.lock` file (for `composer`)


### Continuous Integration
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.8.0
5.8.1
14 changes: 14 additions & 0 deletions features/features/package_managers/composer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require_relative '../../support/feature_helper'

describe 'Composer Dependencies' do
let(:php_developer) { LicenseFinder::TestingDSL::User.new }

specify 'are shown in reports' do
LicenseFinder::TestingDSL::ComposerProject.create
php_developer.run_license_finder
expect(php_developer).to be_seeing_line 'vlucas/phpdotenv, v3.3.3, "New BSD"'
expect(php_developer).to be_seeing_line 'symfony/debug, v4.2.8, MIT'
end
end
12 changes: 12 additions & 0 deletions features/fixtures/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "license_finder/fixture",
"description": "A sample composer.json file.",
"version": "1.0.0",
"license": "MIT",
"require": {
"vlucas/phpdotenv": "3.3.3"
},
"require-dev": {
"symfony/debug": "4.2.8"
}
}
10 changes: 10 additions & 0 deletions features/support/testing_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ def install
end
end

class ComposerProject < Project
def add_dep
install_fixture('composer.json')
end

def install
shell_out('composer install')
end
end

class YarnProject < Project
def add_dep
add_to_file('yarn.lock', '')
Expand Down
1 change: 1 addition & 0 deletions lib/license_finder/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,4 @@ def log_activation(activation)
require 'license_finder/packages/yarn_package'
require 'license_finder/packages/sbt_package'
require 'license_finder/packages/cargo_package'
require 'license_finder/packages/composer_package'
1 change: 1 addition & 0 deletions lib/license_finder/package_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,6 @@ def log_to_file(contents)
require 'license_finder/package_managers/conan'
require 'license_finder/package_managers/sbt'
require 'license_finder/package_managers/cargo'
require 'license_finder/package_managers/composer'

require 'license_finder/package'
57 changes: 57 additions & 0 deletions lib/license_finder/package_managers/composer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

require 'json'

module LicenseFinder
class Composer < PackageManager
SHELL_COMMAND = 'composer licenses --format=json'

def possible_package_paths
[project_path.join('composer.lock'), project_path.join('composer.json')]
end

def current_packages
dependency_list.map do |name, dependency|
ComposerPackage.new(name, dependency['version'], spec_licenses: dependency['license'])
end
end

def prepare
_stdout, stderr, status = Dir.chdir(project_path) { Cmd.run(Composer.prepare_command) }
return if status.success?

log_errors stderr
raise "Prepare command '#{prep_cmd}' failed" unless @prepare_no_fail
end

def self.package_management_command
'composer'
end

def self.prepare_command
'composer install'
end

def package_path
project_path.join('composer.json')
end

def lockfile_path
project_path.join('composer.lock')
end

def dependency_list
json ||= composer_json
json.fetch('dependencies', {}).reject { |_, d| d.is_a?(String) }
end

def composer_json
stdout, _stderr, status = Cmd.run(Composer::SHELL_COMMAND)
return [] unless status.success?

json = JSON(stdout)

json
end
end
end
9 changes: 9 additions & 0 deletions lib/license_finder/packages/composer_package.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module LicenseFinder
class ComposerPackage < Package
def package_manager
'Composer'
end
end
end
2 changes: 1 addition & 1 deletion lib/license_finder/scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module LicenseFinder
class Scanner
PACKAGE_MANAGERS = [GoModules, GoDep, GoWorkspace, Go15VendorExperiment, Glide, Gvt, Govendor, Trash, Dep, Bundler, NPM, Pip,
Yarn, Bower, Maven, Gradle, CocoaPods, Rebar, Nuget, Carthage, Mix, Conan, Sbt, Cargo, Dotnet].freeze
Yarn, Bower, Maven, Gradle, CocoaPods, Rebar, Nuget, Carthage, Mix, Conan, Sbt, Cargo, Dotnet, Composer].freeze

def initialize(config = { project_path: Pathname.new('') })
@config = config
Expand Down
8 changes: 8 additions & 0 deletions spec/fixtures/all_pms/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"require": {
"vlucas/phpdotenv": "^3.3"
},
"require-dev": {
"symfony/debug": "^4.2"
}
}
Empty file.
25 changes: 25 additions & 0 deletions spec/lib/license_finder/package_managers/composer_package_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require 'spec_helper'

module LicenseFinder
describe ComposerPackage do
subject do
described_class.new('symfony/debug', 'v3.0.7', 'license' => [{ 'name' => 'MIT' }])
end

its(:name) { should == 'symfony/debug' }
its(:version) { should == 'v3.0.7' }
its(:summary) { should eq '' }
its(:description) { should == '' }
its(:homepage) { should == '' }
its(:package_manager) { should eq 'Composer' }

describe '#license_names_from_spec' do
it 'finds the license for both license structures' do
package = ComposerPackage.new('test', 'v1.2.3', spec_licenses: ['MIT'])
expect(package.license_names_from_spec).to eq ['MIT']
end
end
end
end
41 changes: 41 additions & 0 deletions spec/lib/license_finder/package_managers/composer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require 'spec_helper'
require 'fakefs/spec_helpers'
require 'json'

module LicenseFinder
describe Composer do
let(:root) { '/fake-composer-project' }
let(:composer) { Composer.new project_path: Pathname.new(root) }

it_behaves_like 'a PackageManager'

let(:composer_shell_command_output) do
{
'require' => {
'vlucas/phpdotenv' => '3.3.*'
},
'require-dev' => {
'symfony/debug' => '4.2.*'
}
}.to_json
end

describe '.prepare' do
subject { Composer.new(project_path: Pathname(root), logger: double(:logger, active: nil)) }

include FakeFS::SpecHelpers
before do
FileUtils.mkdir_p(Dir.tmpdir)
FileUtils.mkdir_p(root)
end

it 'should call composer install' do
expect(SharedHelpers::Cmd).to receive(:run).with('composer install')
.and_return([composer_shell_command_output, '', cmd_success])
subject.prepare
end
end
end
end

0 comments on commit 13ecaab

Please sign in to comment.