diff --git a/README.md b/README.md index a2b7477..39456fb 100644 --- a/README.md +++ b/README.md @@ -134,11 +134,38 @@ And look at the dependencies tree lock_jars --tree ``` -As `lock_jars` uses ruby-maven to resolve the jar dependencies. -Since jar-dependencies does not declare ruby-maven as runtime dependency -(you just not need ruby-maven during runtime only when you want to -setup the project it is needed) it is advicable to have it as -development dependency in your Gemfile. +`lock_jars` resolves the jars with the Maven resolver bundled inside +jar-dependencies (Mima), so you no longer need Maven or the ruby-maven gem +installed to run it. + +# Jarfile + +If your project is not a gem, or you would rather keep jar versions out of the +gemspec, list them in a `Jarfile`. The `lock_jars` will read these together with +your gemspec and bundled gems and resolves everything in one go into `Jars.lock`. + +A `Jarfile` is plain Ruby and reads similar to a *Gemfile*: + +```ruby +jar 'org.yaml:snakeyaml', '2.7' +pom 'org.springframework.boot:spring-boot-dependencies', '3.2.5' + +# using artifacts that aren't on maven central +repository 'my-nexus', 'https://nexus.example.com/repo' + +# drop a transitive dependency you do not want +jar 'io.dropwizard:dropwizard-logging', '5.0.2', exclusions: ['jakarta.validation:jakarta.validation-api'] + +# keep some jars out of the runtime +scope :test do + jar 'org.junit.jupiter:junit-jupiter', '5.11.0' +end +``` + +Both coordinate styles work, `'group:artifact', 'version'` or the packed +`'group:artifact:version'`, and you can pass a `:classifier`, a `:scope`, or a +block with nested `exclusion`s. Use `snapshot_repository` for a snapshot repo +and `local 'path/to/some.jar'` to point at a jar that already sits on disk. # Proxy and mirror setup diff --git a/lib/jar_dependencies.rb b/lib/jar_dependencies.rb index f16e65d..2844726 100644 --- a/lib/jar_dependencies.rb +++ b/lib/jar_dependencies.rb @@ -56,7 +56,7 @@ module Jars autoload :Mima, 'jars/mima' @jars = {} - @jars_lock = false + @jars_lock = nil @jars_locked = nil class JarLoadError < LoadError; end @@ -177,13 +177,13 @@ def lock_path(basedir = nil) def reset instance_variables.each do |var| - next if var == :@jars_lock + next if var == :@jars instance_variable_set(var, nil) end + @jars.clear + Jars::MavenSettings.reset - @jars = {} - @jars_locked = nil end def maven_local_settings @@ -263,11 +263,15 @@ def setup(options = nil) end end + # Requires *Jars.lock* (once). + # @return whether a lock was actually required by this call def require_jars_lock - return if @jars_lock + return false unless @jars_lock.nil? - require_jars_lock! - @jars_lock ||= true # rubocop:disable Naming/MemoizedInstanceVariableName + require_jars_lock! # sets @jars_lock to the loaded lock (truthy) when there is one + required = !@jars_lock.nil? + @jars_lock = false # do not require again + required end def mark_as_required(group_id, artifact_id, *classifier_version) diff --git a/lib/jars/gemspec_artifacts.rb b/lib/jars/gemspec_artifacts.rb index e3571b3..9609a82 100644 --- a/lib/jars/gemspec_artifacts.rb +++ b/lib/jars/gemspec_artifacts.rb @@ -183,6 +183,7 @@ def key end end + # @return [Array[Artifact]] attr_reader :artifacts def initialize(spec) diff --git a/lib/jars/jarfile.rb b/lib/jars/jarfile.rb new file mode 100644 index 0000000..cb96527 --- /dev/null +++ b/lib/jars/jarfile.rb @@ -0,0 +1,193 @@ +# frozen_string_literal: true + +require 'jars/gemspec_artifacts' + +module Jars + # Self-contained reader for the (j)bundler Jarfile DSL. + class Jarfile + def self.load(file) + new.tap { |jarfile| jarfile.eval_file(file) } + end + + Repository = Struct.new(:name, :url, :snapshot) + LocalJar = Struct.new(:group_id, :artifact_id, :version, :path) do + # a system-scoped Jars.lock entry: "group:artifact:version:system:path" + def to_lock_entry + "#{group_id}:#{artifact_id}:#{version}:system:#{path}" + end + end + + def initialize + @artifacts = [] + @repositories = [] + @local_jars = [] + end + + attr_reader :artifacts, :repositories, :local_jars + + def eval_file(file) + instance_eval(File.read(file), file.to_s) + self + end + + # -- DSL -- + + def jar(*args, &block) + add(:jar, args, &block) + end + + def pom(*args, &block) + add(:pom, args, &block) + end + + def scope(scope) + previous = @scope + @scope = scope + yield if block_given? + ensure + @scope = previous + end + + def repository(name, url = nil) + @repositories << Repository.new(name.to_s, url || name, false) + end + alias_method :source, :repository + + def snapshot_repository(name, url = nil) + @repositories << Repository.new(name.to_s, url || name, true) + end + + # a local jar on disk - carried through as a system-scoped dependency + def local(path) + abs = File.expand_path(path) + name = File.basename(abs).sub(/\.jar$/, '') + if (dash = name.rindex('-')) + version = name[(dash + 1)..] + name = name[0...dash] + else + version = '0' + end + @local_jars << LocalJar.new('ruby.maven-tools.jar', name, version, abs) + end + + # @deprecated in maven-tools, kept as a no-op so existing Jarfiles still load + def jruby(*) + nil + end + + # Normalizes a DSL exclusion list ('group:artifact' or ['group', 'artifact']) to coordinate strings + def self.normalize_exclusions(list) + return [] if list.nil? + + list.map { |e| e.is_a?(Array) ? e.join(':') : e.to_s } + end + + private + + def add(type, args, &block) + options = args.last.is_a?(Hash) ? args.pop : {} + group_id, artifact_id, classifier, version = coordinate(args) + + config = DepConfig.new(options) + config.instance_eval(&block) if block + + classifier ||= config.classifier + scope = (config.scope || @scope)&.to_s + exclusions = build_exclusions(config.exclusions) + @artifacts << GemspecArtifacts::Artifact.new( + type.to_s, group_id, artifact_id, classifier, version, exclusions, scope: scope + ) + end + + # Normalizes the positional coordinate forms into [group, artifact, classifier, version]. + # Mirrors maven-tools Artifact.from: the colon-packed string carries the classifier. + def coordinate(args) + case args.size + when 1 + # 'group:artifact:version' or 'group:artifact:version:classifier' + group_id, artifact_id, version, classifier = args[0].split(':') + when 2 + # 'group:artifact', 'version' or 'group:artifact:classifier', 'version' + group_id, artifact_id, classifier = args[0].split(':') + version = args[1] + when 3 + if args[0].include?(':') + # 'group:artifact[:classifier]', version-constraint, version-constraint + group_id, artifact_id, classifier = args[0].split(':') + version = args[1..].join(', ') + else + # 'group', 'artifact', 'version' + group_id, artifact_id, version = args + end + end + [group_id, artifact_id, classifier, version] + end + + # @param coords [Array] normalized 'group:artifact' exclusion coordinates + def build_exclusions(coords) + return nil if coords.nil? || coords.empty? + + GemspecArtifacts::Exclusions.new("[#{coords.join(', ')}]") + end + + # Collects scope / classifier / exclusions declared in a jar/pom block, seeded by inline options. + # scope / classifier / exclusions double as DSL setters (with a value) and readers (no value). + class DepConfig + def initialize(options) + @scope = options[:scope] + @classifier = options[:classifier] + @exclusions = Jarfile.normalize_exclusions(options[:exclusions]) + end + + # dual getter (no arg, read after eval) / setter (DSL call with a value) + def scope(value = nil) + value.nil? ? @scope : (@scope = value) + end + + def classifier(value = nil) + value.nil? ? @classifier : (@classifier = value) + end + + def exclusion(*args, &block) + @exclusions << ExclusionBuilder.coordinate(*args, &block) + end + + def exclusions(*list, &block) + list.each { |e| @exclusions << ExclusionBuilder.coordinate(e) } + instance_eval(&block) if block + @exclusions + end + end + + # Resolves one exclusion declaration (any DSL form) to a 'group:artifact' coordinate. + module ExclusionBuilder + def self.coordinate(*args, &block) + return from_block(args, &block) if block + + case args.first + when Hash then "#{args.first[:group_id]}:#{args.first[:artifact_id]}" + when Array then args.first.join(':') + else args.join(':') # 'group:artifact', or 'group', 'artifact' + end + end + + def self.from_block(args, &block) + receiver = Receiver.new + receiver.group_id(args.first) if args.first.is_a?(String) + receiver.instance_eval(&block) + "#{receiver.group_id}:#{receiver.artifact_id}" + end + + # Receiver for the `exclusion do group_id ..; artifact_id .. end` form + class Receiver + def group_id(value = nil) + value.nil? ? @group_id : (@group_id = value) + end + + def artifact_id(value = nil) + value.nil? ? @artifact_id : (@artifact_id = value) + end + end + end + end +end diff --git a/lib/jars/lock_down.rb b/lib/jars/lock_down.rb index 328e54a..9391568 100644 --- a/lib/jars/lock_down.rb +++ b/lib/jars/lock_down.rb @@ -3,18 +3,28 @@ require 'jar_dependencies' require 'jars/version' require 'jars/gemspec_artifacts' +require 'jars/jarfile' module Jars class LockDown + attr_reader :repositories, :local_jars + + def initialize + @repositories = [] + @local_jars = [] + end + def basedir File.expand_path('.') end + private + + # @return [Array[Jars::GemspecArtifacts::Artifact]] def collect_artifacts - artifacts = [] - done = [] + collected = {} # artifact.key => artifact - attach_jar_coordinates_from_bundler_dependencies(artifacts, done) + attach_jar_coordinates_from_bundler_dependencies(collected) # Also collect from local gemspec if present specs = Dir.children(basedir).sort.select { |f| f.end_with?('.gemspec') } @@ -23,20 +33,32 @@ def collect_artifacts spec = Gem::Specification.load(spec_file) raise "failed to load gemspec #{spec_file}" unless spec - ga = GemspecArtifacts.new(spec) - ga.artifacts.each do |a| - unless done.include?(a.key) - artifacts << a - done << a.key - end - end + GemspecArtifacts.new(spec).artifacts.each { |a| collected[a.key] ||= a } end - artifacts + append_artifacts_from_jarfile_if_any(collected) + + collected.values end - private :collect_artifacts - def attach_jar_coordinates_from_bundler_dependencies(artifacts, done) + # jar/pom coordinates, repositories and local jars from a Jarfile (if any) + def append_artifacts_from_jarfile_if_any(collected) + file_path = File.expand_path(Jars.jarfile, basedir) + return unless File.exist?(file_path) + + Jars.debug { "Jarfile loading from #{file_path}" } + jarfile = Jars::Jarfile.load(file_path) + @repositories.concat(jarfile.repositories) + @local_jars.concat(jarfile.local_jars) + + jarfile.artifacts.each do |a| + previous = collected[a.key] + Jars.info { "Jarfile overrides #{previous.to_gacv} with #{a.to_gacv}" } if previous + collected[a.key] = a # Jarfile entry overrides the same coordinate from a gemspec + end + end + + def attach_jar_coordinates_from_bundler_dependencies(collected) load_path = $LOAD_PATH.dup require 'bundler' # TODO: make this group a commandline option @@ -44,13 +66,11 @@ def attach_jar_coordinates_from_bundler_dependencies(artifacts, done) cwd = File.expand_path('.') Gem.loaded_specs.each_value do |spec| all = cwd == spec.full_gem_path # if gemspec is local then include all dependencies - ga = GemspecArtifacts.new(spec) - ga.artifacts.each do |a| - next if done.include?(a.key) + GemspecArtifacts.new(spec).artifacts.each do |a| + next if collected.key?(a.key) next unless all || (a.scope != 'provided' && a.scope != 'test') - artifacts << a - done << a.key + collected[a.key] = a end end rescue LoadError => e @@ -64,6 +84,8 @@ def attach_jar_coordinates_from_bundler_dependencies(artifacts, done) $LOAD_PATH.replace(load_path) end + public + def lock_down(vendor_dir = nil, force: false, update: false, tree: nil) # rubocop:disable Lint/UnusedMethodArgument lock_file = File.expand_path(Jars.lock) @@ -74,7 +96,7 @@ def lock_down(vendor_dir = nil, force: false, update: false, tree: nil) # ruboco artifacts = collect_artifacts - if artifacts.empty? + if artifacts.empty? && @local_jars.empty? puts 'no jar dependencies found' return end @@ -87,11 +109,14 @@ def lock_down(vendor_dir = nil, force: false, update: false, tree: nil) # ruboco puts " exclusions: #{a.exclusions}" if a.exclusions && !a.exclusions.empty? end - context = Jars::Mima.create_context - begin - resolved = Jars::Mima.resolve_with_context(context, artifacts, all_dependencies: true) - ensure - context.close + resolved = [] + unless artifacts.empty? + context = Jars::Mima.create_context(repositories: @repositories) + begin + resolved = Jars::Mima.resolve_with_context(context, artifacts, all_dependencies: true) + ensure + context.close + end end # Write Jars.lock @@ -101,6 +126,7 @@ def lock_down(vendor_dir = nil, force: false, update: false, tree: nil) # ruboco f.puts dep.to_lock_entry end + @local_jars.each { |local| f.puts local.to_lock_entry } end # Optionally vendor jars diff --git a/lib/jars/mima.rb b/lib/jars/mima.rb index 5cab010..f941f7e 100644 --- a/lib/jars/mima.rb +++ b/lib/jars/mima.rb @@ -34,7 +34,7 @@ def ensure_jars_loaded # (local repo, user/global settings.xml). # # @return [Java::eu.maveniverse.maven.mima.context.ContextOverrides] - def context_overrides + def context_overrides(repositories: []) ensure_jars_loaded builder = Java::eu.maveniverse.maven.mima.context.ContextOverrides.create @@ -52,20 +52,43 @@ def context_overrides global = ::Jars::MavenSettings.global_settings builder.withGlobalSettingsXmlOverride(java.nio.file.Paths.get(global)) if global + # extra repositories (e.g. declared in a Jarfile), appended to Central + settings mirrors + unless repositories.empty? + add_op = Java::eu.maveniverse.maven.mima.context.ContextOverrides::AddRepositoriesOp::APPEND + builder.repositories(remote_repositories(repositories)) + builder.addRepositoriesOp(add_op) + end + builder.repositoryListener(aether_repository_listener) builder.transferListener(aether_transfer_listener) builder.build end + # Converts repository declarations (responding to +name+, +url+, +snapshot+) to Aether + # +RemoteRepository+ objects. A snapshot repository enables snapshots and disables releases; + # a regular one does the opposite. + def remote_repositories(repositories) + ensure_jars_loaded + + enabled = repository_policy(true) + disabled = repository_policy(false) + repositories.map do |repo| + builder = org.eclipse.aether.repository.RemoteRepository::Builder.new(repo.name, 'default', repo.url) + builder.setReleasePolicy(repo.snapshot ? disabled : enabled) + builder.setSnapshotPolicy(repo.snapshot ? enabled : disabled) + builder.build + end + end + # Creates a Mima Context. Caller is responsible for closing it. # # @param overrides [Java::eu.maveniverse.maven.mima.context.ContextOverrides, nil] # optional overrides; when +nil+, {#context_overrides} is used # @return [Java::eu.maveniverse.maven.mima.context.Context] - def create_context(overrides = nil) + def create_context(overrides = nil, repositories: []) ensure_jars_loaded - overrides ||= context_overrides + overrides ||= context_overrides(repositories: repositories) runtime = Java::eu.maveniverse.maven.mima.context.Runtimes::INSTANCE.getRuntime runtime.create(overrides) end @@ -110,6 +133,12 @@ def resolve_with_context(context, artifacts, all_dependencies: false) private + def repository_policy(enabled) + update = org.eclipse.aether.repository.RepositoryPolicy::UPDATE_POLICY_DAILY + checksum = org.eclipse.aether.repository.RepositoryPolicy::CHECKSUM_POLICY_WARN + org.eclipse.aether.repository.RepositoryPolicy.new(enabled, update, checksum) + end + def configure_logging return unless Object.const_defined?(:ENV_JAVA) diff --git a/specs/jarfile_spec.rb b/specs/jarfile_spec.rb new file mode 100644 index 0000000..eea96ce --- /dev/null +++ b/specs/jarfile_spec.rb @@ -0,0 +1,188 @@ +# frozen_string_literal: true + +require File.expand_path('setup', File.dirname(__FILE__)) + +require 'tmpdir' +require 'jars/jarfile' + +describe Jars::Jarfile do + def jarfile(content) + file = File.expand_path("Jarfile.#{rand(1_000_000)}", Dir.tmpdir) + File.write(file, content) + Jars::Jarfile.load(file) + ensure + File.delete(file) if file && File.exist?(file) + end + + it 'reads jar and pom coordinates' do + jf = jarfile(<<~JARFILE) + jar 'org.yaml:snakeyaml', '1.14' + pom 'org.apache.camel:camel-core', '2.9.2' + JARFILE + _(jf.artifacts.map(&:to_gacv)).must_equal ['org.yaml:snakeyaml:1.14', + 'org.apache.camel:camel-core:2.9.2'] + _(jf.artifacts.map(&:type)).must_equal %w[jar pom] + end + + it 'reads a single-string coordinate (group:artifact:version)' do + jf = jarfile("jar 'org.yaml:snakeyaml:1.14'\n") + a = jf.artifacts.first + _(a.group_id).must_equal 'org.yaml' + _(a.artifact_id).must_equal 'snakeyaml' + _(a.version).must_equal '1.14' + _(a.classifier).must_be_nil + end + + it 'reads a single-string coordinate with classifier (group:artifact:version:classifier)' do + jf = jarfile("jar 'org.foo:bar:1.0:sources'\n") + a = jf.artifacts.first + _(a.to_gacv).must_equal 'org.foo:bar:sources:1.0' + _(a.classifier).must_equal 'sources' + end + + it 'reads a classifier in the two-arg form (group:artifact:classifier, version)' do + jf = jarfile("jar 'org.foo:bar:jdk15', '1.0'\n") + a = jf.artifacts.first + _(a.classifier).must_equal 'jdk15' + _(a.version).must_equal '1.0' + end + + it 'reads the three-arg form (group, artifact, version)' do + jf = jarfile("jar 'org.foo', 'bar', '1.0'\n") + a = jf.artifacts.first + _(a.group_id).must_equal 'org.foo' + _(a.artifact_id).must_equal 'bar' + _(a.version).must_equal '1.0' + end + + it 'applies the :scope option' do + jf = jarfile("jar 'org.foo:bar', '1.0', :scope => 'test'\n") + _(jf.artifacts.first.scope).must_equal 'test' + end + + it 'applies the :classifier option' do + jf = jarfile("jar 'org.foo:bar', '1.0', :classifier => 'jdk15'\n") + _(jf.artifacts.first.classifier).must_equal 'jdk15' + end + + it 'applies inline :exclusions (strings and arrays)' do + jf = jarfile("jar 'org.foo:bar', '1.0', :exclusions => ['x:y', ['p', 'q']]\n") + ex = jf.artifacts.first.exclusions + _(ex.map(&:to_s)).must_equal ['x:y', 'p:q'] + end + + it 'applies an enclosing scope block only to its declarations' do + jf = jarfile(<<~JARFILE) + scope :test do + jar 'org.foo:bar', '1.0' + end + jar 'org.foo:baz', '2.0' + JARFILE + _(jf.artifacts.map(&:scope)).must_equal ['test', nil] + end + + it 'lets an inline :scope override the enclosing scope block' do + jf = jarfile(<<~JARFILE) + scope :test do + jar 'org.foo:bar', '1.0', :scope => 'provided' + end + JARFILE + _(jf.artifacts.first.scope).must_equal 'provided' + end + + it 'reads scope and classifier from a jar block' do + jf = jarfile(<<~JARFILE) + jar 'org.foo:bar', '1.0' do + scope 'test' + classifier 'sources' + end + JARFILE + a = jf.artifacts.first + _(a.scope).must_equal 'test' + _(a.classifier).must_equal 'sources' + end + + it 'reads exclusions declared inside a jar block' do + jf = jarfile(<<~JARFILE) + jar 'org.foo:bar', '1.0' do + exclusion 'group:a1' + exclusion 'group', 'a2' + end + JARFILE + _(jf.artifacts.first.exclusions.map(&:to_s)).must_equal ['group:a1', 'group:a2'] + end + + it 'combines inline :exclusions with a block exclusion' do + jf = jarfile(<<~JARFILE) + jar 'dsa:dsa', '12', :exclusions => ['group:b1', ['group', 'b2']] do + exclusion 'group:b3' + end + JARFILE + _(jf.artifacts.first.exclusions.map(&:to_s)).must_equal ['group:b1', 'group:b2', 'group:b3'] + end + + it 'collects repositories, source alias and snapshot repositories' do + jf = jarfile(<<~JARFILE) + source "http://1.2.3.4/artifactory/" + repository 'takari', "http://takari.io/repo" + snapshot_repository 'tesla', "http://tesla.io/snapshots" + + jar 'junit:junit', '4.11' + JARFILE + _(jf.repositories.map { |r| [r.name, r.url, r.snapshot] }).must_equal [ + ['http://1.2.3.4/artifactory/', 'http://1.2.3.4/artifactory/', false], + ['takari', 'http://takari.io/repo', false], + ['tesla', 'http://tesla.io/snapshots', true] + ] + _(jf.artifacts.map(&:to_gacv)).must_equal ['junit:junit:4.11'] + end + + it 'records a local jar as a system-scoped entry with derived coordinates' do + jf = jarfile("local 'vendor/foo-1.2.jar'\n") + lj = jf.local_jars.first + _(lj.group_id).must_equal 'ruby.maven-tools.jar' + _(lj.artifact_id).must_equal 'foo' + _(lj.version).must_equal '1.2' + _(lj.path).must_match(%r{/vendor/foo-1\.2\.jar$}) + end + + it 'emits a system-scoped lock entry for a local jar' do + jf = jarfile("local 'vendor/foo-1.2.jar'\n") + entry = jf.local_jars.first.to_lock_entry + _(entry).must_match(%r{^ruby\.maven-tools\.jar:foo:1\.2:system:.*/vendor/foo-1\.2\.jar$}) + end + + it 'treats jruby as a no-op (deprecated) and still reads other declarations' do + jf = jarfile(<<~JARFILE) + jruby '9.4.0.0' + jar 'org.foo:bar', '1.0' + JARFILE + _(jf.artifacts.map(&:to_gacv)).must_equal ['org.foo:bar:1.0'] + _(jf.local_jars).must_equal [] + end + + it 'supports the full nested exclusions DSL (maven-tools parity)' do + jf = jarfile(<<~JARFILE) + jar 'asd:asd:123' do + scope 'test' + classifier 'source' + exclusions 'group:a1', ['group', 'a2'] do + exclusion 'group', 'a3' + exclusion :artifact_id => 'a4', :group_id => 'group' + exclusion 'group:a5' + exclusion 'group' do + artifact_id 'a6' + end + exclusion do + group_id 'group' + artifact_id 'a7' + end + end + end + JARFILE + a = jf.artifacts.first + _(a.scope).must_equal 'test' + _(a.classifier).must_equal 'source' + _(a.exclusions.map(&:to_s)).must_equal %w[group:a1 group:a2 group:a3 group:a4 group:a5 group:a6 group:a7] + end +end diff --git a/specs/jars_spec.rb b/specs/jars_spec.rb index c2cfb91..e81eb7a 100644 --- a/specs/jars_spec.rb +++ b/specs/jars_spec.rb @@ -365,6 +365,31 @@ def fake.lock_down(...) _(Jars.lock).must_equal 'Jars_no_jline.lock' end + it 'require_jars_lock reports false when there is no lock to require' do + ENV['JARS_LOCK'] = File.join(Dir.pwd, "no-such-#{rand(1_000_000)}.lock") + Jars.reset + + _(Jars.require_jars_lock).must_equal false + _(Jars.require_jars_lock).must_equal false + ensure + ENV['JARS_LOCK'] = nil + end + + it 'require_jars_lock reports true the once it requires the lock' do + lock_file = File.join(Dir.pwd, "require-lock-#{rand(1_000_000)}.lock") + File.write(lock_file, "org.slf4j:slf4j-simple:1.6.6:compile:\n") + ENV['JARS_HOME'] = File.join('specs', 'repo') + ENV['JARS_LOCK'] = lock_file + Jars.reset + + _(Jars.require_jars_lock).must_equal true # required now + _(Jars.require_jars_lock).must_equal false # already required + ensure + FileUtils.rm_rf(lock_file) if lock_file + ENV['JARS_HOME'] = nil + ENV['JARS_LOCK'] = nil + end + it 'only suppresses version conflict warnings for jars in the lockfile' do lock_file = File.join(Dir.pwd, 'Jars_test_46.lock') File.write(lock_file, "org.slf4j:slf4j-simple:1.6.6:compile:\n") diff --git a/specs/lock_down_spec.rb b/specs/lock_down_spec.rb new file mode 100644 index 0000000..2861dfe --- /dev/null +++ b/specs/lock_down_spec.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require File.expand_path('setup', File.dirname(__FILE__)) + +require 'tmpdir' +require 'jars/lock_down' + +describe Jars::LockDown do + def with_jarfile(content) + file = File.expand_path("Jarfile.#{rand(1_000_000)}", Dir.tmpdir) + File.write(file, content) + ENV['JARFILE'] = file + yield Jars::LockDown.new + ensure + ENV.delete('JARFILE') + File.delete(file) if file && File.exist?(file) + end + + # regression: issue #116 - the Jarfile was not consulted at all after 0.5.7 + it 'collects jar coordinates declared in a Jarfile' do + with_jarfile("jar 'org.yaml:snakeyaml', '1.14'\n") do |lockdown| + collected = {} + lockdown.send(:append_artifacts_from_jarfile_if_any, collected) + _(collected.values.map(&:to_gacv)).must_equal ['org.yaml:snakeyaml:1.14'] + end + end + + it 'collects repositories and local jars declared in a Jarfile' do + content = <<~JARFILE + repository 'acme', 'https://acme.example/repo' + local 'vendor/foo-1.2.jar' + jar 'org.yaml:snakeyaml', '1.14' + JARFILE + with_jarfile(content) do |lockdown| + lockdown.send(:append_artifacts_from_jarfile_if_any, {}) + _(lockdown.repositories.map(&:name)).must_equal ['acme'] + _(lockdown.local_jars.map(&:artifact_id)).must_equal ['foo'] + end + end + + # a Jarfile entry is authoritative: it overrides the same coordinate from a gemspec + it 'overrides a coordinate already collected from a gemspec' do + with_jarfile("jar 'org.yaml:snakeyaml', '2.0'\n") do |lockdown| + from_gemspec = Jars::GemspecArtifacts::Artifact.parse('jar org.yaml:snakeyaml, 1.0') + collected = { from_gemspec.key => from_gemspec } + lockdown.send(:append_artifacts_from_jarfile_if_any, collected) + _(collected.values.map(&:to_gacv)).must_equal ['org.yaml:snakeyaml:2.0'] + end + end + + it 'is a no-op when no Jarfile is present' do + ENV['JARFILE'] = File.expand_path("does-not-exist-#{rand(1_000_000)}", Dir.tmpdir) + lockdown = Jars::LockDown.new + collected = {} + lockdown.send(:append_artifacts_from_jarfile_if_any, collected) + _(collected).must_equal({}) + _(lockdown.repositories).must_equal [] + ensure + ENV.delete('JARFILE') + end +end diff --git a/specs/mima_spec.rb b/specs/mima_spec.rb index f875e6d..ff7e8e7 100644 --- a/specs/mima_spec.rb +++ b/specs/mima_spec.rb @@ -3,6 +3,7 @@ require File.expand_path('setup', File.dirname(__FILE__)) require 'jars/mima' +require 'jars/jarfile' require 'stringio' describe Jars::Mima do @@ -39,4 +40,23 @@ ensure $stderr = STDERR end + + it 'builds aether remote repositories from Jarfile repository declarations' do + repos = [ + Jars::Jarfile::Repository.new('acme', 'https://acme.example/repo', false), + Jars::Jarfile::Repository.new('acme-snap', 'https://acme.example/snapshots', true) + ] + remotes = Jars::Mima.remote_repositories(repos) + + _(remotes.map(&:getId)).must_equal %w[acme acme-snap] + _(remotes.map(&:getUrl)).must_equal ['https://acme.example/repo', 'https://acme.example/snapshots'] + + release = remotes[0] + _(release.getPolicy(false).isEnabled).must_equal true # releases enabled + _(release.getPolicy(true).isEnabled).must_equal false # snapshots disabled + + snapshot = remotes[1] + _(snapshot.getPolicy(true).isEnabled).must_equal true # snapshots enabled + _(snapshot.getPolicy(false).isEnabled).must_equal false # releases disabled + end end