Module: TestIds

Defined in:
lib/test_ids.rb,
lib/test_ids/git.rb,
lib/test_ids/allocator.rb,
lib/test_ids/bin_array.rb,
lib/test_ids/configuration.rb,
lib/test_ids/shutdown_handler.rb

Defined Under Namespace

Classes: Allocator, BinArray, Configuration, Git, ShutdownHandler

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.bin_configObject



142
143
144
# File 'lib/test_ids.rb', line 142

def bin_config
  @bin_config ? configuration(@bin_config, false) : current_configuration
end

.gitObject (readonly)

Returns the value of attribute git.



198
199
200
# File 'lib/test_ids.rb', line 198

def git
  @git
end

.number_configObject



150
151
152
# File 'lib/test_ids.rb', line 150

def number_config
  @number_config ? configuration(@number_config, false) : current_configuration
end

.repoObject

Returns the value of attribute repo.



198
199
200
# File 'lib/test_ids.rb', line 198

def repo
  @repo
end

.softbin_configObject



146
147
148
# File 'lib/test_ids.rb', line 146

def softbin_config
  @softbin_config ? configuration(@softbin_config, false) : current_configuration
end

Class Method Details

.allocate(instance, options = {}) ⇒ Object

Allocates a number to the given test and returns a new hash containing :bin, :softbin and :number keys.

The given options hash is not modified by calling this method.

Use the same arguments as you would normally pass to flow.test, the numbers returned will be the same as would be injected into flow.test.



27
28
29
30
31
32
33
# File 'lib/test_ids.rb', line 27

def allocate(instance, options = {})
  opts = options.dup
  inject_flow_id(opts)
  current_configuration.allocator.allocate(instance, opts)
  { bin: opts[:bin], bin_size: opts[:bin_size], softbin: opts[:softbin], softbin_size: opts[:softbin_size],
    number: opts[:number], number_size: opts[:number_size] }
end

.allocate_bin(instance, options = {}) ⇒ Object

Similar to allocate, but allocates a bin number only, i.e. no softbin or test number



61
62
63
64
65
66
67
68
69
70
# File 'lib/test_ids.rb', line 61

def allocate_bin(instance, options = {})
  opts = options.dup
  opts[:softbin] = :none
  opts[:number] = :none
  inject_flow_id(opts)
  current_configuration.allocator.allocate(instance, opts)
  {
    softbin: opts[:bin], softbin_size: opts[:bin_size]
  }
end

.allocate_number(instance, options = {}) ⇒ Object

Similar to allocate, but allocates a test number only, i.e. no bin or softbin



36
37
38
39
40
41
42
43
44
45
# File 'lib/test_ids.rb', line 36

def allocate_number(instance, options = {})
  opts = options.dup
  opts[:bin] = :none
  opts[:softbin] = :none
  inject_flow_id(opts)
  current_configuration.allocator.allocate(instance, opts)
  {
    number: opts[:number], number_size: opts[:number_size]
  }
end

.allocate_softbin(instance, options = {}) ⇒ Object Also known as: allocate_soft_bin

Similar to allocate, but allocates a softbin number only, i.e. no bin or test number



48
49
50
51
52
53
54
55
56
57
# File 'lib/test_ids.rb', line 48

def allocate_softbin(instance, options = {})
  opts = options.dup
  opts[:bin] = :none
  opts[:number] = :none
  inject_flow_id(opts)
  current_configuration.allocator.allocate(instance, opts)
  {
    softbin: opts[:softbin], softbin_size: opts[:softbin_size]
  }
end

.config=(id) ⇒ Object

Switch the current configuration to the given ID



129
130
131
132
133
# File 'lib/test_ids.rb', line 129

def config=(id)
  fail "The TestIds configuration '#{id}' has not been defined yet!" unless @configuration[id]

  @configuration_id = id
end

.configsObject

Return an Array of configuration IDs



136
137
138
# File 'lib/test_ids.rb', line 136

def configs
  @configuration.ids
end

.configuration(id, fail_on_missing = true) ⇒ Object Also known as: config



97
98
99
100
101
102
# File 'lib/test_ids.rb', line 97

def configuration(id, fail_on_missing = true)
  return @configuration[id] if @configuration && @configuration[id]
  return unless fail_on_missing

  fail('You have to create the configuration first before you can access it')
end

.configure(id = nil, options = {}) {|config| ... } ⇒ Object

Yields:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/test_ids.rb', line 105

def configure(id = nil, options = {})
  if id.is_a?(Hash)
    options = id
    id = nil
  end

  @configuration_id = id || options[:id] || :not_specified

  @configuration ||= {}

  return if @configuration[@configuration_id]

  @configuration[@configuration_id] = Configuration.new(@configuration_id)

  config = @configuration[@configuration_id]

  yield config

  config.validate!

  initialize_git
end

.configured?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/test_ids.rb', line 163

def configured?
  !!@configuration_id
end

.current_configurationObject



93
94
95
# File 'lib/test_ids.rb', line 93

def current_configuration
  configuration(@configuration_id)
end

.database_file(id) ⇒ Object

Returns a full path to the database file for the given id, returns nil if git storage has not been enabled



179
180
181
182
183
184
185
186
187
188
# File 'lib/test_ids.rb', line 179

def database_file(id)
  return unless repo

  f = if id == :not_specified || !id || id == ''
        'store.json'
      else
        "store_#{id.to_s.downcase}.json"
      end
  "#{git_database_dir}/#{f}"
end

.git_database_dirObject



190
191
192
193
194
195
196
# File 'lib/test_ids.rb', line 190

def git_database_dir
  @git_database_dir ||= begin
    d = "#{Origen.app.imports_directory}/test_ids/#{Pathname.new(repo).basename}"
    FileUtils.mkdir_p(d)
    d
  end
end

.initialize_gitObject



167
168
169
170
171
172
173
174
175
# File 'lib/test_ids.rb', line 167

def initialize_git
  @git_initialized ||= begin
    if repo
      @git = Git.new(local: git_database_dir, remote: repo)
      git.get_lock if publish?
    end
    true
  end
end

.inject_flow_id(options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



73
74
75
76
77
78
# File 'lib/test_ids.rb', line 73

def inject_flow_id(options)
  return unless Origen.interface_loaded?

  flow = Origen.interface.flow
  options[:test_ids_flow_id] = flow.try(:top_level).try(:id) || flow.id
end

.load_allocator(id = nil) ⇒ Object

Load an existing allocator, which will be loaded with a configuration based on what has been serialized into the database if present, otherwise it will have an empty configuration. Returns nil if the given database can not be found.



84
85
86
87
88
89
90
91
# File 'lib/test_ids.rb', line 84

def load_allocator(id = nil)
  f = TestIds.database_file(id)
  return unless File.exist?(f)

  a = Configuration.new(id).allocator
  a.load_configuration_from_store
  a
end

.lsf_init(git_repo, lsf_publish) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/test_ids.rb', line 232

def lsf_init(git_repo, lsf_publish)
  @lsf_manual_init_shutdown = true
  local_var_git_database_dir = "#{Origen.app.imports_directory}/test_ids/#{Pathname.new(git_repo).basename}"
  FileUtils.mkdir_p(local_var_git_database_dir)
  unless File.exist?("#{local_var_git_database_dir}/.git")
    FileUtils.rm_rf(local_var_git_database_dir) if File.exist?(local_var_git_database_dir)
    FileUtils.mkdir_p(local_var_git_database_dir)
    Dir.chdir local_var_git_database_dir do
      `git clone #{git_repo} .`
      unless File.exist?('lock.json')
        # Should really try to use the Git driver for this
        exec 'touch lock.json'
        exec 'git add lock.json'
        exec 'git commit -m "Initial commit"'
        exec 'git push'
      end
    end
  end
  @local = local_var_git_database_dir
  @repo = ::Git.open(local_var_git_database_dir)
  # Get rid of any local edits coming in here, this is only called once at the start
  # of the program generation run.
  # No need to pull latest as that will be done when we obtain a lock.
  @repo.reset_hard
  @git = Git.new(local: local_var_git_database_dir, remote: @repo)
  return unless lsf_publish
  return if @lock_open

  Origen.profile 'Obtaining test IDs lock' do
    until @git.available_to_lock?(@repo)
      puts
      puts "Waiting for lock, currently locked by #{@git.lock_user} (the lock will expire in less than #{@git.lock_minutes_remaining} #{'minute'.pluralize(@git.lock_minutes_remaining)} if not released before that)"
      puts
      sleep 5
    end
    data = {
      'user'    => User.current.name,
      'expires' => (Time.now + @git.minutes(5)).to_f
    }
    @git.write('lock.json', JSON.pretty_generate(data))
    repo.commit('Obtaining lock')
    repo.push('origin')
  end
  @lock_open = true
end

.lsf_manual_init_shutdownObject



223
224
225
226
# File 'lib/test_ids.rb', line 223

def lsf_manual_init_shutdown
  true if @lsf_manual_init_shutdown
  false
end

.lsf_shutdown(lsf_publish) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/test_ids.rb', line 278

def lsf_shutdown(lsf_publish)
  return unless lsf_publish

  Origen.profile 'Publishing the test IDs store' do
    data = {
      'user'    => nil,
      'expires' => nil
    }
    @git.write('lock.json', JSON.pretty_generate(data))
    repo.add # Checkin everything
    repo.commit('Publishing latest store')
    repo.push('origin', 'master', force: true)
  end
end

.next_in_range(range, options) ⇒ Object



228
229
230
# File 'lib/test_ids.rb', line 228

def next_in_range(range, options)
  current_configuration.allocator.next_in_range(range, options)
end

.publish=(val) ⇒ Object



214
215
216
217
218
219
220
221
# File 'lib/test_ids.rb', line 214

def publish=(val)
  return if @publish && publish? == val
  fail 'You can only use a single setting for publish per program generation run' if @publish && publish? != val
  fail 'TestIds.publish must be set before creating the first configuration' if @configuration
  fail 'TestIds.publish must be set to either true or false' unless [true, false].include?(val)

  @publish = val ? :save : :dont_save
end

.publish?Boolean

Returns:

  • (Boolean)


210
211
212
# File 'lib/test_ids.rb', line 210

def publish?
  @publish ? @publish == :save : true
end

.reset_everything(are_you_sure: false) ⇒ Object

Reset everything related to the TestIds module only needed for cases where running several targets back to back, e.g. for regression testing



310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/test_ids.rb', line 310

def reset_everything(are_you_sure: false)
  return unless are_you_sure

  @repo = nil # accessor
  @git = nil # accessor
  @git_database_dir = nil
  @git_initialized = nil
  @configuration = nil
  @configuration_id = nil
  @bin_config = nil
  @softbin_config = nil
  @number_config = nil
  @publish = nil
end

.with_config(id) ⇒ Object

Temporarily switches the current configuration to the given ID for the duration of the given block, then switches it back to what it was



156
157
158
159
160
161
# File 'lib/test_ids.rb', line 156

def with_config(id)
  orig = @configuration_id
  @configuration_id = id
  yield
  @configuration_id = orig
end