class LdpTestsuiteWrapper::Instance

Attributes

options[R]
pid[R]

Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options @option options [String] :url @option options [String] :download_dir Local directory to store the downloaded test suite zip and its md5 file in (overridden by :download_path) @option options [String] :download_path Local path for storing the downloaded test suite zip file @option options [Boolean] :verbose return verbose info when running commands @option options [Hash] :env

# File lib/ldp_testsuite_wrapper/instance.rb, line 23
def initialize(options = {})
  @options = options
end

Public Instance Methods

clean!() click to toggle source

Clean up any files ldp_testsuite_wrapper may have downloaded

# File lib/ldp_testsuite_wrapper/instance.rb, line 96
def clean!
  remove_instance_dir!
  FileUtils.remove_entry(download_path) if File.exist?(download_path)
  FileUtils.remove_entry(tmp_save_dir, true) if File.exist? tmp_save_dir
  FileUtils.remove_entry(md5sum_path) if File.exist? md5sum_path
  FileUtils.remove_entry(version_file) if File.exist? version_file
end
configure() click to toggle source
# File lib/ldp_testsuite_wrapper/instance.rb, line 104
def configure
  return if File.exist? ldp_testsuite_binary

  Dir.chdir(instance_dir) do
    `mvn package`
  end
end
exec(options = {}) click to toggle source

Run the LDP test suite @param [Hash] options key-value pairs to transform into command line arguments @return [StringIO] an IO object for the executed shell command If you want to pass a boolean flag, include it in the options hash with its value set to true the key will be converted into a boolean flag for you.

# File lib/ldp_testsuite_wrapper/instance.rb, line 33
def exec(options = {})
  extract_and_configure

  silence_output = !options.delete(:output)

  args = if options.is_a? Array
           options
         else
           ldp_testsuite_options.merge(options).map do |k, v|
             case v
             when true
               "-#{k}"
             when false, nil
               # don't return anything
             else
               ["-#{k}", v.to_s]
             end
           end.flatten.compact
  end

  args = ['java', '-jar', ldp_testsuite_binary] + args

  if IO.respond_to? :popen4
    # JRuby
    env_str = env.map { |k, v| "#{Shellwords.escape(k)}=#{Shellwords.escape(v)}" }.join(' ')
    pid, input, output, error = IO.popen4(env_str + ' ' + args.join(' '))
    @pid = pid
    stringio = StringIO.new
    if verbose? && !silence_output
      IO.copy_stream(output, $stderr)
      IO.copy_stream(error, $stderr)
    else
      IO.copy_stream(output, stringio)
      IO.copy_stream(error, stringio)
    end

    input.close
    output.close
    error.close
    exit_status = Process.waitpid2(@pid).last
  else
    IO.popen(env, args + [err: [:child, :out]]) do |io|
      stringio = StringIO.new

      if verbose? && !silence_output
        IO.copy_stream(io, $stderr)
      else
        IO.copy_stream(io, stringio)
      end

      @pid = io.pid

      _, exit_status = Process.wait2(io.pid)
    end
  end

  stringio.rewind

  [exit_status, stringio]
end
extract() click to toggle source

extract a copy of test suite to instance_dir Does noting if test suite already exists at instance_dir @return [String] instance_dir Directory where test suite has been installed

# File lib/ldp_testsuite_wrapper/instance.rb, line 127
def extract
  return instance_dir if extracted?

  zip_path = download

  begin
    Zip::File.open(zip_path) do |zip_file|
      # Handle entries one by one
      zip_file.each do |entry|
        dest_file = File.join(tmp_save_dir, entry.name)
        FileUtils.remove_entry(dest_file, true)
        entry.extract(dest_file)
      end
    end

  rescue Exception => e
    abort "Unable to unzip #{zip_path} into #{tmp_save_dir}: #{e.message}"
  end

  begin
    FileUtils.remove_dir(instance_dir, true)
    FileUtils.cp_r File.join(tmp_save_dir, zip_root_directory), instance_dir
  rescue Exception => e
    abort "Unable to copy #{tmp_save_dir} to #{instance_dir}: #{e.message}"
  end

  instance_dir
ensure
  FileUtils.remove_entry tmp_save_dir if File.exist? tmp_save_dir
end
extract_and_configure() click to toggle source
# File lib/ldp_testsuite_wrapper/instance.rb, line 116
def extract_and_configure
  instance_dir = extract
  configure
  instance_dir
end
instance_dir() click to toggle source
# File lib/ldp_testsuite_wrapper/instance.rb, line 112
def instance_dir
  @instance_dir ||= options.fetch(:instance_dir, File.join(Dir.tmpdir, File.basename(download_url, '.zip')))
end
version() click to toggle source

rubocop:enable Lint/RescueException

# File lib/ldp_testsuite_wrapper/instance.rb, line 159
def version
  options.fetch(:version)
end

Protected Instance Methods

download() click to toggle source
# File lib/ldp_testsuite_wrapper/instance.rb, line 169
def download
  unless File.exist?(download_path)
    fetch_with_progressbar download_url, download_path
  end
  download_path
end
extracted?() click to toggle source
# File lib/ldp_testsuite_wrapper/instance.rb, line 165
def extracted?
  File.exist?(ldp_testsuite_binary)
end
ldp_testsuite_options() click to toggle source
# File lib/ldp_testsuite_wrapper/instance.rb, line 176
def ldp_testsuite_options
  {}
end

Private Instance Methods

default_download_path() click to toggle source
# File lib/ldp_testsuite_wrapper/instance.rb, line 198
def default_download_path
  File.join(download_dir, File.basename(download_url))
end
default_download_url() click to toggle source
# File lib/ldp_testsuite_wrapper/instance.rb, line 186
def default_download_url
  "https://github.com/w3c/ldp-testsuite/archive/#{version}.zip"
end
download_dir() click to toggle source
# File lib/ldp_testsuite_wrapper/instance.rb, line 202
def download_dir
  @download_dir ||= options.fetch(:download_dir, Dir.tmpdir)
  FileUtils.mkdir_p @download_dir
  @download_dir
end
download_path() click to toggle source
# File lib/ldp_testsuite_wrapper/instance.rb, line 194
def download_path
  @download_path ||= options.fetch(:download_path, default_download_path)
end
download_url() click to toggle source
# File lib/ldp_testsuite_wrapper/instance.rb, line 182
def download_url
  @download_url ||= options.fetch(:url, default_download_url)
end
env() click to toggle source
# File lib/ldp_testsuite_wrapper/instance.rb, line 190
def env
  options.fetch(:env, {})
end
fetch_with_progressbar(url, output) click to toggle source
# File lib/ldp_testsuite_wrapper/instance.rb, line 224
def fetch_with_progressbar(url, output)
  pbar = ProgressBar.create(title: File.basename(url), total: nil, format: '%t: |%B| %p%% (%e )')
  open(url, content_length_proc: lambda do |t|
    pbar.total = t if t && 0 < t
  end,
            progress_proc: lambda do |s|
              pbar.progress = s
            end) do |io|
    IO.copy_stream(io, output)
  end
end
ldp_testsuite_binary() click to toggle source
# File lib/ldp_testsuite_wrapper/instance.rb, line 212
def ldp_testsuite_binary
  File.join(instance_dir, 'target', "ldp-testsuite-#{version}-shaded.jar")
end
tmp_save_dir() click to toggle source
# File lib/ldp_testsuite_wrapper/instance.rb, line 216
def tmp_save_dir
  @tmp_save_dir ||= Dir.mktmpdir
end
verbose?() click to toggle source
# File lib/ldp_testsuite_wrapper/instance.rb, line 208
def verbose?
  !!options.fetch(:verbose, false)
end
zip_root_directory() click to toggle source
# File lib/ldp_testsuite_wrapper/instance.rb, line 220
def zip_root_directory
  options.fetch(:zip_root_directory, "ldp-testsuite-#{version}")
end