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 Method Summary collapse

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.



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

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



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

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



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

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



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

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

.bin_configObject



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

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

.bin_config=(id) ⇒ Object



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

def bin_config=(id)
  @bin_config = id
end

.config=(id) ⇒ Object

Switch the current configuration to the given ID



125
126
127
128
129
130
# File 'lib/test_ids.rb', line 125

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

.configsObject

Return an Array of configuration IDs



133
134
135
# File 'lib/test_ids.rb', line 133

def configs
  @configuration.ids
end

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



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

def configuration(id, fail_on_missing = true)
  return @configuration[id] if @configuration && @configuration[id]
  if fail_on_missing
    fail('You have to create the configuration first before you can access it')
  end
end

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

Yields:



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

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

  @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)


170
171
172
# File 'lib/test_ids.rb', line 170

def configured?
  !!@configuration_id
end

.current_configurationObject



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

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



186
187
188
189
190
191
192
193
194
195
# File 'lib/test_ids.rb', line 186

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

.gitObject



205
206
207
# File 'lib/test_ids.rb', line 205

def git
  @git
end

.git_database_dirObject



197
198
199
200
201
202
203
# File 'lib/test_ids.rb', line 197

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



174
175
176
177
178
179
180
181
182
# File 'lib/test_ids.rb', line 174

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.



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

def inject_flow_id(options)
  if Origen.interface_loaded?
    flow = Origen.interface.flow
    options[:test_ids_flow_id] = flow.try(:top_level).try(:id) || flow.id
  end
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.



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

def load_allocator(id = nil)
  f = TestIds.database_file(id)
  if File.exist?(f)
    a = Configuration.new(id).allocator
    a.load_configuration_from_store
    a
  end
end

.next_in_range(range, options) ⇒ Object



242
243
244
# File 'lib/test_ids.rb', line 242

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

.number_configObject



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

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

.number_config=(id) ⇒ Object



153
154
155
# File 'lib/test_ids.rb', line 153

def number_config=(id)
  @number_config = id
end

.publish=(val) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/test_ids.rb', line 228

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

.publish?Boolean

Returns:

  • (Boolean)


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

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

.repoObject



220
221
222
# File 'lib/test_ids.rb', line 220

def repo
  @repo
end

.repo=(val) ⇒ Object



209
210
211
212
213
214
215
216
217
218
# File 'lib/test_ids.rb', line 209

def repo=(val)
  return if @repo && @repo == val
  if @repo && @repo != val
    fail 'You can only use a single test ids repository per program generation run, one per application is recommended'
  end
  if @configuration
    fail 'TestIds.repo must be set before creating the first configuration'
  end
  @repo = val
end

.softbin_configObject



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

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

.softbin_config=(id) ⇒ Object



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

def softbin_config=(id)
  @softbin_config = id
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



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

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