require 'shell_mock/monkey_patch'
require 'shell_mock/no_stub_specified'
require 'shell_mock/spawn_arguments'
require 'shell_mock/stub_registry'

module ShellMock
  class SpawnMonkeyPatch < MonkeyPatch
    def method_name
      :spawn
    end

    def override(*args)
      env, command, options = SpawnArguments(*args)

      stub = StubRegistry.stub_matching(env, command, options)

      if stub
        stub.ran

        stub.side_effect.call

        __un_shell_mocked_spawn(stub.to_oneliner, options)
      else
        if ShellMock.let_commands_run?
          __un_shell_mocked_spawn(env, command, options)
        else
          raise NoStubSpecified.new(env, command, options)
        end
      end
    end

    def enable
      enable_for(Process.singleton_class) unless enabled_for?(Process)

      super
    end

    def disable
      super

      disable_for(Process.singleton_class) if enabled_for?(Process)
    end
  end
end
