Skip to content

Commit

Permalink
Merge pull request #606 from github/fix-gradle-source-when-gradle-una…
Browse files Browse the repository at this point in the history
…vailable

Fix running gradle and gradle source tests when gradle is unavailable
  • Loading branch information
jonabc authored Jan 6, 2023
2 parents 89e6718 + 0716671 commit ef60311
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 143 deletions.
39 changes: 18 additions & 21 deletions lib/licensed/sources/gradle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def project_files
end

def enabled?
gradle_runner.enabled? && File.exist?(config.pwd.join("build.gradle"))
!executable.to_s.empty? && File.exist?(config.pwd.join("build.gradle"))
end

def enumerate_dependencies
Expand All @@ -62,8 +62,19 @@ def enumerate_dependencies

private

def executable
return @executable if defined?(@executable)

@executable = begin
gradlew = File.join(config.pwd, "gradlew")
return gradlew if File.executable?(gradlew)

"gradle" if Licensed::Shell.tool_available?("gradle")
end
end

def gradle_runner
@gradle_runner ||= Runner.new(config.pwd, configurations)
@gradle_runner ||= Runner.new(config.pwd, configurations, executable)
end

# Returns the configurations to include in license generation.
Expand Down Expand Up @@ -116,8 +127,9 @@ def url_for(dependency)
# The Gradle::Runner class is a wrapper which provides
# an interface to run gradle commands with the init script initialized
class Runner
def initialize(root_path, configurations)
def initialize(root_path, configurations, executable)
@root_path = root_path
@executable = executable
@init_script = create_init_script(root_path, configurations)
end

Expand All @@ -126,28 +138,13 @@ def run(command, source_path)
# The configuration cache is an incubating feature that can be activated manually.
# The gradle plugin for licenses does not support it so we prevent it to run for gradle version supporting it.
args << "--no-configuration-cache" if gradle_version >= "6.6"
Licensed::Shell.execute(executable, "-q", "--init-script", @init_script.path, *args)
end

def enabled?
!executable.to_s.empty?
Licensed::Shell.execute(@executable, "-q", "--init-script", @init_script.path, *args)
end

private

def gradle_version
@gradle_version ||= Licensed::Shell.execute(executable, "--version").scan(/Gradle [\d+]\.[\d+]/).last.split(" ").last
end

def executable
return @executable if defined?(@executable)

@executable = begin
gradlew = File.join(@root_path, "gradlew")
return gradlew if File.executable?(gradlew)

"gradle" if Licensed::Shell.tool_available?("gradle")
end
@gradle_version ||= Licensed::Shell.execute(@executable, "--version").scan(/Gradle [\d+]\.[\d+]/).last.split(" ").last
end

def create_init_script(path, configurations)
Expand Down Expand Up @@ -202,7 +199,7 @@ def id = artifact.moduleVersion.id
# Prefixes the gradle command with the project name for multi-build projects.
def format_command(command, source_path)
Dir.chdir(source_path) do
path = Licensed::Shell.execute(executable, "properties", "-Dorg.gradle.logging.level=quiet").scan(/path:.*/).last.split(" ").last
path = Licensed::Shell.execute(@executable, "properties", "-Dorg.gradle.logging.level=quiet").scan(/path:.*/).last.split(" ").last
path == ":" ? command : "#{path}:#{command}"
end
end
Expand Down
246 changes: 124 additions & 122 deletions test/sources/gradle_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,12 @@
require "tmpdir"
require "fileutils"

describe Licensed::Sources::Gradle do
describe "Single project" do
let(:fixtures) { File.expand_path("../../fixtures/gradle/single_project", __FILE__) }
let(:config) { Licensed::AppConfiguration.new({ "source_path" => Dir.pwd, "root" => fixtures }) }
let(:source) { Licensed::Sources::Gradle.new(config) }

describe "enabled?" do
it "is true if build.gradle exists and gradle is available" do
Dir.chdir(fixtures) do
assert source.enabled?
end
end

it "is false if build.gradle does not exist" do
Dir.chdir(Dir.tmpdir) do
refute source.enabled?
end
end
end

describe "dependencies" do
it "includes declared dependencies" do
Dir.chdir fixtures do
dep = source.dependencies.detect { |d| d.name == "io.netty:netty-all" }
assert dep
assert_equal "gradle", dep.record["type"]
assert_equal "4.1.33.Final", dep.version
end
end

it "does not include test dependencies" do
Dir.chdir fixtures do
refute source.dependencies.detect { |d| d.name == "org.junit.jupiter:junit-jupiter" }
end
end

it "cleans up grade licenses csv content" do
Dir.chdir fixtures do
dep = source.dependencies.detect { |d| d.name == "io.netty:netty-all" }
# load the dependency record which creates temp license-*.gradle files
dep.record

refute Dir.glob(Pathname.pwd.join("license-*.gradle").to_path).any?
end
end
end
end

describe "Multi project" do
let(:fixtures) { File.expand_path("../../fixtures/gradle/multi_project", __FILE__) }
let(:config) { Licensed::Configuration.new({
"apps" => [{ "source_path" => "#{Dir.pwd}/lib" }, { "source_path" => "#{Dir.pwd}/app" }],
"gradle" => { "configurations" => "runtimeClasspath" },
"root" => fixtures
})
}
let(:appConfig) { config.apps.last }
let(:libConfig) { config.apps.last }
let(:source) { Licensed::Sources::Gradle.new(appConfig) }

describe "app subproject" do
let(:appConfig) { config.apps.last }
let(:source) { Licensed::Sources::Gradle.new(appConfig) }
if Licensed::Shell.tool_available?("gradle")
describe Licensed::Sources::Gradle do
describe "Single project" do
let(:fixtures) { File.expand_path("../../fixtures/gradle/single_project", __FILE__) }
let(:config) { Licensed::AppConfiguration.new({ "source_path" => Dir.pwd, "root" => fixtures }) }
let(:source) { Licensed::Sources::Gradle.new(config) }

describe "enabled?" do
it "is true if build.gradle exists and gradle is available" do
Expand All @@ -84,22 +27,22 @@
describe "dependencies" do
it "includes declared dependencies" do
Dir.chdir fixtures do
dep = source.dependencies.detect { |d| d.name == "com.google.guava:guava" }
dep = source.dependencies.detect { |d| d.name == "io.netty:netty-all" }
assert dep
assert_equal "gradle", dep.record["type"]
assert_equal "31.1-jre", dep.version
assert_equal "4.1.33.Final", dep.version
end
end

it "does not include test dependencies" do
Dir.chdir fixtures do
refute source.dependencies.detect { |d| d.name == "org.junit.jupiter:junit-jupiter-engine" }
refute source.dependencies.detect { |d| d.name == "org.junit.jupiter:junit-jupiter" }
end
end

it "cleans up grade licenses csv content" do
Dir.chdir fixtures do
dep = source.dependencies.detect { |d| d.name == "com.google.guava:guava" }
dep = source.dependencies.detect { |d| d.name == "io.netty:netty-all" }
# load the dependency record which creates temp license-*.gradle files
dep.record

Expand All @@ -109,89 +52,148 @@
end
end

describe "lib subproject" do
let(:appConfig) { config.apps.first }
describe "Multi project" do
let(:fixtures) { File.expand_path("../../fixtures/gradle/multi_project", __FILE__) }
let(:config) { Licensed::Configuration.new({
"apps" => [{ "source_path" => "#{Dir.pwd}/lib" }, { "source_path" => "#{Dir.pwd}/app" }],
"gradle" => { "configurations" => "runtimeClasspath" },
"root" => fixtures
})
}
let(:appConfig) { config.apps.last }
let(:libConfig) { config.apps.last }
let(:source) { Licensed::Sources::Gradle.new(appConfig) }
describe "enabled?" do
it "is true if build.gradle exists and gradle is available" do
Dir.chdir(fixtures) do
assert source.enabled?

describe "app subproject" do
let(:appConfig) { config.apps.last }
let(:source) { Licensed::Sources::Gradle.new(appConfig) }

describe "enabled?" do
it "is true if build.gradle exists and gradle is available" do
Dir.chdir(fixtures) do
assert source.enabled?
end
end

it "is false if build.gradle does not exist" do
Dir.chdir(Dir.tmpdir) do
refute source.enabled?
end
end
end

it "is false if build.gradle does not exist" do
Dir.chdir(Dir.tmpdir) do
refute source.enabled?
describe "dependencies" do
it "includes declared dependencies" do
Dir.chdir fixtures do
dep = source.dependencies.detect { |d| d.name == "com.google.guava:guava" }
assert dep
assert_equal "gradle", dep.record["type"]
assert_equal "31.1-jre", dep.version
end
end

it "does not include test dependencies" do
Dir.chdir fixtures do
refute source.dependencies.detect { |d| d.name == "org.junit.jupiter:junit-jupiter-engine" }
end
end

it "cleans up grade licenses csv content" do
Dir.chdir fixtures do
dep = source.dependencies.detect { |d| d.name == "com.google.guava:guava" }
# load the dependency record which creates temp license-*.gradle files
dep.record

refute Dir.glob(Pathname.pwd.join("license-*.gradle").to_path).any?
end
end
end
end

describe "dependencies" do
it "includes declared dependencies" do
Dir.chdir fixtures do
dep = source.dependencies.detect { |d| d.name == "com.google.guava:guava" }
assert dep
assert_equal "gradle", dep.record["type"]
assert_equal "31.1-jre", dep.version
describe "lib subproject" do
let(:appConfig) { config.apps.first }
let(:source) { Licensed::Sources::Gradle.new(appConfig) }
describe "enabled?" do
it "is true if build.gradle exists and gradle is available" do
Dir.chdir(fixtures) do
assert source.enabled?
end
end
end

it "does not include test dependencies" do
Dir.chdir fixtures do
refute source.dependencies.detect { |d| d.name == "org.junit.jupiter:junit-jupiter-engine" }
it "is false if build.gradle does not exist" do
Dir.chdir(Dir.tmpdir) do
refute source.enabled?
end
end
end

it "cleans up grade licenses csv content" do
Dir.chdir fixtures do
dep = source.dependencies.detect { |d| d.name == "com.google.guava:guava" }
# load the dependency record which creates temp license-*.gradle files
dep.record
describe "dependencies" do
it "includes declared dependencies" do
Dir.chdir fixtures do
dep = source.dependencies.detect { |d| d.name == "com.google.guava:guava" }
assert dep
assert_equal "gradle", dep.record["type"]
assert_equal "31.1-jre", dep.version
end
end

refute Dir.glob(Pathname.pwd.join("license-*.gradle").to_path).any?
it "does not include test dependencies" do
Dir.chdir fixtures do
refute source.dependencies.detect { |d| d.name == "org.junit.jupiter:junit-jupiter-engine" }
end
end

it "cleans up grade licenses csv content" do
Dir.chdir fixtures do
dep = source.dependencies.detect { |d| d.name == "com.google.guava:guava" }
# load the dependency record which creates temp license-*.gradle files
dep.record

refute Dir.glob(Pathname.pwd.join("license-*.gradle").to_path).any?
end
end
end
end
end
end
end

describe Licensed::Sources::Gradle::Dependency do
let(:fixtures) { File.expand_path("../../fixtures/gradle/single_project", __FILE__) }
let(:config) { Licensed::AppConfiguration.new({ "source_path" => Dir.pwd, "root" => fixtures }) }
let(:source) { Licensed::Sources::Gradle.new(config) }
describe Licensed::Sources::Gradle::Dependency do
let(:fixtures) { File.expand_path("../../fixtures/gradle/single_project", __FILE__) }
let(:config) { Licensed::AppConfiguration.new({ "source_path" => Dir.pwd, "root" => fixtures }) }
let(:source) { Licensed::Sources::Gradle.new(config) }

it "returns the dependency license" do
Dir.chdir fixtures do
dep = source.dependencies.detect { |d| d.name == "io.netty:netty-all" }
assert dep
assert_equal "apache-2.0", dep.license.key
it "returns the dependency license" do
Dir.chdir fixtures do
dep = source.dependencies.detect { |d| d.name == "io.netty:netty-all" }
assert dep
assert_equal "apache-2.0", dep.license.key

license = dep.record.licenses.find { |l| l.text =~ /Apache License/ }
assert license
assert_equal ["https://www.apache.org/licenses/LICENSE-2.0"], license.sources
license = dep.record.licenses.find { |l| l.text =~ /Apache License/ }
assert license
assert_equal ["https://www.apache.org/licenses/LICENSE-2.0"], license.sources
end
end
end

it "cleans up grade licenses csv content" do
Dir.chdir fixtures do
dep = source.dependencies.detect { |d| d.name == "io.netty:netty-all" }
# load the dependency record, pulling license files
dep.record
it "cleans up grade licenses csv content" do
Dir.chdir fixtures do
dep = source.dependencies.detect { |d| d.name == "io.netty:netty-all" }
# load the dependency record, pulling license files
dep.record

refute Pathname.pwd.join(Licensed::Sources::Gradle::GRADLE_LICENSES_PATH).exist?
refute Pathname.pwd.join(Licensed::Sources::Gradle::GRADLE_LICENSES_PATH).exist?
end
end
end

it "does not make any network requests when accessing non-license data" do
Licensed::Sources::Gradle::Dependency.expects(:retrieve_license).never
Licensed::Sources::Gradle::Dependency.expects(:load_csv).never
it "does not make any network requests when accessing non-license data" do
Licensed::Sources::Gradle::Dependency.expects(:retrieve_license).never
Licensed::Sources::Gradle::Dependency.expects(:load_csv).never

Dir.chdir fixtures do
dep = source.dependencies.detect { |d| d.name == "io.netty:netty-all" }
# accessing non-license dependency data does not make network requests
dep.name
dep.version
Dir.chdir fixtures do
dep = source.dependencies.detect { |d| d.name == "io.netty:netty-all" }
# accessing non-license dependency data does not make network requests
dep.name
dep.version
end
end
end
end

0 comments on commit ef60311

Please sign in to comment.