require 'spec_helper' module LicenseFinder describe Gradle do let(:options) { {} } subject { Gradle.new(options.merge(project_path: Pathname('/fake/path'))) } let(:content) { [] } it_behaves_like 'a PackageManager' describe '#current_packages' do before do allow(Dir).to receive(:chdir).with(Pathname('/fake/path')).and_return(['', true]) dependencies = double(:subject_dependency_file, dependencies: content) expect(GradleDependencyFinder).to receive(:new).and_return(dependencies) end it 'uses custom subject command, if provided' do subject = Gradle.new(gradle_command: 'subjectfoo', project_path: Pathname('/fake/path')) expect(Dir).to receive(:chdir).with(Pathname('/fake/path')) { |&block| block.call } expect(subject).to receive(:capture).with('subjectfoo downloadLicenses').and_return(['', true]) subject.current_packages end it 'sets the working directory to project_path, if provided' do subject = Gradle.new(project_path: Pathname('/Users/foo/bar')) expect(Dir).to receive(:chdir).with(Pathname('/Users/foo/bar')) { |&block| block.call } expect(subject).to receive(:capture).with('gradle --console plain downloadLicenses').and_return(['', true]) subject.current_packages end context 'when dependencies are found' do let(:content) do [ " " ] end it 'lists all dependencies' do expect(subject.current_packages.map(&:name)).to eq ['spring-aop', 'spring-core'] end context 'when gradle group ids option is enabled' do let(:options) { { gradle_include_groups: true } } it 'lists the dependencies with the group id' do expect(subject.current_packages.map(&:name)).to eq ['org.springframework:spring-aop', 'org.springframework:spring-core'] end end end context 'when multiple licenses exist' do let(:content) do [ " " ] end it 'lists all dependencies' do expect(subject.current_packages.first.licenses.map(&:name)).to eq ['License 1', 'License 2'] end end context 'when no licenses exist' do let(:content) do [ " " ] end it 'returns unknown' do expect(subject.current_packages.first.licenses.map(&:name)).to eq ['unknown'] end end context 'when multiple license files exist' do let(:content) do [ " ", " " ] end it 'lists all dependencies' do expect(subject.current_packages.map(&:name)).to eq ['junit', 'mockito-core'] end context 'and there are duplicate dependencies' do let(:content) do [ " ", " ", " " ] end it 'removes duplicates' do expect(subject.current_packages.map(&:name)).to eq ['junit', 'mockito-core'] end end end end end end