Class: Makit::Examples::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/examples/runner.rb

Overview

Centralized management of example discovery, execution, and verification

This class provides a clean interface for running all examples in the examples directory, with support for different execution strategies, artifact verification, and comprehensive reporting.

Examples:

Basic usage

runner = Makit::Examples::Runner.new
runner.run_all

Custom configuration

runner = Makit::Examples::Runner.new(
  strategy: :parallel,
  verify_artifacts: true,
  cleanup_after: false
)
runner.run_all

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Runner

Initialize the examples runner

Parameters:

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

    configuration options

Options Hash (options):

  • :examples_dir (String)

    directory containing examples (default: “examples”)

  • :strategy (Symbol)

    execution strategy (:sequential, :parallel, :filtered)

  • :verify_artifacts (Boolean)

    whether to verify expected artifacts (default: true)

  • :cleanup_after (Boolean)

    whether to clean up artifacts after testing (default: true)

  • :filter (Regexp)

    pattern to filter examples (default: nil)

  • :timeout (Integer)

    timeout per example in seconds (default: 30)

  • :verbose (Boolean)

    whether to show detailed output (default: false)



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/makit/examples/runner.rb', line 42

def initialize(options = {})
  @examples_dir = options[:examples_dir] || "examples"
  @execution_strategy = options[:strategy] || :sequential
  @verify_artifacts = options[:verify_artifacts] || true
  @cleanup_after = options[:cleanup_after] || true
  @filter = options[:filter]
  @timeout = options[:timeout] || 30
  @verbose = options[:verbose] || false

  @results = []
  @failed_examples = []
  @passed_examples = []
  @examples = []
end

Instance Attribute Details

#cleanup_afterObject (readonly)

Configuration options



27
28
29
# File 'lib/makit/examples/runner.rb', line 27

def cleanup_after
  @cleanup_after
end

#examplesObject (readonly)

Execution results



30
31
32
# File 'lib/makit/examples/runner.rb', line 30

def examples
  @examples
end

#examples_dirObject (readonly)

Configuration options



27
28
29
# File 'lib/makit/examples/runner.rb', line 27

def examples_dir
  @examples_dir
end

#execution_strategyObject (readonly)

Configuration options



27
28
29
# File 'lib/makit/examples/runner.rb', line 27

def execution_strategy
  @execution_strategy
end

#failed_examplesObject (readonly)

Execution results



30
31
32
# File 'lib/makit/examples/runner.rb', line 30

def failed_examples
  @failed_examples
end

#filterObject (readonly)

Configuration options



27
28
29
# File 'lib/makit/examples/runner.rb', line 27

def filter
  @filter
end

#passed_examplesObject (readonly)

Execution results



30
31
32
# File 'lib/makit/examples/runner.rb', line 30

def passed_examples
  @passed_examples
end

#resultsObject (readonly)

Execution results



30
31
32
# File 'lib/makit/examples/runner.rb', line 30

def results
  @results
end

#timeoutObject (readonly)

Configuration options



27
28
29
# File 'lib/makit/examples/runner.rb', line 27

def timeout
  @timeout
end

#verboseObject (readonly)

Configuration options



27
28
29
# File 'lib/makit/examples/runner.rb', line 27

def verbose
  @verbose
end

#verify_artifactsObject (readonly)

Configuration options



27
28
29
# File 'lib/makit/examples/runner.rb', line 27

def verify_artifacts
  @verify_artifacts
end

Instance Method Details

#discover_examplesObject

Discover all examples in the examples directory



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/makit/examples/runner.rb', line 83

def discover_examples
  @examples = Dir.glob("#{@examples_dir}/**/Rakefile").map do |rakefile|
    example_dir = File.dirname(rakefile)
    example_name = example_dir.gsub("#{@examples_dir}/", "")

    {
      path: example_dir,
      name: example_name,
      rakefile: rakefile,
      expected_artifacts: determine_expected_artifacts(example_name),
    }
  end

  # Apply filter if specified
  @examples = @examples.select { |ex| ex[:name].match?(@filter) } if @filter

  log "Discovered #{@examples.count} examples" if @verbose
end

#run_allBoolean

Run all discovered examples

Returns:

  • (Boolean)

    true if all examples passed, false otherwise



60
61
62
63
64
65
66
67
68
# File 'lib/makit/examples/runner.rb', line 60

def run_all
  discover_examples
  execute_examples
  verify_results
  report_results
  cleanup_if_needed

  @failed_examples.empty?
end

#run_example(example_path) ⇒ Hash

Run a specific example

Parameters:

  • example_path (String)

    path to the example directory

Returns:

  • (Hash)

    result hash with success status and details



74
75
76
77
78
79
80
# File 'lib/makit/examples/runner.rb', line 74

def run_example(example_path)
  discover_examples if @examples.empty?
  example = @examples.find { |ex| ex[:name] == example_path }
  return { success: false, error: "Example not found: #{example_path}" } unless example

  execute_single_example(example)
end