
import pytest

from cookiecutter.main import cookiecutter


class Cookies(object):
    """Class to provide convenient access to the cookiecutter API."""
    error = None
    project = None

    def __init__(self, template, output_dir):
        self._template = template
        self._output_dir = output_dir

    def bake(self, extra_context=None):
        try:
            project_dir = cookiecutter(
                self._template,
                no_input=True,
                extra_context=extra_context,
                output_dir=self._output_dir
            )
        except Exception as e:
            self.error = e
        else:
            self.project = project_dir


@pytest.fixture
def cookies(request, tmpdir):
    output_dir = request.config.option.output_dir

    if not output_dir:
        output_dir = str(tmpdir.mkdir('cookies_output'))

    _cookies = Cookies('.', output_dir)
    return _cookies


def pytest_addoption(parser):
    group = parser.getgroup('cookies')
    group.addoption(
        '--output-dir',
        action='store',
        dest='output_dir',
        help='Set the output directory for Cookiecutter'
    )

    parser.addini('HELLO', 'Dummy pytest.ini setting')


import pytest

from cookiecutter.main import cookiecutter


class Cookies(object):
    """Class to provide convenient access to the cookiecutter API."""
    exception = None
    exit_code = 0
    project = None

    def __init__(self, template, output_dir):
        self._template = template
        self._output_dir = output_dir

    def bake(self, extra_context=None):
        try:
            project_dir = cookiecutter(
                self._template,
                no_input=True,
                extra_context=extra_context,
                output_dir=self._output_dir
            )
        except SystemExit as e:
            if e.code != 0:
                self.exception = e
            self.exit_code = e.code
        except Exception as e:
            self.exception = e
            self.exit_code = -1
        else:
            self.project = project_dir


@pytest.fixture
def cookies(request, tmpdir):
    output_dir = request.config.option.output_dir

    if not output_dir:
        output_dir = str(tmpdir.mkdir('cookies_output'))

    _cookies = Cookies('.', output_dir)
    return _cookies


def pytest_addoption(parser):
    group = parser.getgroup('cookies')
    group.addoption(
        '--output-dir',
        action='store',
        dest='output_dir',
        help='Set the output directory for Cookiecutter'
    )

    parser.addini('HELLO', 'Dummy pytest.ini setting')

Handle SystemExit errors and add exit_code
Handle SystemExit errors and add exit_code

  
  import pytest
  
  from cookiecutter.main import cookiecutter
  
  
  class Cookies(object):
      """Class to provide convenient access to the cookiecutter API."""
-     error = None
+     exception = None
+     exit_code = 0
      project = None
  
      def __init__(self, template, output_dir):
          self._template = template
          self._output_dir = output_dir
  
      def bake(self, extra_context=None):
          try:
              project_dir = cookiecutter(
                  self._template,
                  no_input=True,
                  extra_context=extra_context,
                  output_dir=self._output_dir
              )
+         except SystemExit as e:
+             if e.code != 0:
+                 self.exception = e
+             self.exit_code = e.code
          except Exception as e:
-             self.error = e
+             self.exception = e
+             self.exit_code = -1
          else:
              self.project = project_dir
  
  
  @pytest.fixture
  def cookies(request, tmpdir):
      output_dir = request.config.option.output_dir
  
      if not output_dir:
          output_dir = str(tmpdir.mkdir('cookies_output'))
  
      _cookies = Cookies('.', output_dir)
      return _cookies
  
  
  def pytest_addoption(parser):
      group = parser.getgroup('cookies')
      group.addoption(
          '--output-dir',
          action='store',
          dest='output_dir',
          help='Set the output directory for Cookiecutter'
      )
  
      parser.addini('HELLO', 'Dummy pytest.ini setting')
  
Handle SystemExit errors and add exit_code
## Code Before:

import pytest

from cookiecutter.main import cookiecutter


class Cookies(object):
    """Class to provide convenient access to the cookiecutter API."""
    error = None
    project = None

    def __init__(self, template, output_dir):
        self._template = template
        self._output_dir = output_dir

    def bake(self, extra_context=None):
        try:
            project_dir = cookiecutter(
                self._template,
                no_input=True,
                extra_context=extra_context,
                output_dir=self._output_dir
            )
        except Exception as e:
            self.error = e
        else:
            self.project = project_dir


@pytest.fixture
def cookies(request, tmpdir):
    output_dir = request.config.option.output_dir

    if not output_dir:
        output_dir = str(tmpdir.mkdir('cookies_output'))

    _cookies = Cookies('.', output_dir)
    return _cookies


def pytest_addoption(parser):
    group = parser.getgroup('cookies')
    group.addoption(
        '--output-dir',
        action='store',
        dest='output_dir',
        help='Set the output directory for Cookiecutter'
    )

    parser.addini('HELLO', 'Dummy pytest.ini setting')

## Instruction:
Handle SystemExit errors and add exit_code
## Code After:

import pytest

from cookiecutter.main import cookiecutter


class Cookies(object):
    """Class to provide convenient access to the cookiecutter API."""
    exception = None
    exit_code = 0
    project = None

    def __init__(self, template, output_dir):
        self._template = template
        self._output_dir = output_dir

    def bake(self, extra_context=None):
        try:
            project_dir = cookiecutter(
                self._template,
                no_input=True,
                extra_context=extra_context,
                output_dir=self._output_dir
            )
        except SystemExit as e:
            if e.code != 0:
                self.exception = e
            self.exit_code = e.code
        except Exception as e:
            self.exception = e
            self.exit_code = -1
        else:
            self.project = project_dir


@pytest.fixture
def cookies(request, tmpdir):
    output_dir = request.config.option.output_dir

    if not output_dir:
        output_dir = str(tmpdir.mkdir('cookies_output'))

    _cookies = Cookies('.', output_dir)
    return _cookies


def pytest_addoption(parser):
    group = parser.getgroup('cookies')
    group.addoption(
        '--output-dir',
        action='store',
        dest='output_dir',
        help='Set the output directory for Cookiecutter'
    )

    parser.addini('HELLO', 'Dummy pytest.ini setting')
