Is there a way to configure if_enable to either always pass or remove the variable check conditionally?

49 views Asked by At

Ran into a case where depending on the job, I'd like to wrap portions of code in an if_enable block, but conditionally.

If we're running our characterization job, then we partition tests based off testcodes to reduce test time on our experiments. For any other job, these tests are all always ran for production testing.

The orignal code is in this format:

Flow.create() do |options|
  test_a
  test_b
end

My current solution to work around this is:

def a_tests
  if job == :charz
    if_enable '$enableA' do
      yield
    end
  else
   yield
  end
end
def b_tests
  if job == :charz
    if_enable '$enableB' do
      yield
    end
  else
   yield
  end
end
Flow.create() do |options|
  a_tests do
    test_a
  end
  b_tests do
    test_b
  end
end

What I'd like to do is something like this:

Flow.create() do |options|
  a_flag = job == :charz ? '$enableA' : ''
  b_flag = job == :charz ? '$enableB' : ''
  if_enable a_flag do
    test_a
  end
  if_enable b_flag do
    test_b
  end
end

Where passing an empty (or could be nil or false) value would result in the tests within the if_enable block still being generated but not gated by any variable checking. Does this functionality exist somewhere already or is my current solution currently the best way? Or is there some other cleaner ruby solution?

1

There are 1 answers

0
Ginty On BEST ANSWER

One purely flow-level solution using existing APIs would be to force the enable at runtime for non-cz jobs, like this:

Flow.create do |options|
  if job != :charz
    enable '$enableA'
    enable '$enableB'
  end

  if_enable '$enableA' do
    test_a
  end
  if_enable '$enableB' do
    test_b
  end
end

Your current solution is also pretty good, though I would enhance it by moving the method definition to your interface, and making it more generic:

# lib/my_app/my_interface.rb
def if_enable_when_charz(var)
  if job == :charz
    if_enable var do
      yield
    end
  else
    yield
  end
end

Then you have a nice helper to keep your flow logic very clean:

# program/my_flow.rb
Flow.create do |options|
  if_enable_when_charz '$enableA' do
    test_a
  end
  if_enable_when_charz '$enableB' do
    test_b
  end
end