Class: TestIds::Allocator
- Inherits:
-
Object
- Object
- TestIds::Allocator
- Includes:
- Origen::Callbacks
- Defined in:
- lib/test_ids/allocator.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#allocate(instance, options) ⇒ Object
Main method to inject generated bin and test numbers, the given options instance is modified accordingly.
-
#file ⇒ Object
Returns a path to the file that will be used to store the allocated bins/numbers.
- #git ⇒ Object
-
#initialize ⇒ Allocator
constructor
A new instance of Allocator.
- #on_origen_shutdown ⇒ Object
- #prepare ⇒ Object
-
#save ⇒ Object
Saves the current allocator state to the repository.
- #store ⇒ Object
Constructor Details
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
4 5 6 |
# File 'lib/test_ids/allocator.rb', line 4 def config @config end |
Instance Method Details
#allocate(instance, options) ⇒ Object
Main method to inject generated bin and test numbers, the given options instance is modified accordingly
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 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 |
# File 'lib/test_ids/allocator.rb', line 16 def allocate(instance, ) @changes_made = true clean() @callbacks = [] name = extract_test_name(instance, ) name = "#{name}_#{[:index]}" if [:index] store['tests'][name] ||= {} t = store['tests'][name] # If the user has supplied any of these, that number should be used # and reserved so that it is not automatically generated later if [:bin] t['bin'] = [:bin] store['manually_assigned']['bin'][[:bin].to_s] = true # Regenerate the bin if the original allocation has since been applied # manually elsewhere elsif store['manually_assigned']['bin'][t['bin'].to_s] t['bin'] = nil # Also regenerate these as they could be a function of the bin t['softbin'] = nil if config.softbins.function? t['number'] = nil if config.numbers.function? end if [:softbin] t['softbin'] = [:softbin] store['manually_assigned']['softbin'][[:softbin].to_s] = true elsif store['manually_assigned']['softbin'][t['softbin'].to_s] t['softbin'] = nil # Also regenerate the number as it could be a function of the softbin t['number'] = nil if config.numbers.function? end if [:number] t['number'] = [:number] store['manually_assigned']['number'][[:number].to_s] = true elsif store['manually_assigned']['number'][t['number'].to_s] t['number'] = nil end # Otherwise generate the missing ones t['bin'] ||= allocate_bin t['softbin'] ||= allocate_softbin(t['bin']) t['number'] ||= allocate_number(t['bin'], t['softbin']) # Record that there has been a reference to the final numbers time = Time.now.to_f store['references']['bin'][t['bin'].to_s] = time if t['bin'] store['references']['softbin'][t['softbin'].to_s] = time if t['softbin'] store['references']['number'][t['number'].to_s] = time if t['number'] # Update the supplied options hash that will be forwarded to the # program generator [:bin] = t['bin'] [:softbin] = t['softbin'] [:number] = t['number'] end |
#file ⇒ Object
Returns a path to the file that will be used to store the allocated bins/numbers. If config.repo has not been set it returns nil.
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/test_ids/allocator.rb', line 110 def file if config.repo @file ||= begin if git? dir = "#{Origen.app.imports_directory}/test_ids/#{Pathname.new(config.repo).basename}" FileUtils.mkdir_p(dir) "#{dir}/store.json" else config.repo end end end end |
#git ⇒ Object
124 125 126 |
# File 'lib/test_ids/allocator.rb', line 124 def git @git ||= Git.new(local: Pathname.new(file).dirname, remote: config.repo, no_pull: publish?) end |
#on_origen_shutdown ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/test_ids/allocator.rb', line 99 def on_origen_shutdown unless TestIds.send(:testing?) if config.repo && @changes_made && config.on_completion != :discard save git.publish if publish? end end end |
#prepare ⇒ Object
128 129 130 131 132 133 |
# File 'lib/test_ids/allocator.rb', line 128 def prepare if git? git # Pulls the latest repo git.get_lock if publish? end end |
#save ⇒ Object
Saves the current allocator state to the repository
91 92 93 94 95 96 97 |
# File 'lib/test_ids/allocator.rb', line 91 def save if file p = Pathname.new(file) FileUtils.mkdir_p(p.dirname) File.open(p, 'w') { |f| f.puts JSON.pretty_generate(store) } end end |
#store ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/test_ids/allocator.rb', line 72 def store @store ||= begin s = JSON.load(File.read(file)) if file && File.exist?(file) if s @last_bin = s['pointers']['bin'] @last_softbin = s['pointers']['softbin'] @last_number = s['pointers']['number'] s else { 'tests' => {}, 'manually_assigned' => { 'bin' => {}, 'softbin' => {}, 'number' => {} }, 'pointers' => { 'bin' => nil, 'softbin' => nil, 'number' => nil }, 'references' => { 'bin' => {}, 'softbin' => {}, 'number' => {} } } end end end |