Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement optional logging #8

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/selective-ruby-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def self.runner_for(name)
class Init
def initialize(args)
@debug = !args.delete("--debug").nil?
@log = !args.delete("--log").nil?
@runner_name, @args, @command = parse_args(args)
require_runner
end
Expand All @@ -34,10 +35,10 @@ def self.run(args)

private

attr_reader :debug, :runner_name, :args, :command
attr_reader :debug, :log, :runner_name, :args, :command

def run
Selective::Ruby::Core::Controller.new(runner, debug).send(command)
Selective::Ruby::Core::Controller.new(runner, debug: debug, log: log).send(command)
end

def parse_args(args)
Expand Down
14 changes: 12 additions & 2 deletions lib/selective/ruby/core/controller.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
require "logger"
require "uri"
require "json"
require 'fileutils'

module Selective
module Ruby
module Core
class Controller
@@selective_suppress_reporting = false

def initialize(runner, debug = false)
def initialize(runner, debug: false, log: false)
@debug = debug
@runner = runner
@retries = 0
@runner_id = ENV.fetch("SELECTIVE_RUNNER_ID", generate_runner_id)
@logger = Logger.new("log/#{runner_id}.log")
@logger = init_logger(log)
end

def start(reconnect: false)
Expand Down Expand Up @@ -52,6 +53,15 @@ def self.suppress_reporting?

BUILD_ENV_SCRIPT_PATH = "../../../bin/build_env.sh".freeze

def init_logger(enabled)
if enabled
FileUtils.mkdir_p("log")
Logger.new("log/#{runner_id}.log")
else
Logger.new("/dev/null")
end
end

def run_main_loop
loop do
message = pipe.read
Expand Down
Empty file removed log/.keep
Empty file.
20 changes: 15 additions & 5 deletions spec/selective/ruby/core/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
let(:args) { %w[exec mock_runner] }

it "initializes runner and calls exec" do
expect(Selective::Ruby::Core::Controller).to have_received(:new).with(mock_runner_instance, false)
expect(Selective::Ruby::Core::Controller).to have_received(:new).with(mock_runner_instance, debug: false, log: false)
expect(mock_runner_class).to have_received(:new).with([])
expect(mock_controller).to have_received(:exec)
end
Expand All @@ -24,7 +24,7 @@
let(:args) { %w[exec mock_runner --dry-run] }

it "initializes runner with the --dry-run option and calls exec" do
expect(Selective::Ruby::Core::Controller).to have_received(:new).with(mock_runner_instance, false)
expect(Selective::Ruby::Core::Controller).to have_received(:new).with(mock_runner_instance, debug: false, log: false)
expect(mock_runner_class).to have_received(:new).with(["--dry-run"])
expect(mock_controller).to have_received(:exec)
end
Expand All @@ -34,7 +34,7 @@
let(:args) { %w[mock_runner] }

it "initializes runner and calls start" do
expect(Selective::Ruby::Core::Controller).to have_received(:new).with(mock_runner_instance, false)
expect(Selective::Ruby::Core::Controller).to have_received(:new).with(mock_runner_instance, debug: false, log: false)
expect(mock_runner_class).to have_received(:new).with([])
expect(mock_controller).to have_received(:start)
end
Expand All @@ -44,7 +44,17 @@
let(:args) { %w[mock_runner --debug] }

it "initializes runner with debug and calls start" do
expect(Selective::Ruby::Core::Controller).to have_received(:new).with(mock_runner_instance, true)
expect(Selective::Ruby::Core::Controller).to have_received(:new).with(mock_runner_instance, debug: true, log: false)
expect(mock_runner_class).to have_received(:new).with([])
expect(mock_controller).to have_received(:start)
end
end

context "with 'selective mock_runner --log'" do
let(:args) { %w[mock_runner --log] }

it "initializes runner with debug and calls start" do
expect(Selective::Ruby::Core::Controller).to have_received(:new).with(mock_runner_instance, debug: false, log: true)
expect(mock_runner_class).to have_received(:new).with([])
expect(mock_controller).to have_received(:start)
end
Expand All @@ -54,7 +64,7 @@
let(:args) { %w[mock_runner spec/foo/bar_spec.rb] }

it "initializes runner with file path and calls start" do
expect(Selective::Ruby::Core::Controller).to have_received(:new).with(mock_runner_instance, false)
expect(Selective::Ruby::Core::Controller).to have_received(:new).with(mock_runner_instance, debug: false, log: false)
expect(mock_runner_class).to have_received(:new).with(["spec/foo/bar_spec.rb"])
expect(mock_controller).to have_received(:start)
end
Expand Down
Loading