Class: LdpTestsuiteWrapper::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/ldp_testsuite_wrapper/instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Instance

Returns a new instance of Instance.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :url (String)
  • :download_dir (String)

    Local directory to store the downloaded test suite zip and its md5 file in (overridden by :download_path)

  • :download_path (String)

    Local path for storing the downloaded test suite zip file

  • :verbose (Boolean)

    return verbose info when running commands

  • :env (Hash)


23
24
25
# File 'lib/ldp_testsuite_wrapper/instance.rb', line 23

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/ldp_testsuite_wrapper/instance.rb', line 14

def options
  @options
end

#pidObject (readonly)

Returns the value of attribute pid.



14
15
16
# File 'lib/ldp_testsuite_wrapper/instance.rb', line 14

def pid
  @pid
end

Instance Method Details

#clean!Object

Clean up any files ldp_testsuite_wrapper may have downloaded



96
97
98
99
100
101
102
# 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

#configureObject



104
105
106
107
108
109
110
# 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 = {}) ⇒ StringIO

Run the LDP test suite 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.

Parameters:

  • options (Hash) (defaults to: {})

    key-value pairs to transform into command line arguments

Returns:

  • (StringIO)

    an IO object for the executed shell command



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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

#extractString

extract a copy of test suite to instance_dir Does noting if test suite already exists at instance_dir

Returns:

  • (String)

    instance_dir Directory where test suite has been installed



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# 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_configureObject



116
117
118
119
120
# File 'lib/ldp_testsuite_wrapper/instance.rb', line 116

def extract_and_configure
  instance_dir = extract
  configure
  instance_dir
end

#instance_dirObject



112
113
114
# 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

#versionObject

rubocop:enable Lint/RescueException



159
160
161
# File 'lib/ldp_testsuite_wrapper/instance.rb', line 159

def version
  options.fetch(:version)
end